@jfalava/ts-standard 0.0.0-stage → 0.1.0
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 +197 -2
- package/package.json +42 -4
- package/reference/ci.yml +64 -0
- package/src/check.ts +54 -0
- package/src/cli.ts +34 -0
- package/src/config.ts +41 -0
- package/src/finding.ts +8 -0
- package/src/pins.ts +233 -0
- package/src/repo.ts +84 -0
- package/src/scripts.ts +535 -0
- package/src/shell.ts +131 -0
- package/src/workflows.ts +279 -0
package/src/scripts.ts
ADDED
|
@@ -0,0 +1,535 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
|
|
3
|
+
import type { Finding } from "./finding.ts";
|
|
4
|
+
import type { Pkg, Repo } from "./repo.ts";
|
|
5
|
+
import { asBunRun, type Command, isBareBunTest, isBunx, isWriter, parseScript } from "./shell.ts";
|
|
6
|
+
|
|
7
|
+
const VERBS = [
|
|
8
|
+
"check",
|
|
9
|
+
"fix",
|
|
10
|
+
"lint",
|
|
11
|
+
"lint:fix",
|
|
12
|
+
"format",
|
|
13
|
+
"format:check",
|
|
14
|
+
"typecheck",
|
|
15
|
+
"test",
|
|
16
|
+
] as const;
|
|
17
|
+
const DEPLOY = ["plan", "deploy", "ci:deploy", "destroy"] as const;
|
|
18
|
+
const LEAF_REQUIRED = ["check", "fix", "lint", "lint:fix", "format", "format:check", "typecheck"];
|
|
19
|
+
const ROOT_REQUIRED = ["check", "fix", "lint", "format", "format:check", "typecheck", "test"];
|
|
20
|
+
|
|
21
|
+
interface Leaf {
|
|
22
|
+
readonly pkg: Pkg;
|
|
23
|
+
readonly script: string;
|
|
24
|
+
readonly cmd: Command;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Follows `bun run` through the package, `--cwd` and `--workspaces`, calling back on every other command. */
|
|
28
|
+
const walk = (
|
|
29
|
+
repo: Repo,
|
|
30
|
+
pkg: Pkg,
|
|
31
|
+
script: string,
|
|
32
|
+
on: {
|
|
33
|
+
leaf?: (l: Leaf) => void;
|
|
34
|
+
script?: (p: Pkg, s: string) => void;
|
|
35
|
+
missing?: (p: Pkg, s: string) => void;
|
|
36
|
+
},
|
|
37
|
+
seen = new Set<string>(),
|
|
38
|
+
): void => {
|
|
39
|
+
const key = `${pkg.dir}#${script}`;
|
|
40
|
+
if (seen.has(key)) return;
|
|
41
|
+
seen.add(key);
|
|
42
|
+
const body = pkg.scripts[script];
|
|
43
|
+
if (body === undefined) return;
|
|
44
|
+
on.script?.(pkg, script);
|
|
45
|
+
for (const cmd of parseScript(body)) {
|
|
46
|
+
const run = asBunRun(cmd);
|
|
47
|
+
if (!run) {
|
|
48
|
+
on.leaf?.({ pkg, script, cmd });
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const targets = run.workspaces
|
|
52
|
+
? repo.workspaces
|
|
53
|
+
: [run.cwd ? repo.pkgAt(join(pkg.dir, run.cwd)) : pkg];
|
|
54
|
+
for (const target of targets) {
|
|
55
|
+
if (!target) continue;
|
|
56
|
+
if (target.scripts[run.script] === undefined) {
|
|
57
|
+
if (!run.ifPresent) on.missing?.(target, run.script);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
walk(repo, target, run.script, on, seen);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const raws = (body: string | undefined): string[] =>
|
|
66
|
+
body === undefined ? [] : parseScript(body).map((c) => c.raw);
|
|
67
|
+
const leaves = (repo: Repo): Pkg[] => (repo.isWorkspace ? [...repo.workspaces] : [repo.rootPkg]);
|
|
68
|
+
const sameArgv = (a: readonly string[], b: readonly string[]) =>
|
|
69
|
+
a.length === b.length && a.every((x, i) => x === b[i]);
|
|
70
|
+
|
|
71
|
+
export const scriptRules = (repo: Repo): Finding[] => {
|
|
72
|
+
const out: Finding[] = [];
|
|
73
|
+
const f = (rule: string, pkg: Pkg, script: string | undefined, message: string) =>
|
|
74
|
+
out.push({ rule, where: pkg.dir, script, message });
|
|
75
|
+
|
|
76
|
+
// --- names that must exist
|
|
77
|
+
for (const pkg of leaves(repo)) {
|
|
78
|
+
for (const name of LEAF_REQUIRED) {
|
|
79
|
+
if (pkg.scripts[name] === undefined) f("scripts/required", pkg, name, `missing \`${name}\``);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (repo.isWorkspace) {
|
|
83
|
+
for (const name of ROOT_REQUIRED) {
|
|
84
|
+
if (repo.rootPkg.scripts[name] === undefined)
|
|
85
|
+
f("scripts/required", repo.rootPkg, name, `missing \`${name}\``);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// --- leaf bodies
|
|
90
|
+
for (const pkg of leaves(repo)) {
|
|
91
|
+
const s = pkg.scripts;
|
|
92
|
+
if (s.check !== undefined) {
|
|
93
|
+
const want = [
|
|
94
|
+
"bun run lint",
|
|
95
|
+
"bun run format:check",
|
|
96
|
+
"bun run typecheck",
|
|
97
|
+
...(s.test ? ["bun run test"] : []),
|
|
98
|
+
];
|
|
99
|
+
const got = raws(s.check);
|
|
100
|
+
const head = got.slice(0, want.length);
|
|
101
|
+
const tail = got.slice(want.length);
|
|
102
|
+
if (!sameArgv(head, want) || tail.some((c) => !/^bun run [\w:.-]+:check$/.test(c))) {
|
|
103
|
+
f(
|
|
104
|
+
"scripts/check-body",
|
|
105
|
+
pkg,
|
|
106
|
+
"check",
|
|
107
|
+
`expected \`${want.join(" && ")}\` then only \`bun run <thing>:check\`; got \`${s.check}\``,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (s.fix !== undefined && s.fix !== "bun run lint:fix && bun run format") {
|
|
112
|
+
f(
|
|
113
|
+
"scripts/fix-body",
|
|
114
|
+
pkg,
|
|
115
|
+
"fix",
|
|
116
|
+
`expected \`bun run lint:fix && bun run format\`; got \`${s.fix}\``,
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
const typecheck = {
|
|
120
|
+
oxc: "effect-tsgo tsrs --noEmit",
|
|
121
|
+
"vite-plus": "effect-tsgo tsrs --noEmit",
|
|
122
|
+
astro: "astro check",
|
|
123
|
+
pulumi: "tsc --noEmit",
|
|
124
|
+
}[pkg.toolchain];
|
|
125
|
+
if (s.typecheck !== undefined && s.typecheck !== typecheck) {
|
|
126
|
+
f(
|
|
127
|
+
"scripts/typecheck",
|
|
128
|
+
pkg,
|
|
129
|
+
"typecheck",
|
|
130
|
+
`expected \`${typecheck}\` (${pkg.toolchain}); got \`${s.typecheck}\``,
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
if (
|
|
134
|
+
pkg.toolchain === "astro" &&
|
|
135
|
+
s.lint !== undefined &&
|
|
136
|
+
s.lint !== "bun run lint:oxlint && bun run lint:astro && bun run lint:docs"
|
|
137
|
+
) {
|
|
138
|
+
f(
|
|
139
|
+
"scripts/lint-body",
|
|
140
|
+
pkg,
|
|
141
|
+
"lint",
|
|
142
|
+
`Astro docs lint is \`bun run lint:oxlint && bun run lint:astro && bun run lint:docs\`; got \`${s.lint}\``,
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
if (s.lint !== undefined && (pkg.toolchain === "oxc" || pkg.toolchain === "astro")) {
|
|
146
|
+
walk(repo, pkg, "lint", {
|
|
147
|
+
leaf: ({ script, cmd }) => {
|
|
148
|
+
if (
|
|
149
|
+
cmd.argv[0] === "oxlint" &&
|
|
150
|
+
!(cmd.argv.includes("--type-aware") && cmd.argv.includes("--type-check"))
|
|
151
|
+
) {
|
|
152
|
+
f(
|
|
153
|
+
"scripts/lint-type-aware",
|
|
154
|
+
pkg,
|
|
155
|
+
script,
|
|
156
|
+
`\`${cmd.raw}\` lacks \`--type-aware --type-check\``,
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
if (s.test !== undefined) {
|
|
163
|
+
for (const cmd of parseScript(s.test)) {
|
|
164
|
+
const [bin, sub] = cmd.argv;
|
|
165
|
+
if (
|
|
166
|
+
(bin === "vitest" && sub !== "run") ||
|
|
167
|
+
(bin === "vp" && sub === "test" && cmd.argv[2] !== "run")
|
|
168
|
+
) {
|
|
169
|
+
f(
|
|
170
|
+
"scripts/test-runs-once",
|
|
171
|
+
pkg,
|
|
172
|
+
"test",
|
|
173
|
+
`\`${cmd.raw}\` starts watch mode; use \`vitest run\``,
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// --- pairs: X:fix is X plus --fix, format:check is format in check mode
|
|
181
|
+
for (const pkg of repo.isWorkspace ? leaves(repo) : [repo.rootPkg]) {
|
|
182
|
+
for (const [name, body] of Object.entries(pkg.scripts)) {
|
|
183
|
+
const fixBody = pkg.scripts[`${name}:fix`];
|
|
184
|
+
if (!name.startsWith("lint") || name.endsWith(":fix") || fixBody === undefined) continue;
|
|
185
|
+
const a = parseScript(body);
|
|
186
|
+
const b = parseScript(fixBody);
|
|
187
|
+
// Each command is the same, or the same plus `--fix`, or `bun run X` → `bun run X:fix`; at least one must fix.
|
|
188
|
+
let fixes = false;
|
|
189
|
+
const ok =
|
|
190
|
+
a.length === b.length &&
|
|
191
|
+
a.every((ca, i) => {
|
|
192
|
+
const cb = b[i]!;
|
|
193
|
+
const ra = asBunRun(ca);
|
|
194
|
+
const rb = asBunRun(cb);
|
|
195
|
+
if (ra && rb) {
|
|
196
|
+
if (rb.script === `${ra.script}:fix`) fixes = true;
|
|
197
|
+
return rb.script === `${ra.script}:fix` || rb.script === ra.script;
|
|
198
|
+
}
|
|
199
|
+
if (sameArgv(ca.argv, cb.argv)) return true;
|
|
200
|
+
const plusFix =
|
|
201
|
+
!ca.argv.includes("--fix") &&
|
|
202
|
+
cb.argv.includes("--fix") &&
|
|
203
|
+
sameArgv(
|
|
204
|
+
cb.argv.filter((x) => x !== "--fix"),
|
|
205
|
+
ca.argv,
|
|
206
|
+
);
|
|
207
|
+
if (plusFix) fixes = true;
|
|
208
|
+
return plusFix;
|
|
209
|
+
}) &&
|
|
210
|
+
fixes;
|
|
211
|
+
if (!ok)
|
|
212
|
+
f(
|
|
213
|
+
"scripts/fix-pairs",
|
|
214
|
+
pkg,
|
|
215
|
+
`${name}:fix`,
|
|
216
|
+
`\`${name}:fix\` should be \`${name}\` with \`--fix\`; got \`${fixBody}\``,
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
const fmt = pkg.scripts.format;
|
|
220
|
+
const fmtCheck = pkg.scripts["format:check"];
|
|
221
|
+
if (
|
|
222
|
+
fmt !== undefined &&
|
|
223
|
+
fmtCheck !== undefined &&
|
|
224
|
+
!(repo.isWorkspace && pkg === repo.rootPkg)
|
|
225
|
+
) {
|
|
226
|
+
const a = parseScript(fmt);
|
|
227
|
+
const b = parseScript(fmtCheck);
|
|
228
|
+
const toCheck = (argv: readonly string[]) => {
|
|
229
|
+
if (argv.includes("--write")) return argv.map((x) => (x === "--write" ? "--check" : x));
|
|
230
|
+
return [...argv, "--check"];
|
|
231
|
+
};
|
|
232
|
+
const ok =
|
|
233
|
+
a.length === b.length &&
|
|
234
|
+
a.every((ca, i) => sameArgv([...b[i]!.argv].sort(), [...toCheck(ca.argv)].sort()));
|
|
235
|
+
if (!ok)
|
|
236
|
+
f(
|
|
237
|
+
"scripts/format-pair",
|
|
238
|
+
pkg,
|
|
239
|
+
"format:check",
|
|
240
|
+
`\`format:check\` should be \`format\` in check mode; got \`${fmtCheck}\` for \`${fmt}\``,
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// --- root fan-out
|
|
246
|
+
if (repo.isWorkspace) {
|
|
247
|
+
const root = repo.rootPkg;
|
|
248
|
+
for (const verb of VERBS) {
|
|
249
|
+
const body = root.scripts[verb];
|
|
250
|
+
if (body === undefined) continue;
|
|
251
|
+
const cmds = parseScript(body);
|
|
252
|
+
const fanIdx = cmds.findIndex((c) => {
|
|
253
|
+
const r = asBunRun(c);
|
|
254
|
+
return r?.workspaces && r.script === verb;
|
|
255
|
+
});
|
|
256
|
+
if (fanIdx < 0) {
|
|
257
|
+
f(
|
|
258
|
+
"scripts/root-fan-out",
|
|
259
|
+
root,
|
|
260
|
+
verb,
|
|
261
|
+
`expected \`bun run --workspaces${verb === "check" ? "" : " --if-present"} ${verb}\`; got \`${body}\``,
|
|
262
|
+
);
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
const fan = asBunRun(cmds[fanIdx]!)!;
|
|
266
|
+
if (verb === "check" && fan.ifPresent) {
|
|
267
|
+
f(
|
|
268
|
+
"scripts/root-fan-out",
|
|
269
|
+
root,
|
|
270
|
+
verb,
|
|
271
|
+
"root `check` must not pass `--if-present`, so a package without `check` fails the gate",
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
if (verb !== "check" && !fan.ifPresent) {
|
|
275
|
+
f("scripts/root-fan-out", root, verb, `root \`${verb}\` should pass \`--if-present\``);
|
|
276
|
+
}
|
|
277
|
+
const before = cmds.slice(0, fanIdx);
|
|
278
|
+
for (const c of before) {
|
|
279
|
+
const r = asBunRun(c);
|
|
280
|
+
if (!r || r.workspaces || r.cwd || (VERBS as readonly string[]).includes(r.script)) {
|
|
281
|
+
f(
|
|
282
|
+
"scripts/root-fan-out",
|
|
283
|
+
root,
|
|
284
|
+
verb,
|
|
285
|
+
`only a repo-wide prerequisite (\`bun run <script>\`) may run before the fan-out; got \`${c.raw}\``,
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// --- every script, every package
|
|
293
|
+
const reachedFromCheck = new Map<Pkg, Set<string>>();
|
|
294
|
+
for (const pkg of [repo.rootPkg, ...repo.workspaces]) {
|
|
295
|
+
for (const [name, body] of Object.entries(pkg.scripts)) {
|
|
296
|
+
if (name.endsWith(":global"))
|
|
297
|
+
f(
|
|
298
|
+
"scripts/no-global-suffix",
|
|
299
|
+
pkg,
|
|
300
|
+
name,
|
|
301
|
+
"`:global` names are replaced by root fan-outs under the same name",
|
|
302
|
+
);
|
|
303
|
+
for (const cmd of parseScript(body)) {
|
|
304
|
+
if (isBunx(cmd.argv))
|
|
305
|
+
f("scripts/no-bunx", pkg, name, `\`${cmd.raw}\`: install the tool and call it directly`);
|
|
306
|
+
if (isBareBunTest(cmd.argv) && name !== "test" && !name.startsWith("test:")) {
|
|
307
|
+
f(
|
|
308
|
+
"scripts/no-bare-bun-test",
|
|
309
|
+
pkg,
|
|
310
|
+
name,
|
|
311
|
+
`\`${cmd.raw}\` bypasses the \`test\` script; call \`bun run test\``,
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
const run = asBunRun(cmd);
|
|
315
|
+
if (run?.filter !== undefined)
|
|
316
|
+
f(
|
|
317
|
+
"scripts/no-filter",
|
|
318
|
+
pkg,
|
|
319
|
+
name,
|
|
320
|
+
`\`${cmd.raw}\`: use \`--workspaces\` or \`--cwd\`; \`--filter\` drops stdin`,
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
if (
|
|
324
|
+
name.startsWith("pre") &&
|
|
325
|
+
pkg.scripts[name.slice(3)] !== undefined &&
|
|
326
|
+
name !== "prepare"
|
|
327
|
+
) {
|
|
328
|
+
f(
|
|
329
|
+
"scripts/no-hooks",
|
|
330
|
+
pkg,
|
|
331
|
+
name,
|
|
332
|
+
`\`${name}\` hook runs implicitly; chain it explicitly or move it to \`ci:build\``,
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
if (pkg.scripts.check !== undefined) {
|
|
337
|
+
const reached = new Set<string>();
|
|
338
|
+
walk(repo, pkg, "check", {
|
|
339
|
+
script: (p, s) => {
|
|
340
|
+
if (p === pkg) reached.add(s);
|
|
341
|
+
},
|
|
342
|
+
leaf: ({ pkg: lp, script, cmd }) => {
|
|
343
|
+
if (isWriter(cmd.argv)) {
|
|
344
|
+
out.push({
|
|
345
|
+
rule: "scripts/check-read-only",
|
|
346
|
+
where: lp.dir,
|
|
347
|
+
script,
|
|
348
|
+
message: `\`${cmd.raw}\` writes files and runs inside \`check\``,
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
},
|
|
352
|
+
missing: (p, s) => {
|
|
353
|
+
if (s === "check")
|
|
354
|
+
out.push({
|
|
355
|
+
rule: "scripts/root-fan-out",
|
|
356
|
+
where: p.dir,
|
|
357
|
+
script: "check",
|
|
358
|
+
message: "package has no `check`, so the root fan-out fails",
|
|
359
|
+
});
|
|
360
|
+
},
|
|
361
|
+
});
|
|
362
|
+
reachedFromCheck.set(pkg, reached);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
dedupe(out);
|
|
366
|
+
|
|
367
|
+
for (const [pkg, reached] of reachedFromCheck) {
|
|
368
|
+
for (const name of Object.keys(pkg.scripts)) {
|
|
369
|
+
if (
|
|
370
|
+
name.endsWith(":check") &&
|
|
371
|
+
!(VERBS as readonly string[]).includes(name) &&
|
|
372
|
+
!reached.has(name)
|
|
373
|
+
) {
|
|
374
|
+
f("scripts/check-runs-own-checks", pkg, name, `\`${name}\` is not reached from \`check\``);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// --- root shortcuts and ci:build
|
|
380
|
+
const root = repo.rootPkg;
|
|
381
|
+
if (repo.isWorkspace) {
|
|
382
|
+
for (const [name, body] of Object.entries(root.scripts)) {
|
|
383
|
+
const cmds = parseScript(body);
|
|
384
|
+
const run = cmds.length === 1 ? asBunRun(cmds[0]!) : undefined;
|
|
385
|
+
if (run?.cwd && !(DEPLOY as readonly string[]).includes(name) && name !== "dev") {
|
|
386
|
+
f(
|
|
387
|
+
"scripts/no-root-shortcuts",
|
|
388
|
+
root,
|
|
389
|
+
name,
|
|
390
|
+
`\`${body}\`: call \`bun run --cwd ${run.cwd} ${run.script}\` directly`,
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
const ciBuild = root.scripts["ci:build"];
|
|
396
|
+
if (
|
|
397
|
+
ciBuild !== undefined &&
|
|
398
|
+
ciBuild !== "bun run check" &&
|
|
399
|
+
ciBuild !== "bun run check && bun run build"
|
|
400
|
+
) {
|
|
401
|
+
f(
|
|
402
|
+
"scripts/ci-build",
|
|
403
|
+
root,
|
|
404
|
+
"ci:build",
|
|
405
|
+
`expected \`bun run check\` (optionally \`&& bun run build\`); got \`${ciBuild}\``,
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
if (ciBuild === undefined && root.scripts["ci:deploy"] !== undefined) {
|
|
409
|
+
f("scripts/ci-build", root, "ci:build", "`ci:deploy` exists but `ci:build` does not");
|
|
410
|
+
}
|
|
411
|
+
for (const pkg of repo.workspaces) {
|
|
412
|
+
if (pkg.scripts["ci:build"] !== undefined)
|
|
413
|
+
f("scripts/ci-build", pkg, "ci:build", "`ci:build` belongs to the repo root only");
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
out.push(...deployRules(repo));
|
|
417
|
+
return out;
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
const dedupe = (out: Finding[]) => {
|
|
421
|
+
const seen = new Set<string>();
|
|
422
|
+
for (let i = out.length - 1; i >= 0; i--) {
|
|
423
|
+
const k = JSON.stringify(out[i]);
|
|
424
|
+
if (seen.has(k)) out.splice(i, 1);
|
|
425
|
+
else seen.add(k);
|
|
426
|
+
}
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
/** The explicit target of an alchemy/pulumi command: `--stage X`, `--stack X`, or the env assignments in front of it. */
|
|
430
|
+
const targetOf = (cmd: Command): string | undefined => {
|
|
431
|
+
const i = cmd.argv.findIndex((a) => a === "--stage" || a === "--stack");
|
|
432
|
+
const flag =
|
|
433
|
+
i >= 0
|
|
434
|
+
? `${cmd.argv[i]} ${cmd.argv[i + 1]}`
|
|
435
|
+
: cmd.argv.find((a) => a.startsWith("--stage=") || a.startsWith("--stack="));
|
|
436
|
+
const env = [...cmd.env]
|
|
437
|
+
.map(([k, v]) => `${k}=${v}`)
|
|
438
|
+
.sort()
|
|
439
|
+
.join(" ");
|
|
440
|
+
const parts = [env, flag].filter(Boolean);
|
|
441
|
+
return parts.length > 0 ? parts.join(" ") : undefined;
|
|
442
|
+
};
|
|
443
|
+
|
|
444
|
+
const deployRules = (repo: Repo): Finding[] => {
|
|
445
|
+
const out: Finding[] = [];
|
|
446
|
+
for (const pkg of [repo.rootPkg, ...repo.workspaces]) {
|
|
447
|
+
const targets = new Map<string, string | undefined>();
|
|
448
|
+
for (const name of DEPLOY) {
|
|
449
|
+
const body = pkg.scripts[name];
|
|
450
|
+
if (body === undefined) continue;
|
|
451
|
+
for (const cmd of parseScript(body)) {
|
|
452
|
+
const [bin, sub] = cmd.argv;
|
|
453
|
+
if (bin !== "alchemy" && bin !== "pulumi") continue;
|
|
454
|
+
const target = targetOf(cmd);
|
|
455
|
+
targets.set(name, target);
|
|
456
|
+
if (target === undefined) {
|
|
457
|
+
out.push({
|
|
458
|
+
rule: "deploy/explicit-target",
|
|
459
|
+
where: pkg.dir,
|
|
460
|
+
script: name,
|
|
461
|
+
message: `\`${cmd.raw}\` relies on the implicit stage`,
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
const yes = cmd.argv.includes("--yes") || cmd.argv.includes("-y");
|
|
465
|
+
if (name === "ci:deploy" && !yes)
|
|
466
|
+
out.push({
|
|
467
|
+
rule: "deploy/interactive",
|
|
468
|
+
where: pkg.dir,
|
|
469
|
+
script: name,
|
|
470
|
+
message: "`ci:deploy` must pass `--yes`",
|
|
471
|
+
});
|
|
472
|
+
if ((name === "deploy" || name === "destroy") && yes) {
|
|
473
|
+
out.push({
|
|
474
|
+
rule: "deploy/interactive",
|
|
475
|
+
where: pkg.dir,
|
|
476
|
+
script: name,
|
|
477
|
+
message: `\`${name}\` must ask for confirmation; drop \`--yes\``,
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
if (name === "plan" && bin === "alchemy" && sub !== "plan") {
|
|
481
|
+
out.push({
|
|
482
|
+
rule: "deploy/plan",
|
|
483
|
+
where: pkg.dir,
|
|
484
|
+
script: name,
|
|
485
|
+
message: `\`plan\` is \`alchemy plan\`; got \`${cmd.raw}\``,
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
const distinct = new Set([...targets.values()].filter((t) => t !== undefined));
|
|
491
|
+
if (distinct.size > 1) {
|
|
492
|
+
out.push({
|
|
493
|
+
rule: "deploy/same-target",
|
|
494
|
+
where: pkg.dir,
|
|
495
|
+
message: `deploy scripts disagree on the target: ${[...targets].map(([n, t]) => `${n}=${t}`).join(", ")}`,
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
if (targets.has("deploy") && !targets.has("plan")) {
|
|
499
|
+
out.push({
|
|
500
|
+
rule: "deploy/plan",
|
|
501
|
+
where: pkg.dir,
|
|
502
|
+
script: "plan",
|
|
503
|
+
message: "`deploy` exists but `plan` does not",
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
for (const name of ["deploy", "ci:deploy"]) {
|
|
507
|
+
if (pkg.scripts[name] === undefined) continue;
|
|
508
|
+
const checks: string[] = [];
|
|
509
|
+
walk(repo, pkg, name, {
|
|
510
|
+
script: (_p, s) => {
|
|
511
|
+
if ((VERBS as readonly string[]).includes(s)) checks.push(s);
|
|
512
|
+
},
|
|
513
|
+
leaf: ({ pkg: lp, script, cmd }) => {
|
|
514
|
+
if (isWriter(cmd.argv))
|
|
515
|
+
out.push({
|
|
516
|
+
rule: "deploy/no-checks",
|
|
517
|
+
where: lp.dir,
|
|
518
|
+
script,
|
|
519
|
+
message: `\`${cmd.raw}\` rewrites tracked files during \`${name}\``,
|
|
520
|
+
});
|
|
521
|
+
},
|
|
522
|
+
});
|
|
523
|
+
if (checks.length > 0) {
|
|
524
|
+
const list = checks.map((c) => `\`${c}\``).join(", ");
|
|
525
|
+
out.push({
|
|
526
|
+
rule: "deploy/no-checks",
|
|
527
|
+
where: pkg.dir,
|
|
528
|
+
script: name,
|
|
529
|
+
message: `\`${name}\` runs ${list}; \`ci:build\` already checked`,
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
return out;
|
|
535
|
+
};
|
package/src/shell.ts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// Just enough shell parsing for package.json script bodies: split on && || ; |,
|
|
2
|
+
// honour quotes, and peel leading VAR=value assignments.
|
|
3
|
+
|
|
4
|
+
export interface Command {
|
|
5
|
+
readonly env: ReadonlyMap<string, string>;
|
|
6
|
+
readonly argv: readonly string[];
|
|
7
|
+
readonly raw: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const tokenize = (body: string): string[][] => {
|
|
11
|
+
const commands: string[][] = [];
|
|
12
|
+
let current: string[] = [];
|
|
13
|
+
let token = "";
|
|
14
|
+
let hasToken = false;
|
|
15
|
+
let quote: "'" | '"' | null = null;
|
|
16
|
+
const pushToken = () => {
|
|
17
|
+
if (hasToken) current.push(token);
|
|
18
|
+
token = "";
|
|
19
|
+
hasToken = false;
|
|
20
|
+
};
|
|
21
|
+
const pushCommand = () => {
|
|
22
|
+
pushToken();
|
|
23
|
+
if (current.length > 0) commands.push(current);
|
|
24
|
+
current = [];
|
|
25
|
+
};
|
|
26
|
+
for (let i = 0; i < body.length; i++) {
|
|
27
|
+
const c = body[i]!;
|
|
28
|
+
if (quote) {
|
|
29
|
+
if (c === quote) quote = null;
|
|
30
|
+
else token += c;
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
// GitHub expressions such as `${{ matrix.target }}` are one word.
|
|
34
|
+
if (c === "$" && body.startsWith("${{", i)) {
|
|
35
|
+
const end = body.indexOf("}}", i);
|
|
36
|
+
const stop = end < 0 ? body.length : end + 2;
|
|
37
|
+
token += body.slice(i, stop);
|
|
38
|
+
hasToken = true;
|
|
39
|
+
i = stop - 1;
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
if (c === "'" || c === '"') {
|
|
43
|
+
quote = c;
|
|
44
|
+
hasToken = true;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (c === "&" && body[i + 1] === "&") {
|
|
48
|
+
pushCommand();
|
|
49
|
+
i++;
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (c === "|" || c === ";") {
|
|
53
|
+
pushCommand();
|
|
54
|
+
if (body[i + 1] === "|") i++;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (c === " " || c === "\t" || c === "\n") {
|
|
58
|
+
pushToken();
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
token += c;
|
|
62
|
+
hasToken = true;
|
|
63
|
+
}
|
|
64
|
+
pushCommand();
|
|
65
|
+
return commands;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export const parseScript = (body: string): Command[] =>
|
|
69
|
+
tokenize(body).map((tokens) => {
|
|
70
|
+
const env = new Map<string, string>();
|
|
71
|
+
let i = 0;
|
|
72
|
+
for (; i < tokens.length; i++) {
|
|
73
|
+
const m = /^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/s.exec(tokens[i]!);
|
|
74
|
+
if (!m) break;
|
|
75
|
+
env.set(m[1]!, m[2]!);
|
|
76
|
+
}
|
|
77
|
+
const argv = tokens.slice(i);
|
|
78
|
+
return { env, argv, raw: tokens.join(" ") };
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
export interface BunRun {
|
|
82
|
+
readonly workspaces: boolean;
|
|
83
|
+
readonly ifPresent: boolean;
|
|
84
|
+
readonly cwd: string | undefined;
|
|
85
|
+
readonly filter: string | undefined;
|
|
86
|
+
readonly script: string;
|
|
87
|
+
readonly args: readonly string[];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** `bun run [flags] <script> [args]`, or undefined for anything else (including `bun run file.ts`). */
|
|
91
|
+
export const asBunRun = (cmd: Command): BunRun | undefined => {
|
|
92
|
+
const [bin, sub, ...rest] = cmd.argv;
|
|
93
|
+
if (bin !== "bun" || sub !== "run") return undefined;
|
|
94
|
+
let workspaces = false;
|
|
95
|
+
let ifPresent = false;
|
|
96
|
+
let cwd: string | undefined;
|
|
97
|
+
let filter: string | undefined;
|
|
98
|
+
let i = 0;
|
|
99
|
+
for (; i < rest.length; i++) {
|
|
100
|
+
const a = rest[i]!;
|
|
101
|
+
if (a === "--workspaces") workspaces = true;
|
|
102
|
+
else if (a === "--if-present") ifPresent = true;
|
|
103
|
+
else if (a === "--sequential" || a === "--silent" || a === "--bun") continue;
|
|
104
|
+
else if (a === "--cwd") cwd = rest[++i];
|
|
105
|
+
else if (a.startsWith("--cwd=")) cwd = a.slice(6);
|
|
106
|
+
else if (a === "--filter" || a === "-F") filter = rest[++i];
|
|
107
|
+
else if (a.startsWith("--filter=")) filter = a.slice(9);
|
|
108
|
+
else break;
|
|
109
|
+
}
|
|
110
|
+
const script = rest[i];
|
|
111
|
+
if (script === undefined || /\.(m?[jt]s|tsx?)$/.test(script)) return undefined;
|
|
112
|
+
return { workspaces, ifPresent, cwd, filter, script, args: rest.slice(i + 1) };
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/** Commands that rewrite tracked files. */
|
|
116
|
+
export const isWriter = (argv: readonly string[]): boolean => {
|
|
117
|
+
const [bin, sub] = argv;
|
|
118
|
+
const has = (flag: string) => argv.includes(flag);
|
|
119
|
+
if (has("--fix") || has("--write") || has("-w")) return true;
|
|
120
|
+
if (bin === "oxfmt") return !has("--check") && !has("--list-different");
|
|
121
|
+
if (bin === "vp" && sub === "fmt") return !has("--check");
|
|
122
|
+
if (bin === "cargo" && sub === "fmt") return !has("--check");
|
|
123
|
+
if (bin === "biome" && (sub === "format" || sub === "check")) return has("--write");
|
|
124
|
+
return false;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
export const isBareBunTest = (argv: readonly string[]): boolean =>
|
|
128
|
+
argv[0] === "bun" && argv[1] === "test";
|
|
129
|
+
|
|
130
|
+
export const isBunx = (argv: readonly string[]): boolean =>
|
|
131
|
+
argv[0] === "bunx" || (argv[0] === "bun" && argv[1] === "x");
|