Skip to content

Commit 990307a

Browse files
authored
Merge pull request #458 from preactjs/changesets-based-publishing
Add changesets based publishing
2 parents 797a662 + bcd3b0b commit 990307a

4 files changed

Lines changed: 118 additions & 12 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { appendFileSync } from 'node:fs';
2+
import { readFile } from 'node:fs/promises';
3+
4+
async function hasPublishedVersion(pkg) {
5+
const response = await fetch(
6+
`https://registry.npmjs.org/${encodeURIComponent(pkg.name)}`,
7+
{ headers: { accept: 'application/vnd.npm.install-v1+json' } }
8+
);
9+
10+
if (response.status === 404) {
11+
return false;
12+
}
13+
14+
if (!response.ok) {
15+
throw new Error(
16+
`Failed to query ${pkg.name}: ${response.status} ${response.statusText}`
17+
);
18+
}
19+
20+
const metadata = await response.json();
21+
return Object.prototype.hasOwnProperty.call(
22+
metadata.versions ?? {},
23+
pkg.version
24+
);
25+
}
26+
27+
async function main() {
28+
const pkg = JSON.parse(await readFile('package.json', 'utf8'));
29+
const isPublished = await hasPublishedVersion(pkg);
30+
31+
if (isPublished) {
32+
console.log(`${pkg.name}@${pkg.version} is already published`);
33+
} else {
34+
console.log(`${pkg.name}@${pkg.version} is not published yet`);
35+
}
36+
37+
const shouldPublish = !isPublished;
38+
const output =
39+
[
40+
`has_unpublished=${String(shouldPublish)}`,
41+
`should_publish=${String(shouldPublish)}`
42+
].join('\n') + '\n';
43+
44+
if (process.env.GITHUB_OUTPUT) {
45+
appendFileSync(process.env.GITHUB_OUTPUT, output);
46+
} else {
47+
process.stdout.write(output);
48+
}
49+
}
50+
51+
main().catch((error) => {
52+
console.error(error);
53+
process.exit(1);
54+
});

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
cache: npm
2222

2323
- name: Update npm
24-
run: npm install -g npm@latest
24+
run: npm install -g npm@11.11.1
2525

2626
- run: npm ci
2727
- name: test

.github/workflows/release.yml

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,89 @@ on:
55
branches:
66
- main
77

8+
concurrency: ${{ github.workflow }}-${{ github.ref }}
9+
810
jobs:
911
release:
1012
name: Release
11-
runs-on: ubuntu-latest
13+
outputs:
14+
has_changesets: ${{ steps.changesets.outputs.hasChangesets }}
15+
should_publish: ${{ steps.publish-check.outputs.should_publish }}
1216
permissions:
1317
contents: write
14-
id-token: write
1518
issues: read
1619
pull-requests: write
20+
runs-on: ubuntu-latest
1721
steps:
18-
- name: Checkout Repo
22+
- name: Checkout
1923
uses: actions/checkout@v4
2024
with:
25+
persist-credentials: false
2126
# This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
2227
fetch-depth: 0
2328

24-
- name: Setup Node.js 22.x
29+
- name: Install Node.js
30+
uses: actions/setup-node@v4
31+
with:
32+
node-version: 22
33+
registry-url: "https://registry.npmjs.org"
34+
35+
- name: Update npm
36+
run: npm install -g npm@11.11.1
37+
38+
- name: Install dependencies
39+
run: npm ci --ignore-scripts
40+
41+
- name: Create Release Pull Request
42+
id: changesets
43+
uses: changesets/action@e0145edc7d9d8679003495b11f87bd8ef63c0cba # v1.5.3
44+
with:
45+
version: npm run version
46+
commitMode: github-api
47+
env:
48+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
49+
50+
- name: Check for unpublished package versions
51+
if: steps.changesets.outputs.hasChangesets == 'false'
52+
id: publish-check
53+
run: node .github/scripts/has-unpublished-packages.mjs
54+
55+
publish:
56+
name: Publish
57+
needs: release
58+
if: needs.release.outputs.should_publish == 'true'
59+
environment:
60+
name: npm
61+
url: https://www.npmjs.com/package/preact-render-to-string
62+
permissions:
63+
contents: write
64+
id-token: write
65+
runs-on: ubuntu-latest
66+
steps:
67+
- name: Checkout
68+
uses: actions/checkout@v4
69+
with:
70+
persist-credentials: false
71+
72+
- name: Install Node.js
2573
uses: actions/setup-node@v4
2674
with:
2775
node-version: 22
28-
cache: npm
2976
registry-url: "https://registry.npmjs.org"
3077

3178
- name: Update npm
32-
run: npm install -g npm@latest
79+
run: npm install -g npm@11.11.1
80+
81+
- name: Install dependencies
82+
run: npm ci --ignore-scripts
3383

34-
- name: Install Dependencies
35-
run: npm ci
84+
- name: Build package
85+
run: npm run build
3686

37-
- name: Create Release PR or Publish
38-
uses: changesets/action@v1.5.3
87+
- name: Publish packages
88+
uses: changesets/action@e0145edc7d9d8679003495b11f87bd8ef63c0cba # v1.5.3
3989
with:
40-
publish: npx changeset publish
90+
publish: npm exec -- changeset publish
91+
commitMode: github-api
4192
env:
4293
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"test:vitest": "vitest",
5252
"test:vitest:run": "vitest run",
5353
"format": "oxfmt --write .",
54+
"version": "changeset version && npm install --package-lock-only --ignore-scripts",
5455
"prepublishOnly": "npm run build",
5556
"release": "npm run build && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish"
5657
},

0 commit comments

Comments
 (0)