From d11632d44b740ab8b78ec27544afb480f406a9d5 Mon Sep 17 00:00:00 2001 From: Soccera1 <129828376+Soccera1@users.noreply.github.com> Date: Thu, 6 Aug 2026 01:58:10 +1000 Subject: Automatically publish to npm --- .github/workflows/publish.yml | 77 +++++++++++++++++++++++++++++++++++++++++++ .gitignore | 1 + bin/bnu.js | 8 ++--- bin/npm.js | 20 +++++++++++ package.json | 9 ++++- scripts/build.js | 46 ++++++++++++++++++++++++++ 6 files changed, 155 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/publish.yml create mode 100644 bin/npm.js create mode 100644 scripts/build.js diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..ea1108d --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,77 @@ +name: Publish to npm + +on: + push: + branches: + - master + +concurrency: + group: npm-publish + cancel-in-progress: false + +permissions: + contents: read + id-token: write + +jobs: + publish: + runs-on: ubuntu-latest + + steps: + - name: Check out repository + uses: actions/checkout@v4 + + - name: Set up Bun + uses: oven-sh/setup-bun@v2 + + - name: Set up Node for npm + uses: actions/setup-node@v4 + with: + node-version: 24 + registry-url: https://registry.npmjs.org + + - name: Build optimized package + run: bun run build + + - name: Choose daily version + id: release + shell: bash + run: | + version="$(node --input-type=module <<'NODE' + import { readFile } from "node:fs/promises"; + + const packageJson = JSON.parse(await readFile("package.json", "utf8")); + const date = new Date().toISOString().slice(0, 10).replaceAll("-", ""); + const escapedName = packageJson.name.replace("/", "%2f"); + const response = await fetch(`https://registry.npmjs.org/${escapedName}`); + + let versions = []; + if (response.status !== 404) { + if (!response.ok) { + throw new Error(`npm registry returned ${response.status} ${response.statusText}`); + } + const metadata = await response.json(); + versions = Object.keys(metadata.versions ?? {}); + } + + const pattern = new RegExp(`^${date}\\.0\\.(\\d+)$`); + const previous = versions + .map((version) => pattern.exec(version)?.[1]) + .filter((increment) => increment !== undefined) + .map(Number); + const increment = previous.length === 0 ? 0 : Math.max(...previous) + 1; + + process.stdout.write(`${date}.0.${increment}`); + NODE + )" + + npm pkg set version="$version" + echo "version=$version" >> "$GITHUB_OUTPUT" + + - name: Verify package contents + run: npm pack --dry-run + + - name: Publish ${{ steps.release.outputs.version }} + run: npm publish --access public --provenance + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/.gitignore b/.gitignore index 885abc1..289f297 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /audit/ +/dist/ diff --git a/bin/bnu.js b/bin/bnu.js index 9d1917e..0ea480b 100755 --- a/bin/bnu.js +++ b/bin/bnu.js @@ -1,12 +1,10 @@ #!/usr/bin/env bun import { realpathSync } from "node:fs"; -import { basename, dirname, join } from "node:path"; -import { pathToFileURL } from "node:url"; +import { basename } from "node:path"; +import { commandNames } from "../src/shared/catalog.js"; +import { main } from "../src/shared/runtime.js"; -const here = dirname(realpathSync(Bun.argv[1])); globalThis[Symbol.for("bnu.cli")] = true; -const { commandNames } = await import(pathToFileURL(join(here, "../src/shared/catalog.js")).href); -const { main } = await import(pathToFileURL(join(here, "../src/shared/runtime.js")).href); let invokedPath = Bun.argv[1]; try { if (process.env._ && realpathSync(process.env._) === realpathSync(Bun.argv[1])) invokedPath = process.env._; diff --git a/bin/npm.js b/bin/npm.js new file mode 100644 index 0000000..4f45f0e --- /dev/null +++ b/bin/npm.js @@ -0,0 +1,20 @@ +#!/usr/bin/env node + +import { spawn } from "node:child_process"; +import { createRequire } from "node:module"; +import { fileURLToPath } from "node:url"; + +const require = createRequire(import.meta.url); +const bun = require.resolve("bun/bin/bun.exe"); +const launcher = fileURLToPath(new URL("../runtime/bnu.js", import.meta.url)); +const child = spawn(bun, [launcher, ...process.argv.slice(2)], { stdio: "inherit" }); + +child.on("error", (error) => { + console.error(`bnu: failed to start bundled Bun runtime: ${error.message}`); + process.exit(1); +}); + +child.on("exit", (code, signal) => { + if (signal) process.kill(process.pid, signal); + else process.exit(code ?? 1); +}); diff --git a/package.json b/package.json index c2a28c8..213d7d4 100644 --- a/package.json +++ b/package.json @@ -3,9 +3,13 @@ "version": "0.1.0", "type": "module", "bin": { - "bnu": "./bin/bnu.js" + "bnu": "./dist/bin/npm.js" }, + "files": [ + "dist/" + ], "scripts": { + "build": "bun scripts/build.js", "docs:import": "bun scripts/import-gnu-docs.js coreutils-9.11.tar.xz", "link-commands": "bun scripts/link-commands.js", "test": "bun scripts/run-bun-tests-bounded.js", @@ -14,6 +18,9 @@ "test:qemu": "scripts/run-gnu-qemu-matrix.sh", "test:all": "scripts/run-all-tests.sh" }, + "dependencies": { + "bun": "1.3.14" + }, "devDependencies": { "bun-types": "latest" } diff --git a/scripts/build.js b/scripts/build.js new file mode 100644 index 0000000..8adcc3c --- /dev/null +++ b/scripts/build.js @@ -0,0 +1,46 @@ +import { readdir, rm } from "node:fs/promises"; +import { dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; + +const root = dirname(dirname(fileURLToPath(import.meta.url))); +const dist = join(root, "dist"); +const commandsSource = join(root, "src/commands"); + +await rm(dist, { recursive: true, force: true }); + +const commandEntrypoints = (await readdir(commandsSource)) + .filter((name) => name.endsWith(".js")) + .map((name) => join(commandsSource, name)); + +const commonOptions = { + target: "bun", + minify: true, + sourcemap: "none", +}; + +const npmLauncher = await Bun.build({ + entrypoints: [join(root, "bin/npm.js")], + outdir: join(dist, "bin"), + target: "node", + minify: true, + sourcemap: "none", +}); + +const runtimeLauncher = await Bun.build({ + ...commonOptions, + entrypoints: [join(root, "bin/bnu.js")], + outdir: join(dist, "runtime"), +}); + +const commands = await Bun.build({ + ...commonOptions, + entrypoints: commandEntrypoints, + outdir: join(dist, "commands"), + root: commandsSource, + splitting: false, +}); + +if (!npmLauncher.success || !runtimeLauncher.success || !commands.success) { + for (const log of [...npmLauncher.logs, ...runtimeLauncher.logs, ...commands.logs]) console.error(log); + process.exit(1); +} -- cgit v1.3