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>
111 lines
5.1 KiB
JavaScript
111 lines
5.1 KiB
JavaScript
"use strict";
|
|
// scripts/build.mjs on a package of its own. The script takes src/, the tsconfig files and typescript from where it
|
|
// is, so a copy of it builds the directory it is put in and the tracked dist/ is never written.
|
|
const { test } = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs");
|
|
const os = require("node:os");
|
|
const path = require("node:path");
|
|
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. 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"]) {
|
|
fs.mkdirSync(path.join(dir, path.dirname(file)), { recursive: true });
|
|
if (file.startsWith("src/")) fs.writeFileSync(path.join(dir, file), source);
|
|
else fs.copyFileSync(path.join(ROOT, file), path.join(dir, file));
|
|
}
|
|
for (const part of ["cjs", "esm", "types"]) {
|
|
fs.mkdirSync(path.join(dir, "dist", part), { recursive: true });
|
|
fs.writeFileSync(path.join(dir, "dist", part, "earlier.txt"), `${part} of the earlier build\n`);
|
|
}
|
|
if (installed) fs.symlinkSync(path.join(ROOT, "node_modules"), path.join(dir, "node_modules"), "dir");
|
|
return dir;
|
|
}
|
|
|
|
// Every directory and file under dir with its content, node_modules left out.
|
|
function snapshot(dir, prefix = "") {
|
|
const found = {};
|
|
for (const entry of fs.readdirSync(path.join(dir, prefix), { withFileTypes: true })) {
|
|
const name = path.join(prefix, entry.name);
|
|
if (name === "node_modules") continue;
|
|
if (!entry.isDirectory()) found[name] = fs.readFileSync(path.join(dir, name), "utf8");
|
|
else Object.assign(found, { [`${name}/`]: "" }, snapshot(dir, name));
|
|
}
|
|
return found;
|
|
}
|
|
|
|
function build(dir, ...args) {
|
|
return spawnSync(process.execPath, [path.join(dir, "scripts", "build.mjs"), ...args], { encoding: "utf8" });
|
|
}
|
|
|
|
test("a build replaces dist/ whole and clears the staging directory a killed build left beside it", (t) => {
|
|
const dir = scratchPackage(t, "export const port = 1883;\n");
|
|
fs.mkdirSync(path.join(dir, ".dist-staging", "cjs"), { recursive: true });
|
|
fs.writeFileSync(path.join(dir, ".dist-staging", "cjs", "Killed.js"), "");
|
|
|
|
const run = build(dir);
|
|
assert.equal(run.status, 0, run.stdout + run.stderr);
|
|
const inputs = /^(scripts|src|tsconfig)/;
|
|
assert.deepEqual(Object.keys(snapshot(dir)).filter((name) => !inputs.test(name)).sort(), [
|
|
"dist/",
|
|
"dist/cjs/", "dist/cjs/index.js",
|
|
"dist/esm/", "dist/esm/index.d.ts", "dist/esm/index.js", "dist/esm/package.json",
|
|
"dist/types/", "dist/types/index.d.ts",
|
|
]);
|
|
});
|
|
|
|
test("a type error in src/: the build exits 1 and dist/ is as the earlier build left it", (t) => {
|
|
const dir = scratchPackage(t, "export const port: number = \"1883\";\n");
|
|
const before = snapshot(dir);
|
|
|
|
const run = build(dir);
|
|
assert.equal(run.status, 1, run.stdout + run.stderr);
|
|
assert.match(run.stdout, /error TS2322/);
|
|
assert.match(run.stderr, /^build: tsc -p tsconfig\.cjs\.json failed/m);
|
|
assert.deepEqual(snapshot(dir), before);
|
|
});
|
|
|
|
test("the second compiler run fails: what the first one wrote is dropped, dist/ is as the earlier build left it", (t) => {
|
|
// "export =" compiles as CommonJS and is refused in an ES module (TS1203)
|
|
const dir = scratchPackage(t, "const port = 1883;\nexport = port;\n");
|
|
const before = snapshot(dir);
|
|
|
|
const run = build(dir);
|
|
assert.equal(run.status, 1, run.stdout + run.stderr);
|
|
assert.match(run.stdout, /error TS1203/);
|
|
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 <dir>]\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);
|
|
});
|