@antelopejs/tooling-configs 0.5.1 → 0.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -9
- package/dist/bin/api-diff.mjs +3 -3
- package/dist/bin/body-check.mjs +2 -2
- package/dist/bin/check-size.mjs +1 -1
- package/dist/bin/checks.mjs +2 -774
- package/dist/bin/comment-proof.mjs +2 -2
- package/dist/bin/garden.mjs +221 -215
- package/dist/bin/lease.mjs +1 -1
- package/dist/bin/ratchet.mjs +4 -4
- package/dist/bin/suppressions.mjs +4 -4
- package/dist/bin/sync.mjs +624 -557
- package/dist/checks-BFGuy1KI.mjs +1506 -0
- package/dist/oxc/lint.mjs +1 -1
- package/dist/{oxlint-DIV-lLSS.mjs → oxlint-BbpTFFmf.mjs} +7 -2
- package/dist/{package-Dn7VTFUW.mjs → package-DiaBP7bI.mjs} +65 -15
- package/dist/{proof-stamp-DLWpPwQE.mjs → proof-stamp-BPIdT5ik.mjs} +2 -2
- package/dist/{ratchet-ClfblHNJ.mjs → ratchet-BZvlFCWL.mjs} +3 -3
- package/dist/{suppression-base-Drnl6Y7F.mjs → suppression-base-DZgUDXiI.mjs} +2 -2
- package/dist/{suppression-budget-Dvb_IqBo.mjs → suppression-budget-DgYdu6JK.mjs} +1 -1
- package/dist/{suppressions-BroaaGJ_.mjs → suppressions-CQTcKCi1.mjs} +1 -1
- package/dist/{templates-lJBZFskp.mjs → templates-C6cfOd2H.mjs} +1 -1
- package/dist/{typescript-BBbry7uU.mjs → typescript-DJzdex32.mjs} +1 -1
- package/dist/{ci-workflow-COJUT12b.mjs → workflow-CPcNc8KL.mjs} +112 -109
- package/package.json +3 -2
|
@@ -0,0 +1,1506 @@
|
|
|
1
|
+
import { t as CEILINGS_FILE } from "./ceilings-B7KayYIG.mjs";
|
|
2
|
+
import { o as packageManager, s as parseManifest } from "./package-DiaBP7bI.mjs";
|
|
3
|
+
import { a as gitFiles } from "./files-DJ0TeaTU.mjs";
|
|
4
|
+
import { t as accepting } from "./ports-DGP6bPJy.mjs";
|
|
5
|
+
import { createRequire } from "node:module";
|
|
6
|
+
import { existsSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
7
|
+
import { basename, dirname, join, posix, relative, resolve, sep } from "node:path";
|
|
8
|
+
import { spawn } from "node:child_process";
|
|
9
|
+
import { isMap, isScalar, isSeq, parseAllDocuments, parseDocument } from "yaml";
|
|
10
|
+
import ts from "typescript";
|
|
11
|
+
import { createHash } from "node:crypto";
|
|
12
|
+
import { tmpdir } from "node:os";
|
|
13
|
+
import { minimatch } from "minimatch";
|
|
14
|
+
//#region src/guardrails/run-command.ts
|
|
15
|
+
const KILL_GRACE_MS = 5e3;
|
|
16
|
+
function stopGroup(child, signal) {
|
|
17
|
+
if (child.pid === void 0 || child.exitCode !== null) return;
|
|
18
|
+
try {
|
|
19
|
+
if (process.platform === "win32") child.kill(signal);
|
|
20
|
+
else process.kill(-child.pid, signal);
|
|
21
|
+
} catch {
|
|
22
|
+
child.kill(signal);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Runs `command` in its own process group and collects stdout and stderr together. When `signal` aborts, the group
|
|
27
|
+
* gets SIGTERM, then SIGKILL after a grace period, so a step's own children stop with it. On Windows the command runs
|
|
28
|
+
* through a shell, so `npm` and `pnpm` resolve to their `.cmd` shims, unless `shell` is `false`: pass that for an
|
|
29
|
+
* executable path and arguments that a shell must not split, such as paths with spaces.
|
|
30
|
+
*/
|
|
31
|
+
function runCommand(command, args, options) {
|
|
32
|
+
const { cwd, env = process.env, signal, shell = process.platform === "win32" } = options;
|
|
33
|
+
return new Promise((resolve) => {
|
|
34
|
+
const child = spawn(command, args, {
|
|
35
|
+
cwd,
|
|
36
|
+
env,
|
|
37
|
+
shell,
|
|
38
|
+
detached: process.platform !== "win32",
|
|
39
|
+
stdio: [
|
|
40
|
+
"ignore",
|
|
41
|
+
"pipe",
|
|
42
|
+
"pipe"
|
|
43
|
+
]
|
|
44
|
+
});
|
|
45
|
+
const chunks = [];
|
|
46
|
+
const stdout = [];
|
|
47
|
+
child.stdout.on("data", (chunk) => {
|
|
48
|
+
chunks.push(chunk);
|
|
49
|
+
stdout.push(chunk);
|
|
50
|
+
});
|
|
51
|
+
child.stderr.on("data", (chunk) => chunks.push(chunk));
|
|
52
|
+
const interrupt = () => {
|
|
53
|
+
stopGroup(child, "SIGTERM");
|
|
54
|
+
setTimeout(() => stopGroup(child, "SIGKILL"), KILL_GRACE_MS).unref();
|
|
55
|
+
};
|
|
56
|
+
if (signal?.aborted) interrupt();
|
|
57
|
+
else signal?.addEventListener("abort", interrupt, { once: true });
|
|
58
|
+
child.once("error", (error) => {
|
|
59
|
+
signal?.removeEventListener("abort", interrupt);
|
|
60
|
+
resolve({
|
|
61
|
+
status: null,
|
|
62
|
+
stdout: "",
|
|
63
|
+
output: `${error.message}\n`
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
child.once("close", (code, killed) => {
|
|
67
|
+
signal?.removeEventListener("abort", interrupt);
|
|
68
|
+
const text = Buffer.concat(chunks).toString("utf8");
|
|
69
|
+
resolve({
|
|
70
|
+
status: code,
|
|
71
|
+
stdout: Buffer.concat(stdout).toString("utf8"),
|
|
72
|
+
output: `${text}${killed ? `Terminated by ${killed}\n` : ""}`
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
//#endregion
|
|
78
|
+
//#region src/guardrails/git-identity.ts
|
|
79
|
+
/** Config scopes stored inside the repository, which every checkout and agent sharing it commits under. */
|
|
80
|
+
const SHARED_SCOPES = /* @__PURE__ */ new Set(["local", "worktree"]);
|
|
81
|
+
/** Whether `root` is a git checkout, a `.git` folder or a linked worktree's `.git` file. */
|
|
82
|
+
function isGitCheckout(root) {
|
|
83
|
+
return existsSync(join(root, ".git"));
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Fails when the repository's own git config, its `.git/config` shared by every worktree or a worktree's
|
|
87
|
+
* `config.worktree`, sets `user.name` or `user.email`: whoever commits in any checkout of it then signs as that identity.
|
|
88
|
+
* Global, system and command-line settings pass. Returns what failed, naming each file and value, empty when it passed.
|
|
89
|
+
*/
|
|
90
|
+
async function checkGitIdentity(root, signal) {
|
|
91
|
+
const result = await runCommand("git", [
|
|
92
|
+
"config",
|
|
93
|
+
"--show-scope",
|
|
94
|
+
"--show-origin",
|
|
95
|
+
"--get-regexp",
|
|
96
|
+
String.raw`^user\.(name|email)$`
|
|
97
|
+
], {
|
|
98
|
+
cwd: root,
|
|
99
|
+
signal,
|
|
100
|
+
shell: false
|
|
101
|
+
});
|
|
102
|
+
if (result.status === 1) return "";
|
|
103
|
+
if (result.status !== 0) return result.output;
|
|
104
|
+
const shared = result.stdout.split("\n").filter((line) => SHARED_SCOPES.has(line.split(" ")[0] ?? ""));
|
|
105
|
+
if (!shared.length) return "";
|
|
106
|
+
return `The repository's git config sets a commit identity, so every checkout and agent sharing it commits as that identity:\n${shared.map((line) => ` ${line.split(" ").slice(1).join(" ")}\n`).join("")}Remove it with git config --local --unset-all user.name and git config --local --unset-all user.email (--worktree for a config.worktree), and set an identity with --global or per command with git -c.\n`;
|
|
107
|
+
}
|
|
108
|
+
//#endregion
|
|
109
|
+
//#region src/guardrails/services.ts
|
|
110
|
+
/** The compose file the shared CI starts through its `services-compose-file` input, relative to the repository root. */
|
|
111
|
+
const COMPOSE_FILE = ".github/compose.yml";
|
|
112
|
+
function shortPort(text) {
|
|
113
|
+
const parts = text.replace(/\/\w+$/, "").split(":");
|
|
114
|
+
return parts.length < 2 ? void 0 : wholePort(parts.at(-2));
|
|
115
|
+
}
|
|
116
|
+
function wholePort(text) {
|
|
117
|
+
return text !== void 0 && /^\d+$/.test(text) ? Number(text) : void 0;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* The host ports the compose file publishes, or `undefined` when a service publishes none or one this cannot read
|
|
121
|
+
* (a range, an interpolation, a container-only port), since such a service cannot be probed.
|
|
122
|
+
*/
|
|
123
|
+
function publishedPorts(text) {
|
|
124
|
+
const services = parseDocument(text).get("services");
|
|
125
|
+
if (!isMap(services) || services.items.length === 0) return void 0;
|
|
126
|
+
const ports = [];
|
|
127
|
+
for (const { value } of services.items) {
|
|
128
|
+
const entries = isMap(value) ? value.get("ports") : void 0;
|
|
129
|
+
if (!isSeq(entries) || entries.items.length === 0) return void 0;
|
|
130
|
+
for (const entry of entries.items) {
|
|
131
|
+
const published = isMap(entry) ? entry.get("published") : void 0;
|
|
132
|
+
const port = isScalar(entry) ? shortPort(String(entry.value)) : wholePort(published === void 0 ? void 0 : String(published));
|
|
133
|
+
if (port === void 0) return void 0;
|
|
134
|
+
ports.push(port);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return ports;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* The environment compose runs in. A project named by `COMPOSE_PROJECT_NAME` or by the compose file's own `name:` is
|
|
141
|
+
* kept; otherwise the project is named after the repository root, since compose would name every repository's project
|
|
142
|
+
* `github` after the compose file's directory and a run could adopt or remove another checkout's containers.
|
|
143
|
+
*/
|
|
144
|
+
function composeEnv(root) {
|
|
145
|
+
if (process.env.COMPOSE_PROJECT_NAME) return process.env;
|
|
146
|
+
if (parseDocument(readFileSync(join(root, COMPOSE_FILE), "utf8")).has("name")) return process.env;
|
|
147
|
+
const slug = basename(root).toLowerCase().replace(/[^a-z0-9_-]+/g, "-").replace(/^[^a-z0-9]+/, "");
|
|
148
|
+
const digest = createHash("sha256").update(root).digest("hex").slice(0, 8);
|
|
149
|
+
return {
|
|
150
|
+
...process.env,
|
|
151
|
+
COMPOSE_PROJECT_NAME: `${slug || "repo"}-${digest}`
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
async function compose(root, args, signal) {
|
|
155
|
+
const result = await runCommand("docker", [
|
|
156
|
+
"compose",
|
|
157
|
+
"-f",
|
|
158
|
+
COMPOSE_FILE,
|
|
159
|
+
...args
|
|
160
|
+
], {
|
|
161
|
+
cwd: root,
|
|
162
|
+
env: composeEnv(root),
|
|
163
|
+
signal
|
|
164
|
+
});
|
|
165
|
+
if (result.status === 0) return {
|
|
166
|
+
failure: "",
|
|
167
|
+
stdout: result.stdout
|
|
168
|
+
};
|
|
169
|
+
const outcome = result.status === null ? "did not finish" : `exited with status ${result.status}`;
|
|
170
|
+
return {
|
|
171
|
+
failure: `${result.output}docker compose -f ${COMPOSE_FILE} ${args.join(" ")}: ${outcome}\n`,
|
|
172
|
+
stdout: result.stdout
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Decides whether the checks start the services of `.github/compose.yml`: not when the file is absent or every port
|
|
177
|
+
* it publishes already accepts connections, and fail at once when Docker Compose cannot run, because the
|
|
178
|
+
* tests that need those services would wait for them forever.
|
|
179
|
+
*/
|
|
180
|
+
async function planServices(root) {
|
|
181
|
+
if (!existsSync(join(root, COMPOSE_FILE))) return { kind: "none" };
|
|
182
|
+
const ports = publishedPorts(readFileSync(join(root, COMPOSE_FILE), "utf8"));
|
|
183
|
+
if (ports && (await Promise.all(ports.map(accepting))).every(Boolean)) return { kind: "none" };
|
|
184
|
+
const probe = await compose(root, ["version"]);
|
|
185
|
+
if (!probe.failure) return { kind: "start" };
|
|
186
|
+
return {
|
|
187
|
+
kind: "unavailable",
|
|
188
|
+
output: `Docker Compose is not available, so the services in ${COMPOSE_FILE} cannot start and the tests that need them would hang. Start Docker, start the services yourself, or pass --no-services.\n${probe.failure}`
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Runs `docker compose up -d --wait` on the compose file, stopping it when `signal` aborts. `failure` is empty on
|
|
193
|
+
* success; `owned` is true when the project had no container before, so the run may remove it afterwards without
|
|
194
|
+
* taking a stack the developer started.
|
|
195
|
+
*/
|
|
196
|
+
async function startServices(root, signal) {
|
|
197
|
+
const before = await compose(root, [
|
|
198
|
+
"ps",
|
|
199
|
+
"--all",
|
|
200
|
+
"--quiet"
|
|
201
|
+
], signal);
|
|
202
|
+
if (before.failure) return {
|
|
203
|
+
failure: before.failure,
|
|
204
|
+
owned: false
|
|
205
|
+
};
|
|
206
|
+
const owned = before.stdout.trim() === "";
|
|
207
|
+
return {
|
|
208
|
+
failure: (await compose(root, [
|
|
209
|
+
"up",
|
|
210
|
+
"-d",
|
|
211
|
+
"--wait"
|
|
212
|
+
], signal)).failure,
|
|
213
|
+
owned
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
/** Runs `docker compose down -v` on the compose file; returns the failure output, empty on success. */
|
|
217
|
+
async function stopServices(root) {
|
|
218
|
+
return (await compose(root, ["down", "-v"])).failure;
|
|
219
|
+
}
|
|
220
|
+
//#endregion
|
|
221
|
+
//#region src/guardrails/verify-layers.ts
|
|
222
|
+
const SKILLS_FOLDER = ".agents/skills";
|
|
223
|
+
const LAYER_PREFIX = "verify-";
|
|
224
|
+
const TSCONFIG = "tsconfig.json";
|
|
225
|
+
/** Folders of a layer that are not the layer's own code: verify builds its playground with the playground's own tsconfig. */
|
|
226
|
+
const NOT_LAYER_CODE = /* @__PURE__ */ new Set(["node_modules", "dist"]);
|
|
227
|
+
const LAYER_PLAYGROUND = "playground";
|
|
228
|
+
const VERIFY_CONFIG = "verify.config.ts";
|
|
229
|
+
/**
|
|
230
|
+
* Options the generated config sets over the layer's: nothing is emitted, and each declaration or build-cache option the
|
|
231
|
+
* layer may inherit is unset with `null`, so no `.tsbuildinfo` is written, no option is left needing `declaration` or
|
|
232
|
+
* `composite`, and the declaration checks of files that are never emitted are not run.
|
|
233
|
+
*/
|
|
234
|
+
const NO_OUTPUT = {
|
|
235
|
+
noEmit: true,
|
|
236
|
+
declaration: null,
|
|
237
|
+
composite: null,
|
|
238
|
+
incremental: null,
|
|
239
|
+
tsBuildInfoFile: null,
|
|
240
|
+
declarationDir: null,
|
|
241
|
+
declarationMap: null,
|
|
242
|
+
emitDeclarationOnly: null,
|
|
243
|
+
isolatedDeclarations: null
|
|
244
|
+
};
|
|
245
|
+
/** The verify layers of a repository, relative to its root: each `.agents/skills/verify-*` folder that holds a `tsconfig.json` or a `verify.config.ts`. */
|
|
246
|
+
function verifyLayers(root) {
|
|
247
|
+
const skills = join(root, SKILLS_FOLDER);
|
|
248
|
+
if (!existsSync(skills)) return [];
|
|
249
|
+
return readdirSync(skills, { withFileTypes: true }).filter((entry) => entry.isDirectory() && entry.name.startsWith(LAYER_PREFIX) && [TSCONFIG, VERIFY_CONFIG].some((file) => existsSync(join(skills, entry.name, file)))).map((entry) => `${SKILLS_FOLDER}/${entry.name}`).sort();
|
|
250
|
+
}
|
|
251
|
+
/** Whether a file the layer's tsconfig includes is the layer's own code, not its playground, a dependency or a build output. */
|
|
252
|
+
function isLayerCode(layer, file) {
|
|
253
|
+
const segments = relative(layer, file).split(sep);
|
|
254
|
+
return segments[0] !== LAYER_PLAYGROUND && !segments.some((segment) => NOT_LAYER_CODE.has(segment));
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* The repository's own `tsc` entry, run with Node so no shell parses its path. It is found through the `bin.tsc` field of
|
|
258
|
+
* `typescript/package.json`, a subpath every TypeScript version exports, because TypeScript 7 declares an `exports` map that
|
|
259
|
+
* leaves `typescript/bin/tsc` out.
|
|
260
|
+
*/
|
|
261
|
+
function compilerOf(root) {
|
|
262
|
+
try {
|
|
263
|
+
const manifestPath = createRequire(join(root, "package.json")).resolve("typescript/package.json");
|
|
264
|
+
const tsc = parseManifest(readFileSync(manifestPath, "utf8")).bin?.tsc;
|
|
265
|
+
return tsc === void 0 ? void 0 : join(dirname(manifestPath), tsc);
|
|
266
|
+
} catch {
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
async function typecheckLayer(root, compiler, layer, signal) {
|
|
271
|
+
const configPath = join(root, layer, TSCONFIG);
|
|
272
|
+
if (!existsSync(configPath)) return `${layer}: has a verify.config.ts and no tsconfig.json. Add a tsconfig.json that includes its TypeScript files; antelopejs-sync writes one for the shared kinds.\n`;
|
|
273
|
+
const ts = (await import("typescript")).default;
|
|
274
|
+
const parsed = ts.getParsedCommandLineOfConfigFile(configPath, void 0, {
|
|
275
|
+
...ts.sys,
|
|
276
|
+
onUnRecoverableConfigFileDiagnostic: () => void 0
|
|
277
|
+
});
|
|
278
|
+
if (parsed === void 0) return `${layer}/${TSCONFIG}: could not read it.\n`;
|
|
279
|
+
const verifyConfig = join(root, layer, VERIFY_CONFIG);
|
|
280
|
+
if (existsSync(verifyConfig) && !parsed.fileNames.some((file) => resolve(file) === verifyConfig)) return `${layer}/${TSCONFIG}: does not include ${VERIFY_CONFIG}, so it is never type-checked. Add it to the tsconfig's "include" or "files".\n`;
|
|
281
|
+
const files = parsed.fileNames.filter((file) => isLayerCode(join(root, layer), resolve(file)));
|
|
282
|
+
if (files.length === 0) return "";
|
|
283
|
+
const folder = mkdtempSync(join(tmpdir(), "antelopejs-verify-types-"));
|
|
284
|
+
try {
|
|
285
|
+
const generated = join(folder, TSCONFIG);
|
|
286
|
+
writeFileSync(generated, JSON.stringify({
|
|
287
|
+
extends: configPath,
|
|
288
|
+
compilerOptions: {
|
|
289
|
+
...NO_OUTPUT,
|
|
290
|
+
typeRoots: ts.getEffectiveTypeRoots(parsed.options, ts.sys)
|
|
291
|
+
},
|
|
292
|
+
files,
|
|
293
|
+
include: []
|
|
294
|
+
}));
|
|
295
|
+
const result = await runCommand(process.execPath, [
|
|
296
|
+
compiler,
|
|
297
|
+
"-p",
|
|
298
|
+
generated,
|
|
299
|
+
"--pretty",
|
|
300
|
+
"false"
|
|
301
|
+
], {
|
|
302
|
+
cwd: root,
|
|
303
|
+
env: {
|
|
304
|
+
...process.env,
|
|
305
|
+
CI: "1"
|
|
306
|
+
},
|
|
307
|
+
signal,
|
|
308
|
+
shell: false
|
|
309
|
+
});
|
|
310
|
+
return result.status === 0 ? "" : result.output;
|
|
311
|
+
} finally {
|
|
312
|
+
rmSync(folder, {
|
|
313
|
+
recursive: true,
|
|
314
|
+
force: true
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Type-checks the own code of each verify layer with the repository's own TypeScript. The files are those the layer's
|
|
320
|
+
* `tsconfig.json` includes, minus its `playground` folder, which verify compiles with the playground's own config after
|
|
321
|
+
* installing it, and any `node_modules` or `dist` folder. A config generated in the system temp folder extends the layer's,
|
|
322
|
+
* lists those files, turns off emit and unsets the emit and build-cache options in `NO_OUTPUT`, so nothing is written in the
|
|
323
|
+
* repository and no package is fetched. A layer whose `tsconfig.json` does not include its `verify.config.ts` fails. Returns the compiler output of the layers that fail, empty when all pass.
|
|
324
|
+
*/
|
|
325
|
+
async function typecheckLayers(root, signal) {
|
|
326
|
+
const layers = verifyLayers(root);
|
|
327
|
+
if (layers.length === 0) return "";
|
|
328
|
+
const compiler = compilerOf(root);
|
|
329
|
+
if (compiler === void 0) return "TypeScript is not installed in the repository root: run the package manager's install first.\n";
|
|
330
|
+
return (await Promise.all(layers.map((layer) => typecheckLayer(root, compiler, layer, signal)))).join("");
|
|
331
|
+
}
|
|
332
|
+
//#endregion
|
|
333
|
+
//#region src/guardrails/verify-lockfiles.ts
|
|
334
|
+
/** The folder a repository's playground lives in; verify installs a fixture project from it. */
|
|
335
|
+
const PLAYGROUND = "playground";
|
|
336
|
+
const PNPM_LOCK$1 = "pnpm-lock.yaml";
|
|
337
|
+
const NPM_LOCK = "package-lock.json";
|
|
338
|
+
const MANIFEST = "package.json";
|
|
339
|
+
const PNPM_WORKSPACE = "pnpm-workspace.yaml";
|
|
340
|
+
/** Lockfiles this check does not compare, which still mean a folder is locked. */
|
|
341
|
+
const OTHER_LOCKS = [
|
|
342
|
+
"npm-shrinkwrap.json",
|
|
343
|
+
"yarn.lock",
|
|
344
|
+
"bun.lock",
|
|
345
|
+
"bun.lockb"
|
|
346
|
+
];
|
|
347
|
+
/** The manifest fields both package managers record as a project's direct dependencies. */
|
|
348
|
+
const DIRECT_FIELDS = [
|
|
349
|
+
"dependencies",
|
|
350
|
+
"devDependencies",
|
|
351
|
+
"optionalDependencies"
|
|
352
|
+
];
|
|
353
|
+
/** A lockfile or manifest this check cannot read as a version it knows. */
|
|
354
|
+
var Unreadable = class extends Error {};
|
|
355
|
+
/** Parses a JSON or YAML file whose top level is a mapping; throws `Unreadable` otherwise. */
|
|
356
|
+
function readMapping(path) {
|
|
357
|
+
const document = parseDocument(readFileSync(path, "utf8"), {
|
|
358
|
+
schema: "failsafe",
|
|
359
|
+
uniqueKeys: false
|
|
360
|
+
});
|
|
361
|
+
const { contents } = document;
|
|
362
|
+
if (document.errors.length > 0 || !isMap(contents)) throw new Unreadable(path);
|
|
363
|
+
return contents;
|
|
364
|
+
}
|
|
365
|
+
/** The mapping under `key`; `undefined` when absent or not a mapping. */
|
|
366
|
+
function mapping(map, key) {
|
|
367
|
+
const value = map?.get(key, true);
|
|
368
|
+
return isMap(value) ? value : void 0;
|
|
369
|
+
}
|
|
370
|
+
/** The source text of the scalar under `key`; `undefined` when absent or not a scalar. */
|
|
371
|
+
function scalar(map, key) {
|
|
372
|
+
const value = map?.get(key, true);
|
|
373
|
+
return isScalar(value) ? String(value.value) : void 0;
|
|
374
|
+
}
|
|
375
|
+
/** The scalar keys of a mapping, in order; none when it is absent. */
|
|
376
|
+
function keys(map) {
|
|
377
|
+
return (map?.items ?? []).flatMap((pair) => isScalar(pair.key) ? [String(pair.key.value)] : []);
|
|
378
|
+
}
|
|
379
|
+
/** Reads a project's direct dependencies, with `specifierOf` reading the specifier of one name from its field. */
|
|
380
|
+
function directOf(node, specifierOf) {
|
|
381
|
+
const direct = /* @__PURE__ */ new Map();
|
|
382
|
+
for (const field of DIRECT_FIELDS) {
|
|
383
|
+
const entries = mapping(node, field);
|
|
384
|
+
for (const name of keys(entries)) {
|
|
385
|
+
const specifier = specifierOf(entries, name);
|
|
386
|
+
if (specifier === void 0) throw new Unreadable(name);
|
|
387
|
+
direct.set(name, /* @__PURE__ */ new Set([...direct.get(name) ?? [], specifier]));
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
return direct;
|
|
391
|
+
}
|
|
392
|
+
/** A `package.json` or a `package-lock.json` root entry, where each name maps to its specifier. */
|
|
393
|
+
function declared(node) {
|
|
394
|
+
return directOf(node, scalar);
|
|
395
|
+
}
|
|
396
|
+
/** A `pnpm-lock.yaml` importer, where each name maps to its `specifier` and resolved `version`. */
|
|
397
|
+
function pnpmLocked(node) {
|
|
398
|
+
return directOf(node, (field, name) => scalar(mapping(field, name), "specifier"));
|
|
399
|
+
}
|
|
400
|
+
function listed(specifiers) {
|
|
401
|
+
return specifiers === void 0 ? "none" : [...specifiers].map((specifier) => `"${specifier}"`).join(" or ");
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* The names whose specifiers differ between a manifest and what its lockfile records, in either direction. A name counts as
|
|
405
|
+
* present whichever of the three fields records it, and matches when the two sides share a specifier. A name the lockfile
|
|
406
|
+
* records and the manifest declares only as a peer passes, since the package manager may or may not install peers. `skip`
|
|
407
|
+
* names the manifest entries the lockfile may leave out or record with another specifier.
|
|
408
|
+
*/
|
|
409
|
+
function differences(manifest, locked, skip) {
|
|
410
|
+
const wanted = declared(manifest);
|
|
411
|
+
const peers = new Set(keys(mapping(manifest, "peerDependencies")));
|
|
412
|
+
const line = (name) => `"${name}": package.json asks for ${listed(wanted.get(name))}, the lockfile has ${listed(locked.get(name))}.`;
|
|
413
|
+
const stale = [...wanted].filter(([name, specifiers]) => ![...locked.get(name) ?? []].some((specifier) => specifiers.has(specifier)) && !skip(name, specifiers)).map(([name]) => line(name));
|
|
414
|
+
const extra = [...locked.keys()].filter((name) => !wanted.has(name) && !peers.has(name)).map(line);
|
|
415
|
+
return [...stale, ...extra].sort();
|
|
416
|
+
}
|
|
417
|
+
/** Every package name an override selector such as `foo`, `@scope/foo@^1` or `bar@1>foo@2` mentions, its target among them. */
|
|
418
|
+
function overrideNames(selector) {
|
|
419
|
+
return selector.split(">").flatMap((segment) => /^(?:@[^/@]+\/)?[^@]+/u.exec(segment) ?? []);
|
|
420
|
+
}
|
|
421
|
+
function pnpmDifferences(path) {
|
|
422
|
+
const lockfile = readMapping(join(path, PNPM_LOCK$1));
|
|
423
|
+
if (Number(scalar(lockfile, "lockfileVersion") ?? "0") < 6) throw new Unreadable(path);
|
|
424
|
+
const overridden = new Set(keys(mapping(lockfile, "overrides")).flatMap(overrideNames));
|
|
425
|
+
const linksLeftOut = scalar(mapping(lockfile, "settings"), "excludeLinksFromLockfile") === "true";
|
|
426
|
+
const skip = (name, specifiers) => overridden.has(name) || linksLeftOut && [...specifiers].every((specifier) => specifier.startsWith("link:"));
|
|
427
|
+
const importers = mapping(lockfile, "importers");
|
|
428
|
+
return (importers ? keys(importers) : ["."]).flatMap((importer) => {
|
|
429
|
+
const manifest = join(path, importer, MANIFEST);
|
|
430
|
+
if (!existsSync(manifest)) return [];
|
|
431
|
+
const node = importers ? mapping(importers, importer) : lockfile;
|
|
432
|
+
return differences(readMapping(manifest), pnpmLocked(node), skip).map((line) => `importer "${importer}" ${line}`);
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
function npmDifferences(path) {
|
|
436
|
+
const lockfile = readMapping(join(path, NPM_LOCK));
|
|
437
|
+
const rootEntry = mapping(mapping(lockfile, "packages"), "");
|
|
438
|
+
if (Number(scalar(lockfile, "lockfileVersion") ?? "0") < 2 || rootEntry === void 0) throw new Unreadable(path);
|
|
439
|
+
const manifest = join(path, MANIFEST);
|
|
440
|
+
if (!existsSync(manifest)) return [];
|
|
441
|
+
return differences(readMapping(manifest), declared(rootEntry), () => false);
|
|
442
|
+
}
|
|
443
|
+
function lockfileIn(path) {
|
|
444
|
+
if (existsSync(join(path, PNPM_LOCK$1))) return PNPM_LOCK$1;
|
|
445
|
+
if (existsSync(join(path, NPM_LOCK))) return NPM_LOCK;
|
|
446
|
+
return OTHER_LOCKS.some((name) => existsSync(join(path, name))) ? "other" : void 0;
|
|
447
|
+
}
|
|
448
|
+
/** Each folder at or under `folder`, `node_modules` and `dist` aside, that holds a lockfile. */
|
|
449
|
+
function lockedFolders(root, folder) {
|
|
450
|
+
const path = join(root, folder);
|
|
451
|
+
if (!existsSync(path)) return [];
|
|
452
|
+
const lockfile = lockfileIn(path);
|
|
453
|
+
return [...lockfile === void 0 ? [] : [{
|
|
454
|
+
folder,
|
|
455
|
+
lockfile
|
|
456
|
+
}], ...readdirSync(path, { withFileTypes: true }).filter((entry) => entry.isDirectory() && entry.name !== "node_modules" && entry.name !== "dist").flatMap((entry) => lockedFolders(root, `${folder}/${entry.name}`))];
|
|
457
|
+
}
|
|
458
|
+
/** Whether a `package.json` declares a direct dependency; `false` when it is missing or cannot be read, so this check cannot tell. */
|
|
459
|
+
function hasDependencies(path) {
|
|
460
|
+
try {
|
|
461
|
+
if (!existsSync(path)) return false;
|
|
462
|
+
const manifest = readMapping(path);
|
|
463
|
+
return DIRECT_FIELDS.some((field) => keys(mapping(manifest, field)).length > 0);
|
|
464
|
+
} catch (error) {
|
|
465
|
+
if (error instanceof Unreadable) return false;
|
|
466
|
+
throw error;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* The folders this check reads, when a layer exists: each folder under a layer, `node_modules` and `dist` aside, and the
|
|
471
|
+
* root `playground`, that holds a lockfile; and each layer's `playground` and the root `playground`, the folders verify
|
|
472
|
+
* installs from, when its `package.json` has dependencies and it holds no lockfile.
|
|
473
|
+
*/
|
|
474
|
+
function verifyInstallFolders(root) {
|
|
475
|
+
const layers = verifyLayers(root);
|
|
476
|
+
if (layers.length === 0) return [];
|
|
477
|
+
const playgrounds = [...layers.map((layer) => `${layer}/${PLAYGROUND}`), PLAYGROUND];
|
|
478
|
+
const rootPlayground = lockfileIn(join(root, PLAYGROUND));
|
|
479
|
+
return [
|
|
480
|
+
...layers.flatMap((layer) => lockedFolders(root, layer)),
|
|
481
|
+
...rootPlayground === void 0 ? [] : [{
|
|
482
|
+
folder: PLAYGROUND,
|
|
483
|
+
lockfile: rootPlayground
|
|
484
|
+
}],
|
|
485
|
+
...playgrounds.filter((folder) => lockfileIn(join(root, folder)) === void 0 && hasDependencies(join(root, folder, MANIFEST))).map((folder) => ({
|
|
486
|
+
folder,
|
|
487
|
+
lockfile: void 0
|
|
488
|
+
}))
|
|
489
|
+
];
|
|
490
|
+
}
|
|
491
|
+
/** The folders above `folder`, nearest first, up to the repository root. */
|
|
492
|
+
function ancestors(folder) {
|
|
493
|
+
const parent = posix.dirname(folder);
|
|
494
|
+
return parent === folder ? [] : [parent, ...ancestors(parent)];
|
|
495
|
+
}
|
|
496
|
+
/** Whether a manifest declares workspaces, or cannot be read and so might. */
|
|
497
|
+
function declaresWorkspaces(path) {
|
|
498
|
+
try {
|
|
499
|
+
return existsSync(path) && readMapping(path).has("workspaces");
|
|
500
|
+
} catch (error) {
|
|
501
|
+
if (error instanceof Unreadable) return true;
|
|
502
|
+
throw error;
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Whether a folder above `folder` holds a workspace that may list it, a `pnpm-workspace.yaml` or a `package.json` with
|
|
507
|
+
* `workspaces`: its membership then follows patterns this check does not evaluate, and that workspace's lockfile may hold it.
|
|
508
|
+
*/
|
|
509
|
+
function inWorkspace(root, folder) {
|
|
510
|
+
return ancestors(folder).some((above) => existsSync(join(root, above, PNPM_WORKSPACE)) || declaresWorkspaces(join(root, above, MANIFEST)));
|
|
511
|
+
}
|
|
512
|
+
function fixCommand(root, folder, lockfile) {
|
|
513
|
+
if (lockfile === NPM_LOCK) return `npm install --package-lock-only --prefix ${folder}`;
|
|
514
|
+
return existsSync(join(root, folder, PNPM_WORKSPACE)) ? `pnpm install --dir ${folder} --lockfile-only` : `pnpm install --dir ${folder} --ignore-workspace --lockfile-only`;
|
|
515
|
+
}
|
|
516
|
+
function unlocked(root, folder) {
|
|
517
|
+
if (inWorkspace(root, folder)) return "";
|
|
518
|
+
const manager = ancestors(folder).some((above) => existsSync(join(root, above, NPM_LOCK))) ? NPM_LOCK : PNPM_LOCK$1;
|
|
519
|
+
return `${folder}/${MANIFEST} has dependencies and no lockfile: run \`${fixCommand(root, folder, manager)}\`.\n`;
|
|
520
|
+
}
|
|
521
|
+
function checkFolder(root, { folder, lockfile }) {
|
|
522
|
+
if (lockfile === "other") return "";
|
|
523
|
+
try {
|
|
524
|
+
if (lockfile === void 0) return unlocked(root, folder);
|
|
525
|
+
const path = join(root, folder);
|
|
526
|
+
const found = lockfile === PNPM_LOCK$1 ? pnpmDifferences(path) : npmDifferences(path);
|
|
527
|
+
return found.length === 0 ? "" : `${folder}/${lockfile} does not match ${folder}/${MANIFEST}: run \`${fixCommand(root, folder, lockfile)}\`.\n${found.map((line) => ` ${line}\n`).join("")}`;
|
|
528
|
+
} catch (error) {
|
|
529
|
+
if (error instanceof Unreadable) return `${folder}/${lockfile ?? MANIFEST}: could not read it, or a ${MANIFEST} it locks, as a version this check knows.\n`;
|
|
530
|
+
throw error;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* Compares, in code, the direct dependencies each lockfile records with the `package.json` files it locks, and fails on a
|
|
535
|
+
* difference in either direction. A `pnpm-lock.yaml` is compared importer by importer, for each importer it lists that has a
|
|
536
|
+
* `package.json` on disk; a `package-lock.json` by its root entry. Only `dependencies`, `devDependencies` and
|
|
537
|
+
* `optionalDependencies` are compared, by name and specifier: a name counts as present whichever of the three fields records
|
|
538
|
+
* it, and specifiers such as `catalog:` or `workspace:^` compare as written. A name the lockfile records that `package.json`
|
|
539
|
+
* declares only in `peerDependencies` passes. A `package.json` name that an override selector in the `pnpm-lock.yaml`
|
|
540
|
+
* mentions, or a `link:` one the lockfile leaves out under `excludeLinksFromLockfile`, is not compared. A missing lockfile fails only in a layer's
|
|
541
|
+
* `playground` or the root `playground` whose `package.json` has dependencies, unless a folder above it holds a workspace.
|
|
542
|
+
*
|
|
543
|
+
* It spawns no package manager, writes no file and fetches nothing. It does not check workspace membership, overrides,
|
|
544
|
+
* settings, catalogs, `.npmrc` or user configuration, resolved versions, integrity hashes, transitive entries, npm workspace
|
|
545
|
+
* members, or `yarn.lock` and `bun.lock`: verify's own frozen install still rejects a lockfile stale there. Returns what
|
|
546
|
+
* failed, empty when every lockfile matches.
|
|
547
|
+
*/
|
|
548
|
+
function checkLockfiles(root) {
|
|
549
|
+
return verifyInstallFolders(root).map((install) => checkFolder(root, install)).join("");
|
|
550
|
+
}
|
|
551
|
+
//#endregion
|
|
552
|
+
//#region src/guardrails/singleton-packages.ts
|
|
553
|
+
const PNPM_LOCK = "pnpm-lock.yaml";
|
|
554
|
+
/** Packages one process must load once: every AntelopeJS interface package shares one runtime with its peers. */
|
|
555
|
+
const SINGLETON = /^@antelopejs\/interface-/u;
|
|
556
|
+
/** The fields of a lockfile package entry that point at the copy each dependency resolves to. */
|
|
557
|
+
const PACKAGE_FIELDS = ["dependencies", "optionalDependencies"];
|
|
558
|
+
/** How to move every importer onto one copy while pnpm keeps every other locked version, which `pnpm dedupe` does not. */
|
|
559
|
+
const REPAIR = " Set the direct specifier that locks the older copy to the newer version, run `pnpm install --lockfile-only`, then restore the specifier.\n";
|
|
560
|
+
/** A `name@version` key or a resolved version without its peer suffix, `(peer@1)`, which names no other copy. */
|
|
561
|
+
function withoutPeers(reference) {
|
|
562
|
+
const open = reference.indexOf("(");
|
|
563
|
+
return open === -1 ? reference : reference.slice(0, open);
|
|
564
|
+
}
|
|
565
|
+
/** The name and version of a `packages` key: `@scope/name@1.0.0` in version 9, `/@scope/name@1.0.0` in version 6. */
|
|
566
|
+
function packageKey(key) {
|
|
567
|
+
const match = /^\/?((?:@[^/@]+\/)?[^@]+)@(.+)$/u.exec(withoutPeers(key));
|
|
568
|
+
return match?.[1] && match[2] ? {
|
|
569
|
+
name: match[1],
|
|
570
|
+
version: match[2]
|
|
571
|
+
} : void 0;
|
|
572
|
+
}
|
|
573
|
+
/** Records `by` as a puller of the copy a dependency resolves to: `version`, or the `name@version` an `npm:` alias resolves to. */
|
|
574
|
+
function pull(copies, name, version, by) {
|
|
575
|
+
if (version === void 0) return;
|
|
576
|
+
const alias = packageKey(version);
|
|
577
|
+
const target = alias && copies.has(alias.name) ? alias : {
|
|
578
|
+
name,
|
|
579
|
+
version: withoutPeers(version)
|
|
580
|
+
};
|
|
581
|
+
copies.get(target.name)?.get(target.version)?.add(by);
|
|
582
|
+
}
|
|
583
|
+
function addPullers(copies, node, fields, by) {
|
|
584
|
+
for (const field of fields) {
|
|
585
|
+
const entries = mapping(node, field);
|
|
586
|
+
for (const name of keys(entries)) pull(copies, name, scalar(entries, name) ?? scalar(mapping(entries, name), "version"), by);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
/** Each document of a lockfile: pnpm 11 writes its config dependencies, installed apart from the project, as a document before the project's. */
|
|
590
|
+
function lockfileDocuments(path) {
|
|
591
|
+
return parseAllDocuments(readFileSync(path, "utf8"), {
|
|
592
|
+
schema: "failsafe",
|
|
593
|
+
uniqueKeys: false
|
|
594
|
+
}).map((document) => {
|
|
595
|
+
const { contents } = document;
|
|
596
|
+
if (document.errors.length > 0 || !isMap(contents) || !(Number.parseFloat(scalar(contents, "lockfileVersion") ?? "") >= 6)) throw new Unreadable(path);
|
|
597
|
+
return contents;
|
|
598
|
+
});
|
|
599
|
+
}
|
|
600
|
+
function documentCopies(lockfile) {
|
|
601
|
+
const copies = /* @__PURE__ */ new Map();
|
|
602
|
+
const packages = mapping(lockfile, "packages");
|
|
603
|
+
for (const parsed of keys(packages).map(packageKey)) if (parsed && SINGLETON.test(parsed.name)) copies.set(parsed.name, (copies.get(parsed.name) ?? /* @__PURE__ */ new Map()).set(parsed.version, /* @__PURE__ */ new Set()));
|
|
604
|
+
const importers = mapping(lockfile, "importers");
|
|
605
|
+
for (const importer of importers ? keys(importers) : ["."]) addPullers(copies, importers ? mapping(importers, importer) : lockfile, DIRECT_FIELDS, `importer "${importer}"`);
|
|
606
|
+
const resolved = mapping(lockfile, "snapshots") ?? packages;
|
|
607
|
+
for (const key of keys(resolved)) addPullers(copies, mapping(resolved, key), PACKAGE_FIELDS, withoutPeers(key.replace(/^\//u, "")));
|
|
608
|
+
return copies;
|
|
609
|
+
}
|
|
610
|
+
function report(file, copies) {
|
|
611
|
+
return [...copies].filter(([, versions]) => versions.size > 1).map(([name, versions]) => `${file} resolves "${name}" at ${versions.size} versions, and an interface package must load once:\n${[...versions].map(([version, by]) => ` ${version}, pulled by ${[...by].join(", ") || "nothing"}\n`).join("")}${REPAIR}`).join("");
|
|
612
|
+
}
|
|
613
|
+
/** The `pnpm-lock.yaml` files on disk in the repository, tracked or not yet added, outside what `.gitignore` leaves out. */
|
|
614
|
+
function pnpmLockfiles(root) {
|
|
615
|
+
return gitFiles(root).filter((file) => (file === PNPM_LOCK || file.endsWith(`/${PNPM_LOCK}`)) && existsSync(join(root, file)));
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Fails when a `pnpm-lock.yaml` of the repository resolves an AntelopeJS interface package, any `@antelopejs/interface-*`,
|
|
619
|
+
* at more than one version: each copy carries its own runtime, so two copies in one process break the contract between
|
|
620
|
+
* modules, or throw on a runtime protocol mismatch. Names, for each version, the importers and packages that pull it.
|
|
621
|
+
* Reads the lockfiles only: it spawns no package manager, writes no file and fetches nothing. Returns what failed, empty
|
|
622
|
+
* when every interface package resolves once.
|
|
623
|
+
*/
|
|
624
|
+
function checkSingletonPackages(root) {
|
|
625
|
+
return pnpmLockfiles(root).map((file) => {
|
|
626
|
+
try {
|
|
627
|
+
return lockfileDocuments(join(root, file)).map((document) => report(file, documentCopies(document))).join("");
|
|
628
|
+
} catch (error) {
|
|
629
|
+
if (error instanceof Unreadable) return `${file}: could not read it as a lockfile version this check knows.\n`;
|
|
630
|
+
throw error;
|
|
631
|
+
}
|
|
632
|
+
}).join("");
|
|
633
|
+
}
|
|
634
|
+
//#endregion
|
|
635
|
+
//#region src/guardrails/markdown-table.ts
|
|
636
|
+
/** The lines of a Markdown table whose first row is the header, columns padded as oxfmt leaves them. */
|
|
637
|
+
function tableLines(table) {
|
|
638
|
+
const widths = table[0].map((_, column) => Math.max(3, ...table.map((row) => row[column].length)));
|
|
639
|
+
const line = (cells) => `| ${cells.map((cell, column) => cell.padEnd(widths[column])).join(" | ")} |`;
|
|
640
|
+
return [
|
|
641
|
+
line(table[0]),
|
|
642
|
+
line(widths.map((width) => "-".repeat(width))),
|
|
643
|
+
...table.slice(1).map(line)
|
|
644
|
+
];
|
|
645
|
+
}
|
|
646
|
+
//#endregion
|
|
647
|
+
//#region src/guardrails/docs-modules.ts
|
|
648
|
+
const DMS_MODULES_START = "<!-- antelopejs-dms-modules:start -->";
|
|
649
|
+
const DMS_MODULES_END = "<!-- antelopejs-dms-modules:end -->";
|
|
650
|
+
const ORGANIZATION = "https://github.com/AntelopeJS";
|
|
651
|
+
/** The AntelopeJS DMS modules in display order. Every README holding the markers lists exactly these. */
|
|
652
|
+
const DMS_MODULES = [
|
|
653
|
+
{
|
|
654
|
+
repo: "dms-builder",
|
|
655
|
+
row: "Edit pages, tables and data sources by clicking, as TypeScript that compiles"
|
|
656
|
+
},
|
|
657
|
+
{
|
|
658
|
+
repo: "dms-ai",
|
|
659
|
+
row: "An assistant chat in the dashboard, on your own Claude Code or Codex"
|
|
660
|
+
},
|
|
661
|
+
{
|
|
662
|
+
repo: "dms-database",
|
|
663
|
+
row: "Schemas, relation diagrams, a data browser and a query console"
|
|
664
|
+
},
|
|
665
|
+
{
|
|
666
|
+
repo: "dms-api",
|
|
667
|
+
row: "API health, request logs, per-route statistics and a request tester"
|
|
668
|
+
},
|
|
669
|
+
{
|
|
670
|
+
repo: "dms-automation",
|
|
671
|
+
row: "Node-graph automations on schedules, webhooks and DMS events, with a trace for every run"
|
|
672
|
+
},
|
|
673
|
+
{
|
|
674
|
+
repo: "dms-mailing",
|
|
675
|
+
row: "E-mail templates in a block editor, versions, a send log and delivery metrics"
|
|
676
|
+
},
|
|
677
|
+
{
|
|
678
|
+
repo: "dms-lang",
|
|
679
|
+
row: "Translation coverage, a translation matrix and a translation queue"
|
|
680
|
+
},
|
|
681
|
+
{
|
|
682
|
+
repo: "dms-media",
|
|
683
|
+
row: "A media library with folders, per-folder permissions, image sizes and light editing"
|
|
684
|
+
},
|
|
685
|
+
{
|
|
686
|
+
repo: "dms-saas",
|
|
687
|
+
row: "Self-service sign-up, plans that gate features, and Stripe billing"
|
|
688
|
+
},
|
|
689
|
+
{
|
|
690
|
+
repo: "dms-marketing",
|
|
691
|
+
row: "Traffic, conversion funnels with A/B splits and click heatmaps, without cookies"
|
|
692
|
+
},
|
|
693
|
+
{
|
|
694
|
+
repo: "dms-frontend",
|
|
695
|
+
row: "The `ajs dms` CLI, the Vue renderer and the SDK every frontend module builds on"
|
|
696
|
+
}
|
|
697
|
+
];
|
|
698
|
+
/** The generated block, formatted as oxfmt leaves it. */
|
|
699
|
+
function modulesBlock() {
|
|
700
|
+
return [
|
|
701
|
+
DMS_MODULES_START,
|
|
702
|
+
"",
|
|
703
|
+
...tableLines([["Module", "What it adds"], ...DMS_MODULES.map(({ repo, row }) => [`[\`${repo}\`](${ORGANIZATION}/${repo})`, row])]),
|
|
704
|
+
"",
|
|
705
|
+
DMS_MODULES_END,
|
|
706
|
+
""
|
|
707
|
+
].join("\n");
|
|
708
|
+
}
|
|
709
|
+
/** The README with the block between its markers rewritten. A README without markers comes back as it is. */
|
|
710
|
+
function placeModulesBlock(current) {
|
|
711
|
+
const start = current.indexOf(DMS_MODULES_START);
|
|
712
|
+
const end = current.indexOf(DMS_MODULES_END);
|
|
713
|
+
const starts = current.split(DMS_MODULES_START).length - 1;
|
|
714
|
+
const ends = current.split(DMS_MODULES_END).length - 1;
|
|
715
|
+
if (starts === 0 && ends === 0) return current;
|
|
716
|
+
if (starts !== 1 || ends !== 1 || end < start) throw new Error("invalid antelopejs-dms-modules markers: a README holds exactly one start marker followed by one end marker. Repair or delete them, then rerun antelopejs-sync.");
|
|
717
|
+
return `${current.slice(0, start)}${modulesBlock()}${current.slice(end + 35).replace(/^\r?\n/, "")}`;
|
|
718
|
+
}
|
|
719
|
+
//#endregion
|
|
720
|
+
//#region src/guardrails/docs-compat.ts
|
|
721
|
+
const DOCS_COMPAT_START = "<!-- antelopejs-docs-compat:start -->";
|
|
722
|
+
const DOCS_COMPAT_END = "<!-- antelopejs-docs-compat:end -->";
|
|
723
|
+
const DOCS_NODE_START = "<!-- antelopejs-docs-node:start -->";
|
|
724
|
+
const DOCS_NODE_END = "<!-- antelopejs-docs-node:end -->";
|
|
725
|
+
const LOCKFILE = "pnpm-lock.yaml";
|
|
726
|
+
const REQUIREMENTS_HEADING = /^##\s+Requirements\s*$/i;
|
|
727
|
+
const SCOPE = "@antelopejs/";
|
|
728
|
+
const UNRESOLVED = "—";
|
|
729
|
+
const WORKSPACE = "workspace";
|
|
730
|
+
const LEGACY_MARKER = /^<!--\s*docs-compat:\s*\.\/package\.json#\w+\s*-->\s*$/;
|
|
731
|
+
const COMPATIBILITY_HEADING = /^##\s+Compatibility\s*$/i;
|
|
732
|
+
const RESOLVED_SECTIONS = [
|
|
733
|
+
"dependencies",
|
|
734
|
+
"optionalDependencies",
|
|
735
|
+
"devDependencies"
|
|
736
|
+
];
|
|
737
|
+
/** The generated block, rows sorted by package name, formatted as oxfmt leaves it. */
|
|
738
|
+
function compatBlock(rows) {
|
|
739
|
+
const sorted = [...rows].sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
740
|
+
return [
|
|
741
|
+
DOCS_COMPAT_START,
|
|
742
|
+
"",
|
|
743
|
+
...tableLines([[
|
|
744
|
+
"Package",
|
|
745
|
+
"Supported",
|
|
746
|
+
"Tested with"
|
|
747
|
+
], ...sorted.map(({ name, supported, tested }) => [
|
|
748
|
+
`\`${name}\``,
|
|
749
|
+
`\`${supported.replaceAll("|", "\\|")}\``,
|
|
750
|
+
tested === void 0 ? UNRESOLVED : `\`${tested}\``
|
|
751
|
+
])]),
|
|
752
|
+
"",
|
|
753
|
+
DOCS_COMPAT_END,
|
|
754
|
+
""
|
|
755
|
+
].join("\n");
|
|
756
|
+
}
|
|
757
|
+
function legacySpanEnd(lines, marker) {
|
|
758
|
+
let index = marker + 1;
|
|
759
|
+
while (index < lines.length && !lines[index].trim()) index++;
|
|
760
|
+
while (index < lines.length && lines[index].trimStart().startsWith("|")) index++;
|
|
761
|
+
while (index < lines.length && !lines[index].trim()) index++;
|
|
762
|
+
return index;
|
|
763
|
+
}
|
|
764
|
+
function replaceLegacyTables(lines, block) {
|
|
765
|
+
const placed = [];
|
|
766
|
+
let replaced = false;
|
|
767
|
+
for (let index = 0; index < lines.length;) {
|
|
768
|
+
if (!LEGACY_MARKER.test(lines[index])) {
|
|
769
|
+
placed.push(lines[index]);
|
|
770
|
+
index++;
|
|
771
|
+
continue;
|
|
772
|
+
}
|
|
773
|
+
if (!replaced) placed.push(...block.split("\n"));
|
|
774
|
+
replaced = true;
|
|
775
|
+
index = legacySpanEnd(lines, index);
|
|
776
|
+
}
|
|
777
|
+
return replaced ? placed : void 0;
|
|
778
|
+
}
|
|
779
|
+
function finalNewline(text) {
|
|
780
|
+
return text.endsWith("\n") ? text : `${text}\n`;
|
|
781
|
+
}
|
|
782
|
+
/** The README with the block in place: replacing the markers, a legacy `docs-compat` table, or under `## Compatibility`. */
|
|
783
|
+
function placeCompatBlock(current, block) {
|
|
784
|
+
const text = current ?? "";
|
|
785
|
+
const start = text.indexOf(DOCS_COMPAT_START);
|
|
786
|
+
const end = text.indexOf(DOCS_COMPAT_END);
|
|
787
|
+
if (start === -1 !== (end === -1) || end < start) throw new Error("incomplete antelopejs-docs-compat markers. Repair or delete them, then rerun antelopejs-sync.");
|
|
788
|
+
if (start !== -1) return `${text.slice(0, start)}${block}${text.slice(end + 35).replace(/^\r?\n/, "")}`;
|
|
789
|
+
const lines = text.split("\n");
|
|
790
|
+
const legacy = replaceLegacyTables(lines, block);
|
|
791
|
+
if (legacy) return finalNewline(legacy.join("\n"));
|
|
792
|
+
const heading = lines.findIndex((line) => COMPATIBILITY_HEADING.test(line));
|
|
793
|
+
if (heading !== -1) {
|
|
794
|
+
let rest = heading + 1;
|
|
795
|
+
while (rest < lines.length && !lines[rest].trim()) rest++;
|
|
796
|
+
return finalNewline([
|
|
797
|
+
...lines.slice(0, heading + 1),
|
|
798
|
+
"",
|
|
799
|
+
...block.split("\n"),
|
|
800
|
+
...lines.slice(rest)
|
|
801
|
+
].join("\n"));
|
|
802
|
+
}
|
|
803
|
+
const before = text.trimEnd();
|
|
804
|
+
return `${before}${before ? "\n\n" : ""}## Compatibility\n\n${block}`;
|
|
805
|
+
}
|
|
806
|
+
/** The generated Node prerequisite block for an `engines.node` range, formatted as oxfmt leaves it. */
|
|
807
|
+
function nodeBlock(range) {
|
|
808
|
+
return `${DOCS_NODE_START}\n\nThis package requires Node.js \`${range}\`.\n${DOCS_NODE_END}\n`;
|
|
809
|
+
}
|
|
810
|
+
/** The README with the Node block in place: between its markers, under `## Requirements`, or in a new `## Requirements` section at the end. */
|
|
811
|
+
function placeNodeBlock(current, block) {
|
|
812
|
+
const start = current.indexOf(DOCS_NODE_START);
|
|
813
|
+
const end = current.indexOf(DOCS_NODE_END);
|
|
814
|
+
if (start === -1 !== (end === -1) || end < start) throw new Error("incomplete antelopejs-docs-node markers. Repair or delete them, then rerun antelopejs-sync.");
|
|
815
|
+
if (start !== -1) return `${current.slice(0, start)}${block}${current.slice(end + 33).replace(/^\r?\n/, "")}`;
|
|
816
|
+
const lines = current.split("\n");
|
|
817
|
+
const heading = lines.findIndex((line) => REQUIREMENTS_HEADING.test(line));
|
|
818
|
+
if (heading !== -1) {
|
|
819
|
+
let rest = heading + 1;
|
|
820
|
+
while (rest < lines.length && !lines[rest].trim()) rest++;
|
|
821
|
+
return finalNewline([
|
|
822
|
+
...lines.slice(0, heading + 1),
|
|
823
|
+
"",
|
|
824
|
+
...block.trimEnd().split("\n"),
|
|
825
|
+
...rest < lines.length ? ["", ...lines.slice(rest)] : []
|
|
826
|
+
].join("\n"));
|
|
827
|
+
}
|
|
828
|
+
return `${current.trimEnd()}\n\n## Requirements\n\n${block}`;
|
|
829
|
+
}
|
|
830
|
+
function relative$1(directory, name) {
|
|
831
|
+
return directory ? `${directory}/${name}` : name;
|
|
832
|
+
}
|
|
833
|
+
function isDirectory(root, path) {
|
|
834
|
+
return statSync(join(root, path), { throwIfNoEntry: false })?.isDirectory() === true;
|
|
835
|
+
}
|
|
836
|
+
function subdirectories(root, directory) {
|
|
837
|
+
return readdirSync(join(root, directory), { withFileTypes: true }).filter((entry) => entry.isDirectory() && entry.name !== "node_modules" && !entry.name.startsWith(".")).map((entry) => relative$1(directory, entry.name));
|
|
838
|
+
}
|
|
839
|
+
function descendants(root, directory) {
|
|
840
|
+
return subdirectories(root, directory).flatMap((child) => [child, ...descendants(root, child)]);
|
|
841
|
+
}
|
|
842
|
+
function globExpression(pattern) {
|
|
843
|
+
const source = pattern.split("/").map((segment) => segment === "**" ? "(?:.*)" : segment.replace(/[.+^${}()|[\]\\]/g, "\\$&").replaceAll("*", "[^/]*").replaceAll("?", "[^/]")).join("/").replaceAll("(?:.*)/", "(?:.*/)?");
|
|
844
|
+
return new RegExp(`^${source}$`);
|
|
845
|
+
}
|
|
846
|
+
function expand(root, pattern) {
|
|
847
|
+
let directories = [""];
|
|
848
|
+
for (const segment of pattern.split("/")) if (segment === "**") directories = directories.flatMap((directory) => [directory, ...descendants(root, directory)]);
|
|
849
|
+
else if (/[*?]/.test(segment)) {
|
|
850
|
+
const expression = globExpression(segment);
|
|
851
|
+
directories = directories.flatMap((directory) => subdirectories(root, directory).filter((child) => expression.test(child.slice(child.lastIndexOf("/") + 1))));
|
|
852
|
+
} else directories = directories.map((directory) => relative$1(directory, segment)).filter((directory) => isDirectory(root, directory));
|
|
853
|
+
return directories;
|
|
854
|
+
}
|
|
855
|
+
function normalizePattern(pattern) {
|
|
856
|
+
return pattern.replace(/^(?:\.\/)+/, "").replace(/\/+$/, "").replace(/^\.$/, "");
|
|
857
|
+
}
|
|
858
|
+
function scalarItems(document, path) {
|
|
859
|
+
const node = document.getIn(path, true);
|
|
860
|
+
return isSeq(node) ? node.items.flatMap((item) => isScalar(item) ? [String(item.value)] : []) : [];
|
|
861
|
+
}
|
|
862
|
+
/** pnpm-workspace.yaml `packages`, else package.json `workspaces` as an array or as `{ packages }`; JSON parses as YAML. */
|
|
863
|
+
function workspacePatterns(root) {
|
|
864
|
+
const pnpm = join(root, "pnpm-workspace.yaml");
|
|
865
|
+
if (existsSync(pnpm)) return scalarItems(parseDocument(readFileSync(pnpm, "utf8")), ["packages"]);
|
|
866
|
+
const manifest = parseDocument(readFileSync(join(root, "package.json"), "utf8"));
|
|
867
|
+
return scalarItems(manifest, isMap(manifest.get("workspaces", true)) ? ["workspaces", "packages"] : ["workspaces"]);
|
|
868
|
+
}
|
|
869
|
+
/** The workspace package directories, relative to `root` and sorted, that `pnpm-workspace.yaml` or package.json `workspaces` list. */
|
|
870
|
+
function workspaceDirectories(root) {
|
|
871
|
+
const patterns = workspacePatterns(root);
|
|
872
|
+
const excluded = patterns.filter((pattern) => pattern.startsWith("!")).map((pattern) => globExpression(normalizePattern(pattern.slice(1))));
|
|
873
|
+
const found = /* @__PURE__ */ new Set();
|
|
874
|
+
for (const pattern of patterns.filter((item) => !item.startsWith("!"))) {
|
|
875
|
+
const normalized = normalizePattern(pattern);
|
|
876
|
+
for (const directory of normalized ? expand(root, normalized) : [""]) if (directory && !excluded.some((expression) => expression.test(directory)) && existsSync(join(root, directory, "package.json"))) found.add(directory);
|
|
877
|
+
}
|
|
878
|
+
return [...found].sort();
|
|
879
|
+
}
|
|
880
|
+
function declaredRanges(manifest) {
|
|
881
|
+
const ranges = /* @__PURE__ */ new Map();
|
|
882
|
+
for (const field of [
|
|
883
|
+
manifest.optionalDependencies,
|
|
884
|
+
manifest.dependencies,
|
|
885
|
+
manifest.peerDependencies
|
|
886
|
+
]) for (const [name, range] of Object.entries(field ?? {})) if (name.startsWith(SCOPE)) ranges.set(name, range);
|
|
887
|
+
return ranges;
|
|
888
|
+
}
|
|
889
|
+
function readManifest(root, directory) {
|
|
890
|
+
return parseManifest(readFileSync(join(root, directory, "package.json"), "utf8"));
|
|
891
|
+
}
|
|
892
|
+
/** The version the lockfile resolves for a dependency; a `link:` resolves to `workspace`, so a release that bumps the linked package leaves this README unchanged. */
|
|
893
|
+
function resolvedVersion({ document, importer }, name) {
|
|
894
|
+
for (const section of RESOLVED_SECTIONS) {
|
|
895
|
+
const entry = document.getIn([
|
|
896
|
+
"importers",
|
|
897
|
+
importer,
|
|
898
|
+
section,
|
|
899
|
+
name
|
|
900
|
+
], true);
|
|
901
|
+
const version = isMap(entry) ? entry.get("version", true) : entry;
|
|
902
|
+
if (!isScalar(version)) continue;
|
|
903
|
+
const resolved = String(version.value).split("(")[0];
|
|
904
|
+
return resolved.startsWith("link:") ? WORKSPACE : resolved;
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
const UNSHARED_NPMRC = /^\s*shared-workspace-lockfile\s*=\s*false\s*(?:[#;].*)?$/m;
|
|
908
|
+
/** Whether the workspace keeps one root lockfile, or `shared-workspace-lockfile=false` gives each package its own. */
|
|
909
|
+
function sharesLockfile(root) {
|
|
910
|
+
const npmrc = join(root, ".npmrc");
|
|
911
|
+
if (existsSync(npmrc) && UNSHARED_NPMRC.test(readFileSync(npmrc, "utf8"))) return false;
|
|
912
|
+
const workspace = join(root, "pnpm-workspace.yaml");
|
|
913
|
+
return !(existsSync(workspace) && parseDocument(readFileSync(workspace, "utf8")).get("sharedWorkspaceLockfile") === false);
|
|
914
|
+
}
|
|
915
|
+
/** The lockfile path of a package, relative to the root. A shared lockfile is the root's, indexed by the package directory. */
|
|
916
|
+
function lockPath(shared, directory) {
|
|
917
|
+
return shared ? LOCKFILE : relative$1(directory, LOCKFILE);
|
|
918
|
+
}
|
|
919
|
+
function readLock(root, shared, directory, documents) {
|
|
920
|
+
const path = join(root, lockPath(shared, directory));
|
|
921
|
+
if (!documents.has(path) && existsSync(path)) documents.set(path, parseDocument(readFileSync(path, "utf8")));
|
|
922
|
+
const document = documents.get(path);
|
|
923
|
+
return document && {
|
|
924
|
+
document,
|
|
925
|
+
importer: shared ? directory || "." : "."
|
|
926
|
+
};
|
|
927
|
+
}
|
|
928
|
+
/** Adds one package's `docs-files` lines and, when it needs one, its README with the generated blocks. */
|
|
929
|
+
function planPackage(root, plan, lock, standard, { directory, ranges, published, node }) {
|
|
930
|
+
const readme = relative$1(directory, "README.md");
|
|
931
|
+
const docs = relative$1(directory, "docs");
|
|
932
|
+
if (!published) return;
|
|
933
|
+
if (ranges.size > 0 || existsSync(join(root, readme))) plan.docsFiles.push(readme);
|
|
934
|
+
if (isDirectory(root, docs)) plan.docsFiles.push(docs);
|
|
935
|
+
const current = existsSync(join(root, readme)) ? readFileSync(join(root, readme), "utf8") : void 0;
|
|
936
|
+
const nodeRange = standard && current !== void 0 ? node : void 0;
|
|
937
|
+
if (ranges.size === 0 && current === void 0) return;
|
|
938
|
+
const rows = [...ranges].map(([name, supported]) => ({
|
|
939
|
+
name,
|
|
940
|
+
supported,
|
|
941
|
+
tested: lock && resolvedVersion(lock, name)
|
|
942
|
+
}));
|
|
943
|
+
try {
|
|
944
|
+
const withRequirements = nodeRange !== void 0 && current !== void 0 ? placeNodeBlock(current, nodeBlock(nodeRange)) : current;
|
|
945
|
+
plan.readmes.push({
|
|
946
|
+
path: readme,
|
|
947
|
+
current,
|
|
948
|
+
content: placeModulesBlock(rows.length > 0 ? placeCompatBlock(withRequirements, compatBlock(rows)) : withRequirements ?? "")
|
|
949
|
+
});
|
|
950
|
+
} catch (error) {
|
|
951
|
+
plan.errors.push(`${readme}: ${error instanceof Error ? error.message : String(error)}`);
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
/**
|
|
955
|
+
* Plans the docs-compat block of the root and of every workspace package that is not private and declares an
|
|
956
|
+
* `@antelopejs/*` peer, runtime or optional dependency, and the `docs-files` lines covering the root and every workspace
|
|
957
|
+
* package that is not private.
|
|
958
|
+
* Tested with comes from pnpm-lock.yaml, each package's own when `shared-workspace-lockfile=false`, and is `workspace` for a `link:` dependency; other package managers get a dash.
|
|
959
|
+
*/
|
|
960
|
+
function planDocs(root, manager) {
|
|
961
|
+
const rootManifest = readManifest(root, "");
|
|
962
|
+
const packages = ["", ...workspaceDirectories(root)].map((directory) => {
|
|
963
|
+
const manifest = directory ? readManifest(root, directory) : rootManifest;
|
|
964
|
+
return {
|
|
965
|
+
directory,
|
|
966
|
+
published: !directory || !manifest.private,
|
|
967
|
+
node: manifest.engines?.node,
|
|
968
|
+
ranges: manifest.private ? /* @__PURE__ */ new Map() : declaredRanges(manifest)
|
|
969
|
+
};
|
|
970
|
+
});
|
|
971
|
+
const qualifying = packages.filter(({ ranges }) => ranges.size > 0);
|
|
972
|
+
const plan = {
|
|
973
|
+
readmes: [],
|
|
974
|
+
docsFiles: [],
|
|
975
|
+
errors: [],
|
|
976
|
+
exists: (path) => existsSync(join(root, path))
|
|
977
|
+
};
|
|
978
|
+
const shared = sharesLockfile(root);
|
|
979
|
+
const documents = /* @__PURE__ */ new Map();
|
|
980
|
+
const locks = new Map(qualifying.map(({ directory }) => [directory, manager === "pnpm" ? readLock(root, shared, directory, documents) : void 0]));
|
|
981
|
+
const missing = manager === "pnpm" ? qualifying.filter(({ directory }) => !locks.get(directory)) : [];
|
|
982
|
+
if (missing.length) {
|
|
983
|
+
for (const path of new Set(missing.map(({ directory }) => lockPath(shared, directory)))) plan.errors.push(`${path} is missing, and the docs-compat block reads Tested with from it: run pnpm install, then rerun antelopejs-sync.`);
|
|
984
|
+
return plan;
|
|
985
|
+
}
|
|
986
|
+
const standard = rootManifest.antelopeJs?.docs?.standard === true;
|
|
987
|
+
for (const item of packages) planPackage(root, plan, locks.get(item.directory), standard, item);
|
|
988
|
+
return plan;
|
|
989
|
+
}
|
|
990
|
+
//#endregion
|
|
991
|
+
//#region src/guardrails/script-steps.ts
|
|
992
|
+
const SEPARATORS = /&&|\|\||[;|\n]|(?<![>&])&(?!&)/u;
|
|
993
|
+
const ENV_ASSIGNMENT = /^[A-Za-z_]\w*=/u;
|
|
994
|
+
const TSC_COMMAND = /(?:^|[\\/])tsc(?:\.js)?$/u;
|
|
995
|
+
const MANAGERS = /* @__PURE__ */ new Set([
|
|
996
|
+
"pnpm",
|
|
997
|
+
"npm",
|
|
998
|
+
"yarn",
|
|
999
|
+
"bun"
|
|
1000
|
+
]);
|
|
1001
|
+
const NPM_BUILTINS = /* @__PURE__ */ new Set([
|
|
1002
|
+
"test",
|
|
1003
|
+
"start",
|
|
1004
|
+
"stop",
|
|
1005
|
+
"restart"
|
|
1006
|
+
]);
|
|
1007
|
+
const WRAPPERS = /* @__PURE__ */ new Set([
|
|
1008
|
+
"cross-env",
|
|
1009
|
+
"cross-env-shell",
|
|
1010
|
+
"env",
|
|
1011
|
+
"corepack"
|
|
1012
|
+
]);
|
|
1013
|
+
const EXEC_RUNNERS = /* @__PURE__ */ new Set([
|
|
1014
|
+
"npx",
|
|
1015
|
+
"bunx",
|
|
1016
|
+
"pnpx"
|
|
1017
|
+
]);
|
|
1018
|
+
const EXEC_VERBS = /* @__PURE__ */ new Set([
|
|
1019
|
+
"exec",
|
|
1020
|
+
"x",
|
|
1021
|
+
"dlx"
|
|
1022
|
+
]);
|
|
1023
|
+
const RUN_ALL = /* @__PURE__ */ new Set([
|
|
1024
|
+
"run-s",
|
|
1025
|
+
"run-p",
|
|
1026
|
+
"npm-run-all",
|
|
1027
|
+
"npm-run-all2"
|
|
1028
|
+
]);
|
|
1029
|
+
const RUN_ALL_VALUED = /* @__PURE__ */ new Set(["--max-parallel", "--npm-path"]);
|
|
1030
|
+
const EXEC_VALUED = /* @__PURE__ */ new Set([
|
|
1031
|
+
"-p",
|
|
1032
|
+
"--package",
|
|
1033
|
+
"-c",
|
|
1034
|
+
"--call"
|
|
1035
|
+
]);
|
|
1036
|
+
const DIR_OPTIONS = /* @__PURE__ */ new Set([
|
|
1037
|
+
"--dir",
|
|
1038
|
+
"-C",
|
|
1039
|
+
"--prefix",
|
|
1040
|
+
"--cwd"
|
|
1041
|
+
]);
|
|
1042
|
+
const SELECT_OPTIONS = /* @__PURE__ */ new Set([
|
|
1043
|
+
"--filter",
|
|
1044
|
+
"-F",
|
|
1045
|
+
"--filter-prod",
|
|
1046
|
+
"--workspace"
|
|
1047
|
+
]);
|
|
1048
|
+
/** Manager options that take their value as the next word and point at no package. */
|
|
1049
|
+
const OTHER_VALUED = [
|
|
1050
|
+
"--workspace-concurrency",
|
|
1051
|
+
"--reporter",
|
|
1052
|
+
"--loglevel",
|
|
1053
|
+
"--test-pattern",
|
|
1054
|
+
"--changed-files-ignore-pattern",
|
|
1055
|
+
"--resume-from"
|
|
1056
|
+
];
|
|
1057
|
+
const MANAGER_VALUED = /* @__PURE__ */ new Set([
|
|
1058
|
+
...DIR_OPTIONS,
|
|
1059
|
+
...SELECT_OPTIONS,
|
|
1060
|
+
...OTHER_VALUED
|
|
1061
|
+
]);
|
|
1062
|
+
const TARGET_FLAGS = /* @__PURE__ */ new Set([
|
|
1063
|
+
"-r",
|
|
1064
|
+
"--recursive",
|
|
1065
|
+
"--workspaces",
|
|
1066
|
+
"-ws"
|
|
1067
|
+
]);
|
|
1068
|
+
/** The package an option points at; npm's `-w` selects only where `valued` lists it, since pnpm's `-w` is the workspace root. */
|
|
1069
|
+
function target(name, value, valued) {
|
|
1070
|
+
if (TARGET_FLAGS.has(name)) return { kind: "all" };
|
|
1071
|
+
if (value === void 0) return void 0;
|
|
1072
|
+
if (DIR_OPTIONS.has(name)) return {
|
|
1073
|
+
kind: "dir",
|
|
1074
|
+
path: value
|
|
1075
|
+
};
|
|
1076
|
+
if (SELECT_OPTIONS.has(name) || name === "-w" && valued.has(name)) return {
|
|
1077
|
+
kind: "select",
|
|
1078
|
+
selector: value
|
|
1079
|
+
};
|
|
1080
|
+
}
|
|
1081
|
+
function words(segment) {
|
|
1082
|
+
return segment.replace(/[()]/gu, " ").split(/\s+/u).map((word) => word.replace(/^["']|["']$/gu, "")).filter(Boolean);
|
|
1083
|
+
}
|
|
1084
|
+
/** The simple commands of a script: `&&`, `||`, `;`, `|`, `&`, newlines and parentheses all end one. */
|
|
1085
|
+
function lines(script) {
|
|
1086
|
+
return script.split(SEPARATORS).map(words).filter((list) => list.length > 0);
|
|
1087
|
+
}
|
|
1088
|
+
/** Skips the options from `from`, and the values of those that take one; `targets` are the packages they point at. */
|
|
1089
|
+
function skipOptions(args, from, valued) {
|
|
1090
|
+
let index = from;
|
|
1091
|
+
const targets = [];
|
|
1092
|
+
while (index < args.length && args[index].startsWith("-")) {
|
|
1093
|
+
const [name, ...inline] = args[index].split("=");
|
|
1094
|
+
const separate = valued.has(name) && inline.length === 0;
|
|
1095
|
+
const pointed = target(name, separate ? args[index + 1] : inline.length ? inline.join("=") : void 0, valued);
|
|
1096
|
+
if (pointed) targets.push(pointed);
|
|
1097
|
+
index += separate ? 2 : 1;
|
|
1098
|
+
}
|
|
1099
|
+
return {
|
|
1100
|
+
index,
|
|
1101
|
+
targets
|
|
1102
|
+
};
|
|
1103
|
+
}
|
|
1104
|
+
function isTsc(word) {
|
|
1105
|
+
return TSC_COMMAND.test(word);
|
|
1106
|
+
}
|
|
1107
|
+
/** `tsc`, a path to it, or `node` running one. */
|
|
1108
|
+
function programSteps(line) {
|
|
1109
|
+
const [head, ...rest] = line;
|
|
1110
|
+
if (head === void 0) return [];
|
|
1111
|
+
if (isTsc(head)) return [{
|
|
1112
|
+
kind: "tsc",
|
|
1113
|
+
args: rest,
|
|
1114
|
+
targets: []
|
|
1115
|
+
}];
|
|
1116
|
+
if (head !== "node") return [];
|
|
1117
|
+
if (rest[0] === "--run" && rest[1] !== void 0) return [{
|
|
1118
|
+
kind: "script",
|
|
1119
|
+
names: [rest[1]],
|
|
1120
|
+
targets: []
|
|
1121
|
+
}];
|
|
1122
|
+
const at = rest.findIndex((word) => !word.startsWith("-"));
|
|
1123
|
+
return at !== -1 && isTsc(rest[at]) ? [{
|
|
1124
|
+
kind: "tsc",
|
|
1125
|
+
args: rest.slice(at + 1),
|
|
1126
|
+
targets: []
|
|
1127
|
+
}] : [];
|
|
1128
|
+
}
|
|
1129
|
+
/** What `npx`, `pnpm exec` and `npm exec` run: the command after the options and the packages they name. */
|
|
1130
|
+
function execSteps(args) {
|
|
1131
|
+
return programSteps(args.slice(skipOptions(args, 0, EXEC_VALUED).index));
|
|
1132
|
+
}
|
|
1133
|
+
/** The steps with `targets` added, for a command the manager runs in other packages. */
|
|
1134
|
+
function pointedAt(steps, targets) {
|
|
1135
|
+
return steps.map((step) => step.kind === "cd" ? step : {
|
|
1136
|
+
...step,
|
|
1137
|
+
targets: [...targets, ...step.targets]
|
|
1138
|
+
});
|
|
1139
|
+
}
|
|
1140
|
+
/** The packages npm options after the script name point at, up to `--`: npm reads its options anywhere on the line. */
|
|
1141
|
+
function trailingTargets(args, from, valued) {
|
|
1142
|
+
const targets = [];
|
|
1143
|
+
for (let index = from; index < args.length && args[index] !== "--"; index++) if (args[index].startsWith("-")) {
|
|
1144
|
+
const found = skipOptions(args, index, valued);
|
|
1145
|
+
targets.push(...found.targets);
|
|
1146
|
+
index = found.index - 1;
|
|
1147
|
+
}
|
|
1148
|
+
return targets;
|
|
1149
|
+
}
|
|
1150
|
+
function managerSteps(manager, args) {
|
|
1151
|
+
const valued = new Set(MANAGER_VALUED);
|
|
1152
|
+
if (manager === "npm") valued.add("-w");
|
|
1153
|
+
const first = skipOptions(args, 0, valued);
|
|
1154
|
+
const verb = args[first.index];
|
|
1155
|
+
if (verb === void 0) return [];
|
|
1156
|
+
if (EXEC_VERBS.has(verb)) return pointedAt(execSteps(args.slice(first.index + 1)), first.targets);
|
|
1157
|
+
if (isTsc(verb)) return pointedAt(programSteps(args.slice(first.index)), first.targets);
|
|
1158
|
+
const run = verb === "run" || verb === "run-script";
|
|
1159
|
+
const second = run ? skipOptions(args, first.index + 1, valued) : {
|
|
1160
|
+
index: first.index,
|
|
1161
|
+
targets: []
|
|
1162
|
+
};
|
|
1163
|
+
const name = args[second.index];
|
|
1164
|
+
if (name === void 0 || !run && manager === "npm" && !NPM_BUILTINS.has(verb)) return [];
|
|
1165
|
+
const trailing = manager === "npm" ? trailingTargets(args, second.index + 1, valued) : [];
|
|
1166
|
+
return [{
|
|
1167
|
+
kind: "script",
|
|
1168
|
+
names: [name],
|
|
1169
|
+
targets: [
|
|
1170
|
+
...first.targets,
|
|
1171
|
+
...second.targets,
|
|
1172
|
+
...trailing
|
|
1173
|
+
]
|
|
1174
|
+
}];
|
|
1175
|
+
}
|
|
1176
|
+
function runAllSteps(args) {
|
|
1177
|
+
const names = [];
|
|
1178
|
+
for (let index = 0; index < args.length && args[index] !== "--"; index++) if (!args[index].startsWith("-")) names.push(args[index]);
|
|
1179
|
+
else if (RUN_ALL_VALUED.has(args[index])) index++;
|
|
1180
|
+
return names.length === 0 ? [] : [{
|
|
1181
|
+
kind: "script",
|
|
1182
|
+
names,
|
|
1183
|
+
targets: []
|
|
1184
|
+
}];
|
|
1185
|
+
}
|
|
1186
|
+
/** The line without its leading environment assignments and wrappers (`cross-env A=1`, `env`, `corepack`). */
|
|
1187
|
+
function unwrapped(line) {
|
|
1188
|
+
let index = 0;
|
|
1189
|
+
let wrapped = false;
|
|
1190
|
+
while (index < line.length) {
|
|
1191
|
+
const word = line[index];
|
|
1192
|
+
if (WRAPPERS.has(word)) wrapped = true;
|
|
1193
|
+
else if (!ENV_ASSIGNMENT.test(word) && !(wrapped && word.startsWith("-"))) break;
|
|
1194
|
+
index++;
|
|
1195
|
+
}
|
|
1196
|
+
return line.slice(index);
|
|
1197
|
+
}
|
|
1198
|
+
function lineSteps(line) {
|
|
1199
|
+
const [head, ...args] = unwrapped(line);
|
|
1200
|
+
if (head === void 0) return [];
|
|
1201
|
+
if (head === "cd") return [{
|
|
1202
|
+
kind: "cd",
|
|
1203
|
+
path: args[0] ?? "~"
|
|
1204
|
+
}];
|
|
1205
|
+
if (RUN_ALL.has(head)) return runAllSteps(args);
|
|
1206
|
+
if (MANAGERS.has(head)) return managerSteps(head, args);
|
|
1207
|
+
if (EXEC_RUNNERS.has(head)) return execSteps(args);
|
|
1208
|
+
return programSteps([head, ...args]);
|
|
1209
|
+
}
|
|
1210
|
+
/** The scripts and `tsc` runs a script's command lines start. */
|
|
1211
|
+
function stepsOf(script) {
|
|
1212
|
+
return lines(script).flatMap(lineSteps);
|
|
1213
|
+
}
|
|
1214
|
+
/** The script names a `run-s` pattern list selects among `available`. */
|
|
1215
|
+
function matchingScripts(names, available) {
|
|
1216
|
+
const all = [...available];
|
|
1217
|
+
return names.flatMap((name) => {
|
|
1218
|
+
if (!name.includes("*")) return [name];
|
|
1219
|
+
const pattern = new RegExp(`^${name.split("**").map((part) => part.split("*").map((piece) => piece.replace(/[.+?^${}()|[\]\\]/gu, "\\$&")).join("[^:]*")).join(".*")}$`, "u");
|
|
1220
|
+
return all.filter((script) => pattern.test(script));
|
|
1221
|
+
});
|
|
1222
|
+
}
|
|
1223
|
+
//#endregion
|
|
1224
|
+
//#region src/guardrails/typecheck-writes.ts
|
|
1225
|
+
const MAX_DEPTH = 5;
|
|
1226
|
+
const BUILD = "build";
|
|
1227
|
+
const UNRESOLVED_SELECTOR = /^!|\.\.\.|\^|\[|\$/u;
|
|
1228
|
+
function scriptsIn(directory) {
|
|
1229
|
+
const file = join(directory, "package.json");
|
|
1230
|
+
if (!existsSync(file)) return /* @__PURE__ */ new Map();
|
|
1231
|
+
const { scripts = {} } = parseManifest(readFileSync(file, "utf8"));
|
|
1232
|
+
return new Map(Object.entries(scripts));
|
|
1233
|
+
}
|
|
1234
|
+
function workspacePackages(walk) {
|
|
1235
|
+
walk.packages ??= workspaceDirectories(walk.root).map((path) => {
|
|
1236
|
+
const directory = join(walk.root, path);
|
|
1237
|
+
const { name } = parseManifest(readFileSync(join(directory, "package.json"), "utf8"));
|
|
1238
|
+
return {
|
|
1239
|
+
directory,
|
|
1240
|
+
path,
|
|
1241
|
+
name
|
|
1242
|
+
};
|
|
1243
|
+
});
|
|
1244
|
+
return walk.packages;
|
|
1245
|
+
}
|
|
1246
|
+
/** The workspace packages a `--filter` or `--workspace` selector matches by name or by path, `undefined` for one this does not model. */
|
|
1247
|
+
function selected(walk, from, selector) {
|
|
1248
|
+
if (UNRESOLVED_SELECTOR.test(selector)) return void 0;
|
|
1249
|
+
const path = relative(walk.root, resolve(from, selector.replace(/^\{|\}$/gu, "")));
|
|
1250
|
+
const matches = workspacePackages(walk).filter((item) => item.name !== void 0 && minimatch(item.name, selector) || minimatch(item.path, path)).map((item) => item.directory);
|
|
1251
|
+
return matches.length ? matches : void 0;
|
|
1252
|
+
}
|
|
1253
|
+
function targetDirectories(walk, from, target) {
|
|
1254
|
+
if (target.kind === "all") return workspacePackages(walk).map((item) => item.directory);
|
|
1255
|
+
if (target.kind === "select") return selected(walk, from, target.selector);
|
|
1256
|
+
const directory = resolve(from, target.path);
|
|
1257
|
+
return target.path.includes("$") || !existsSync(join(directory, "package.json")) ? void 0 : [directory];
|
|
1258
|
+
}
|
|
1259
|
+
/** The package directories a script step runs in, `undefined` when a target cannot be resolved. */
|
|
1260
|
+
function stepDirectories(walk, from, targets) {
|
|
1261
|
+
if (targets.length === 0) return [from];
|
|
1262
|
+
const directories = /* @__PURE__ */ new Set();
|
|
1263
|
+
for (const target of targets) {
|
|
1264
|
+
const found = targetDirectories(walk, from, target);
|
|
1265
|
+
if (found === void 0) return void 0;
|
|
1266
|
+
for (const directory of found) directories.add(directory);
|
|
1267
|
+
}
|
|
1268
|
+
return [...directories];
|
|
1269
|
+
}
|
|
1270
|
+
function configFile(directory, project) {
|
|
1271
|
+
const path = resolve(directory, project);
|
|
1272
|
+
return existsSync(path) && statSync(path).isDirectory() ? join(path, "tsconfig.json") : path;
|
|
1273
|
+
}
|
|
1274
|
+
/** Whether a `tsc` command line writes output: `-b`, or a run whose command line or tsconfig chain leaves `noEmit` unset. */
|
|
1275
|
+
function tscWrites(directory, args) {
|
|
1276
|
+
if (/^--?b(?:uild)?$/iu.test(args[0] ?? "")) return true;
|
|
1277
|
+
const { options } = ts.parseCommandLine([...args]);
|
|
1278
|
+
if (options.noEmit !== void 0) return !options.noEmit;
|
|
1279
|
+
const file = configFile(directory, options.project ?? ".");
|
|
1280
|
+
if (!existsSync(file)) return true;
|
|
1281
|
+
return ts.getParsedCommandLineOfConfigFile(file, {}, {
|
|
1282
|
+
...ts.sys,
|
|
1283
|
+
onUnRecoverableConfigFileDiagnostic: () => void 0
|
|
1284
|
+
})?.options.noEmit !== true;
|
|
1285
|
+
}
|
|
1286
|
+
/** Whether one step, run from `directory`, writes: a `tsc` that emits, or a script that reaches one or a `build`. */
|
|
1287
|
+
function stepWrites(walk, directory, step, depth) {
|
|
1288
|
+
const directories = stepDirectories(walk, directory, step.targets);
|
|
1289
|
+
if (directories === void 0) return true;
|
|
1290
|
+
if (step.kind === "tsc") return directories.some((target) => tscWrites(target, step.args));
|
|
1291
|
+
return directories.some((target) => matchingScripts(step.names, scriptsIn(target).keys()).some((script) => scriptWrites(walk, target, script, depth + 1)));
|
|
1292
|
+
}
|
|
1293
|
+
/** Whether a script or the `pre` and `post` hooks the package manager runs around it write, its lines run from where each `cd` left them. */
|
|
1294
|
+
function scriptWrites(walk, directory, name, depth) {
|
|
1295
|
+
const key = `${directory}\0${name}`;
|
|
1296
|
+
const text = scriptsIn(directory).get(name);
|
|
1297
|
+
if (text === void 0 || walk.seen.has(key)) return false;
|
|
1298
|
+
walk.seen.add(key);
|
|
1299
|
+
if (name === BUILD || depth > MAX_DEPTH) return true;
|
|
1300
|
+
if (scriptWrites(walk, directory, `pre${name}`, depth) || scriptWrites(walk, directory, `post${name}`, depth)) return true;
|
|
1301
|
+
let current = directory;
|
|
1302
|
+
for (const step of stepsOf(text)) if (step.kind === "cd") {
|
|
1303
|
+
if (step.path.includes("$") || step.path.startsWith("~")) return true;
|
|
1304
|
+
current = resolve(current, step.path);
|
|
1305
|
+
} else if (stepWrites(walk, current, step, depth)) return true;
|
|
1306
|
+
return false;
|
|
1307
|
+
}
|
|
1308
|
+
/**
|
|
1309
|
+
* Whether the `typecheck` script of the package at `root` can write files other steps read: it reaches a `build`
|
|
1310
|
+
* script or a `tsc` run that emits, in this package or a workspace package it points the package manager at. A
|
|
1311
|
+
* target it cannot resolve, such as a `--dir` read from a variable or a dependency `--filter`, counts as writing, and
|
|
1312
|
+
* so does a workspace whose manifests cannot be read.
|
|
1313
|
+
*/
|
|
1314
|
+
function typecheckWrites(root) {
|
|
1315
|
+
try {
|
|
1316
|
+
return scriptWrites({
|
|
1317
|
+
root,
|
|
1318
|
+
seen: /* @__PURE__ */ new Set()
|
|
1319
|
+
}, root, "typecheck", 0);
|
|
1320
|
+
} catch {
|
|
1321
|
+
return true;
|
|
1322
|
+
}
|
|
1323
|
+
}
|
|
1324
|
+
//#endregion
|
|
1325
|
+
//#region src/guardrails/checks.ts
|
|
1326
|
+
const CHECK_STEPS = [
|
|
1327
|
+
"typecheck",
|
|
1328
|
+
"lint",
|
|
1329
|
+
"format:check",
|
|
1330
|
+
"knip",
|
|
1331
|
+
"test",
|
|
1332
|
+
"size",
|
|
1333
|
+
"ratchet",
|
|
1334
|
+
"suppressions",
|
|
1335
|
+
"sync:check",
|
|
1336
|
+
"verify:types",
|
|
1337
|
+
"verify:lockfiles",
|
|
1338
|
+
"lockfile:singletons",
|
|
1339
|
+
"git:identity"
|
|
1340
|
+
];
|
|
1341
|
+
/** Steps `antelopejs-checks` runs itself, no package.json script behind them: each returns what failed, empty when it passed. */
|
|
1342
|
+
const OWN_STEPS = /* @__PURE__ */ new Map([
|
|
1343
|
+
["verify:types", typecheckLayers],
|
|
1344
|
+
["verify:lockfiles", (root) => Promise.resolve(checkLockfiles(root))],
|
|
1345
|
+
["lockfile:singletons", (root) => Promise.resolve(checkSingletonPackages(root))],
|
|
1346
|
+
["git:identity", checkGitIdentity]
|
|
1347
|
+
]);
|
|
1348
|
+
function hasWork(root, step) {
|
|
1349
|
+
if (step === "ratchet") return existsSync(join(root, CEILINGS_FILE));
|
|
1350
|
+
if (step === "verify:types") return verifyLayers(root).length > 0;
|
|
1351
|
+
if (step === "verify:lockfiles") return verifyInstallFolders(root).length > 0;
|
|
1352
|
+
if (step === "lockfile:singletons") return pnpmLockfiles(root).length > 0;
|
|
1353
|
+
if (step === "git:identity") return isGitCheckout(root);
|
|
1354
|
+
return true;
|
|
1355
|
+
}
|
|
1356
|
+
/** A command that runs a package script: the package manager, an optional `run`, then the script name. */
|
|
1357
|
+
const SCRIPT_RUN = /^(?:pnpm|npm|yarn|bun)\s+(?:run(?:-script)?\s+)?([^\s-]\S*)/;
|
|
1358
|
+
/**
|
|
1359
|
+
* The steps of `script`, a `checks` script sync replaces, that `antelopejs-checks` does not run: each command between
|
|
1360
|
+
* `&&`, `||` and `;` that is neither `antelopejs-checks` itself, a run of one of its steps or of `checks:prepare`, nor
|
|
1361
|
+
* the very command one of those holds in `scripts`, the scripts sync writes. A run of another package script is named
|
|
1362
|
+
* by that script, any other command by its text.
|
|
1363
|
+
*/
|
|
1364
|
+
function stepsNotRun(script, scripts) {
|
|
1365
|
+
const run = /* @__PURE__ */ new Set([...CHECK_STEPS, PREPARE]);
|
|
1366
|
+
const held = new Set([...run].flatMap((step) => Object.hasOwn(scripts, step) ? [scripts[step]] : []));
|
|
1367
|
+
return script.split(/&&|\|\||;/).map((command) => command.trim()).filter((command) => command && !/(?:^|\s)antelopejs-checks$/.test(command) && !held.has(command)).flatMap((command) => {
|
|
1368
|
+
const name = SCRIPT_RUN.exec(command)?.[1];
|
|
1369
|
+
if (name !== void 0 && run.has(name)) return [];
|
|
1370
|
+
return name !== void 0 && Object.hasOwn(scripts, name) ? [name] : [command];
|
|
1371
|
+
});
|
|
1372
|
+
}
|
|
1373
|
+
/** The sync note naming the steps of the `checks` script in the package.json text `current` that the one in `synced`, the text sync writes, replaces and does not run; none when the script is kept or loses no step. */
|
|
1374
|
+
function droppedChecksSteps(current, synced) {
|
|
1375
|
+
const previous = parseManifest(current ?? "{}").scripts?.checks;
|
|
1376
|
+
const scripts = parseManifest(synced).scripts ?? {};
|
|
1377
|
+
const command = scripts.checks;
|
|
1378
|
+
if (previous === void 0 || previous === command) return [];
|
|
1379
|
+
const dropped = stepsNotRun(previous, scripts);
|
|
1380
|
+
if (!dropped.length) return [];
|
|
1381
|
+
return [`package.json: the checks script now runs ${command}, which does not run ${dropped.map((step) => `"${step}"`).join(", ")} from the replaced script. Call each from one of its steps, such as test or lint, or it no longer runs in checks.`];
|
|
1382
|
+
}
|
|
1383
|
+
/** The step that runs after the others, never beside them: tests often hold time limits that CPU contention would break. */
|
|
1384
|
+
const RUNS_ALONE = "test";
|
|
1385
|
+
/** The step that runs before the others when it writes files, such as a workspace package's `dist`, that they read. */
|
|
1386
|
+
const RUNS_FIRST = "typecheck";
|
|
1387
|
+
/** The groups of steps that run one after another, the steps of a group at the same time. */
|
|
1388
|
+
function phases(root, steps) {
|
|
1389
|
+
const first = steps.includes(RUNS_FIRST) && typecheckWrites(root) ? [RUNS_FIRST] : [];
|
|
1390
|
+
const last = steps.includes(RUNS_ALONE) ? [RUNS_ALONE] : [];
|
|
1391
|
+
return [
|
|
1392
|
+
first,
|
|
1393
|
+
steps.filter((step) => !first.includes(step) && !last.includes(step)),
|
|
1394
|
+
last
|
|
1395
|
+
].filter((phase) => phase.length > 0);
|
|
1396
|
+
}
|
|
1397
|
+
/** The environment variable naming, comma-separated, the steps `runChecks` leaves to another CI job. */
|
|
1398
|
+
const CHECKS_SKIP_ENV = "ANTELOPEJS_CHECKS_SKIP";
|
|
1399
|
+
function skippedNames(setting) {
|
|
1400
|
+
return setting.split(",").map((name) => name.trim()).filter(Boolean);
|
|
1401
|
+
}
|
|
1402
|
+
async function runSteps(root, manifest, steps, signal) {
|
|
1403
|
+
const manager = packageManager(manifest);
|
|
1404
|
+
const runStep = async (step) => {
|
|
1405
|
+
if (signal?.aborted) return [];
|
|
1406
|
+
const own = OWN_STEPS.get(step);
|
|
1407
|
+
if (own) {
|
|
1408
|
+
const failure = await own(root, signal);
|
|
1409
|
+
return failure ? [`[${step}]\n${failure}`] : [];
|
|
1410
|
+
}
|
|
1411
|
+
if (!manifest.scripts?.[step]) return [`[${step}] Missing package.json script "${step}". Run antelopejs-sync.\n`];
|
|
1412
|
+
const result = await runCommand(manager, ["run", step], {
|
|
1413
|
+
cwd: root,
|
|
1414
|
+
env: {
|
|
1415
|
+
...process.env,
|
|
1416
|
+
CI: "1"
|
|
1417
|
+
},
|
|
1418
|
+
signal
|
|
1419
|
+
});
|
|
1420
|
+
return result.status === 0 ? [] : [`[${step}]\n${result.output}`];
|
|
1421
|
+
};
|
|
1422
|
+
const failures = /* @__PURE__ */ new Map();
|
|
1423
|
+
for (const phase of phases(root, steps)) await Promise.all(phase.map(async (step) => failures.set(step, await runStep(step))));
|
|
1424
|
+
return steps.flatMap((step) => failures.get(step) ?? []);
|
|
1425
|
+
}
|
|
1426
|
+
/** The optional package script run once before the steps, to build what several steps read, such as a workspace package's `dist`. */
|
|
1427
|
+
const PREPARE = "checks:prepare";
|
|
1428
|
+
/** Runs `checks:prepare` when the manifest has it: the run's result when it failed or `signal` aborted it, else undefined. */
|
|
1429
|
+
async function prepare(root, manifest, signal) {
|
|
1430
|
+
if (!manifest.scripts?.[PREPARE]) return void 0;
|
|
1431
|
+
const result = await runCommand(packageManager(manifest), ["run", PREPARE], {
|
|
1432
|
+
cwd: root,
|
|
1433
|
+
env: {
|
|
1434
|
+
...process.env,
|
|
1435
|
+
CI: "1"
|
|
1436
|
+
},
|
|
1437
|
+
signal
|
|
1438
|
+
});
|
|
1439
|
+
const failure = result.status === 0 ? "" : `[${PREPARE}]\n${result.output}`;
|
|
1440
|
+
if (signal?.aborted) return {
|
|
1441
|
+
failed: failure ? 1 : 0,
|
|
1442
|
+
interrupted: true,
|
|
1443
|
+
output: `${failure}Checks interrupted by ${String(signal.reason)}.\n`
|
|
1444
|
+
};
|
|
1445
|
+
if (failure) return {
|
|
1446
|
+
failed: 1,
|
|
1447
|
+
interrupted: false,
|
|
1448
|
+
output: `${failure}Checks: 0 passed, 1 failed.\n`
|
|
1449
|
+
};
|
|
1450
|
+
}
|
|
1451
|
+
function summary(failures, passed, skipped) {
|
|
1452
|
+
return `${failures.join("\n")}Checks: ${passed} passed, ${failures.length} failed${skipped ? `, ${skipped} skipped` : ""}.\n`;
|
|
1453
|
+
}
|
|
1454
|
+
/**
|
|
1455
|
+
* Runs the `checks:prepare` package script first when it exists and a step will run, and stops there if it fails.
|
|
1456
|
+
* Then runs `typecheck` first when it writes files the other steps read (see `typecheckWrites`), every other check step
|
|
1457
|
+
* except `test` at the same time, then `test` alone, and reports only what failed, each step's
|
|
1458
|
+
* output buffered and listed in step order whichever step finishes first. `skipSetting` is the comma-separated value of
|
|
1459
|
+
* `ANTELOPEJS_CHECKS_SKIP`: the steps it names are not run and are counted as skipped, never as passed, and a name that
|
|
1460
|
+
* is not a step fails the run before any step starts so a typo cannot skip nothing. Unless `services` is false, the
|
|
1461
|
+
* services of `.github/compose.yml` are started before the steps when `test` runs, and removed after them if the run created them, also when a step fails or
|
|
1462
|
+
* `signal` aborts the run; an aborted run stops every running step and reports `interrupted`.
|
|
1463
|
+
*/
|
|
1464
|
+
async function runChecks(root, options = {}) {
|
|
1465
|
+
const { skipSetting = "", services = true, signal } = options;
|
|
1466
|
+
const requested = skippedNames(skipSetting);
|
|
1467
|
+
const unknown = requested.filter((name) => !CHECK_STEPS.includes(name));
|
|
1468
|
+
if (unknown.length) return {
|
|
1469
|
+
failed: 1,
|
|
1470
|
+
interrupted: false,
|
|
1471
|
+
output: `[${CHECKS_SKIP_ENV}]\nUnknown step ${unknown.map((name) => `"${name}"`).join(", ")}. Valid steps: ${CHECK_STEPS.join(", ")}.\nChecks: 0 passed, 1 failed.\n`
|
|
1472
|
+
};
|
|
1473
|
+
const present = CHECK_STEPS.filter((step) => hasWork(root, step));
|
|
1474
|
+
const steps = present.filter((step) => !requested.includes(step));
|
|
1475
|
+
const skipped = present.length - steps.length;
|
|
1476
|
+
const manifest = parseManifest(readFileSync(join(root, "package.json"), "utf8"));
|
|
1477
|
+
const plan = services && steps.includes("test") ? await planServices(root) : { kind: "none" };
|
|
1478
|
+
if (plan.kind === "unavailable") return {
|
|
1479
|
+
failed: 1,
|
|
1480
|
+
interrupted: false,
|
|
1481
|
+
output: `[services]\n${plan.output}Checks: 0 passed, 1 failed.\n`
|
|
1482
|
+
};
|
|
1483
|
+
const unprepared = steps.length ? await prepare(root, manifest, signal) : void 0;
|
|
1484
|
+
if (unprepared) return unprepared;
|
|
1485
|
+
const started = plan.kind === "start" ? await startServices(root, signal) : {
|
|
1486
|
+
failure: "",
|
|
1487
|
+
owned: false
|
|
1488
|
+
};
|
|
1489
|
+
let failures = [];
|
|
1490
|
+
let stopped = "";
|
|
1491
|
+
try {
|
|
1492
|
+
failures = started.failure ? [`[services]\n${started.failure}`] : await runSteps(root, manifest, steps, signal);
|
|
1493
|
+
} finally {
|
|
1494
|
+
if (started.owned) stopped = await stopServices(root);
|
|
1495
|
+
}
|
|
1496
|
+
const passed = started.failure ? 0 : steps.length - failures.length;
|
|
1497
|
+
if (stopped) failures.push(`[services]\n${stopped}`);
|
|
1498
|
+
const interrupted = signal?.aborted ?? false;
|
|
1499
|
+
return {
|
|
1500
|
+
failed: failures.length,
|
|
1501
|
+
interrupted,
|
|
1502
|
+
output: interrupted ? `${failures.join("\n")}Checks interrupted by ${String(signal?.reason)}.\n` : summary(failures, passed, skipped)
|
|
1503
|
+
};
|
|
1504
|
+
}
|
|
1505
|
+
//#endregion
|
|
1506
|
+
export { stepsOf as a, matchingScripts as i, droppedChecksSteps as n, planDocs as o, runChecks as r, workspaceDirectories as s, CHECKS_SKIP_ENV as t };
|