@stupify/cli 0.0.16 → 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.
Files changed (74) hide show
  1. package/.review/CORPUS.md +73 -0
  2. package/.review/REVIEW-PROMPT.md +52 -0
  3. package/.review/RUBRIC.md +46 -0
  4. package/LICENSE +1 -1
  5. package/README.md +41 -39
  6. package/package.json +24 -25
  7. package/src/cli.ts +358 -0
  8. package/src/review-sweep.ts +492 -0
  9. package/dist/analysis.d.ts +0 -16
  10. package/dist/analysis.js +0 -168
  11. package/dist/cache.d.ts +0 -2
  12. package/dist/cache.js +0 -57
  13. package/dist/checks.d.ts +0 -4
  14. package/dist/checks.js +0 -228
  15. package/dist/command.d.ts +0 -2
  16. package/dist/command.js +0 -147
  17. package/dist/constants.d.ts +0 -4
  18. package/dist/constants.js +0 -53
  19. package/dist/counter-scout.d.ts +0 -21
  20. package/dist/counter-scout.js +0 -167
  21. package/dist/diff.d.ts +0 -1
  22. package/dist/diff.js +0 -10
  23. package/dist/doctor.d.ts +0 -16
  24. package/dist/doctor.js +0 -143
  25. package/dist/git.d.ts +0 -17
  26. package/dist/git.js +0 -368
  27. package/dist/hooks.d.ts +0 -5
  28. package/dist/hooks.js +0 -135
  29. package/dist/index.d.ts +0 -1
  30. package/dist/index.js +0 -1
  31. package/dist/model.d.ts +0 -11
  32. package/dist/model.js +0 -296
  33. package/dist/prompts.d.ts +0 -8
  34. package/dist/prompts.js +0 -89
  35. package/dist/render.d.ts +0 -6
  36. package/dist/render.js +0 -295
  37. package/dist/repomix-provider.d.ts +0 -12
  38. package/dist/repomix-provider.js +0 -196
  39. package/dist/search-bench.d.ts +0 -1
  40. package/dist/search-bench.js +0 -677
  41. package/dist/search-profile.d.ts +0 -6
  42. package/dist/search-profile.js +0 -73
  43. package/dist/sem-provider.d.ts +0 -2
  44. package/dist/sem-provider.js +0 -255
  45. package/dist/stupify.d.ts +0 -38
  46. package/dist/stupify.js +0 -505
  47. package/dist/trace.d.ts +0 -31
  48. package/dist/trace.js +0 -86
  49. package/dist/types.d.ts +0 -341
  50. package/dist/types.js +0 -6
  51. package/dist/ui.d.ts +0 -34
  52. package/dist/ui.js +0 -143
  53. package/src/analysis.ts +0 -223
  54. package/src/cache.ts +0 -63
  55. package/src/checks.ts +0 -231
  56. package/src/command.ts +0 -173
  57. package/src/constants.ts +0 -56
  58. package/src/counter-scout.ts +0 -195
  59. package/src/diff.ts +0 -9
  60. package/src/doctor.ts +0 -166
  61. package/src/git.ts +0 -380
  62. package/src/hooks.ts +0 -151
  63. package/src/index.ts +0 -1
  64. package/src/model.ts +0 -367
  65. package/src/prompts.ts +0 -100
  66. package/src/render.ts +0 -328
  67. package/src/repomix-provider.ts +0 -219
  68. package/src/search-bench.ts +0 -783
  69. package/src/search-profile.ts +0 -89
  70. package/src/sem-provider.ts +0 -300
  71. package/src/stupify.ts +0 -604
  72. package/src/trace.ts +0 -126
  73. package/src/types.ts +0 -362
  74. package/src/ui.ts +0 -187
package/src/git.ts DELETED
@@ -1,380 +0,0 @@
1
- import { execFile } from "node:child_process";
2
- import { promisify } from "node:util";
3
- import { sourceId, type BlameSummary, type NetDiff, type NetDiffStats, type SourceRange, type StagedDiff } from "./types.ts";
4
-
5
- const execFileAsync = promisify(execFile);
6
-
7
- export async function netDiffSince(since: string): Promise<NetDiff> {
8
- const range = await sourceRangeSince(since);
9
- return netDiff(range.base, range.target, range.label, range.id);
10
- }
11
-
12
- export async function netDiffForCommit(commit: string): Promise<NetDiff> {
13
- const range = await sourceRangeForCommit(commit);
14
- return netDiff(range.base, range.target, range.label, range.id);
15
- }
16
-
17
- export async function netDiffForRecentCommits(count: number): Promise<NetDiff> {
18
- const range = await sourceRangeForRecentCommits(count);
19
- return netDiff(range.base, range.target, range.label, range.id);
20
- }
21
-
22
- export async function sourceRangeSince(since: string): Promise<SourceRange> {
23
- const [base, target] = await Promise.all([baseBefore(since), revParse("HEAD")]);
24
- return sourceRange(base, target, `last ${since}`);
25
- }
26
-
27
- export async function sourceRangeForCommit(commit: string): Promise<SourceRange> {
28
- const [base, target, shortTarget, message] = await Promise.all([
29
- revParse(`${commit}^1`),
30
- revParse(commit),
31
- shortCommit(commit),
32
- commitMessage(commit),
33
- ]);
34
- return sourceRange(base, target, firstLine(message) || shortTarget, sourceId(shortTarget));
35
- }
36
-
37
- export async function sourceRangeForRecentCommits(count: number): Promise<SourceRange> {
38
- const commits = await recentCommits(count);
39
- if (commits.length === 0) throw new Error("No non-merge commits found.");
40
-
41
- const oldest = commits[0];
42
- const newest = commits[commits.length - 1];
43
- if (!oldest || !newest) throw new Error("Could not resolve recent commit range.");
44
- const [base, target, shortBase, shortTarget] = await Promise.all([
45
- revParse(`${oldest}^1`),
46
- revParse(newest),
47
- shortCommit(`${oldest}^1`),
48
- shortCommit(newest),
49
- ]);
50
-
51
- return sourceRange(base, target, `${commits.length} recent commits`, sourceId(`range:${shortBase}..${shortTarget}`));
52
- }
53
-
54
- export async function netDiffFromStdin(text: string): Promise<NetDiff> {
55
- if (!text.trim()) throw new Error("No diff received on stdin.");
56
- return {
57
- id: sourceId("stdin"),
58
- label: "stdin",
59
- base: "stdin",
60
- target: "stdin",
61
- text,
62
- stats: statsFromDiff(text),
63
- };
64
- }
65
-
66
- export async function stagedDiff(): Promise<StagedDiff> {
67
- try {
68
- const { stdout } = await execFileAsync("git", [
69
- "diff",
70
- "--cached",
71
- "--no-ext-diff",
72
- "--no-color",
73
- "--unified=3",
74
- "--",
75
- ], { maxBuffer: 64 * 1024 * 1024 });
76
- return { text: stdout, stats: statsFromDiff(stdout) };
77
- } catch {
78
- throw new Error("Could not read staged changes. Run stupify inside a git repository.");
79
- }
80
- }
81
-
82
- export async function gitRoot(): Promise<string> {
83
- try {
84
- const { stdout } = await execFileAsync("git", ["rev-parse", "--show-toplevel"]);
85
- return stdout.trim();
86
- } catch {
87
- throw new Error("Could not find a git repository.");
88
- }
89
- }
90
-
91
- export async function gitPath(pathspec: string): Promise<string> {
92
- try {
93
- const { stdout } = await execFileAsync("git", ["rev-parse", "--git-path", pathspec]);
94
- return stdout.trim();
95
- } catch {
96
- throw new Error(`Could not resolve git path: ${pathspec}`);
97
- }
98
- }
99
-
100
- export async function gitUserLabel(): Promise<string> {
101
- const [name, email] = await Promise.all([
102
- gitConfig("user.name"),
103
- gitConfig("user.email"),
104
- ]);
105
- if (name && email) return `${name} <${email}>`;
106
- return name || email || "working tree";
107
- }
108
-
109
- export async function blameEntity(input: Readonly<{
110
- filePath: string;
111
- entityName: string;
112
- rev: string;
113
- }>): Promise<BlameSummary | null> {
114
- try {
115
- const { stdout } = await execFileAsync("git", [
116
- "blame",
117
- "--line-porcelain",
118
- "-L",
119
- `:${input.entityName}`,
120
- input.rev,
121
- "--",
122
- input.filePath,
123
- ], { maxBuffer: 16 * 1024 * 1024 });
124
- return summarizeBlame(stdout);
125
- } catch {
126
- return null;
127
- }
128
- }
129
-
130
- async function netDiff(base: string, target: string, label: string, id?: NetDiff["id"]): Promise<NetDiff> {
131
- const [text, stats, shortBase, shortTarget] = await Promise.all([
132
- diff(base, target),
133
- diffStats(base, target),
134
- shortCommit(base),
135
- shortCommit(target),
136
- ]);
137
- return {
138
- id: id ?? sourceId(`net:${shortBase}..${shortTarget}`),
139
- label,
140
- base,
141
- target,
142
- text,
143
- stats,
144
- };
145
- }
146
-
147
- async function sourceRange(base: string, target: string, label: string, id?: SourceRange["id"]): Promise<SourceRange> {
148
- const [stats, shortBase, shortTarget, committers, commitSubjects] = await Promise.all([
149
- diffStats(base, target),
150
- shortCommit(base),
151
- shortCommit(target),
152
- committersForRange(base, target),
153
- commitSubjectsForRange(base, target),
154
- ]);
155
- return {
156
- id: id ?? sourceId(`net:${shortBase}..${shortTarget}`),
157
- label,
158
- base,
159
- target,
160
- committers,
161
- commitSubjects,
162
- stats,
163
- };
164
- }
165
-
166
- async function gitConfig(key: string): Promise<string> {
167
- try {
168
- const { stdout } = await execFileAsync("git", ["config", "--get", key]);
169
- return stdout.trim();
170
- } catch {
171
- return "";
172
- }
173
- }
174
-
175
- async function committersForRange(base: string, target: string): Promise<readonly string[]> {
176
- try {
177
- const { stdout } = await execFileAsync("git", ["log", "--format=%cn <%ce>", `${base}..${target}`], {
178
- maxBuffer: 4 * 1024 * 1024,
179
- });
180
- return uniqueLines(stdout);
181
- } catch {
182
- return [];
183
- }
184
- }
185
-
186
- async function commitSubjectsForRange(base: string, target: string): Promise<readonly string[]> {
187
- try {
188
- const { stdout } = await execFileAsync("git", ["log", "--format=%s", `${base}..${target}`], {
189
- maxBuffer: 4 * 1024 * 1024,
190
- });
191
- return uniqueLines(stdout);
192
- } catch {
193
- return [];
194
- }
195
- }
196
-
197
- function uniqueLines(value: string): readonly string[] {
198
- const seen = new Set<string>();
199
- const lines: string[] = [];
200
- for (const line of value.split(/\r?\n/)) {
201
- const trimmed = line.trim();
202
- if (!trimmed || seen.has(trimmed)) continue;
203
- seen.add(trimmed);
204
- lines.push(trimmed);
205
- }
206
- return lines;
207
- }
208
-
209
- async function baseBefore(since: string): Promise<string> {
210
- try {
211
- const { stdout } = await execFileAsync("git", [
212
- "log",
213
- "--first-parent",
214
- "--before",
215
- since,
216
- "-1",
217
- "--format=%H",
218
- ]);
219
- const commit = stdout.trim();
220
- if (commit) return commit;
221
- return rootCommit();
222
- } catch {
223
- throw new Error(`Could not resolve base commit before ${since}.`);
224
- }
225
- }
226
-
227
- async function rootCommit(): Promise<string> {
228
- try {
229
- const { stdout } = await execFileAsync("git", ["rev-list", "--max-parents=0", "HEAD"]);
230
- return stdout.trim().split(/\r?\n/, 1)[0] ?? "";
231
- } catch {
232
- throw new Error("Could not resolve repository root commit.");
233
- }
234
- }
235
-
236
- async function diff(base: string, target: string): Promise<string> {
237
- try {
238
- const { stdout } = await execFileAsync("git", [
239
- "diff",
240
- "--no-ext-diff",
241
- "--no-color",
242
- "--unified=8",
243
- base,
244
- target,
245
- "--",
246
- ], { maxBuffer: 128 * 1024 * 1024 });
247
- if (!stdout.trim()) throw new Error("empty diff");
248
- return stdout;
249
- } catch {
250
- throw new Error(`No diff found for ${base}..${target}.`);
251
- }
252
- }
253
-
254
- async function diffStats(base: string, target: string): Promise<NetDiffStats> {
255
- try {
256
- const { stdout } = await execFileAsync("git", ["diff", "--numstat", base, target, "--"], {
257
- maxBuffer: 16 * 1024 * 1024,
258
- });
259
- return statsFromNumstat(stdout);
260
- } catch {
261
- return { filesChanged: 0, additions: 0, deletions: 0 };
262
- }
263
- }
264
-
265
- function statsFromDiff(diffText: string): NetDiffStats {
266
- const files = new Set<string>();
267
- let additions = 0;
268
- let deletions = 0;
269
- for (const line of diffText.split(/\r?\n/)) {
270
- const fileMatch = /^diff --git a\/.+ b\/(.+)$/.exec(line);
271
- if (fileMatch?.[1]) files.add(fileMatch[1]);
272
- else if (line.startsWith("+") && !line.startsWith("+++")) additions += 1;
273
- else if (line.startsWith("-") && !line.startsWith("---")) deletions += 1;
274
- }
275
- return { filesChanged: files.size, additions, deletions };
276
- }
277
-
278
- function statsFromNumstat(numstat: string): NetDiffStats {
279
- let filesChanged = 0;
280
- let additions = 0;
281
- let deletions = 0;
282
-
283
- for (const line of numstat.split(/\r?\n/)) {
284
- if (!line.trim()) continue;
285
- const [added, deleted] = line.split(/\s+/, 3);
286
- filesChanged += 1;
287
- additions += Number(added) || 0;
288
- deletions += Number(deleted) || 0;
289
- }
290
-
291
- return { filesChanged, additions, deletions };
292
- }
293
-
294
- async function recentCommits(count: number): Promise<readonly string[]> {
295
- try {
296
- const { stdout } = await execFileAsync("git", [
297
- "log",
298
- "--first-parent",
299
- "--no-merges",
300
- "--format=%H",
301
- `-${count}`,
302
- ]);
303
- return stdout.split(/\r?\n/).filter(Boolean).reverse();
304
- } catch {
305
- throw new Error(`Could not read last ${count} commits.`);
306
- }
307
- }
308
-
309
- async function revParse(rev: string): Promise<string> {
310
- try {
311
- const { stdout } = await execFileAsync("git", ["rev-parse", rev]);
312
- return stdout.trim();
313
- } catch {
314
- throw new Error(`Could not resolve ${rev}.`);
315
- }
316
- }
317
-
318
- async function shortCommit(commit: string): Promise<string> {
319
- try {
320
- const { stdout } = await execFileAsync("git", ["rev-parse", "--short", commit]);
321
- return stdout.trim();
322
- } catch {
323
- throw new Error(`Could not resolve commit ${commit}.`);
324
- }
325
- }
326
-
327
- async function commitMessage(commit: string): Promise<string> {
328
- try {
329
- const { stdout } = await execFileAsync("git", ["show", "--no-patch", "--format=%B", commit], {
330
- maxBuffer: 1024 * 1024,
331
- });
332
- return stdout;
333
- } catch {
334
- throw new Error(`Could not read commit message for ${commit}.`);
335
- }
336
- }
337
-
338
- function firstLine(value: string): string {
339
- return value.trim().split(/\r?\n/, 1)[0]?.trim() ?? "";
340
- }
341
-
342
- function summarizeBlame(output: string): BlameSummary | null {
343
- const entries = new Map<string, { commit: string; author: string; subject: string; count: number }>();
344
- let currentCommit = "";
345
- let currentAuthor = "";
346
- let currentSubject = "";
347
- for (const line of output.split(/\r?\n/)) {
348
- const header = /^([0-9a-f]{40})\s+/.exec(line);
349
- if (header?.[1]) {
350
- currentCommit = header[1];
351
- currentAuthor = "";
352
- currentSubject = "";
353
- continue;
354
- }
355
- if (line.startsWith("author ")) {
356
- currentAuthor = line.slice("author ".length).trim();
357
- continue;
358
- }
359
- if (line.startsWith("summary ")) {
360
- currentSubject = line.slice("summary ".length).trim();
361
- continue;
362
- }
363
- if (!line.startsWith("\t") || !currentCommit) continue;
364
- const previous = entries.get(currentCommit);
365
- entries.set(currentCommit, {
366
- commit: currentCommit,
367
- author: currentAuthor || previous?.author || "unknown author",
368
- subject: currentSubject || previous?.subject || currentCommit.slice(0, 7),
369
- count: (previous?.count ?? 0) + 1,
370
- });
371
- }
372
-
373
- const [best] = [...entries.values()].sort((a, b) => b.count - a.count);
374
- if (!best) return null;
375
- return {
376
- commit: best.commit.slice(0, 7),
377
- author: best.author,
378
- subject: best.subject,
379
- };
380
- }
package/src/hooks.ts DELETED
@@ -1,151 +0,0 @@
1
- import { execFile } from "node:child_process";
2
- import { existsSync } from "node:fs";
3
- import { chmod, readFile, rm, writeFile } from "node:fs/promises";
4
- import path from "node:path";
5
- import { promisify } from "node:util";
6
- import { gitPath, gitRoot } from "./git.ts";
7
- import type { HookAction } from "./types.ts";
8
- import type { CliUi } from "./ui.ts";
9
-
10
- const execFileAsync = promisify(execFile);
11
- const START = "# stupify hook start";
12
- const END = "# stupify hook end";
13
-
14
- export async function runHookCommand(action: HookAction): Promise<string> {
15
- if (action === "status") return hookStatus();
16
- if (action === "install") return installHook();
17
- return uninstallHook();
18
- }
19
-
20
- export function renderHookResultToUi(result: string, ui: CliUi): void {
21
- const [firstLine = "Stupify hook: no status returned", ...rest] = result.split(/\r?\n/);
22
- if (firstLine.includes("not installed")) {
23
- ui.info(firstLine);
24
- } else if (firstLine.includes("installed") || firstLine.includes("updated") || firstLine.includes("uninstalled")) {
25
- ui.success(firstLine);
26
- } else if (firstLine.includes("existing non-Stupify")) {
27
- ui.warn(firstLine);
28
- } else {
29
- ui.info(firstLine);
30
- }
31
-
32
- const detail = rest.join("\n").trim();
33
- if (detail) ui.note(detail, "Hook");
34
- }
35
-
36
- export function hookSnippet(): string {
37
- return managedBlock("stupify --staged");
38
- }
39
-
40
- async function hookStatus(): Promise<string> {
41
- const hookPath = await preCommitHookPath();
42
- if (!existsSync(hookPath)) return "Stupify hook: not installed";
43
-
44
- const content = await readFile(hookPath, "utf8");
45
- if (hasManagedBlock(content)) return "Stupify hook: installed";
46
- return "Stupify hook: existing non-Stupify pre-commit hook found";
47
- }
48
-
49
- async function installHook(): Promise<string> {
50
- const hookPath = await preCommitHookPath();
51
- const block = await managedBlockForInstall();
52
- if (!existsSync(hookPath)) {
53
- await writeFile(hookPath, `#!/bin/sh\n${block}\n`, "utf8");
54
- await chmod(hookPath, 0o755);
55
- return "Stupify hook: installed";
56
- }
57
-
58
- const content = await readFile(hookPath, "utf8");
59
- if (hasManagedBlock(content)) {
60
- await writeFile(hookPath, `${replaceManagedBlock(content, block).trimEnd()}\n`, "utf8");
61
- await chmod(hookPath, 0o755);
62
- return "Stupify hook: updated";
63
- }
64
-
65
- if (isEffectivelyEmptyHook(content)) {
66
- await writeFile(hookPath, `#!/bin/sh\n${block}\n`, "utf8");
67
- await chmod(hookPath, 0o755);
68
- return "Stupify hook: installed";
69
- }
70
-
71
- return `Stupify hook: existing non-Stupify pre-commit hook found; not modified.
72
- Add this snippet manually if you want Stupify in that hook:
73
- ${block}`;
74
- }
75
-
76
- async function uninstallHook(): Promise<string> {
77
- const hookPath = await preCommitHookPath();
78
- if (!existsSync(hookPath)) return "Stupify hook: not installed";
79
-
80
- const content = await readFile(hookPath, "utf8");
81
- if (!hasManagedBlock(content)) return "Stupify hook: not installed";
82
-
83
- const next = replaceManagedBlock(content, "").trim();
84
- if (isEffectivelyEmptyHook(next)) {
85
- await rm(hookPath, { force: true });
86
- return "Stupify hook: uninstalled";
87
- }
88
-
89
- await writeFile(hookPath, `${next}\n`, "utf8");
90
- await chmod(hookPath, 0o755);
91
- return "Stupify hook: uninstalled";
92
- }
93
-
94
- async function preCommitHookPath(): Promise<string> {
95
- const [root, hook] = await Promise.all([gitRoot(), gitPath("hooks/pre-commit")]);
96
- return path.isAbsolute(hook) ? hook : path.join(root, hook);
97
- }
98
-
99
- function hasManagedBlock(content: string): boolean {
100
- return content.includes(START) && content.includes(END);
101
- }
102
-
103
- async function managedBlockForInstall(): Promise<string> {
104
- if (await commandExists("stupify")) return managedBlock("stupify --staged");
105
-
106
- const root = await gitRoot();
107
- const localEntrypoint = path.join(root, "packages", "cli", "src", "stupify.ts");
108
- if (existsSync(localEntrypoint) && await commandExists("bun")) {
109
- return managedBlock(`bun ${shellQuote(localEntrypoint)} --staged`);
110
- }
111
-
112
- return managedBlock("stupify --staged");
113
- }
114
-
115
- function managedBlock(command: string): string {
116
- return `${START}
117
- ${command} || true
118
- ${END}`;
119
- }
120
-
121
- function replaceManagedBlock(content: string, replacement: string): string {
122
- const pattern = new RegExp(`${escapeRegExp(START)}[\\s\\S]*?${escapeRegExp(END)}`);
123
- return content.replace(pattern, replacement);
124
- }
125
-
126
- function isEffectivelyEmptyHook(content: string): boolean {
127
- return content
128
- .split(/\r?\n/)
129
- .map((line) => line.trim())
130
- .filter((line) => line && line !== "#!/bin/sh" && line !== "#!/usr/bin/env sh")
131
- .length === 0;
132
- }
133
-
134
- function escapeRegExp(value: string): string {
135
- return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
136
- }
137
-
138
- async function commandExists(command: string): Promise<boolean> {
139
- try {
140
- await execFileAsync("sh", ["-c", `command -v ${shellQuote(command)}`], {
141
- maxBuffer: 1024 * 1024,
142
- });
143
- return true;
144
- } catch {
145
- return false;
146
- }
147
- }
148
-
149
- function shellQuote(value: string): string {
150
- return `'${value.replace(/'/g, "'\\''")}'`;
151
- }
package/src/index.ts DELETED
@@ -1 +0,0 @@
1
- export { main } from "./stupify.ts";