Two failures of scripts/build.mjs did not say what to change. `node scripts/build.mjs --outt /some/dir` ignored the misspelt flag, exited 0 and replaced the tracked dist/. Without devDependencies (npm install --omit=dev in a clone runs "prepare") the script died with a stack trace, because typescript was resolved at module level, outside the try block. The script now takes no arguments or `--out <dir>` and answers anything else with one line and the usage, exit 1. A missing compiler gives "build: typescript is not installed: run npm install (it is a devDependency)". The body moved into build(args), so the staging directory and the compiler path are no longer module-level variables. Two tests on a temporary package: three refused argument lists, and a package without node_modules; both compare the package before and after. No change to dist/. npm test: 30 pass in 9.5-10.2 s (load average 7 on 4 cores). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
72 lines
3.3 KiB
JavaScript
72 lines
3.3 KiB
JavaScript
// npm run build - the only writer of dist/.
|
|
//
|
|
// node scripts/build.mjs [--out <dir>] <dir> defaults to dist/ in the package
|
|
//
|
|
// Two compiler runs over src/: CommonJS with its declarations (cjs/, types/), then ES modules with theirs (esm/).
|
|
// TypeScript reads a .d.ts as CommonJS or as an ES module by the package.json above it, so each build needs its own
|
|
// set: the one in types/ would tell an ES module consumer that a default import works.
|
|
//
|
|
// Both runs write to a staging directory that replaces the three directories only after both passed, so a failed
|
|
// build leaves the tracked dist/ as it was. Files of sources that no longer exist go with the old directories.
|
|
import { spawnSync } from "node:child_process";
|
|
import { mkdirSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
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 <dir>]";
|
|
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
|
|
|
function outputDirectory(args) {
|
|
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 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)");
|
|
}
|
|
}
|
|
|
|
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.
|
|
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 });
|
|
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;
|
|
}
|