Skip to content

Commit

Permalink
Make publish-action a callable function
Browse files Browse the repository at this point in the history
  • Loading branch information
codykaup committed Oct 8, 2024
1 parent 742bd7e commit d86499c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"release": "./scripts/release.mjs",
"prepack": "clean-package",
"postpack": "clean-package restore",
"publish-action": "./scripts/publish-action.mjs",
"publish-action": "./scripts/publishAction.mjs",
"trace": "./dist/bin.js trace",
"trim-stats": "./dist/bin.js trim-stats-file",
"storybook": "storybook dev -p 9009",
Expand Down
51 changes: 25 additions & 26 deletions scripts/publish-action.mjs → scripts/publishAction.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,11 @@ const publishAction = async ({ major, version, repo }) => {
return cleanup();
};

/**
* Generally, this script is invoked by auto's `afterShipIt` hook.
*
* For manual (local) use:
* yarn publish-action {context} [--dry-run]
* e.g. yarn publish-action canary
* or yarn publish-action latest --dry-run
*
* Make sure to build the action before publishing manually.
*/
(async () => {
const { stdout: status } = await $`git status --porcelain`;
if (status) {
console.error(`❗️ Working directory is not clean:\n${status}`);
return;
}

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}`);
}
export async function main(context) {
const pkg = JSON.parse(readFileSync('package.json', 'utf8'));
const version = pkg.version;
console.info(`📌 Using context arg: ${context}`);
console.info(`📌 Using package.json version: ${version}`);

const [, major, minor, patch] = version.match(/^(\d+)\.(\d+)\.(\d+)-*(\w+)?/) || [];
if (!major || !minor || !patch) {
Expand All @@ -96,4 +76,23 @@ const publishAction = async ({ major, version, repo }) => {
default:
console.error(`❗️ Unknown context: ${context}`);
}
})();
}

/**
* For manual (local) use:
* yarn publish-action {context} [--dry-run]
* e.g. yarn publish-action canary
* or yarn publish-action latest --dry-run
*
* Make sure to build the action before publishing manually.
*/
// eslint-disable-next-line unicorn/prefer-module
if (require.main === module) {
const { stdout: status } = await $`git status --porcelain`;
if (status) {
console.error(`❗️ Working directory is not clean:\n${status}`);
process.exit(1);
}

main(process.argv[2]);
}
4 changes: 3 additions & 1 deletion scripts/release.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import { $ } from 'execa';

import { main as publishAction } from './publishAction.mjs';

async function main() {
const { stdout: status } = await $`git status --porcelain`;
if (status) {
Expand All @@ -13,7 +15,7 @@ async function main() {
await $({ stdout: 'inherit', stderr: 'inherit' })`auto shipit`;

if (process.env.GITHUB_REF === 'main') {
await $({ stdout: 'inherit', stderr: 'inherit' })`yarn publish-action latest`;
await publishAction('latest');
} else {
console.info('Skipping automatic publish of action-canary.');
console.info('Run `yarn publish-action canary` to publish a canary action.');
Expand Down

0 comments on commit d86499c

Please sign in to comment.