Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions .github/scripts/has-unpublished-packages.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { appendFileSync } from "node:fs";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no problem with merging this as-is, and I'm guessing it's copy/pasted across numerous projects, but it's a tad overengineered for single-package repos like this one.

There's only one package.json that's relevant here, don't need to go hunting for it or abstract out a system to find it 🤷

Much easier to read than the bash though, I'm a slave to syntax highlighting

import { readFile } from "node:fs/promises";

async function hasPublishedVersion(pkg) {
const response = await fetch(
`https://registry.npmjs.org/${encodeURIComponent(pkg.name)}`,
{ headers: { accept: "application/vnd.npm.install-v1+json" } },
);

if (response.status === 404) {
return false;
}

if (!response.ok) {
throw new Error(
`Failed to query ${pkg.name}: ${response.status} ${response.statusText}`,
);
}

const metadata = await response.json();
return Object.prototype.hasOwnProperty.call(
metadata.versions ?? {},
pkg.version,
);
}

async function main() {
const pkg = JSON.parse(await readFile("package.json", "utf8"));
const isPublished = await hasPublishedVersion(pkg);

if (isPublished) {
console.log(`${pkg.name}@${pkg.version} is already published`);
} else {
console.log(`${pkg.name}@${pkg.version} is not published yet`);
}

const shouldPublish = !isPublished;
const output = [
`has_unpublished=${String(shouldPublish)}`,
`should_publish=${String(shouldPublish)}`,
].join("\n") + "\n";

if (process.env.GITHUB_OUTPUT) {
appendFileSync(process.env.GITHUB_OUTPUT, output);
} else {
process.stdout.write(output);
}
}

main().catch(error => {
console.error(error);
process.exit(1);
});
15 changes: 2 additions & 13 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,10 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Check whether this version still needs publishing
- name: Check for unpublished package versions
if: steps.changesets.outputs.hasChangesets == 'false'
id: publish-check
shell: bash
run: |
PACKAGE_NAME=$(node -p "require('./package.json').name")
PACKAGE_VERSION=$(node -p "require('./package.json').version")

echo "Checking whether ${PACKAGE_NAME}@${PACKAGE_VERSION} is already on npm"
if npm view "${PACKAGE_NAME}@${PACKAGE_VERSION}" version >/dev/null 2>&1; then
echo "should_publish=false" >> "$GITHUB_OUTPUT"
exit 0
fi

echo "should_publish=true" >> "$GITHUB_OUTPUT"
run: node .github/scripts/has-unpublished-packages.mjs

publish:
name: Publish
Expand Down
Loading