Skip to content

Commit 8ceb3ca

Browse files
committed
publish: create tag
1 parent 84a876f commit 8ceb3ca

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
run: npm install
2828

2929
- name: Run publish command
30-
run: npx . publish "./test" --webhook "${{ secrets.DISCORD_WEBHOOK }}" --access-token "${{ secrets.NPM_TOKEN }}" --version+hash --tag github --version+tag
30+
run: npx . publish "./test" --webhook "${{ secrets.DISCORD_WEBHOOK }}" --access-token "${{ secrets.NPM_TOKEN }}" --version+hash --tag github --version+tag --create-tag
3131

3232
- name: Run publish without tag
3333
run: npx . publish "./test" --webhook "${{ secrets.DISCORD_WEBHOOK }}" --access-token "${{ secrets.NPM_TOKEN }}" --version+hash

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ program.command('publish', 'Publish npm package')
4444
.option("--tag <tag>", "NPM tag to use", { required: false, validator: program.STRING })
4545
.option("--version+hash", "Include hash in version (default: false)", { required: false, validator: program.BOOLEAN, default: false })
4646
.option("--version+tag", "Include tag in version (default: false)", { required: false, validator: program.BOOLEAN, default: false })
47+
.option("--create-tag", "Create a git tag with the version (default: false)", { required: false, validator: program.BOOLEAN, default: false })
4748
.option("--webhook <webhook>", "Webhook URL to send notifications", { required: false, validator: program.STRING })
4849
.option("--access-token <access-token>", "NPM access token", { required: false, validator: program.STRING })
4950
.option("--dry-run", "Dry run mode, do not publish", { required: false, validator: program.BOOLEAN, default: false })
@@ -61,6 +62,7 @@ program.command('publish', 'Publish npm package')
6162
accessToken: options.accessToken?.toString() || null,
6263
useHashInVersion: options.versionHash === true, // default to false
6364
useTagInVersion: options.versionTag === true, // default to false
65+
createGitTag: options.createTag === true, // default to false
6466
dryRun: options.dryRun === true,
6567
tag: tag,
6668
webhookUrl: options.webhook?.toString() || null,

src/publish.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,20 @@ export async function publish(args) {
233233
logger.info(`♻ Restoring original package.json at ${packageJsonPath}`);
234234
writeFileSync(packageJsonPath, _originalPackageJson, 'utf-8');
235235

236+
if (args.createGitTag) {
237+
const cmd = `git tag -a ${packageJson.version} -m "Published ${packageJson.version}"`;
238+
logger.info(`Creating git tag with command: ${cmd}`);
239+
const res = tryExecSync(cmd, {
240+
cwd: packageDirectory,
241+
env: env
242+
});
243+
if (!res.success) {
244+
logger.error(`❌ Failed to create git tag: ${res.error}`);
245+
if (webhook) {
246+
await sendMessageToWebhook(webhook, `❌ **Failed to create git tag** \`${packageJson.version}\`: ${res.error}`);
247+
}
248+
}
249+
}
236250

237251
if (process.env.GITHUB_OUTPUT) {
238252
appendFileSync(process.env.GITHUB_OUTPUT, `package-version=${packageJson.version}\n`);

types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export type PublishOptions = {
2727
tag: string | null | undefined;
2828
useHashInVersion: boolean;
2929
useTagInVersion: boolean;
30+
createGitTag: boolean;
3031
dryRun: boolean;
3132
webhookUrl: string | null | undefined;
3233
overrideName: string | null | undefined;

0 commit comments

Comments
 (0)