Skip to content

Commit

Permalink
Use context and version data from auto shipIt rather than trying to f…
Browse files Browse the repository at this point in the history
…igure it out ourselves
  • Loading branch information
ghengeveld committed Mar 19, 2024
1 parent 31605ab commit a7ac081
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,13 @@
},
"plugins": [
"npm",
"released"
"released",
[
"exec",
{
"afterShipIt": "yarn run publish-action"
}
]
]
},
"docs": "https://www.chromatic.com/docs/cli",
Expand Down
37 changes: 22 additions & 15 deletions scripts/publish-action.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ const publishAction = async ({ major, version, repo }) => {
* Generally, this script is invoked by auto's `afterShipIt` hook.
*
* For manual (local) use:
* yarn publish-action [context] [--dry-run]
* yarn publish-action {context} [--dry-run]
* e.g. yarn publish-action canary
* or yarn publish-action --dry-run
* or yarn publish-action latest --dry-run
*
* Make sure to build the action before publishing manually.
*/
Expand All @@ -66,36 +66,43 @@ const publishAction = async ({ major, version, repo }) => {
return;
}

const pkg = JSON.parse(readFileSync('package.json', 'utf8'));
let context, version;
if (['canary', 'next', 'latest'].includes(process.argv[2])) {
const pkg = JSON.parse(readFileSync('package.json', 'utf8'));
context = process.argv[2];
version = pkg.version;
console.info(`📌 Using context arg: ${context}`);
console.info(`📌 Using package.json version: ${version}`);
} else {
const data = JSON.parse(process.env.ARG_0);
context = data.context;
version = data.newVersion;
console.info(`📌 Using auto shipIt context: ${context}`);
console.info(`📌 Using auto shipIt version: ${version}`);
}

const [, major, minor, patch, tag] = pkg.version.match(/(\d+)\.(\d+)\.(\d+)-*(\w+)?/) || [];
const [, major, minor, patch] = version.match(/(\d+)\.(\d+)\.(\d+)-*(\w+)?/) || [];
if (!major || !minor || !patch) {
console.error(`❗️ Invalid version: ${pkg.version}`);
console.error(`❗️ Invalid version: ${version}`);
return;
}

const { stdout: branch } = await $`git rev-parse --abbrev-ref HEAD`;
const defaultTag = branch === 'main' ? 'latest' : 'canary';
const context = ['canary', 'next', 'latest'].includes(process.argv[2])
? process.argv[2]
: tag || defaultTag;

switch (context) {
case 'canary':
if (process.argv[2] !== 'canary') {
console.info('Skipping automatic publish of action-canary.');
console.info('Run `yarn publish-action canary` to publish a canary action.');
return;
}
await publishAction({ major, version: pkg.version, repo: 'chromaui/action-canary' });
await publishAction({ major, version, repo: 'chromaui/action-canary' });
break;
case 'next':
await publishAction({ major, version: pkg.version, repo: 'chromaui/action-next' });
await publishAction({ major, version, repo: 'chromaui/action-next' });
break;
case 'latest':
await publishAction({ major, version: pkg.version, repo: 'chromaui/action' });
await publishAction({ major, version, repo: 'chromaui/action' });
break;
default:
console.error(`❗️ Unknown tag: ${tag}`);
console.error(`❗️ Unknown context: ${context}`);
}
})();

0 comments on commit a7ac081

Please sign in to comment.