Skip to content

Commit c394854

Browse files
committed
Fix publish scripts
1 parent 6d7124f commit c394854

File tree

3 files changed

+88
-94
lines changed

3 files changed

+88
-94
lines changed

.github/workflows/publish.yml

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,69 +14,56 @@ on:
1414
- major
1515

1616
jobs:
17-
setup:
18-
runs-on: ubuntu-latest
19-
permissions:
20-
contents: write
21-
steps:
22-
- uses: actions/checkout@v4
23-
with:
24-
fetch-depth: 0
25-
26-
- uses: ./.github/actions/setup
27-
2817
publish:
29-
needs: setup
3018
runs-on: ubuntu-latest
3119
permissions:
3220
contents: write
3321
id-token: write
22+
outputs:
23+
version: ${{ steps.publish.outputs.version }}
24+
tag: ${{ steps.publish.outputs.tag }}
3425
steps:
3526
- uses: actions/checkout@v4
3627
with:
3728
fetch-depth: 0
3829

3930
- uses: ./.github/actions/setup
4031

41-
- name: Publish
32+
- name: Publish to NPM
4233
id: publish
43-
run: bun run script/publish.ts
34+
run: bun run scripts/publish.ts
4435
env:
4536
BUMP: ${{ inputs.bump }}
4637
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
47-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
48-
49-
outputs:
50-
version: ${{ steps.publish.outputs.version }}
51-
tag: ${{ steps.publish.outputs.tag }}
5238

53-
commit:
54-
needs: publish
55-
runs-on: ubuntu-latest
56-
permissions:
57-
contents: write
58-
steps:
59-
- name: Commit release version
39+
- name: Commit version bump
6040
id: commit
6141
uses: stefanzweifel/git-auto-commit-action@v5
6242
with:
63-
commit_message: "release: v${{ needs.publish.outputs.version }}"
43+
commit_message: "release: v${{ steps.publish.outputs.version }}"
6444
file_pattern: "package.json"
65-
token: ${{ secrets.GH_PAT }}
45+
46+
- name: Save Commit Hash
47+
id: save_hash
48+
run: echo "hash=${{ steps.commit.outputs.commit_hash || github.sha }}" >> $GITHUB_OUTPUT
6649

6750
outputs:
68-
commit_hash: ${{ steps.commit.outputs.commit_hash }}
51+
version: ${{ steps.publish.outputs.version }}
52+
tag: ${{ steps.publish.outputs.tag }}
53+
commit_hash: ${{ steps.save_hash.outputs.hash }}
6954

7055
release:
71-
needs: [publish, commit]
56+
needs: publish
7257
runs-on: ubuntu-latest
7358
permissions:
7459
contents: write
7560
steps:
61+
- uses: actions/checkout@v4
62+
7663
- name: Create GitHub release
7764
uses: softprops/action-gh-release@v2
7865
with:
79-
tag_name: "v${{ needs.publish.outputs.tag }}"
80-
name: "v${{ needs.publish.outputs.version }}"
66+
tag_name: ${{ needs.publish.outputs.tag }}
67+
name: ${{ needs.publish.outputs.tag }}
8168
generate_release_notes: true
82-
target_commitish: ${{ needs.commit.outputs.commit_hash }}
69+
target_commitish: ${{ needs.publish.outputs.commit_hash }}

script/publish.ts

Lines changed: 0 additions & 61 deletions
This file was deleted.

scripts/publish.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env bun
2+
3+
import { $ } from "bun";
4+
import { writeFile } from "node:fs/promises";
5+
6+
// 1. Validation
7+
const bump = process.env.BUMP;
8+
if (!bump || !["patch", "minor", "major"].includes(bump)) {
9+
console.error("Missing BUMP env. Use patch, minor, or major.");
10+
process.exit(1);
11+
}
12+
13+
if (!process.env.NPM_TOKEN) {
14+
console.error("NPM_TOKEN is not set.");
15+
process.exit(1);
16+
}
17+
18+
// 2. Version Calculation
19+
const pkgFile = Bun.file("package.json");
20+
const pkg = await pkgFile.json();
21+
const currentVersion = pkg.version;
22+
23+
const [major, minor, patch] = currentVersion.split(".").map(Number);
24+
const newVersion = (() => {
25+
switch (bump) {
26+
case "major":
27+
return `${major + 1}.0.0`;
28+
case "minor":
29+
return `${major}.${minor + 1}.0`;
30+
case "patch":
31+
return `${major}.${minor}.${patch + 1}`;
32+
default:
33+
return currentVersion;
34+
}
35+
})();
36+
37+
console.log(`Bumping ${currentVersion} -> ${newVersion}`);
38+
39+
// 3. Update Files
40+
pkg.version = newVersion;
41+
await writeFile("package.json", JSON.stringify(pkg, null, 2) + "\n");
42+
43+
// 4. Build
44+
console.log("Running build...");
45+
await $`bun run build`;
46+
47+
// 5. Publish using CLI flag for authentication
48+
console.log("Publishing to NPM...");
49+
try {
50+
// We pass the token directly to the publish command
51+
await $`npm publish --access public --//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}`;
52+
console.log("NPM publish successful.");
53+
} catch (error) {
54+
console.error("NPM publish failed.");
55+
process.exit(1);
56+
}
57+
58+
// 6. GitHub Outputs
59+
const tag = `v${newVersion}`;
60+
if (process.env.GITHUB_OUTPUT) {
61+
await writeFile(
62+
process.env.GITHUB_OUTPUT,
63+
`version=${newVersion}\ntag=${tag}\n`,
64+
{ flag: "a" },
65+
);
66+
}
67+
68+
console.log(`Process complete for ${tag}`);

0 commit comments

Comments
 (0)