Skip to content

Commit 178a21b

Browse files
authored
Add changesets based publishing (#204)
* Move publish check into script * Simplify publish check
1 parent cd23650 commit 178a21b

2 files changed

Lines changed: 55 additions & 13 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
`has_unpublished=${String(shouldPublish)}`,
40+
`should_publish=${String(shouldPublish)}`,
41+
].join("\n") + "\n";
42+
43+
if (process.env.GITHUB_OUTPUT) {
44+
appendFileSync(process.env.GITHUB_OUTPUT, output);
45+
} else {
46+
process.stdout.write(output);
47+
}
48+
}
49+
50+
main().catch(error => {
51+
console.error(error);
52+
process.exit(1);
53+
});

.github/workflows/release.yml

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,10 @@ jobs:
4545
env:
4646
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4747

48-
- name: Check whether this version still needs publishing
48+
- name: Check for unpublished package versions
4949
if: steps.changesets.outputs.hasChangesets == 'false'
5050
id: publish-check
51-
shell: bash
52-
run: |
53-
PACKAGE_NAME=$(node -p "require('./package.json').name")
54-
PACKAGE_VERSION=$(node -p "require('./package.json').version")
55-
56-
echo "Checking whether ${PACKAGE_NAME}@${PACKAGE_VERSION} is already on npm"
57-
if npm view "${PACKAGE_NAME}@${PACKAGE_VERSION}" version >/dev/null 2>&1; then
58-
echo "should_publish=false" >> "$GITHUB_OUTPUT"
59-
exit 0
60-
fi
61-
62-
echo "should_publish=true" >> "$GITHUB_OUTPUT"
51+
run: node .github/scripts/has-unpublished-packages.mjs
6352

6453
publish:
6554
name: Publish

0 commit comments

Comments
 (0)