@stupify/cli 0.0.2 → 0.0.4
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 +55 -0
- package/dist/analysis.d.ts +16 -0
- package/dist/analysis.js +133 -0
- package/dist/cache.d.ts +2 -0
- package/dist/cache.js +59 -0
- package/dist/checks.d.ts +4 -0
- package/dist/checks.js +218 -0
- package/dist/command.d.ts +2 -0
- package/dist/command.js +147 -0
- package/dist/constants.d.ts +4 -0
- package/dist/constants.js +53 -0
- package/dist/counter-scout.d.ts +14 -0
- package/dist/counter-scout.js +159 -0
- package/dist/diff.d.ts +1 -0
- package/dist/diff.js +10 -0
- package/dist/doctor.d.ts +4 -0
- package/dist/doctor.js +131 -0
- package/dist/git.d.ts +11 -0
- package/dist/git.js +253 -0
- package/dist/hooks.d.ts +3 -0
- package/dist/hooks.js +117 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/model.d.ts +10 -0
- package/dist/model.js +297 -0
- package/dist/prompts.d.ts +8 -0
- package/dist/prompts.js +87 -0
- package/dist/render.d.ts +3 -0
- package/dist/render.js +93 -0
- package/dist/repomix-provider.d.ts +12 -0
- package/dist/repomix-provider.js +196 -0
- package/dist/search-bench.d.ts +1 -0
- package/dist/search-bench.js +675 -0
- package/dist/search-profile.d.ts +6 -0
- package/dist/search-profile.js +73 -0
- package/dist/sem-provider.d.ts +2 -0
- package/dist/sem-provider.js +247 -0
- package/dist/stupify.d.ts +4 -0
- package/dist/stupify.js +237 -0
- package/dist/trace.d.ts +29 -0
- package/dist/trace.js +64 -0
- package/dist/types.d.ts +320 -0
- package/dist/types.js +6 -0
- package/package.json +42 -5
- package/src/analysis.ts +188 -0
- package/src/cache.ts +65 -0
- package/src/checks.ts +221 -0
- package/src/command.ts +173 -0
- package/src/constants.ts +56 -0
- package/src/counter-scout.ts +175 -0
- package/src/diff.ts +9 -0
- package/src/doctor.ts +140 -0
- package/src/git.ts +262 -0
- package/src/hooks.ts +134 -0
- package/src/index.ts +1 -0
- package/src/model.ts +373 -0
- package/src/prompts.ts +98 -0
- package/src/render.ts +96 -0
- package/src/repomix-provider.ts +219 -0
- package/src/search-bench.ts +783 -0
- package/src/search-profile.ts +89 -0
- package/src/sem-provider.ts +282 -0
- package/src/stupify.ts +285 -0
- package/src/trace.ts +103 -0
- package/src/types.ts +340 -0
- package/bin/stupify.mjs +0 -3
package/src/doctor.ts
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { homedir, platform } from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { promisify } from "node:util";
|
|
6
|
+
import { DEFAULT_MODEL_ID, MODEL_REGISTRY } from "./constants.ts";
|
|
7
|
+
import { runHookCommand } from "./hooks.ts";
|
|
8
|
+
|
|
9
|
+
const execFileAsync = promisify(execFile);
|
|
10
|
+
|
|
11
|
+
type CheckStatus = "ok" | "missing" | "info";
|
|
12
|
+
|
|
13
|
+
type DoctorCheck = Readonly<{
|
|
14
|
+
label: string;
|
|
15
|
+
status: CheckStatus;
|
|
16
|
+
detail: string;
|
|
17
|
+
required?: boolean;
|
|
18
|
+
}>;
|
|
19
|
+
|
|
20
|
+
export async function runDoctor(): Promise<Readonly<{ exitCode: number; text: string }>> {
|
|
21
|
+
const checks = await Promise.all([
|
|
22
|
+
gitCheck(),
|
|
23
|
+
hookCheck(),
|
|
24
|
+
semCheck(),
|
|
25
|
+
repomixCheck(),
|
|
26
|
+
llamaServerCheck(),
|
|
27
|
+
modelCacheCheck(),
|
|
28
|
+
]);
|
|
29
|
+
const requiredMissing = checks.some((check) => check.required && check.status === "missing");
|
|
30
|
+
return {
|
|
31
|
+
exitCode: requiredMissing ? 1 : 0,
|
|
32
|
+
text: renderDoctor(checks),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function gitCheck(): Promise<DoctorCheck> {
|
|
37
|
+
try {
|
|
38
|
+
const { stdout } = await execFileAsync("git", ["rev-parse", "--show-toplevel"], { maxBuffer: 1024 * 1024 });
|
|
39
|
+
return { label: "git repo", status: "ok", detail: stdout.trim(), required: true };
|
|
40
|
+
} catch {
|
|
41
|
+
return { label: "git repo", status: "missing", detail: "not inside a git repository", required: true };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function hookCheck(): Promise<DoctorCheck> {
|
|
46
|
+
try {
|
|
47
|
+
const status = await runHookCommand("status");
|
|
48
|
+
return { label: "pre-commit hook", status: "info", detail: status.replace(/^Stupify hook:\s*/, "") };
|
|
49
|
+
} catch (error) {
|
|
50
|
+
return { label: "pre-commit hook", status: "info", detail: errorMessage(error) };
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function semCheck(): Promise<DoctorCheck> {
|
|
55
|
+
const packageBin = resolvePackage("@ataraxy-labs/sem/bin/sem.js");
|
|
56
|
+
if (packageBin) return { label: "sem", status: "ok", detail: "@ataraxy-labs/sem package binary found", required: true };
|
|
57
|
+
if (await commandExists("sem")) return { label: "sem", status: "ok", detail: "sem found on PATH", required: true };
|
|
58
|
+
return { label: "sem", status: "missing", detail: "install @ataraxy-labs/sem or put sem on PATH", required: true };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function repomixCheck(): Promise<DoctorCheck> {
|
|
62
|
+
if (resolvePackage("repomix")) return { label: "Repomix", status: "ok", detail: "repomix package found", required: true };
|
|
63
|
+
return { label: "Repomix", status: "missing", detail: "repomix package is not installed", required: true };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function llamaServerCheck(): Promise<DoctorCheck> {
|
|
67
|
+
if (await commandExists("llama-server")) return { label: "llama-server", status: "ok", detail: "llama-server found on PATH", required: true };
|
|
68
|
+
return { label: "llama-server", status: "missing", detail: "install llama.cpp, for example `brew install llama.cpp`", required: true };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function modelCacheCheck(): Promise<DoctorCheck> {
|
|
72
|
+
const model = MODEL_REGISTRY[DEFAULT_MODEL_ID];
|
|
73
|
+
const modelPath = path.join(cacheDir(), "models", model.file);
|
|
74
|
+
if (await fileExists(modelPath)) return { label: "default model", status: "ok", detail: `${model.name} cached` };
|
|
75
|
+
return {
|
|
76
|
+
label: "default model",
|
|
77
|
+
status: "info",
|
|
78
|
+
detail: `${model.name} not cached yet; first interactive search can download it locally`,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function renderDoctor(checks: readonly DoctorCheck[]): string {
|
|
83
|
+
const lines = [
|
|
84
|
+
"Stupify doctor",
|
|
85
|
+
"",
|
|
86
|
+
...checks.map((check) => `${icon(check.status)} ${check.label}: ${check.detail}`),
|
|
87
|
+
"",
|
|
88
|
+
"Privacy: local-only. Stupify does not upload source, diffs, filenames, repo URLs, commit messages, author names, or private package names.",
|
|
89
|
+
];
|
|
90
|
+
return lines.join("\n");
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function icon(status: CheckStatus): string {
|
|
94
|
+
if (status === "ok") return "OK";
|
|
95
|
+
if (status === "missing") return "MISSING";
|
|
96
|
+
return "INFO";
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function resolvePackage(specifier: string): string | null {
|
|
100
|
+
try {
|
|
101
|
+
const require = createRequire(import.meta.url);
|
|
102
|
+
return require.resolve(specifier);
|
|
103
|
+
} catch {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async function commandExists(command: string): Promise<boolean> {
|
|
109
|
+
try {
|
|
110
|
+
await execFileAsync("sh", ["-c", `command -v ${shellQuote(command)}`], { maxBuffer: 1024 * 1024 });
|
|
111
|
+
return true;
|
|
112
|
+
} catch {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async function fileExists(filePath: string): Promise<boolean> {
|
|
118
|
+
try {
|
|
119
|
+
const { stat } = await import("node:fs/promises");
|
|
120
|
+
return (await stat(filePath)).isFile();
|
|
121
|
+
} catch {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function cacheDir(): string {
|
|
127
|
+
if (process.env.STUPIFY_CACHE_DIR) return process.env.STUPIFY_CACHE_DIR;
|
|
128
|
+
if (process.env.XDG_CACHE_HOME) return path.join(process.env.XDG_CACHE_HOME, "stupify");
|
|
129
|
+
if (platform() === "darwin") return path.join(homedir(), "Library", "Caches", "stupify");
|
|
130
|
+
if (platform() === "win32" && process.env.LOCALAPPDATA) return path.join(process.env.LOCALAPPDATA, "stupify", "Cache");
|
|
131
|
+
return path.join(homedir(), ".cache", "stupify");
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function shellQuote(value: string): string {
|
|
135
|
+
return `'${value.replace(/'/g, "'\\''")}'`;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function errorMessage(error: unknown): string {
|
|
139
|
+
return error instanceof Error ? error.message : String(error);
|
|
140
|
+
}
|
package/src/git.ts
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { promisify } from "node:util";
|
|
3
|
+
import { sourceId, 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
|
+
const [base, target, shortBase, shortTarget] = await Promise.all([
|
|
44
|
+
revParse(`${oldest}^1`),
|
|
45
|
+
revParse(newest),
|
|
46
|
+
shortCommit(`${oldest}^1`),
|
|
47
|
+
shortCommit(newest),
|
|
48
|
+
]);
|
|
49
|
+
|
|
50
|
+
return sourceRange(base, target, `${commits.length} recent commits`, sourceId(`range:${shortBase}..${shortTarget}`));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export async function netDiffFromStdin(text: string): Promise<NetDiff> {
|
|
54
|
+
if (!text.trim()) throw new Error("No diff received on stdin.");
|
|
55
|
+
return {
|
|
56
|
+
id: sourceId("stdin"),
|
|
57
|
+
label: "stdin",
|
|
58
|
+
base: "stdin",
|
|
59
|
+
target: "stdin",
|
|
60
|
+
text,
|
|
61
|
+
stats: statsFromDiff(text),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export async function stagedDiff(): Promise<StagedDiff> {
|
|
66
|
+
try {
|
|
67
|
+
const { stdout } = await execFileAsync("git", [
|
|
68
|
+
"diff",
|
|
69
|
+
"--cached",
|
|
70
|
+
"--no-ext-diff",
|
|
71
|
+
"--no-color",
|
|
72
|
+
"--unified=3",
|
|
73
|
+
"--",
|
|
74
|
+
], { maxBuffer: 64 * 1024 * 1024 });
|
|
75
|
+
return { text: stdout, stats: statsFromDiff(stdout) };
|
|
76
|
+
} catch {
|
|
77
|
+
throw new Error("Could not read staged changes. Run stupify inside a git repository.");
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export async function gitRoot(): Promise<string> {
|
|
82
|
+
try {
|
|
83
|
+
const { stdout } = await execFileAsync("git", ["rev-parse", "--show-toplevel"]);
|
|
84
|
+
return stdout.trim();
|
|
85
|
+
} catch {
|
|
86
|
+
throw new Error("Could not find a git repository.");
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export async function gitPath(pathspec: string): Promise<string> {
|
|
91
|
+
try {
|
|
92
|
+
const { stdout } = await execFileAsync("git", ["rev-parse", "--git-path", pathspec]);
|
|
93
|
+
return stdout.trim();
|
|
94
|
+
} catch {
|
|
95
|
+
throw new Error(`Could not resolve git path: ${pathspec}`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function netDiff(base: string, target: string, label: string, id?: NetDiff["id"]): Promise<NetDiff> {
|
|
100
|
+
const [text, stats, shortBase, shortTarget] = await Promise.all([
|
|
101
|
+
diff(base, target),
|
|
102
|
+
diffStats(base, target),
|
|
103
|
+
shortCommit(base),
|
|
104
|
+
shortCommit(target),
|
|
105
|
+
]);
|
|
106
|
+
return {
|
|
107
|
+
id: id ?? sourceId(`net:${shortBase}..${shortTarget}`),
|
|
108
|
+
label,
|
|
109
|
+
base,
|
|
110
|
+
target,
|
|
111
|
+
text,
|
|
112
|
+
stats,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async function sourceRange(base: string, target: string, label: string, id?: SourceRange["id"]): Promise<SourceRange> {
|
|
117
|
+
const [stats, shortBase, shortTarget] = await Promise.all([
|
|
118
|
+
diffStats(base, target),
|
|
119
|
+
shortCommit(base),
|
|
120
|
+
shortCommit(target),
|
|
121
|
+
]);
|
|
122
|
+
return {
|
|
123
|
+
id: id ?? sourceId(`net:${shortBase}..${shortTarget}`),
|
|
124
|
+
label,
|
|
125
|
+
base,
|
|
126
|
+
target,
|
|
127
|
+
stats,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async function baseBefore(since: string): Promise<string> {
|
|
132
|
+
try {
|
|
133
|
+
const { stdout } = await execFileAsync("git", [
|
|
134
|
+
"log",
|
|
135
|
+
"--first-parent",
|
|
136
|
+
"--before",
|
|
137
|
+
since,
|
|
138
|
+
"-1",
|
|
139
|
+
"--format=%H",
|
|
140
|
+
]);
|
|
141
|
+
const commit = stdout.trim();
|
|
142
|
+
if (commit) return commit;
|
|
143
|
+
return rootCommit();
|
|
144
|
+
} catch {
|
|
145
|
+
throw new Error(`Could not resolve base commit before ${since}.`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async function rootCommit(): Promise<string> {
|
|
150
|
+
try {
|
|
151
|
+
const { stdout } = await execFileAsync("git", ["rev-list", "--max-parents=0", "HEAD"]);
|
|
152
|
+
return stdout.trim().split(/\r?\n/, 1)[0] ?? "";
|
|
153
|
+
} catch {
|
|
154
|
+
throw new Error("Could not resolve repository root commit.");
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async function diff(base: string, target: string): Promise<string> {
|
|
159
|
+
try {
|
|
160
|
+
const { stdout } = await execFileAsync("git", [
|
|
161
|
+
"diff",
|
|
162
|
+
"--no-ext-diff",
|
|
163
|
+
"--no-color",
|
|
164
|
+
"--unified=8",
|
|
165
|
+
base,
|
|
166
|
+
target,
|
|
167
|
+
"--",
|
|
168
|
+
], { maxBuffer: 128 * 1024 * 1024 });
|
|
169
|
+
if (!stdout.trim()) throw new Error("empty diff");
|
|
170
|
+
return stdout;
|
|
171
|
+
} catch {
|
|
172
|
+
throw new Error(`No diff found for ${base}..${target}.`);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
async function diffStats(base: string, target: string): Promise<NetDiffStats> {
|
|
177
|
+
try {
|
|
178
|
+
const { stdout } = await execFileAsync("git", ["diff", "--numstat", base, target, "--"], {
|
|
179
|
+
maxBuffer: 16 * 1024 * 1024,
|
|
180
|
+
});
|
|
181
|
+
return statsFromNumstat(stdout);
|
|
182
|
+
} catch {
|
|
183
|
+
return { filesChanged: 0, additions: 0, deletions: 0 };
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function statsFromDiff(diffText: string): NetDiffStats {
|
|
188
|
+
const files = new Set<string>();
|
|
189
|
+
let additions = 0;
|
|
190
|
+
let deletions = 0;
|
|
191
|
+
for (const line of diffText.split(/\r?\n/)) {
|
|
192
|
+
const fileMatch = /^diff --git a\/.+ b\/(.+)$/.exec(line);
|
|
193
|
+
if (fileMatch) files.add(fileMatch[1]);
|
|
194
|
+
else if (line.startsWith("+") && !line.startsWith("+++")) additions += 1;
|
|
195
|
+
else if (line.startsWith("-") && !line.startsWith("---")) deletions += 1;
|
|
196
|
+
}
|
|
197
|
+
return { filesChanged: files.size, additions, deletions };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function statsFromNumstat(numstat: string): NetDiffStats {
|
|
201
|
+
let filesChanged = 0;
|
|
202
|
+
let additions = 0;
|
|
203
|
+
let deletions = 0;
|
|
204
|
+
|
|
205
|
+
for (const line of numstat.split(/\r?\n/)) {
|
|
206
|
+
if (!line.trim()) continue;
|
|
207
|
+
const [added, deleted] = line.split(/\s+/, 3);
|
|
208
|
+
filesChanged += 1;
|
|
209
|
+
additions += Number(added) || 0;
|
|
210
|
+
deletions += Number(deleted) || 0;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return { filesChanged, additions, deletions };
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
async function recentCommits(count: number): Promise<readonly string[]> {
|
|
217
|
+
try {
|
|
218
|
+
const { stdout } = await execFileAsync("git", [
|
|
219
|
+
"log",
|
|
220
|
+
"--first-parent",
|
|
221
|
+
"--no-merges",
|
|
222
|
+
"--format=%H",
|
|
223
|
+
`-${count}`,
|
|
224
|
+
]);
|
|
225
|
+
return stdout.split(/\r?\n/).filter(Boolean).reverse();
|
|
226
|
+
} catch {
|
|
227
|
+
throw new Error(`Could not read last ${count} commits.`);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
async function revParse(rev: string): Promise<string> {
|
|
232
|
+
try {
|
|
233
|
+
const { stdout } = await execFileAsync("git", ["rev-parse", rev]);
|
|
234
|
+
return stdout.trim();
|
|
235
|
+
} catch {
|
|
236
|
+
throw new Error(`Could not resolve ${rev}.`);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
async function shortCommit(commit: string): Promise<string> {
|
|
241
|
+
try {
|
|
242
|
+
const { stdout } = await execFileAsync("git", ["rev-parse", "--short", commit]);
|
|
243
|
+
return stdout.trim();
|
|
244
|
+
} catch {
|
|
245
|
+
throw new Error(`Could not resolve commit ${commit}.`);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
async function commitMessage(commit: string): Promise<string> {
|
|
250
|
+
try {
|
|
251
|
+
const { stdout } = await execFileAsync("git", ["show", "--no-patch", "--format=%B", commit], {
|
|
252
|
+
maxBuffer: 1024 * 1024,
|
|
253
|
+
});
|
|
254
|
+
return stdout;
|
|
255
|
+
} catch {
|
|
256
|
+
throw new Error(`Could not read commit message for ${commit}.`);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function firstLine(value: string): string {
|
|
261
|
+
return value.trim().split(/\r?\n/, 1)[0]?.trim() ?? "";
|
|
262
|
+
}
|
package/src/hooks.ts
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
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
|
+
|
|
9
|
+
const execFileAsync = promisify(execFile);
|
|
10
|
+
const START = "# stupify hook start";
|
|
11
|
+
const END = "# stupify hook end";
|
|
12
|
+
|
|
13
|
+
export async function runHookCommand(action: HookAction): Promise<string> {
|
|
14
|
+
if (action === "status") return hookStatus();
|
|
15
|
+
if (action === "install") return installHook();
|
|
16
|
+
return uninstallHook();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function hookSnippet(): string {
|
|
20
|
+
return managedBlock("stupify --staged");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function hookStatus(): Promise<string> {
|
|
24
|
+
const hookPath = await preCommitHookPath();
|
|
25
|
+
if (!existsSync(hookPath)) return "Stupify hook: not installed";
|
|
26
|
+
|
|
27
|
+
const content = await readFile(hookPath, "utf8");
|
|
28
|
+
if (hasManagedBlock(content)) return "Stupify hook: installed";
|
|
29
|
+
return "Stupify hook: existing non-Stupify pre-commit hook found";
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function installHook(): Promise<string> {
|
|
33
|
+
const hookPath = await preCommitHookPath();
|
|
34
|
+
const block = await managedBlockForInstall();
|
|
35
|
+
if (!existsSync(hookPath)) {
|
|
36
|
+
await writeFile(hookPath, `#!/bin/sh\n${block}\n`, "utf8");
|
|
37
|
+
await chmod(hookPath, 0o755);
|
|
38
|
+
return "Stupify hook: installed";
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const content = await readFile(hookPath, "utf8");
|
|
42
|
+
if (hasManagedBlock(content)) {
|
|
43
|
+
await writeFile(hookPath, `${replaceManagedBlock(content, block).trimEnd()}\n`, "utf8");
|
|
44
|
+
await chmod(hookPath, 0o755);
|
|
45
|
+
return "Stupify hook: updated";
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (isEffectivelyEmptyHook(content)) {
|
|
49
|
+
await writeFile(hookPath, `#!/bin/sh\n${block}\n`, "utf8");
|
|
50
|
+
await chmod(hookPath, 0o755);
|
|
51
|
+
return "Stupify hook: installed";
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return `Stupify hook: existing non-Stupify pre-commit hook found; not modified.
|
|
55
|
+
Add this snippet manually if you want Stupify in that hook:
|
|
56
|
+
${block}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function uninstallHook(): Promise<string> {
|
|
60
|
+
const hookPath = await preCommitHookPath();
|
|
61
|
+
if (!existsSync(hookPath)) return "Stupify hook: not installed";
|
|
62
|
+
|
|
63
|
+
const content = await readFile(hookPath, "utf8");
|
|
64
|
+
if (!hasManagedBlock(content)) return "Stupify hook: not installed";
|
|
65
|
+
|
|
66
|
+
const next = replaceManagedBlock(content, "").trim();
|
|
67
|
+
if (isEffectivelyEmptyHook(next)) {
|
|
68
|
+
await rm(hookPath, { force: true });
|
|
69
|
+
return "Stupify hook: uninstalled";
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
await writeFile(hookPath, `${next}\n`, "utf8");
|
|
73
|
+
await chmod(hookPath, 0o755);
|
|
74
|
+
return "Stupify hook: uninstalled";
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function preCommitHookPath(): Promise<string> {
|
|
78
|
+
const [root, hook] = await Promise.all([gitRoot(), gitPath("hooks/pre-commit")]);
|
|
79
|
+
return path.isAbsolute(hook) ? hook : path.join(root, hook);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function hasManagedBlock(content: string): boolean {
|
|
83
|
+
return content.includes(START) && content.includes(END);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function managedBlockForInstall(): Promise<string> {
|
|
87
|
+
if (await commandExists("stupify")) return managedBlock("stupify --staged");
|
|
88
|
+
|
|
89
|
+
const root = await gitRoot();
|
|
90
|
+
const localEntrypoint = path.join(root, "packages", "cli", "src", "stupify.ts");
|
|
91
|
+
if (existsSync(localEntrypoint) && await commandExists("bun")) {
|
|
92
|
+
return managedBlock(`bun ${shellQuote(localEntrypoint)} --staged`);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return managedBlock("stupify --staged");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function managedBlock(command: string): string {
|
|
99
|
+
return `${START}
|
|
100
|
+
${command} || true
|
|
101
|
+
${END}`;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function replaceManagedBlock(content: string, replacement: string): string {
|
|
105
|
+
const pattern = new RegExp(`${escapeRegExp(START)}[\\s\\S]*?${escapeRegExp(END)}`);
|
|
106
|
+
return content.replace(pattern, replacement);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function isEffectivelyEmptyHook(content: string): boolean {
|
|
110
|
+
return content
|
|
111
|
+
.split(/\r?\n/)
|
|
112
|
+
.map((line) => line.trim())
|
|
113
|
+
.filter((line) => line && line !== "#!/bin/sh" && line !== "#!/usr/bin/env sh")
|
|
114
|
+
.length === 0;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function escapeRegExp(value: string): string {
|
|
118
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function commandExists(command: string): Promise<boolean> {
|
|
122
|
+
try {
|
|
123
|
+
await execFileAsync("sh", ["-c", `command -v ${shellQuote(command)}`], {
|
|
124
|
+
maxBuffer: 1024 * 1024,
|
|
125
|
+
});
|
|
126
|
+
return true;
|
|
127
|
+
} catch {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function shellQuote(value: string): string {
|
|
133
|
+
return `'${value.replace(/'/g, "'\\''")}'`;
|
|
134
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { main } from "./stupify.ts";
|