From 3f0574189582d59468eb37b57aaf4222fde78bf1 Mon Sep 17 00:00:00 2001 From: "a.khanteev" Date: Mon, 24 Aug 2026 20:01:00 +0400 Subject: [PATCH] fix: make self-contained CLI bundles runnable --- .changeset/fix-self-contained-cli-bundles.md | 7 +++ packages/gitlab-cli/package.json | 2 +- packages/kaiten-cli/package.json | 2 +- packages/postgres-cli/package.json | 2 +- scripts/build-self-contained-cli.mjs | 3 ++ scripts/test-self-contained-cli.mjs | 45 ++++++++++++++++++++ 6 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 .changeset/fix-self-contained-cli-bundles.md create mode 100644 scripts/test-self-contained-cli.mjs diff --git a/.changeset/fix-self-contained-cli-bundles.md b/.changeset/fix-self-contained-cli-bundles.md new file mode 100644 index 0000000..4ec4769 --- /dev/null +++ b/.changeset/fix-self-contained-cli-bundles.md @@ -0,0 +1,7 @@ +--- +"@khaale/gitlab-cli": patch +"@khaale/kaiten-cli": patch +"@khaale/postgres-cli": patch +--- + +Fix self-contained CLI bundles so CommonJS dependencies load correctly in Node.js. diff --git a/packages/gitlab-cli/package.json b/packages/gitlab-cli/package.json index 05afa49..0be66b7 100644 --- a/packages/gitlab-cli/package.json +++ b/packages/gitlab-cli/package.json @@ -18,7 +18,7 @@ "build:pack": "node ../../scripts/build-self-contained-cli.mjs packages/gitlab-cli glc", "lint": "node --check ./bin/glc.js && node --check ./src/cli.js", "test": "node --test test/*.test.js", - "pack:check": "pnpm build:pack && npm pack --dry-run --cache ./.npm-cache", + "pack:check": "pnpm build:pack && node ../../scripts/test-self-contained-cli.mjs packages/gitlab-cli glc && npm pack --dry-run --cache ./.npm-cache", "prepublishOnly": "pnpm lint && pnpm test && pnpm pack:check" }, "engines": { diff --git a/packages/kaiten-cli/package.json b/packages/kaiten-cli/package.json index daeb654..fddb31b 100644 --- a/packages/kaiten-cli/package.json +++ b/packages/kaiten-cli/package.json @@ -17,7 +17,7 @@ "build:pack": "node ../../scripts/build-self-contained-cli.mjs packages/kaiten-cli ktc", "lint": "node --check ./bin/ktc.js && node --check ./src/cli.js", "test": "node --test test/*.test.js", - "pack:check": "pnpm build:pack && npm pack --dry-run --cache ./.npm-cache", + "pack:check": "pnpm build:pack && node ../../scripts/test-self-contained-cli.mjs packages/kaiten-cli ktc && npm pack --dry-run --cache ./.npm-cache", "prepublishOnly": "pnpm lint && pnpm test && pnpm pack:check" }, "engines": { diff --git a/packages/postgres-cli/package.json b/packages/postgres-cli/package.json index c26b411..353a199 100644 --- a/packages/postgres-cli/package.json +++ b/packages/postgres-cli/package.json @@ -18,7 +18,7 @@ "build:pack": "node ../../scripts/build-self-contained-cli.mjs packages/postgres-cli pgc", "lint": "node --check ./bin/pgc.js && node --check ./src/cli.js", "test": "node --test test/*.test.js", - "pack:check": "pnpm build:pack && npm pack --dry-run --cache ./.npm-cache", + "pack:check": "pnpm build:pack && node ../../scripts/test-self-contained-cli.mjs packages/postgres-cli pgc && npm pack --dry-run --cache ./.npm-cache", "prepublishOnly": "pnpm lint && pnpm test && pnpm pack:check" }, "engines": { diff --git a/scripts/build-self-contained-cli.mjs b/scripts/build-self-contained-cli.mjs index b378ea5..f02dc12 100644 --- a/scripts/build-self-contained-cli.mjs +++ b/scripts/build-self-contained-cli.mjs @@ -30,6 +30,9 @@ await build({ platform: "node", target: "node22", packages: "bundle", + banner: { + js: "import { createRequire } from 'node:module'; const require = createRequire(import.meta.url);" + }, logLevel: "silent" }); diff --git a/scripts/test-self-contained-cli.mjs b/scripts/test-self-contained-cli.mjs new file mode 100644 index 0000000..3f979f9 --- /dev/null +++ b/scripts/test-self-contained-cli.mjs @@ -0,0 +1,45 @@ +import { spawn } from "node:child_process"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; + +const [, , packageDirArg, binNameArg] = process.argv; + +if (!packageDirArg || !binNameArg) { + console.error("usage: node scripts/test-self-contained-cli.mjs "); + process.exit(1); +} + +const scriptDir = path.dirname(fileURLToPath(import.meta.url)); +const repoRoot = path.resolve(scriptDir, ".."); +const binPath = path.join(repoRoot, packageDirArg, "dist", "bin", `${binNameArg}.js`); + +const result = await run(binPath, ["--help"]); +if (result.code !== 0 || !result.stdout.includes(binNameArg)) { + console.error(`self-contained ${binNameArg} smoke test failed`); + if (result.stdout) { + console.error(`stdout:\n${result.stdout}`); + } + if (result.stderr) { + console.error(`stderr:\n${result.stderr}`); + } + process.exit(1); +} + +function run(command, args) { + return new Promise((resolve, reject) => { + const child = spawn(process.execPath, [command, ...args], { + stdio: ["ignore", "pipe", "pipe"] + }); + let stdout = ""; + let stderr = ""; + + child.stdout.on("data", (chunk) => { + stdout += chunk; + }); + child.stderr.on("data", (chunk) => { + stderr += chunk; + }); + child.on("error", reject); + child.on("close", (code, signal) => resolve({ code: code ?? 1, signal, stdout, stderr })); + }); +}