From 139be14dc5b1ab16da7852527e9f24b028e15ce0 Mon Sep 17 00:00:00 2001 From: Ashton Eby Date: Wed, 20 Nov 2024 17:31:16 -0800 Subject: [PATCH] add script to update hashbang --- package.json | 2 +- scripts/update-prod-hashbang.mjs | 15 +++++++++++++++ test/general-cli.mjs | 2 ++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 scripts/update-prod-hashbang.mjs diff --git a/package.json b/package.json index ecc650dc..2f2785dd 100644 --- a/package.json +++ b/package.json @@ -71,7 +71,7 @@ "test:ci": "mocha --recursive ./test --require ./test/mocha-root-hooks.mjs --reporter mocha-multi-reporters --reporter-options configFile=./test/config/reporter.json", "build": "npm run build:app && npm run build:sea", "build:app": "esbuild --bundle ./src/user-entrypoint.mjs --platform=node --outfile=./dist/cli.cjs --format=cjs --inject:./sea/import-meta-url.js --define:import.meta.url=importMetaUrl", - "postbuild:app": "sed -i '' '1s/.*/#!\\/usr\\/bin\\/env NODE_NO_WARNINGS=1 node/' ./dist/cli.cjs", + "postbuild:app": "node ./scripts/update-prod-hashbang.mjs", "build:sea": "node ./sea/build.cjs", "format": "prettier -w ." }, diff --git a/scripts/update-prod-hashbang.mjs b/scripts/update-prod-hashbang.mjs new file mode 100644 index 00000000..350412b5 --- /dev/null +++ b/scripts/update-prod-hashbang.mjs @@ -0,0 +1,15 @@ +#!/usr/bin/env node +import * as fs from "node:fs"; +import * as path from "node:path"; + +const __dirname = import.meta.dirname; + +const filePath = path.resolve(path.join(__dirname, "../dist/cli.cjs")); +let fileContents = fs.readFileSync(filePath); +const endOfFirstLine = fileContents.indexOf("\n"); +fileContents = fileContents.slice(endOfFirstLine); +fileContents = `#!/usr/bin/env NODE_NO_WARNINGS=1 node\n${fileContents}`; +fs.rmSync(filePath); +fs.writeFileSync(filePath, fileContents, { + mode: 755, +}); diff --git a/test/general-cli.mjs b/test/general-cli.mjs index 81c3a1cb..971e95c4 100644 --- a/test/general-cli.mjs +++ b/test/general-cli.mjs @@ -128,6 +128,7 @@ describe("cli operations", function () { it("enables nodeJS warnings from the dev entrypoint", async function () { let stderr = spawnSync("./src/user-entrypoint.mjs", ["warn"], { encoding: "utf8", + shell: true, stdio: "pipe", }).stderr; @@ -140,6 +141,7 @@ describe("cli operations", function () { it("suppresses nodeJS warnings from the prod entrypoint", async function () { let stderr = spawnSync("./dist/cli.cjs", ["warn"], { encoding: "utf8", + shell: true, stdio: "pipe", }).stderr;