-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd-apply.ts
More file actions
638 lines (604 loc) · 16.2 KB
/
Copy pathcmd-apply.ts
File metadata and controls
638 lines (604 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
import { readFileSync } from "node:fs";
import { createInterface } from "node:readline/promises";
import type { ApplyJsonPayload } from "../application/apply-engine";
import {
ApplyRunError,
gitCommitAfterApplyIfEligible,
runApplyFromDiffText,
runApplyFromRecipe,
runApplyFromRows,
runApplyUntilEmpty,
} from "../application/apply-run";
import {
getQueryRecipeParams,
getQueryRecipeSql,
listQueryRecipeIds,
} from "../application/query-recipes";
import {
mergeParams,
parseParamsCli,
resolveRecipeParams,
} from "../application/recipe-params";
import type { RecipeParamValues } from "../application/recipe-params";
import { getProjectRoot } from "../runtime";
import { bootstrapCodemap } from "./bootstrap-codemap";
interface ApplyOpts {
root: string;
configFile: string | undefined;
stateDir?: string | undefined;
recipeId?: string;
params: RecipeParamValues | undefined;
dryRun: boolean;
yes: boolean;
force: boolean;
json: boolean;
rowsPath?: string;
diffInputPath?: string;
untilEmpty: boolean;
maxPasses: number;
commitMessage?: string;
}
/** Print `codemap apply` usage. */
export function printApplyCmdHelp(): void {
console.log(`Usage:
codemap apply <recipe-id> [--params k=v[,k=v]] [--dry-run] [--yes] [--force] [--json]
codemap apply --rows -|<file.json> [--dry-run] [--yes] [--json]
codemap apply --diff-input <file> [--dry-run] [--yes] [--json]
Apply diff hunks ({file_path, line_start, before_pattern, after_pattern}) to disk.
Flags:
--params k=v[,k=v] Parametrised recipes (recipe mode only).
--rows -|<path> JSON array of apply rows (stdin when -).
--diff-input <file> Unified diff → row contract.
--dry-run Phase-1 validate only.
--yes Skip TTY confirmation (required for non-TTY).
--force Bypass auto_fixable and apply.autoApplyRecipes gates.
--until-empty Fixpoint loop (recipe mode): apply → reindex → repeat.
--max-passes N Cap for --until-empty (default 10).
--commit "<msg>" git add touched files + commit after clean apply.
--json Structured envelope on stdout.
--help, -h This help.
Exit codes: 0 clean; 1 conflicts or error.
`);
}
/** Parse argv after bootstrap split. `rest[0]` must be `"apply"`. */
export function parseApplyRest(rest: string[]):
| { kind: "help" }
| { kind: "error"; message: string }
| {
kind: "run";
recipeId?: string;
params: RecipeParamValues | undefined;
dryRun: boolean;
yes: boolean;
force: boolean;
json: boolean;
rowsPath?: string;
diffInputPath?: string;
untilEmpty: boolean;
maxPasses: number;
commitMessage?: string;
} {
if (rest[0] !== "apply") {
throw new Error("parseApplyRest: expected apply");
}
let recipeId: string | undefined;
let params: RecipeParamValues | undefined;
let dryRun = false;
let yes = false;
let force = false;
let json = false;
let rowsPath: string | undefined;
let diffInputPath: string | undefined;
let untilEmpty = false;
let maxPasses = 10;
let commitMessage: string | undefined;
for (let i = 1; i < rest.length; i++) {
const a = rest[i]!;
if (a === "--help" || a === "-h") return { kind: "help" };
if (a === "--json") {
json = true;
continue;
}
if (a === "--dry-run") {
dryRun = true;
continue;
}
if (a === "--yes") {
yes = true;
continue;
}
if (a === "--force") {
force = true;
continue;
}
if (a === "--until-empty") {
untilEmpty = true;
continue;
}
if (a === "--rows") {
const next = rest[i + 1];
if (next === undefined) {
return {
kind: "error",
message: `codemap apply: "--rows" requires - or a file path.`,
};
}
rowsPath = next;
i++;
continue;
}
if (a === "--diff-input") {
const next = rest[i + 1];
if (next === undefined) {
return {
kind: "error",
message: `codemap apply: "--diff-input" requires a file path.`,
};
}
diffInputPath = next;
i++;
continue;
}
if (a === "--max-passes") {
const next = rest[i + 1];
if (next !== undefined && next.startsWith("-")) {
continue;
}
if (next === undefined || !/^\d+$/.test(next)) {
return {
kind: "error",
message: `codemap apply: "--max-passes" requires a positive integer.`,
};
}
maxPasses = Number.parseInt(next, 10);
if (maxPasses < 1) {
return {
kind: "error",
message: `codemap apply: "--max-passes" requires a positive integer.`,
};
}
i++;
continue;
}
if (a === "--commit") {
const next = rest[i + 1];
if (next === undefined) {
return {
kind: "error",
message: `codemap apply: "--commit" requires a message string.`,
};
}
if (!next.startsWith("-")) {
commitMessage = next;
i++;
}
continue;
}
if (a === "--params") {
const next = rest[i + 1];
if (next === undefined) {
return {
kind: "error",
message: `codemap apply: "--params" requires a value (k=v[,k=v]).`,
};
}
params = mergeParams(params, parseParamsCli(next));
i++;
continue;
}
if (a.startsWith("-")) {
return {
kind: "error",
message: `codemap apply: unknown option "${a}". Run \`codemap apply --help\` for usage.`,
};
}
if (recipeId !== undefined) {
return {
kind: "error",
message: `codemap apply: unexpected extra argument "${a}".`,
};
}
recipeId = a;
}
const modeCount =
(recipeId !== undefined ? 1 : 0) +
(rowsPath !== undefined ? 1 : 0) +
(diffInputPath !== undefined ? 1 : 0);
if (modeCount === 0) {
return {
kind: "error",
message: `codemap apply: pass <recipe-id>, --rows, or --diff-input. Run \`codemap apply --help\`.`,
};
}
if (modeCount > 1) {
return {
kind: "error",
message: `codemap apply: choose one of <recipe-id>, --rows, or --diff-input.`,
};
}
if (params !== undefined && recipeId === undefined) {
return {
kind: "error",
message: `codemap apply: --params can only be used with <recipe-id>.`,
};
}
if (untilEmpty && recipeId === undefined) {
return {
kind: "error",
message: `codemap apply: --until-empty requires a <recipe-id>.`,
};
}
if (dryRun && yes) {
return {
kind: "error",
message: `codemap apply: --dry-run and --yes are mutually exclusive.`,
};
}
return {
kind: "run",
recipeId,
params,
dryRun,
yes,
force,
json,
rowsPath,
diffInputPath,
untilEmpty,
maxPasses,
commitMessage,
};
}
/**
* Run `codemap apply`. Sets `process.exitCode = 1` on failure (no `process.exit`).
*/
export async function runApplyCmd(opts: ApplyOpts): Promise<void> {
try {
const parsed = opts;
await bootstrapCodemap(opts);
const projectRoot = getProjectRoot();
if (parsed.recipeId !== undefined) {
await runRecipeApply({
...parsed,
recipeId: parsed.recipeId,
projectRoot,
});
return;
}
if (parsed.rowsPath !== undefined) {
await runRowsApply({
rowsPath: parsed.rowsPath,
dryRun: parsed.dryRun,
yes: parsed.yes,
json: parsed.json,
commitMessage: parsed.commitMessage,
projectRoot,
});
return;
}
if (parsed.diffInputPath !== undefined) {
await runDiffApply({
diffInputPath: parsed.diffInputPath,
dryRun: parsed.dryRun,
yes: parsed.yes,
json: parsed.json,
commitMessage: parsed.commitMessage,
projectRoot,
});
return;
}
} catch (err) {
const msg =
err instanceof ApplyRunError
? err.message
: err instanceof Error
? err.message
: String(err);
emitError(msg, opts.json);
}
}
async function runRecipeApply(opts: {
recipeId: string;
params: RecipeParamValues | undefined;
dryRun: boolean;
yes: boolean;
force: boolean;
json: boolean;
untilEmpty: boolean;
maxPasses: number;
commitMessage?: string;
projectRoot: string;
}): Promise<void> {
if (getQueryRecipeSql(opts.recipeId) === undefined) {
const known = listQueryRecipeIds().join(", ");
emitError(
`codemap apply: unknown recipe "${opts.recipeId}". Known: ${known}.`,
opts.json,
);
return;
}
const resolved = resolveRecipeParams({
recipeId: opts.recipeId,
declared: getQueryRecipeParams(opts.recipeId),
provided: opts.params,
});
if (!resolved.ok) {
emitError(resolved.error, opts.json);
return;
}
const canPrompt =
process.stdin.isTTY === true && process.stderr.isTTY === true;
if (!canPrompt && !opts.yes && !opts.dryRun) {
emitError(
`codemap apply: this verb writes files. Pass --yes for non-interactive runs, or --dry-run for preview.`,
opts.json,
);
return;
}
if (opts.untilEmpty) {
let loopYes = opts.yes;
if (!opts.dryRun && !loopYes && canPrompt) {
const preview = runApplyFromRecipe({
projectRoot: opts.projectRoot,
recipeId: opts.recipeId,
params: opts.params,
dryRun: true,
force: opts.force,
yes: false,
}).payload;
if (preview.conflicts.length > 0 || preview.summary.rows === 0) {
emitResult(preview, opts);
return;
}
printPromptSummary(preview, opts.recipeId);
const proceed = await promptYesNo();
if (!proceed) {
if (opts.json) {
emitResult(preview, opts);
} else {
console.log(
`apply ${opts.recipeId}: aborted by user; no files written.`,
);
}
return;
}
loopYes = true;
}
const loopResult = await runApplyUntilEmpty({
projectRoot: opts.projectRoot,
recipeId: opts.recipeId,
params: opts.params,
dryRun: opts.dryRun,
force: opts.force,
yes: loopYes,
maxPasses: opts.maxPasses,
ttyConfirmed: loopYes && !opts.yes && canPrompt,
});
await finishApply(loopResult.payload, {
recipeId: opts.recipeId,
dryRun: opts.dryRun,
json: opts.json,
commitMessage: opts.commitMessage,
projectRoot: opts.projectRoot,
});
return;
}
if (opts.dryRun || opts.yes) {
const { payload } = runApplyFromRecipe({
projectRoot: opts.projectRoot,
recipeId: opts.recipeId,
params: opts.params,
dryRun: opts.dryRun,
force: opts.force,
yes: opts.yes,
});
await finishApply(payload, opts);
return;
}
const preview = runApplyFromRecipe({
projectRoot: opts.projectRoot,
recipeId: opts.recipeId,
params: opts.params,
dryRun: true,
force: opts.force,
yes: false,
}).payload;
if (preview.conflicts.length > 0 || preview.files.length === 0) {
emitResult(preview, opts);
return;
}
printPromptSummary(preview, opts.recipeId);
const proceed = await promptYesNo();
if (!proceed) {
if (opts.json) {
emitResult(preview, opts);
} else {
console.log(`apply ${opts.recipeId}: aborted by user; no files written.`);
}
return;
}
const { payload } = runApplyFromRecipe({
projectRoot: opts.projectRoot,
recipeId: opts.recipeId,
params: opts.params,
dryRun: false,
force: opts.force,
yes: true,
ttyConfirmed: true,
});
await finishApply(payload, opts);
}
async function runRowsApply(opts: {
rowsPath: string;
dryRun: boolean;
yes: boolean;
json: boolean;
commitMessage?: string;
projectRoot: string;
}): Promise<void> {
const text =
opts.rowsPath === "-"
? readFileSync(0, "utf8")
: readFileSync(opts.rowsPath, "utf8");
let rows: unknown;
try {
rows = JSON.parse(text) as unknown;
} catch {
emitError(`codemap apply: --rows input is not valid JSON.`, opts.json);
return;
}
if (!Array.isArray(rows)) {
emitError(`codemap apply: --rows JSON must be an array.`, opts.json);
return;
}
const canPrompt =
process.stdin.isTTY === true && process.stderr.isTTY === true;
if (!canPrompt && !opts.yes && !opts.dryRun) {
emitError(
`codemap apply: pass --yes for non-interactive --rows apply.`,
opts.json,
);
return;
}
const { payload } = runApplyFromRows({
projectRoot: opts.projectRoot,
rows: rows as Record<string, unknown>[],
dryRun: opts.dryRun,
});
await finishApply(payload, {
recipeId: "--rows",
dryRun: opts.dryRun,
json: opts.json,
commitMessage: opts.commitMessage,
projectRoot: opts.projectRoot,
});
}
async function runDiffApply(opts: {
diffInputPath: string;
dryRun: boolean;
yes: boolean;
json: boolean;
commitMessage?: string;
projectRoot: string;
}): Promise<void> {
const diffText = readFileSync(opts.diffInputPath, "utf8");
const canPrompt =
process.stdin.isTTY === true && process.stderr.isTTY === true;
if (!canPrompt && !opts.yes && !opts.dryRun) {
emitError(
`codemap apply: pass --yes for non-interactive --diff-input apply.`,
opts.json,
);
return;
}
const { payload } = runApplyFromDiffText({
projectRoot: opts.projectRoot,
diffText,
dryRun: opts.dryRun,
});
await finishApply(payload, {
recipeId: "--diff-input",
dryRun: opts.dryRun,
json: opts.json,
commitMessage: opts.commitMessage,
projectRoot: opts.projectRoot,
});
}
async function finishApply(
payload: ApplyJsonPayload,
opts: {
recipeId: string;
dryRun: boolean;
json: boolean;
commitMessage?: string;
projectRoot: string;
},
): Promise<void> {
if (opts.commitMessage !== undefined) {
const gitErr = gitCommitAfterApplyIfEligible({
projectRoot: opts.projectRoot,
message: opts.commitMessage,
payload,
});
if (gitErr !== undefined) {
emitError(gitErr, opts.json);
return;
}
}
emitResult(payload, opts);
}
function emitResult(
result: ApplyJsonPayload,
opts: { recipeId: string; dryRun: boolean; json: boolean },
): void {
if (opts.json) {
console.log(JSON.stringify(result));
} else {
renderTerminal(result, opts.recipeId, opts.dryRun);
}
if (result.conflicts.length > 0) {
process.exitCode = 1;
} else if (result.terminated_by === "cap") {
process.exitCode = 1;
}
}
function renderTerminal(
result: ApplyJsonPayload,
recipeId: string,
dryRun: boolean,
): void {
if (result.conflicts.length > 0) {
console.log(
`apply ${recipeId}: aborted (${result.summary.conflicts} conflicts in ${result.summary.files_with_conflicts} files); see --json for details`,
);
return;
}
if (result.terminated_by !== undefined) {
console.log(
`apply ${recipeId}: loop finished (${result.passes ?? "?"} passes, terminated_by=${result.terminated_by}).`,
);
}
if (dryRun) {
if (result.files.length === 0) {
console.log(`apply ${recipeId} --dry-run: no rows applicable.`);
return;
}
console.log(
`apply ${recipeId} --dry-run: would modify ${result.summary.files} files (${result.summary.rows} rows).`,
);
return;
}
if (!result.applied) {
console.log(`apply ${recipeId}: no rows applicable.`);
return;
}
console.log(
`apply ${recipeId}: modified ${result.summary.files_modified} files, applied ${result.summary.rows_applied} rows.`,
);
}
function printPromptSummary(preview: ApplyJsonPayload, recipeId: string): void {
console.error(
`apply ${recipeId}: ${preview.summary.files} files, ${preview.summary.rows} rows`,
);
for (const file of preview.files) {
console.error(` - ${file.file_path} (${file.rows_applied} rows)`);
}
console.error("");
}
async function promptYesNo(): Promise<boolean> {
const rl = createInterface({ input: process.stdin, output: process.stderr });
try {
const answer = await rl.question("Proceed? [y/N] ");
return /^y(es)?$/i.test(answer.trim());
} finally {
rl.close();
}
}
function emitError(message: string, json: boolean): void {
if (json) {
console.log(JSON.stringify({ error: message }));
} else {
console.error(message);
}
process.exitCode = 1;
}