diff --git a/scripts/build.mjs b/scripts/build.mjs index 3124b54..e170965 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -14,42 +14,59 @@ import { createRequire } from "node:module"; import { basename, dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; +const USAGE = "usage: node scripts/build.mjs [--out ]"; const root = resolve(dirname(fileURLToPath(import.meta.url)), ".."); -const tsc = createRequire(import.meta.url).resolve("typescript/bin/tsc"); function outputDirectory(args) { - const at = args.indexOf("--out"); - if (at === -1) return join(root, "dist"); - if (!args[at + 1]) throw new Error("--out needs a directory"); - return resolve(args[at + 1]); + if (args.length === 0) return join(root, "dist"); + const [flag, dir, ...rest] = args; + // A misspelt --out must not fall back to the default and replace the tracked dist/ + if (flag !== "--out") throw new Error(`unknown argument ${flag}; ${USAGE}`); + if (!dir) throw new Error(`--out needs a directory; ${USAGE}`); + if (rest.length > 0) throw new Error(`unknown argument ${rest[0]}; ${USAGE}`); + return resolve(dir); } -function compile(project, options) { - const run = spawnSync(process.execPath, [tsc, "-p", join(root, project), ...options], { cwd: root, stdio: "inherit" }); - if (run.error) throw run.error; - if (run.status !== 0) throw new Error(`tsc -p ${project} failed, ${out} is unchanged`); +function compiler() { + try { + return createRequire(import.meta.url).resolve("typescript/bin/tsc"); + } catch (err) { + if (err.code !== "MODULE_NOT_FOUND") throw err; + throw new Error("typescript is not installed: run npm install (it is a devDependency)"); + } } -let out; -let staging; -try { - out = outputDirectory(process.argv.slice(2)); +function build(args) { + const out = outputDirectory(args); + const tsc = compiler(); // Beside the output, not in it: the package ships all of dist/, and a build that is killed cannot remove its // staging directory - the next build does. Same parent directory, so the renames below stay on one filesystem. - staging = join(dirname(out), `.${basename(out)}-staging`); + const staging = join(dirname(out), `.${basename(out)}-staging`); + const compile = (project, options) => { + const run = spawnSync(process.execPath, [tsc, "-p", join(root, project), ...options], { cwd: root, stdio: "inherit" }); + if (run.error) throw run.error; + if (run.status !== 0) throw new Error(`tsc -p ${project} failed, ${out} is unchanged`); + }; + rmSync(staging, { recursive: true, force: true }); - compile("tsconfig.cjs.json", ["--outDir", join(staging, "cjs"), "--declarationDir", join(staging, "types")]); - compile("tsconfig.esm.json", ["--outDir", join(staging, "esm")]); - // The package is "type": "commonjs"; this marks the .js files under esm/ as ES modules. - writeFileSync(join(staging, "esm", "package.json"), JSON.stringify({ type: "module" }, null, 2) + "\n"); - mkdirSync(out, { recursive: true }); - for (const part of ["cjs", "esm", "types"]) { - rmSync(join(out, part), { recursive: true, force: true }); - renameSync(join(staging, part), join(out, part)); + try { + compile("tsconfig.cjs.json", ["--outDir", join(staging, "cjs"), "--declarationDir", join(staging, "types")]); + compile("tsconfig.esm.json", ["--outDir", join(staging, "esm")]); + // The package is "type": "commonjs"; this marks the .js files under esm/ as ES modules. + writeFileSync(join(staging, "esm", "package.json"), JSON.stringify({ type: "module" }, null, 2) + "\n"); + mkdirSync(out, { recursive: true }); + for (const part of ["cjs", "esm", "types"]) { + rmSync(join(out, part), { recursive: true, force: true }); + renameSync(join(staging, part), join(out, part)); + } + } finally { + rmSync(staging, { recursive: true, force: true }); } +} + +try { + build(process.argv.slice(2)); } catch (err) { console.error(`build: ${err.message}`); process.exitCode = 1; -} finally { - if (staging) rmSync(staging, { recursive: true, force: true }); } diff --git a/test/build.test.js b/test/build.test.js index 4df325e..281f6f8 100644 --- a/test/build.test.js +++ b/test/build.test.js @@ -11,8 +11,8 @@ const { spawnSync } = require("node:child_process"); const ROOT = path.join(__dirname, ".."); // A package directory with the build script, the three tsconfig files, src/index.ts and the dist/ of an earlier -// build; removed when the test ends. -function scratchPackage(t, source) { +// build; removed when the test ends. installed: false leaves node_modules out, as before npm install. +function scratchPackage(t, source, { installed = true } = {}) { const dir = fs.mkdtempSync(path.join(os.tmpdir(), "alex2node-build-")); t.after(() => fs.rmSync(dir, { recursive: true, force: true })); for (const file of ["scripts/build.mjs", "tsconfig.json", "tsconfig.cjs.json", "tsconfig.esm.json", "src/index.ts"]) { @@ -24,7 +24,7 @@ function scratchPackage(t, source) { fs.mkdirSync(path.join(dir, "dist", part), { recursive: true }); fs.writeFileSync(path.join(dir, "dist", part, "earlier.txt"), `${part} of the earlier build\n`); } - fs.symlinkSync(path.join(ROOT, "node_modules"), path.join(dir, "node_modules"), "dir"); + if (installed) fs.symlinkSync(path.join(ROOT, "node_modules"), path.join(dir, "node_modules"), "dir"); return dir; } @@ -82,3 +82,30 @@ test("the second compiler run fails: what the first one wrote is dropped, dist/ assert.match(run.stderr, /^build: tsc -p tsconfig\.esm\.json failed/m); assert.deepEqual(snapshot(dir), before); }); + +test("an argument the script does not know is refused: a misspelt --out must not build into dist/", (t) => { + const dir = scratchPackage(t, "export const port = 1883;\n"); + const before = snapshot(dir); + const elsewhere = path.join(dir, "elsewhere"); + const refused = [ + [["--outt", elsewhere], "unknown argument --outt"], + [["--out"], "--out needs a directory"], + [["--out", elsewhere, "--watch"], "unknown argument --watch"], + ]; + for (const [args, reason] of refused) { + const run = build(dir, ...args); + assert.equal(run.status, 1, args.join(" ")); + assert.equal(run.stderr, `build: ${reason}; usage: node scripts/build.mjs [--out ]\n`); + assert.deepEqual(snapshot(dir), before, args.join(" ")); + } +}); + +test("without typescript the build says what to install, in one line", (t) => { + const dir = scratchPackage(t, "export const port = 1883;\n", { installed: false }); + const before = snapshot(dir); + + const run = build(dir); + assert.equal(run.status, 1); + assert.equal(run.stderr, "build: typescript is not installed: run npm install (it is a devDependency)\n"); + assert.deepEqual(snapshot(dir), before); +});