wachiman 1.15.0 → 1.16.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.
Files changed (54) hide show
  1. package/README.md +1 -0
  2. package/package.json +1 -1
  3. package/src/accounts/commands.ts +3 -2
  4. package/src/accounts/confirm.ts +1 -1
  5. package/src/accounts/profile-list.ts +24 -0
  6. package/src/cli.ts +54 -20
  7. package/src/config.ts +19 -6
  8. package/src/connectors/local-git.ts +47 -0
  9. package/src/doctor/checks.ts +106 -1
  10. package/src/doctor/run.ts +9 -1
  11. package/src/engine.ts +280 -97
  12. package/src/help.ts +85 -3
  13. package/src/init/args.ts +5 -2
  14. package/src/init/github-login.ts +13 -0
  15. package/src/init/options.ts +31 -3
  16. package/src/init/prompt.ts +46 -15
  17. package/src/init/run.ts +204 -24
  18. package/src/init/settings.ts +127 -0
  19. package/src/init/write.ts +16 -102
  20. package/src/local-ask.ts +5 -4
  21. package/src/local.ts +55 -10
  22. package/src/loop/command.ts +5 -5
  23. package/src/pr-template/adopt.ts +434 -0
  24. package/src/pr-template/catalog.ts +429 -0
  25. package/src/pr-template/check-run.ts +328 -0
  26. package/src/pr-template/check.ts +338 -0
  27. package/src/pr-template/describe.ts +368 -0
  28. package/src/pr-template/format.ts +399 -0
  29. package/src/pr-template/gh.ts +159 -0
  30. package/src/pr-template/install.ts +288 -0
  31. package/src/pr-template/legacy-guides.ts +50 -0
  32. package/src/pr-template/pr-risk.ts +426 -0
  33. package/src/pr-template/render.ts +302 -0
  34. package/src/pr-template/resolve.ts +65 -0
  35. package/src/pr-template/run.ts +88 -0
  36. package/src/pr-template/strip.ts +171 -0
  37. package/src/pr-template/write.ts +139 -0
  38. package/src/providers/claude-code.ts +7 -8
  39. package/src/providers/codex.ts +4 -4
  40. package/src/providers/spawn.ts +95 -0
  41. package/src/providers/types.ts +5 -2
  42. package/src/repo-settings.ts +159 -0
  43. package/src/setup/prompt.ts +49 -33
  44. package/src/setup/run.ts +31 -6
  45. package/src/sinks/common.ts +10 -3
  46. package/src/sinks/markdown.ts +3 -2
  47. package/src/sinks/terminal.ts +3 -2
  48. package/src/upgrade/command.ts +286 -0
  49. package/src/upgrade/pin.ts +112 -0
  50. package/src/wizard/mascot.ts +56 -51
  51. package/src/wizard/prompt-fancy.ts +10 -2
  52. package/src/wizard/prompt.ts +17 -4
  53. package/src/wizard/ui.ts +2 -0
  54. package/templates/pr/sections.json +163 -0
@@ -0,0 +1,368 @@
1
+ /**
2
+ * Reads a PR description into its parts: the status table rows, the `##` sections, the `<details>`
3
+ * blocks, the Wachiman block, the `<sub>` footer and every HTML comment. Pure string work, no
4
+ * template knowledge: `check` judges these parts against a template and `format` moves them.
5
+ *
6
+ * Positions are kept so nothing is lost on the way back out. Lines are 0-based and every range is
7
+ * `[startLine, endLine)` unless a field says it is inclusive. `lines` is the text split on `\n`
8
+ * (so a trailing newline leaves an empty last element) and `lines.join(eol)` is the input again.
9
+ * `clean` and `structural` are the same length as the source: a comment becomes spaces and a
10
+ * newline stays a newline, so an offset or line number in one is valid in the others.
11
+ */
12
+ import { BLOCK_END, BLOCK_START } from "../fidelity/digest";
13
+
14
+ export interface DescriptionComment {
15
+ /** The comment as written, `<!-- ... -->`. */
16
+ raw: string;
17
+ /** What is inside, trimmed. */
18
+ text: string;
19
+ /** Offsets in `source`: `start` is the `<`, `end` is just past the `>`. */
20
+ start: number;
21
+ end: number;
22
+ /** First and last line the comment touches (both inclusive). */
23
+ startLine: number;
24
+ endLine: number;
25
+ /** Nothing but blanks shares its first or last line with it. */
26
+ wholeLine: boolean;
27
+ /** Set for the two Wachiman block markers. */
28
+ marker: "start" | "end" | null;
29
+ /** Between the markers of a whole Wachiman block, markers excluded. */
30
+ inBlock: boolean;
31
+ }
32
+
33
+ export interface DescriptionHeading {
34
+ /** The heading text, without the `##` and any closing hashes. */
35
+ name: string;
36
+ line: number;
37
+ raw: string;
38
+ }
39
+
40
+ export interface DescriptionStatusRow {
41
+ label: string;
42
+ /** The cell after the label, comments removed, trimmed. */
43
+ value: string;
44
+ line: number;
45
+ raw: string;
46
+ }
47
+
48
+ export interface DescriptionSection {
49
+ name: string;
50
+ /** The line of its `##` heading. */
51
+ startLine: number;
52
+ /** Where the next `##` heading starts (or the end of the text): the span the earlier checker read. */
53
+ endLine: number;
54
+ /** Where the section's own text stops: the first `<details>`, Wachiman block or footer inside the span. */
55
+ bodyEndLine: number;
56
+ /** The span under the heading, comments removed and the Wachiman block blanked. */
57
+ clean: string;
58
+ /** The span under the heading as written. */
59
+ raw: string;
60
+ }
61
+
62
+ export interface DescriptionDetails {
63
+ /** The text of the bold title in the summary, or null when the summary has none. */
64
+ name: string | null;
65
+ /** Everything between `<summary>` and `</summary>`, comments removed. */
66
+ summary: string;
67
+ /** The line of `<details>` and the line of `</details>` (inclusive). */
68
+ startLine: number;
69
+ endLine: number;
70
+ /** What follows `</summary>`, comments removed. */
71
+ clean: string;
72
+ raw: string;
73
+ }
74
+
75
+ export interface DescriptionSub {
76
+ /** The text between `<sub>` and `</sub>`. */
77
+ text: string;
78
+ startLine: number;
79
+ endLine: number;
80
+ }
81
+
82
+ export interface DescriptionBlock {
83
+ /** Offsets of the start marker's `<` and just past the end marker's `>`. */
84
+ start: number;
85
+ end: number;
86
+ startLine: number;
87
+ /** The line of the end marker (inclusive). */
88
+ endLine: number;
89
+ raw: string;
90
+ }
91
+
92
+ export type SegmentKind = "preface" | "section" | "details" | "wachiman" | "footer" | "text";
93
+ export interface DescriptionSegment {
94
+ kind: SegmentKind;
95
+ startLine: number;
96
+ /** Exclusive. */
97
+ endLine: number;
98
+ }
99
+
100
+ export interface ParsedDescription {
101
+ /** The input with `\r\n` and `\r` turned into `\n`; every offset refers to it. */
102
+ source: string;
103
+ /** The line ending to put back when the text is written out again. */
104
+ eol: "\n" | "\r\n";
105
+ lines: string[];
106
+ /** The offset in `source` where each line starts. */
107
+ lineStarts: number[];
108
+ /** `source` with comments blanked. */
109
+ clean: string;
110
+ /** `clean` with the inside of every Wachiman block blanked too: what the template rules read. */
111
+ structural: string;
112
+ comments: DescriptionComment[];
113
+ /** Whole blocks only; a marker with no partner is just a comment. */
114
+ blocks: DescriptionBlock[];
115
+ /** Every `##` heading outside fenced code and outside the Wachiman block. */
116
+ headings: DescriptionHeading[];
117
+ /** The lines before the first `##` heading: where the status table lives. */
118
+ headerEndLine: number;
119
+ /** Every table row of the form `| **Label** | value |` before the first heading, in order. */
120
+ statusRows: DescriptionStatusRow[];
121
+ sections: DescriptionSection[];
122
+ details: DescriptionDetails[];
123
+ subs: DescriptionSub[];
124
+ /** The lines split into runs, in order, each line in exactly one: the preface, sections, details blocks, the Wachiman block, footers and loose text. */
125
+ segments: DescriptionSegment[];
126
+ }
127
+
128
+ const COMMENT = /<!--[\s\S]*?-->/g;
129
+ const HEADING = /^ {0,3}##(?!#)\s+(.*?)\s*$/;
130
+ const STATUS_ROW = /^\s*\|\s*\*\*([^*|]+?)\*\*\s*\|(.*?)\|?\s*$/;
131
+ /** The opening tag may carry attributes (`<details open>`); the summary rules are unchanged. */
132
+ const DETAILS = /<details(?:\s[^>]*)?>\s*<summary>([\s\S]*?)<\/summary>([\s\S]*?)<\/details>/dg;
133
+ const SUB = /<sub>([\s\S]*?)<\/sub>/g;
134
+ const FENCE_OPEN = /^(```+|~~~+)/;
135
+
136
+ const markerName = (marker: string) => /^<!--\s*(.*?)\s*-->$/.exec(marker)![1]!;
137
+ const START_TEXT = markerName(BLOCK_START);
138
+ const END_TEXT = markerName(BLOCK_END);
139
+
140
+ /** Blank a set of spans: every character but a newline becomes a space. */
141
+ function blank(text: string, spans: { start: number; end: number }[]): string {
142
+ const sorted = [...spans].sort((a, b) => a.start - b.start);
143
+ let out = "";
144
+ let at = 0;
145
+ for (const s of sorted) {
146
+ if (s.end <= at) continue;
147
+ const from = Math.max(s.start, at);
148
+ out += text.slice(at, from) + text.slice(from, s.end).replace(/[^\n]/g, " ");
149
+ at = s.end;
150
+ }
151
+ return out + text.slice(at);
152
+ }
153
+
154
+ export function parseDescription(input: string): ParsedDescription {
155
+ const eol = input.includes("\r\n") ? "\r\n" : "\n";
156
+ const source = input.replace(/\r\n?/g, "\n");
157
+ const lines = source.split("\n");
158
+ const lineStarts: number[] = [];
159
+ {
160
+ let at = 0;
161
+ for (const line of lines) {
162
+ lineStarts.push(at);
163
+ at += line.length + 1;
164
+ }
165
+ }
166
+ const lineOf = (offset: number) => {
167
+ let lo = 0;
168
+ let hi = lineStarts.length - 1;
169
+ while (lo < hi) {
170
+ const mid = (lo + hi + 1) >> 1;
171
+ if (lineStarts[mid]! <= offset) lo = mid;
172
+ else hi = mid - 1;
173
+ }
174
+ return lo;
175
+ };
176
+ /** Lines of a span that ends just past `end`. */
177
+ const lastLineOf = (end: number) => lineOf(Math.max(end - 1, 0));
178
+
179
+ // comments
180
+ const comments: DescriptionComment[] = [];
181
+ for (const m of source.matchAll(COMMENT)) {
182
+ const start = m.index!;
183
+ const end = start + m[0].length;
184
+ const startLine = lineOf(start);
185
+ const endLine = lastLineOf(end);
186
+ const before = source.slice(lineStarts[startLine], start);
187
+ const after = source.slice(end, lineStarts[endLine]! + lines[endLine]!.length);
188
+ const text = m[0].slice(4, -3).trim();
189
+ comments.push({
190
+ raw: m[0],
191
+ text,
192
+ start,
193
+ end,
194
+ startLine,
195
+ endLine,
196
+ wholeLine: before.trim() === "" && after.trim() === "",
197
+ marker: text === START_TEXT ? "start" : text === END_TEXT ? "end" : null,
198
+ inBlock: false,
199
+ });
200
+ }
201
+
202
+ // Wachiman blocks: an end marker and the nearest start marker before it
203
+ const blocks: DescriptionBlock[] = [];
204
+ {
205
+ let open: DescriptionComment | null = null;
206
+ for (const c of comments) {
207
+ if (c.marker === "start") open = c;
208
+ else if (c.marker === "end" && open) {
209
+ blocks.push({
210
+ start: open.start,
211
+ end: c.end,
212
+ startLine: open.startLine,
213
+ endLine: c.endLine,
214
+ raw: source.slice(open.start, c.end),
215
+ });
216
+ open = null;
217
+ }
218
+ }
219
+ }
220
+ for (const c of comments) {
221
+ if (c.marker === null && blocks.some((b) => c.start > b.start && c.end < b.end)) c.inBlock = true;
222
+ }
223
+
224
+ const clean = blank(source, comments);
225
+ const structural = blank(source, [...comments, ...blocks]);
226
+ const structuralLines = structural.split("\n");
227
+
228
+ // Fenced code is never structure: a heading, `<details>` or `<sub>` written inside a fence is text.
229
+ const fenced: boolean[] = [];
230
+ {
231
+ let fence: { ch: string; len: number } | null = null;
232
+ structuralLines.forEach((line, i) => {
233
+ const s = line.trim();
234
+ if (fence !== null) {
235
+ fenced[i] = true;
236
+ // CommonMark: a fence closes on the same character, at least as many times as the opener, nothing else on the line.
237
+ if (s[0] === fence.ch && s.length >= fence.len && s === fence.ch.repeat(s.length)) fence = null;
238
+ return;
239
+ }
240
+ const open = FENCE_OPEN.exec(s);
241
+ if (open) {
242
+ fence = { ch: open[1]![0]!, len: open[1]!.length };
243
+ fenced[i] = true;
244
+ return;
245
+ }
246
+ fenced[i] = false;
247
+ });
248
+ }
249
+ /** `structural` with the fenced lines blanked: what `<details>` and `<sub>` are looked for in. Same length, same offsets. */
250
+ const outsideFences = structuralLines.map((line, i) => (fenced[i] ? line.replace(/[^\n]/g, " ") : line)).join("\n");
251
+
252
+ const details: DescriptionDetails[] = [];
253
+ for (const m of outsideFences.matchAll(DETAILS)) {
254
+ const start = m.index!;
255
+ const end = start + m[0].length;
256
+ const [sumFrom, sumTo] = m.indices![1]!;
257
+ const [bodyFrom, bodyTo] = m.indices![2]!;
258
+ const summary = structural.slice(sumFrom, sumTo);
259
+ const bold = /^\s*<b>([\s\S]*?)<\/b>/.exec(summary);
260
+ details.push({
261
+ name: bold ? bold[1]!.trim() : null,
262
+ summary,
263
+ startLine: lineOf(start),
264
+ endLine: lastLineOf(end),
265
+ clean: structural.slice(bodyFrom, bodyTo),
266
+ raw: source.slice(start, end),
267
+ });
268
+ }
269
+
270
+ // headings outside fenced code and outside every details block: a heading inside a block is the block's text
271
+ const headings: DescriptionHeading[] = [];
272
+ structuralLines.forEach((line, i) => {
273
+ if (fenced[i] || details.some((d) => i > d.startLine && i <= d.endLine)) return;
274
+ const h = HEADING.exec(line);
275
+ if (h) headings.push({ name: h[1]!.replace(/(?:^|\s+)#+$/, "").trim(), line: i, raw: lines[i]! });
276
+ });
277
+ const headerEndLine = headings[0]?.line ?? lines.length;
278
+
279
+ const statusRows: DescriptionStatusRow[] = [];
280
+ for (let i = 0; i < headerEndLine; i++) {
281
+ if (fenced[i]) continue; // a row inside fenced code is text, not a row
282
+ const m = STATUS_ROW.exec(structuralLines[i]!);
283
+ if (m) statusRows.push({ label: m[1]!.trim(), value: m[2]!.trim(), line: i, raw: lines[i]! });
284
+ }
285
+
286
+ const subs: DescriptionSub[] = [];
287
+ for (const m of outsideFences.matchAll(SUB)) {
288
+ const start = m.index!;
289
+ subs.push({ text: m[1]!, startLine: lineOf(start), endLine: lastLineOf(start + m[0].length) });
290
+ }
291
+
292
+ // Sections: the span to the next heading, plus where their own text stops
293
+ const sections: DescriptionSection[] = headings.map((h, n) => {
294
+ const endLine = headings[n + 1]?.line ?? lines.length;
295
+ const inner = [
296
+ ...details.filter((d) => d.startLine > h.line && d.startLine < endLine).map((d) => d.startLine),
297
+ ...blocks.filter((b) => b.startLine > h.line && b.startLine < endLine).map((b) => b.startLine),
298
+ ...subs.filter((s) => s.startLine > h.line && s.startLine < endLine).map((s) => s.startLine),
299
+ ];
300
+ return {
301
+ name: h.name,
302
+ startLine: h.line,
303
+ endLine,
304
+ bodyEndLine: Math.min(endLine, ...inner),
305
+ clean: structuralLines.slice(h.line + 1, endLine).join("\n"),
306
+ raw: lines.slice(h.line + 1, endLine).join("\n"),
307
+ };
308
+ });
309
+
310
+ const segments = buildSegments(lines.length, headings, details, blocks, subs);
311
+
312
+ return { source, eol, lines, lineStarts, clean, structural, comments, blocks, headings, headerEndLine, statusRows, sections, details, subs, segments };
313
+ }
314
+
315
+ /** Walk the lines once: details, blocks and footers are whole runs; a heading starts a section; the rest is text. */
316
+ function buildSegments(
317
+ total: number,
318
+ headings: DescriptionHeading[],
319
+ details: DescriptionDetails[],
320
+ blocks: DescriptionBlock[],
321
+ subs: DescriptionSub[],
322
+ ): DescriptionSegment[] {
323
+ interface Event { line: number; endLine: number; kind: SegmentKind }
324
+ const atoms: Event[] = [
325
+ ...details.map((d) => ({ line: d.startLine, endLine: d.endLine + 1, kind: "details" as const })),
326
+ ...blocks.map((b) => ({ line: b.startLine, endLine: b.endLine + 1, kind: "wachiman" as const })),
327
+ ...subs.map((s) => ({ line: s.startLine, endLine: s.endLine + 1, kind: "footer" as const })),
328
+ ].sort((a, b) => a.line - b.line || b.endLine - a.endLine);
329
+ const events: Event[] = [...atoms, ...headings.map((h) => ({ line: h.line, endLine: h.line, kind: "section" as const }))].sort(
330
+ (a, b) => a.line - b.line || (a.kind === "section" ? 1 : 0) - (b.kind === "section" ? 1 : 0),
331
+ );
332
+
333
+ const out: DescriptionSegment[] = [];
334
+ let current: { kind: SegmentKind; start: number } = { kind: "preface", start: 0 };
335
+ let cursor = 0;
336
+ const close = (at: number) => {
337
+ if (at > current.start) out.push({ kind: current.kind, startLine: current.start, endLine: at });
338
+ };
339
+ for (const ev of events) {
340
+ if (ev.line < cursor) continue;
341
+ close(ev.line);
342
+ if (ev.kind === "section") {
343
+ current = { kind: "section", start: ev.line };
344
+ cursor = ev.line + 1;
345
+ } else {
346
+ out.push({ kind: ev.kind, startLine: ev.line, endLine: Math.min(ev.endLine, total) });
347
+ cursor = ev.endLine;
348
+ current = { kind: "text", start: Math.min(ev.endLine, total) };
349
+ }
350
+ }
351
+ close(total);
352
+ return out;
353
+ }
354
+
355
+ /** The first row of the status table with this label, or undefined. */
356
+ export function statusRow(parsed: ParsedDescription, label: string): DescriptionStatusRow | undefined {
357
+ return parsed.statusRows.find((r) => r.label === label);
358
+ }
359
+
360
+ /** The first section with this heading, or undefined. */
361
+ export function sectionNamed(parsed: ParsedDescription, name: string): DescriptionSection | undefined {
362
+ return parsed.sections.find((s) => s.name === name);
363
+ }
364
+
365
+ /** The lines `[from, to)` as written, joined with the description's own line ending. */
366
+ export function linesText(parsed: ParsedDescription, from: number, to: number): string {
367
+ return parsed.lines.slice(from, to).join(parsed.eol);
368
+ }