@incremental/typescript 0.1.0 → 0.1.5
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/dist/src/index.d.ts +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/project.d.ts +1 -0
- package/dist/src/project.d.ts.map +1 -1
- package/dist/src/project.js +111 -6
- package/dist/src/project.js.map +1 -1
- package/dist/src/run.js +2 -2
- package/dist/src/run.js.map +1 -1
- package/package.json +16 -4
- package/dist/tests/adapter.test.d.ts +0 -2
- package/dist/tests/adapter.test.d.ts.map +0 -1
- package/dist/tests/adapter.test.js +0 -147
- package/dist/tests/adapter.test.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/src/index.ts +0 -12
- package/src/project.ts +0 -247
- package/src/run.ts +0 -438
- package/tests/adapter.test.ts +0 -169
- package/tsconfig.json +0 -8
package/src/run.ts
DELETED
|
@@ -1,438 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// `incremental-typescript run`: the TypeScript adapter loop (PLAN 10.4).
|
|
3
|
-
//
|
|
4
|
-
// B = store baseline
|
|
5
|
-
// ch = ChangeSet from B to the worktree (incremental-git)
|
|
6
|
-
// plan = affected(ch, context, declared projects)
|
|
7
|
-
// forget the projects whose tsconfig was deleted
|
|
8
|
-
// check each selected project with tsc --noEmit and observe it
|
|
9
|
-
// advance to HEAD only if every selected project ran, the worktree is clean
|
|
10
|
-
// and HEAD did not move; otherwise exit 7 (ran, not advanced).
|
|
11
|
-
//
|
|
12
|
-
// Exit codes: 0 ok and advanced; 1 a project failed to type check (advanced when
|
|
13
|
-
// allowed); 2 usage error; 4 engine or setup error; 7 ran but not advanced.
|
|
14
|
-
import { spawnSync } from "node:child_process";
|
|
15
|
-
import { existsSync, readFileSync } from "node:fs";
|
|
16
|
-
import path from "node:path";
|
|
17
|
-
import { pathToFileURL } from "node:url";
|
|
18
|
-
import {
|
|
19
|
-
connect,
|
|
20
|
-
defaultEngine,
|
|
21
|
-
isIncrementalError,
|
|
22
|
-
repoRoot,
|
|
23
|
-
type Change,
|
|
24
|
-
type Context,
|
|
25
|
-
type Fallback,
|
|
26
|
-
type Input,
|
|
27
|
-
type Observation,
|
|
28
|
-
type Outcome,
|
|
29
|
-
type Plan,
|
|
30
|
-
} from "@incremental/client";
|
|
31
|
-
import {
|
|
32
|
-
allPackageJsons,
|
|
33
|
-
checkProject,
|
|
34
|
-
configChain,
|
|
35
|
-
discoverProjects,
|
|
36
|
-
packageJsonsFor,
|
|
37
|
-
relPath,
|
|
38
|
-
sha256,
|
|
39
|
-
TS_PACKAGE_DIR,
|
|
40
|
-
typescriptVersion,
|
|
41
|
-
} from "./project.js";
|
|
42
|
-
|
|
43
|
-
export interface RunOptions {
|
|
44
|
-
root: string;
|
|
45
|
-
store: string;
|
|
46
|
-
base?: string;
|
|
47
|
-
all: boolean;
|
|
48
|
-
// Timeout for one tsc run, in milliseconds.
|
|
49
|
-
timeoutMs: number;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export interface ProjectRun {
|
|
53
|
-
task: string;
|
|
54
|
-
outcome: Outcome;
|
|
55
|
-
durationMs: number;
|
|
56
|
-
problem: string | null;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export interface RunReport {
|
|
60
|
-
mode: "run" | "all";
|
|
61
|
-
root: string;
|
|
62
|
-
stored: string | null;
|
|
63
|
-
base: string | null;
|
|
64
|
-
head: string;
|
|
65
|
-
headEnd: string;
|
|
66
|
-
dirty: boolean;
|
|
67
|
-
changes: Change[];
|
|
68
|
-
plan: Plan | null;
|
|
69
|
-
fallback: Fallback | null;
|
|
70
|
-
selected: string[];
|
|
71
|
-
discovered: string[];
|
|
72
|
-
results: ProjectRun[];
|
|
73
|
-
observed: number;
|
|
74
|
-
advanced: boolean;
|
|
75
|
-
notAdvanced: string[];
|
|
76
|
-
exit: number;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export class UsageError extends Error {
|
|
80
|
-
constructor(message: string) {
|
|
81
|
-
super(message);
|
|
82
|
-
this.name = "UsageError";
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
interface GitChangeSet {
|
|
87
|
-
baseline: string;
|
|
88
|
-
head: string;
|
|
89
|
-
dirty: boolean;
|
|
90
|
-
changes: Change[];
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// The git change source (PLAN 11.7). Throws UsageError-compatible Error on failure.
|
|
94
|
-
function gitSourcePath(): string {
|
|
95
|
-
return (
|
|
96
|
-
process.env.INCREMENTAL_GIT ||
|
|
97
|
-
path.join(repoRoot(), "adapters", "git", "dist", "incremental-git")
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function gitChangeSet(root: string, ref: string): GitChangeSet {
|
|
102
|
-
const r = spawnSync(gitSourcePath(), ["changes", "--base", ref, "--root", root], {
|
|
103
|
-
encoding: "utf8",
|
|
104
|
-
});
|
|
105
|
-
if (r.error !== undefined)
|
|
106
|
-
throw new Error(`cannot run the git change source: ${r.error.message}`);
|
|
107
|
-
if (r.status !== 0)
|
|
108
|
-
throw new Error(
|
|
109
|
-
`git change source failed (exit ${r.status}): ${(r.stdout || r.stderr || "").trim()}`,
|
|
110
|
-
);
|
|
111
|
-
return JSON.parse(r.stdout) as GitChangeSet;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
function hashOrAbsent(file: string): string {
|
|
115
|
-
return existsSync(file) ? sha256(readFileSync(file)) : "absent";
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// The context (PLAN 8.4). Environment reads and directory listings are not
|
|
119
|
-
// observed by this adapter, so those capabilities are false.
|
|
120
|
-
export function contextFor(root: string): Context {
|
|
121
|
-
const lock = ["package-lock.json", "pnpm-lock.yaml", "yarn.lock"].find((f) =>
|
|
122
|
-
existsSync(path.join(root, f)),
|
|
123
|
-
);
|
|
124
|
-
const pkgs = allPackageJsons(root);
|
|
125
|
-
const pkgjson =
|
|
126
|
-
pkgs.length === 0
|
|
127
|
-
? "absent"
|
|
128
|
-
: sha256(pkgs.map((rel) => `${rel}\0${hashOrAbsent(path.join(root, rel))}`).join("\n"));
|
|
129
|
-
return {
|
|
130
|
-
lockfile: lock === undefined ? "absent" : hashOrAbsent(path.join(root, lock)),
|
|
131
|
-
tsconfig: hashOrAbsent(path.join(root, "tsconfig.json")),
|
|
132
|
-
pkgjson,
|
|
133
|
-
runner: `typescript@${typescriptVersion()}`,
|
|
134
|
-
node: process.version,
|
|
135
|
-
fmt: 1,
|
|
136
|
-
caps: { probes: false, dirs: false, env: false },
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
function ensureStore(store: string): void {
|
|
141
|
-
if (existsSync(store)) return;
|
|
142
|
-
const r = spawnSync(defaultEngine(), ["init", "--store", store], { encoding: "utf8" });
|
|
143
|
-
if (r.status !== 0)
|
|
144
|
-
throw new UsageError(`cannot create the store at ${store}: ${(r.stderr || "").trim()}`);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// Inputs of one project: every file tsc reads (hashed under root), its extends
|
|
148
|
-
// chain, and the package.json files on its paths. Files inside the TypeScript
|
|
149
|
-
// package are covered by the runner version in the context. Anything else
|
|
150
|
-
// outside root cannot be named repo-relative, so the observation is dirty.
|
|
151
|
-
type FileInput = Extract<Input, { kind: "file" }>;
|
|
152
|
-
|
|
153
|
-
function inputsFor(
|
|
154
|
-
root: string,
|
|
155
|
-
configAbs: string,
|
|
156
|
-
listed: readonly string[],
|
|
157
|
-
): { inputs: FileInput[]; dirty: boolean } {
|
|
158
|
-
const chain = configChain(configAbs);
|
|
159
|
-
let dirty = chain.references || chain.unhashable.length > 0;
|
|
160
|
-
const files = new Set<string>(chain.files);
|
|
161
|
-
for (const f of listed) {
|
|
162
|
-
if (!f.startsWith(TS_PACKAGE_DIR + path.sep)) {
|
|
163
|
-
if (f.startsWith(root + path.sep)) {
|
|
164
|
-
files.add(f);
|
|
165
|
-
} else {
|
|
166
|
-
dirty = true;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
const all = [...files, ...packageJsonsFor(root, [...files]).map((rel) => path.join(root, rel))];
|
|
171
|
-
const inputs = new Map<string, FileInput>();
|
|
172
|
-
for (const abs of all) {
|
|
173
|
-
const rel = relPath(root, abs);
|
|
174
|
-
inputs.set(rel, { kind: "file", path: rel, hash: sha256(readFileSync(abs)) });
|
|
175
|
-
}
|
|
176
|
-
return {
|
|
177
|
-
inputs: [...inputs.values()].sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0)),
|
|
178
|
-
dirty,
|
|
179
|
-
};
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
export async function runAdapter(opts: RunOptions): Promise<RunReport> {
|
|
183
|
-
const root = path.resolve(opts.root);
|
|
184
|
-
const store = path.resolve(opts.store);
|
|
185
|
-
ensureStore(store);
|
|
186
|
-
|
|
187
|
-
const context = contextFor(root);
|
|
188
|
-
const discovered = discoverProjects(root);
|
|
189
|
-
|
|
190
|
-
let head0: GitChangeSet;
|
|
191
|
-
try {
|
|
192
|
-
head0 = gitChangeSet(root, "HEAD");
|
|
193
|
-
} catch (e) {
|
|
194
|
-
throw new UsageError(e instanceof Error ? e.message : String(e));
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
const client = await connect({
|
|
198
|
-
store,
|
|
199
|
-
root,
|
|
200
|
-
command: [defaultEngine(), "serve"],
|
|
201
|
-
timeoutMs: 60_000,
|
|
202
|
-
});
|
|
203
|
-
try {
|
|
204
|
-
const stored = (await client.baseline()).baseline;
|
|
205
|
-
|
|
206
|
-
let cs: GitChangeSet | null = null;
|
|
207
|
-
let gitError: string | null = null;
|
|
208
|
-
try {
|
|
209
|
-
cs = gitChangeSet(root, opts.base ?? stored ?? "HEAD");
|
|
210
|
-
} catch (e) {
|
|
211
|
-
gitError = e instanceof Error ? e.message : String(e);
|
|
212
|
-
}
|
|
213
|
-
if (cs === null && (opts.base !== undefined || stored === null)) {
|
|
214
|
-
throw new UsageError(gitError ?? "the git change source failed");
|
|
215
|
-
}
|
|
216
|
-
if (cs !== null && opts.base !== undefined && stored !== null && cs.baseline !== stored) {
|
|
217
|
-
throw new UsageError(
|
|
218
|
-
`--base resolves to ${cs.baseline}, but the store baseline is ${stored}`,
|
|
219
|
-
);
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
let plan: Plan | null = null;
|
|
223
|
-
if (cs !== null) {
|
|
224
|
-
plan = await client.affected(
|
|
225
|
-
{ baseline: cs.baseline, head: cs.head, changes: cs.changes },
|
|
226
|
-
{
|
|
227
|
-
baseline: cs.baseline,
|
|
228
|
-
context,
|
|
229
|
-
declared_tasks: discovered,
|
|
230
|
-
verify: false,
|
|
231
|
-
policy: "balanced",
|
|
232
|
-
},
|
|
233
|
-
);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
const deleted = new Set<string>(plan?.deleted_tasks ?? []);
|
|
237
|
-
for (const c of cs?.changes ?? []) {
|
|
238
|
-
if (c.status === "deleted" && c.path.endsWith("tsconfig.json")) deleted.add(c.path);
|
|
239
|
-
if (
|
|
240
|
-
c.status === "renamed" &&
|
|
241
|
-
c.old_path !== undefined &&
|
|
242
|
-
c.old_path.endsWith("tsconfig.json")
|
|
243
|
-
)
|
|
244
|
-
deleted.add(c.old_path);
|
|
245
|
-
}
|
|
246
|
-
for (const t of [...deleted].sort()) await client.forget(t);
|
|
247
|
-
|
|
248
|
-
const runAll = opts.all || plan === null || plan.fallback !== null;
|
|
249
|
-
const planned = new Set((plan?.tasks ?? []).map((t) => t.id));
|
|
250
|
-
const selected = runAll ? discovered : discovered.filter((t) => planned.has(t));
|
|
251
|
-
|
|
252
|
-
const results: ProjectRun[] = [];
|
|
253
|
-
const observations: Observation[] = [];
|
|
254
|
-
const obsBaseline = stored ?? head0.head;
|
|
255
|
-
for (const task of selected) {
|
|
256
|
-
const configAbs = path.join(root, task);
|
|
257
|
-
const check = checkProject(configAbs, opts.timeoutMs);
|
|
258
|
-
results.push({
|
|
259
|
-
task,
|
|
260
|
-
outcome: check.outcome,
|
|
261
|
-
durationMs: check.durationMs,
|
|
262
|
-
problem: check.problem,
|
|
263
|
-
});
|
|
264
|
-
if (check.outcome !== "error") {
|
|
265
|
-
let inputs: FileInput[];
|
|
266
|
-
let dirtyInputs: boolean;
|
|
267
|
-
try {
|
|
268
|
-
({ inputs, dirty: dirtyInputs } = inputsFor(root, configAbs, check.files));
|
|
269
|
-
observations.push({
|
|
270
|
-
task,
|
|
271
|
-
baseline: obsBaseline,
|
|
272
|
-
dirty: dirtyInputs,
|
|
273
|
-
context,
|
|
274
|
-
inputs,
|
|
275
|
-
outputs: [],
|
|
276
|
-
outcome: check.outcome,
|
|
277
|
-
duration_ms: check.durationMs,
|
|
278
|
-
});
|
|
279
|
-
} catch (e) {
|
|
280
|
-
const problem = e instanceof Error ? e.message : String(e);
|
|
281
|
-
results[results.length - 1] = {
|
|
282
|
-
task,
|
|
283
|
-
outcome: "error",
|
|
284
|
-
durationMs: check.durationMs,
|
|
285
|
-
problem,
|
|
286
|
-
};
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
const end = gitChangeSet(root, "HEAD");
|
|
292
|
-
const dirty = end.dirty;
|
|
293
|
-
const headEnd = end.head;
|
|
294
|
-
if (dirty) for (const o of observations) o.dirty = true;
|
|
295
|
-
|
|
296
|
-
const allRan = results.every((r) => r.outcome !== "error");
|
|
297
|
-
const canAdvance = allRan && !dirty && headEnd === head0.head;
|
|
298
|
-
if (observations.length > 0) await client.observeMany(observations);
|
|
299
|
-
let advanced = false;
|
|
300
|
-
if (canAdvance) {
|
|
301
|
-
if (headEnd !== stored) await client.advance(headEnd, stored);
|
|
302
|
-
advanced = true;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
const notAdvanced: string[] = [];
|
|
306
|
-
if (!canAdvance) {
|
|
307
|
-
for (const r of results)
|
|
308
|
-
if (r.outcome === "error")
|
|
309
|
-
notAdvanced.push(`error: ${r.task}${r.problem ? ` (${r.problem})` : ""}`);
|
|
310
|
-
if (dirty) notAdvanced.push("the worktree is dirty at the end of the run");
|
|
311
|
-
if (headEnd !== head0.head) notAdvanced.push("HEAD moved during the run");
|
|
312
|
-
}
|
|
313
|
-
const failed = results.some((r) => r.outcome === "fail");
|
|
314
|
-
const exit = !advanced ? 7 : failed ? 1 : 0;
|
|
315
|
-
|
|
316
|
-
return {
|
|
317
|
-
mode: opts.all ? "all" : "run",
|
|
318
|
-
root,
|
|
319
|
-
stored,
|
|
320
|
-
base: cs?.baseline ?? null,
|
|
321
|
-
head: head0.head,
|
|
322
|
-
headEnd,
|
|
323
|
-
dirty,
|
|
324
|
-
changes: cs?.changes ?? [],
|
|
325
|
-
plan,
|
|
326
|
-
fallback:
|
|
327
|
-
plan?.fallback ??
|
|
328
|
-
(runAll && plan === null ? { action: "run_all", reason: "git_source" } : null),
|
|
329
|
-
selected,
|
|
330
|
-
discovered,
|
|
331
|
-
results,
|
|
332
|
-
observed: observations.length,
|
|
333
|
-
advanced,
|
|
334
|
-
notAdvanced,
|
|
335
|
-
exit,
|
|
336
|
-
};
|
|
337
|
-
} finally {
|
|
338
|
-
await client.close();
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
interface ParsedArgs {
|
|
343
|
-
opts: RunOptions;
|
|
344
|
-
json: boolean;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
export function parseArgs(argv: string[]): ParsedArgs {
|
|
348
|
-
const [cmd, ...rest] = argv;
|
|
349
|
-
if (cmd !== "run")
|
|
350
|
-
throw new UsageError(
|
|
351
|
-
"usage: incremental-typescript run [--base REF] [--json] [--all] [--store DIR] [--root DIR] [--timeout MS]",
|
|
352
|
-
);
|
|
353
|
-
const opts: RunOptions = { root: process.cwd(), store: "", all: false, timeoutMs: 600_000 };
|
|
354
|
-
let json = false;
|
|
355
|
-
let storeGiven = false;
|
|
356
|
-
for (let i = 0; i < rest.length; i++) {
|
|
357
|
-
const a = rest[i] as string;
|
|
358
|
-
const value = (): string => {
|
|
359
|
-
const v = rest[++i];
|
|
360
|
-
if (v === undefined) throw new UsageError(`${a} needs a value`);
|
|
361
|
-
return v;
|
|
362
|
-
};
|
|
363
|
-
if (a === "--base") {
|
|
364
|
-
opts.base = value();
|
|
365
|
-
} else if (a === "--json") {
|
|
366
|
-
json = true;
|
|
367
|
-
} else if (a === "--all") {
|
|
368
|
-
opts.all = true;
|
|
369
|
-
} else if (a === "--store") {
|
|
370
|
-
opts.store = value();
|
|
371
|
-
storeGiven = true;
|
|
372
|
-
} else if (a === "--root") {
|
|
373
|
-
opts.root = value();
|
|
374
|
-
} else if (a === "--timeout") {
|
|
375
|
-
const n = Number(value());
|
|
376
|
-
if (!Number.isInteger(n) || n < 1) throw new UsageError("--timeout needs a positive integer");
|
|
377
|
-
opts.timeoutMs = n;
|
|
378
|
-
} else {
|
|
379
|
-
throw new UsageError(`unknown option ${a}`);
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
opts.root = path.resolve(opts.root);
|
|
383
|
-
if (!storeGiven)
|
|
384
|
-
opts.store = process.env.INCREMENTAL_STORE || path.join(opts.root, ".incremental");
|
|
385
|
-
return { opts, json };
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
function printText(r: RunReport): void {
|
|
389
|
-
const counts: Record<Outcome, number> = { pass: 0, fail: 0, error: 0, none: 0 };
|
|
390
|
-
for (const t of r.results) counts[t.outcome]++;
|
|
391
|
-
const lines = [
|
|
392
|
-
`incremental typescript ${r.mode}: stored ${r.stored ?? "none"}, query ${r.base ?? "none"}, head ${r.headEnd}${r.dirty ? " (dirty)" : ""}`,
|
|
393
|
-
];
|
|
394
|
-
if (r.fallback !== null) lines.push(`fallback: ${r.fallback.reason}`);
|
|
395
|
-
else if (r.plan !== null)
|
|
396
|
-
lines.push(
|
|
397
|
-
`plan: ${r.plan.tasks.length} invalid of ${r.plan.total_tasks}, confidence ${r.plan.confidence}`,
|
|
398
|
-
);
|
|
399
|
-
lines.push(
|
|
400
|
-
`checked ${r.results.length}: pass ${counts.pass}, fail ${counts.fail}, error ${counts.error}`,
|
|
401
|
-
);
|
|
402
|
-
for (const t of r.results)
|
|
403
|
-
if (t.outcome !== "pass")
|
|
404
|
-
lines.push(` ${t.outcome} ${t.task}${t.problem ? ` (${t.problem})` : ""}`);
|
|
405
|
-
lines.push(r.advanced ? `advanced to ${r.headEnd}` : `not advanced: ${r.notAdvanced.join("; ")}`);
|
|
406
|
-
lines.push(`exit ${r.exit}`);
|
|
407
|
-
process.stdout.write(`${lines.join("\n")}\n`);
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
export async function main(argv: string[]): Promise<number> {
|
|
411
|
-
let parsed: ParsedArgs;
|
|
412
|
-
try {
|
|
413
|
-
parsed = parseArgs(argv);
|
|
414
|
-
} catch (e) {
|
|
415
|
-
process.stderr.write(`${e instanceof Error ? e.message : String(e)}\n`);
|
|
416
|
-
return 2;
|
|
417
|
-
}
|
|
418
|
-
try {
|
|
419
|
-
const report = await runAdapter(parsed.opts);
|
|
420
|
-
if (parsed.json) process.stdout.write(`${JSON.stringify(report)}\n`);
|
|
421
|
-
else printText(report);
|
|
422
|
-
return report.exit;
|
|
423
|
-
} catch (e) {
|
|
424
|
-
const code =
|
|
425
|
-
e instanceof UsageError ? "invalid_params" : isIncrementalError(e) ? e.code : "internal";
|
|
426
|
-
const message = e instanceof Error ? e.message : String(e);
|
|
427
|
-
if (parsed.json)
|
|
428
|
-
process.stdout.write(`${JSON.stringify({ ok: false, error: { code, message } })}\n`);
|
|
429
|
-
process.stderr.write(`incremental typescript: ${message}\n`);
|
|
430
|
-
return e instanceof UsageError ? 2 : 4;
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
if (process.argv[1] !== undefined && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
435
|
-
main(process.argv.slice(2)).then((code) => {
|
|
436
|
-
process.exitCode = code;
|
|
437
|
-
});
|
|
438
|
-
}
|
package/tests/adapter.test.ts
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
// Integration tests: a scratch git repository with two TypeScript projects, run
|
|
2
|
-
// through the real engine and incremental-git (spec/adapters/typescript.md).
|
|
3
|
-
import assert from "node:assert/strict";
|
|
4
|
-
import { spawnSync } from "node:child_process";
|
|
5
|
-
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
6
|
-
import os from "node:os";
|
|
7
|
-
import path from "node:path";
|
|
8
|
-
import { test } from "node:test";
|
|
9
|
-
import { discoverProjects, programFiles, runAdapter, type RunReport } from "../src/index.js";
|
|
10
|
-
|
|
11
|
-
const GIT_ENV = {
|
|
12
|
-
...process.env,
|
|
13
|
-
GIT_AUTHOR_NAME: "t",
|
|
14
|
-
GIT_AUTHOR_EMAIL: "t@example.com",
|
|
15
|
-
GIT_COMMITTER_NAME: "t",
|
|
16
|
-
GIT_COMMITTER_EMAIL: "t@example.com",
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
function git(cwd: string, ...args: string[]): string {
|
|
20
|
-
const r = spawnSync("git", args, { cwd, encoding: "utf8", env: GIT_ENV });
|
|
21
|
-
if (r.status !== 0) throw new Error(`git ${args.join(" ")} failed: ${r.stderr}`);
|
|
22
|
-
return r.stdout.trim();
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function write(root: string, rel: string, content: string): void {
|
|
26
|
-
const abs = path.join(root, rel);
|
|
27
|
-
mkdirSync(path.dirname(abs), { recursive: true });
|
|
28
|
-
writeFileSync(abs, content);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function commit(root: string, message: string): void {
|
|
32
|
-
git(root, "add", "-A");
|
|
33
|
-
git(root, "commit", "-q", "-m", message);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function makeRepo(): { root: string; store: string } {
|
|
37
|
-
const base = mkdtempSync(path.join(os.tmpdir(), "incremental-ts-"));
|
|
38
|
-
const root = path.join(base, "repo");
|
|
39
|
-
mkdirSync(root);
|
|
40
|
-
git(root, "init", "-q", "-b", "main");
|
|
41
|
-
write(root, "package.json", JSON.stringify({ name: "fixture", private: true }));
|
|
42
|
-
for (const pkg of ["a", "b"]) {
|
|
43
|
-
write(
|
|
44
|
-
root,
|
|
45
|
-
`pkg/${pkg}/tsconfig.json`,
|
|
46
|
-
JSON.stringify({ compilerOptions: { strict: true, noEmit: true }, include: ["src"] }),
|
|
47
|
-
);
|
|
48
|
-
write(root, `pkg/${pkg}/src/index.ts`, `export const ${pkg}: number = 1;\n`);
|
|
49
|
-
}
|
|
50
|
-
write(root, "README.md", "fixture\n");
|
|
51
|
-
commit(root, "init");
|
|
52
|
-
return { root, store: path.join(base, "store") };
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function cleanup(root: string): void {
|
|
56
|
-
rmSync(path.dirname(root), { recursive: true, force: true });
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
async function run(root: string, store: string, base?: string): Promise<RunReport> {
|
|
60
|
-
return runAdapter({
|
|
61
|
-
root,
|
|
62
|
-
store,
|
|
63
|
-
all: false,
|
|
64
|
-
timeoutMs: 120_000,
|
|
65
|
-
...(base !== undefined ? { base } : {}),
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
test("discovers each tsconfig.json as a project, skipping node_modules", () => {
|
|
70
|
-
const { root } = makeRepo();
|
|
71
|
-
try {
|
|
72
|
-
write(root, "node_modules/dep/tsconfig.json", "{}");
|
|
73
|
-
assert.deepEqual(discoverProjects(root), ["pkg/a/tsconfig.json", "pkg/b/tsconfig.json"]);
|
|
74
|
-
} finally {
|
|
75
|
-
cleanup(root);
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
test("program files are the project's sources, repo-relative after the adapter", () => {
|
|
80
|
-
const { root } = makeRepo();
|
|
81
|
-
try {
|
|
82
|
-
const files = programFiles(path.join(root, "pkg/a/tsconfig.json"));
|
|
83
|
-
assert.ok(
|
|
84
|
-
files.some((f) => f === path.join(root, "pkg/a/src/index.ts")),
|
|
85
|
-
files.join("\n"),
|
|
86
|
-
);
|
|
87
|
-
assert.ok(
|
|
88
|
-
!files.some((f) => f.startsWith(path.join(root, "pkg/b"))),
|
|
89
|
-
"project a must not list project b",
|
|
90
|
-
);
|
|
91
|
-
} finally {
|
|
92
|
-
cleanup(root);
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
test("first run checks every project and advances the baseline", async () => {
|
|
97
|
-
const { root, store } = makeRepo();
|
|
98
|
-
try {
|
|
99
|
-
const r = await run(root, store);
|
|
100
|
-
assert.deepEqual(r.selected, ["pkg/a/tsconfig.json", "pkg/b/tsconfig.json"]);
|
|
101
|
-
assert.deepEqual(
|
|
102
|
-
r.results.map((x) => x.outcome),
|
|
103
|
-
["pass", "pass"],
|
|
104
|
-
);
|
|
105
|
-
assert.equal(r.advanced, true);
|
|
106
|
-
assert.equal(r.exit, 0);
|
|
107
|
-
} finally {
|
|
108
|
-
cleanup(root);
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
test("a change to one project selects only that project", async () => {
|
|
113
|
-
const { root, store } = makeRepo();
|
|
114
|
-
try {
|
|
115
|
-
await run(root, store);
|
|
116
|
-
write(root, "pkg/a/src/index.ts", "export const a: number = 2;\n");
|
|
117
|
-
commit(root, "edit a");
|
|
118
|
-
const r = await run(root, store);
|
|
119
|
-
assert.deepEqual(r.selected, ["pkg/a/tsconfig.json"]);
|
|
120
|
-
assert.equal(r.advanced, true);
|
|
121
|
-
assert.equal(r.exit, 0);
|
|
122
|
-
} finally {
|
|
123
|
-
cleanup(root);
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
test("a type error fails its project, exits 1 and keeps the project invalid", async () => {
|
|
128
|
-
const { root, store } = makeRepo();
|
|
129
|
-
try {
|
|
130
|
-
await run(root, store);
|
|
131
|
-
write(root, "pkg/b/src/index.ts", 'export const b: number = "not a number";\n');
|
|
132
|
-
commit(root, "break b");
|
|
133
|
-
const r = await run(root, store);
|
|
134
|
-
const b = r.results.find((x) => x.task === "pkg/b/tsconfig.json");
|
|
135
|
-
assert.equal(b?.outcome, "fail");
|
|
136
|
-
assert.equal(r.advanced, true, "a failed check is a complete run, so the baseline advances");
|
|
137
|
-
assert.equal(r.exit, 1);
|
|
138
|
-
|
|
139
|
-
// A docs-only edit is not an input of either project. The adapter declares
|
|
140
|
-
// probes and dirs false (tsc probes during module resolution and the adapter
|
|
141
|
-
// does not record those), so P7 falls back to running every project. That is
|
|
142
|
-
// the sound answer, and it is the cost of this capability set.
|
|
143
|
-
write(root, "README.md", "fixture, edited\n");
|
|
144
|
-
commit(root, "docs only");
|
|
145
|
-
const next = await run(root, store);
|
|
146
|
-
assert.equal(next.fallback?.reason, "unobserved_input");
|
|
147
|
-
assert.deepEqual(next.selected, ["pkg/a/tsconfig.json", "pkg/b/tsconfig.json"]);
|
|
148
|
-
assert.equal(next.exit, 1, "the failed project still fails");
|
|
149
|
-
} finally {
|
|
150
|
-
cleanup(root);
|
|
151
|
-
}
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
test("an uncommitted change leaves the baseline in place and exits 7", async () => {
|
|
155
|
-
const { root, store } = makeRepo();
|
|
156
|
-
try {
|
|
157
|
-
await run(root, store);
|
|
158
|
-
write(root, "pkg/a/src/index.ts", "export const a: number = 3;\n");
|
|
159
|
-
const r = await run(root, store);
|
|
160
|
-
assert.equal(r.advanced, false);
|
|
161
|
-
assert.equal(r.exit, 7);
|
|
162
|
-
assert.ok(
|
|
163
|
-
r.notAdvanced.some((m) => m.includes("dirty")),
|
|
164
|
-
r.notAdvanced.join("; "),
|
|
165
|
-
);
|
|
166
|
-
} finally {
|
|
167
|
-
cleanup(root);
|
|
168
|
-
}
|
|
169
|
-
});
|