hookloom 1.1.0 → 1.2.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.
package/dist/cli.mjs
CHANGED
|
@@ -14,7 +14,7 @@ import { execFile, spawn } from "node:child_process";
|
|
|
14
14
|
import { createInterface } from "node:readline";
|
|
15
15
|
import { homedir, tmpdir } from "node:os";
|
|
16
16
|
import { once } from "node:events";
|
|
17
|
-
import { n as invocation, r as version, t as claudeUsesPowerShell } from "./command-line-
|
|
17
|
+
import { n as invocation, r as version, t as claudeUsesPowerShell } from "./command-line-OrJYMqHx.mjs";
|
|
18
18
|
// src/examples/catalogue.ts
|
|
19
19
|
const BOTH = ["claude", "codex"];
|
|
20
20
|
const LOCAL_SECONDS = 10;
|
|
@@ -71,6 +71,22 @@ const examples = [
|
|
|
71
71
|
timeout: LOCAL_SECONDS
|
|
72
72
|
}]
|
|
73
73
|
},
|
|
74
|
+
{
|
|
75
|
+
name: "dash-guard",
|
|
76
|
+
summary: "Refuse a change or published message that adds an em-dash, or a spaced double hyphen used as one.",
|
|
77
|
+
needs: [],
|
|
78
|
+
registrations: [{
|
|
79
|
+
event: "PreToolUse",
|
|
80
|
+
harnesses: ["claude"],
|
|
81
|
+
matcher: "Edit|Write|NotebookEdit|Bash",
|
|
82
|
+
timeout: LOCAL_SECONDS
|
|
83
|
+
}, {
|
|
84
|
+
event: "PreToolUse",
|
|
85
|
+
harnesses: ["codex"],
|
|
86
|
+
matcher: "Bash|apply_patch",
|
|
87
|
+
timeout: LOCAL_SECONDS
|
|
88
|
+
}]
|
|
89
|
+
},
|
|
74
90
|
{
|
|
75
91
|
name: "private-reference-guard",
|
|
76
92
|
summary: "Refuse to name a private GitHub repository in a public one.",
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { existsSync } from "node:fs";
|
|
2
2
|
import { delimiter, join } from "node:path";
|
|
3
3
|
// src/version.ts
|
|
4
|
-
const version = "1.
|
|
4
|
+
const version = "1.2.0";
|
|
5
5
|
// src/dispatch/command-line.ts
|
|
6
6
|
/**
|
|
7
7
|
* The program and arguments with which each harness runs a command hook on each platform.
|
package/dist/dispatcher.mjs
CHANGED
|
@@ -6,7 +6,7 @@ import { dirname, join, resolve } from "node:path";
|
|
|
6
6
|
import { a as registryPath, n as cacheDirectory, r as claudeDirectory, t as atomicWrite } from "./files-CBn-xAo2.mjs";
|
|
7
7
|
import { createHash } from "node:crypto";
|
|
8
8
|
import { a as stringField, i as parsePayload, n as isRecord, o as toolInput, r as namedField } from "./shell-4WSaabze.mjs";
|
|
9
|
-
import { n as invocation, r as version } from "./command-line-
|
|
9
|
+
import { n as invocation, r as version } from "./command-line-OrJYMqHx.mjs";
|
|
10
10
|
import { a as sessionKey, i as removeOlderThan, n as readRecord, o as writeRecord, r as records, t as digest } from "./state-Db3zojdV.mjs";
|
|
11
11
|
import { spawn } from "node:child_process";
|
|
12
12
|
import { t as withoutHarnessMessages } from "./prompt-text-A_jEvJei.mjs";
|
|
@@ -0,0 +1,907 @@
|
|
|
1
|
+
import { readFileSync, statSync } from "node:fs";
|
|
2
|
+
import { a as stringField, n as isRecord, o as toolInput, t as splitCommand } from "../shell-4WSaabze.mjs";
|
|
3
|
+
import { basename, extname, resolve } from "node:path";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import { a as shellCommand, i as main, r as deny } from "../hook-DIS0wk_5.mjs";
|
|
6
|
+
// src/examples/codex-patch.ts
|
|
7
|
+
/**
|
|
8
|
+
* Rebuilds the file contents a Codex `apply_patch` call would produce, so a hook can judge a patch as whole files before and after.
|
|
9
|
+
*/
|
|
10
|
+
var PatchError = class extends Error {};
|
|
11
|
+
const LOOSE_PUNCTUATION = [
|
|
12
|
+
[/* @__PURE__ */ new RegExp("[\\u2010-\\u2015\\u2212]", "gu"), "-"],
|
|
13
|
+
[/* @__PURE__ */ new RegExp("[\\u2018-\\u201B]", "gu"), "'"],
|
|
14
|
+
[/* @__PURE__ */ new RegExp("[\\u201C-\\u201F]", "gu"), "\""],
|
|
15
|
+
[/* @__PURE__ */ new RegExp("[\\u00A0\\u2002-\\u200A\\u202F\\u205F\\u3000]", "gu"), " "]
|
|
16
|
+
];
|
|
17
|
+
const NORMALISERS = [
|
|
18
|
+
(line) => line,
|
|
19
|
+
(line) => line.trimEnd(),
|
|
20
|
+
(line) => line.trim(),
|
|
21
|
+
(line) => LOOSE_PUNCTUATION.reduce((text, [pattern, plain]) => text.replaceAll(pattern, plain), line.trim())
|
|
22
|
+
];
|
|
23
|
+
function seek(lines, pattern, start, atEnd = false) {
|
|
24
|
+
if (pattern.length === 0) return start;
|
|
25
|
+
const first = atEnd ? Math.max(start, lines.length - pattern.length) : start;
|
|
26
|
+
for (const normalise of NORMALISERS) for (let position = first; position <= lines.length - pattern.length; position += 1) if (pattern.every((expected, offset) => normalise(lines[position + offset] ?? "") === normalise(expected))) return position;
|
|
27
|
+
}
|
|
28
|
+
var Patch = class {
|
|
29
|
+
lines;
|
|
30
|
+
index = 1;
|
|
31
|
+
staged = /* @__PURE__ */ new Map();
|
|
32
|
+
cwd;
|
|
33
|
+
changes = [];
|
|
34
|
+
constructor(text, cwd) {
|
|
35
|
+
this.cwd = cwd;
|
|
36
|
+
this.lines = text.replace(/\r?\n$/u, "").split(/\r?\n/u);
|
|
37
|
+
if (this.lines[0] !== "*** Begin Patch" || this.lines.at(-1) !== "*** End Patch") throw new PatchError("invalid patch envelope");
|
|
38
|
+
}
|
|
39
|
+
apply() {
|
|
40
|
+
while (this.index < this.lines.length - 1) {
|
|
41
|
+
const header = this.take();
|
|
42
|
+
if (header.startsWith("*** Add File: ")) this.add(header.slice(14));
|
|
43
|
+
else if (header.startsWith("*** Delete File: ")) this.remove(header.slice(17));
|
|
44
|
+
else if (header.startsWith("*** Update File: ")) this.update(header.slice(17));
|
|
45
|
+
else throw new PatchError("unsupported patch operation");
|
|
46
|
+
}
|
|
47
|
+
return this.changes;
|
|
48
|
+
}
|
|
49
|
+
get next() {
|
|
50
|
+
return this.index < this.lines.length - 1 ? this.lines[this.index] : void 0;
|
|
51
|
+
}
|
|
52
|
+
take() {
|
|
53
|
+
const line = this.lines[this.index] ?? "";
|
|
54
|
+
this.index += 1;
|
|
55
|
+
return line;
|
|
56
|
+
}
|
|
57
|
+
read(name) {
|
|
58
|
+
const path = resolve(this.cwd, name);
|
|
59
|
+
if (this.staged.has(path)) return this.staged.get(path);
|
|
60
|
+
try {
|
|
61
|
+
return readFileSync(path, "utf8");
|
|
62
|
+
} catch (error) {
|
|
63
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT") return void 0;
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
existing(name) {
|
|
68
|
+
const content = this.read(name);
|
|
69
|
+
if (content === void 0) throw new PatchError(`${name} does not exist`);
|
|
70
|
+
return content;
|
|
71
|
+
}
|
|
72
|
+
add(name) {
|
|
73
|
+
const added = [];
|
|
74
|
+
while (this.next !== void 0 && !this.next.startsWith("*** ")) {
|
|
75
|
+
const line = this.take();
|
|
76
|
+
if (!line.startsWith("+")) throw new PatchError("invalid added file line");
|
|
77
|
+
added.push(line.slice(1));
|
|
78
|
+
}
|
|
79
|
+
const after = added.length === 0 ? "" : `${added.join("\n")}\n`;
|
|
80
|
+
this.changes.push({
|
|
81
|
+
path: name,
|
|
82
|
+
before: this.read(name) ?? "",
|
|
83
|
+
after
|
|
84
|
+
});
|
|
85
|
+
this.staged.set(resolve(this.cwd, name), after);
|
|
86
|
+
}
|
|
87
|
+
remove(name) {
|
|
88
|
+
this.changes.push({
|
|
89
|
+
path: name,
|
|
90
|
+
before: this.existing(name),
|
|
91
|
+
after: ""
|
|
92
|
+
});
|
|
93
|
+
this.staged.set(resolve(this.cwd, name), void 0);
|
|
94
|
+
}
|
|
95
|
+
hunk(first) {
|
|
96
|
+
let marker = this.next ?? "";
|
|
97
|
+
if (marker.startsWith("@@")) this.index += 1;
|
|
98
|
+
else if (first && /^[ +-]/u.test(marker)) marker = "@@";
|
|
99
|
+
else throw new PatchError("invalid patch hunk header");
|
|
100
|
+
const old = [];
|
|
101
|
+
const replacement = [];
|
|
102
|
+
const kept = [];
|
|
103
|
+
while (this.next !== void 0 && !this.next.startsWith("@@") && !this.next.startsWith("*** ")) {
|
|
104
|
+
const line = this.take();
|
|
105
|
+
const kind = line[0];
|
|
106
|
+
if (kind !== " " && kind !== "+" && kind !== "-") throw new PatchError("invalid patch hunk line");
|
|
107
|
+
if (kind === " ") kept.push([old.length, replacement.length]);
|
|
108
|
+
if (kind !== "+") old.push(line.slice(1));
|
|
109
|
+
if (kind !== "-") replacement.push(line.slice(1));
|
|
110
|
+
}
|
|
111
|
+
const atEnd = this.next === "*** End of File";
|
|
112
|
+
if (atEnd) this.index += 1;
|
|
113
|
+
return {
|
|
114
|
+
anchor: marker === "@@" ? void 0 : marker.replace(/^@@ /u, ""),
|
|
115
|
+
old,
|
|
116
|
+
replacement,
|
|
117
|
+
kept,
|
|
118
|
+
atEnd
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
update(name) {
|
|
122
|
+
const before = this.existing(name);
|
|
123
|
+
const rows = before.split(/(?<=\n)/u).filter((row) => row !== "");
|
|
124
|
+
const original = rows.map((row) => row.replace(/\r?\n$/u, ""));
|
|
125
|
+
const ending = rows.find((row) => row.endsWith("\n"))?.endsWith("\r\n") === true ? "\r\n" : "\n";
|
|
126
|
+
const complete = rows.map((row) => row.endsWith("\n") ? row : row + ending);
|
|
127
|
+
const output = [];
|
|
128
|
+
let position = 0;
|
|
129
|
+
let target = name;
|
|
130
|
+
if (this.next?.startsWith("*** Move to: ") === true) target = this.take().slice(13);
|
|
131
|
+
for (let first = true; this.next !== void 0 && !this.next.startsWith("*** "); first = false) {
|
|
132
|
+
const { anchor, old, replacement, kept, atEnd } = this.hunk(first);
|
|
133
|
+
if (anchor !== void 0) {
|
|
134
|
+
const found = seek(original, [anchor], position);
|
|
135
|
+
if (found === void 0) throw new PatchError("patch anchor does not match");
|
|
136
|
+
output.push(...complete.slice(position, found + 1));
|
|
137
|
+
position = found + 1;
|
|
138
|
+
}
|
|
139
|
+
const locate = () => seek(original, old, position, atEnd) ?? (atEnd ? seek(original, old, position) : void 0);
|
|
140
|
+
let match = old.length === 0 ? original.length : locate();
|
|
141
|
+
if (match === void 0 && old.at(-1) === "") {
|
|
142
|
+
old.pop();
|
|
143
|
+
if (replacement.at(-1) === "") replacement.pop();
|
|
144
|
+
match = locate();
|
|
145
|
+
}
|
|
146
|
+
if (match === void 0) throw new PatchError("patch context does not match");
|
|
147
|
+
const written = replacement.map((line) => line + ending);
|
|
148
|
+
for (const [from, to] of kept) {
|
|
149
|
+
const source = from < old.length && to < written.length ? complete[match + from] : void 0;
|
|
150
|
+
if (source !== void 0) written[to] = source;
|
|
151
|
+
}
|
|
152
|
+
output.push(...complete.slice(position, match), ...written);
|
|
153
|
+
position = match + old.length;
|
|
154
|
+
}
|
|
155
|
+
output.push(...complete.slice(position));
|
|
156
|
+
const after = output.join("");
|
|
157
|
+
this.changes.push({
|
|
158
|
+
path: target,
|
|
159
|
+
before,
|
|
160
|
+
after
|
|
161
|
+
});
|
|
162
|
+
if (target !== name) this.staged.set(resolve(this.cwd, name), void 0);
|
|
163
|
+
this.staged.set(resolve(this.cwd, target), after);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
/**
|
|
167
|
+
* The paths a patch affects with their text before and after.
|
|
168
|
+
* @throws PatchError when the patch cannot be rebuilt against the files as they are.
|
|
169
|
+
*/
|
|
170
|
+
function patchChanges(patch, cwd) {
|
|
171
|
+
return new Patch(patch, cwd).apply();
|
|
172
|
+
}
|
|
173
|
+
// src/examples/dash/syntax.ts
|
|
174
|
+
const QUOTES = {
|
|
175
|
+
"\"": true,
|
|
176
|
+
"'": true
|
|
177
|
+
};
|
|
178
|
+
const SLASHES = {
|
|
179
|
+
lineComments: ["//"],
|
|
180
|
+
blockComments: [["/*", "*/"]]
|
|
181
|
+
};
|
|
182
|
+
const none = {
|
|
183
|
+
lineComments: [],
|
|
184
|
+
blockComments: [],
|
|
185
|
+
strings: {},
|
|
186
|
+
multiline: []
|
|
187
|
+
};
|
|
188
|
+
const cLike = {
|
|
189
|
+
...SLASHES,
|
|
190
|
+
strings: QUOTES,
|
|
191
|
+
multiline: [],
|
|
192
|
+
characterLiterals: true
|
|
193
|
+
};
|
|
194
|
+
const go = {
|
|
195
|
+
...SLASHES,
|
|
196
|
+
strings: {
|
|
197
|
+
"`": false,
|
|
198
|
+
...QUOTES
|
|
199
|
+
},
|
|
200
|
+
multiline: ["`"],
|
|
201
|
+
characterLiterals: true
|
|
202
|
+
};
|
|
203
|
+
const tripleQuoted = {
|
|
204
|
+
...SLASHES,
|
|
205
|
+
strings: {
|
|
206
|
+
"\"\"\"": true,
|
|
207
|
+
...QUOTES
|
|
208
|
+
},
|
|
209
|
+
multiline: ["\"\"\""],
|
|
210
|
+
characterLiterals: true
|
|
211
|
+
};
|
|
212
|
+
const dart = {
|
|
213
|
+
...SLASHES,
|
|
214
|
+
strings: {
|
|
215
|
+
"\"\"\"": true,
|
|
216
|
+
"'''": true,
|
|
217
|
+
...QUOTES
|
|
218
|
+
},
|
|
219
|
+
multiline: ["\"\"\"", "'''"]
|
|
220
|
+
};
|
|
221
|
+
const javascript = {
|
|
222
|
+
...SLASHES,
|
|
223
|
+
strings: {
|
|
224
|
+
"`": true,
|
|
225
|
+
...QUOTES
|
|
226
|
+
},
|
|
227
|
+
multiline: ["`"]
|
|
228
|
+
};
|
|
229
|
+
const css = {
|
|
230
|
+
...none,
|
|
231
|
+
blockComments: [["/*", "*/"]],
|
|
232
|
+
strings: QUOTES
|
|
233
|
+
};
|
|
234
|
+
const scss = {
|
|
235
|
+
...SLASHES,
|
|
236
|
+
strings: QUOTES,
|
|
237
|
+
multiline: []
|
|
238
|
+
};
|
|
239
|
+
const php = {
|
|
240
|
+
lineComments: ["//", "#"],
|
|
241
|
+
blockComments: [["/*", "*/"]],
|
|
242
|
+
strings: QUOTES,
|
|
243
|
+
multiline: ["\"", "'"]
|
|
244
|
+
};
|
|
245
|
+
const shell = {
|
|
246
|
+
...none,
|
|
247
|
+
lineComments: ["#"],
|
|
248
|
+
strings: {
|
|
249
|
+
"\"": true,
|
|
250
|
+
"'": false
|
|
251
|
+
},
|
|
252
|
+
multiline: ["\"", "'"],
|
|
253
|
+
commentNeedsSpace: true
|
|
254
|
+
};
|
|
255
|
+
const hash = {
|
|
256
|
+
...none,
|
|
257
|
+
lineComments: ["#"],
|
|
258
|
+
strings: QUOTES,
|
|
259
|
+
commentNeedsSpace: true
|
|
260
|
+
};
|
|
261
|
+
const python = {
|
|
262
|
+
...none,
|
|
263
|
+
lineComments: ["#"],
|
|
264
|
+
strings: {
|
|
265
|
+
"\"\"\"": true,
|
|
266
|
+
"'''": true,
|
|
267
|
+
...QUOTES
|
|
268
|
+
},
|
|
269
|
+
multiline: ["\"\"\"", "'''"]
|
|
270
|
+
};
|
|
271
|
+
const nix = {
|
|
272
|
+
lineComments: ["#"],
|
|
273
|
+
blockComments: [["/*", "*/"]],
|
|
274
|
+
strings: {
|
|
275
|
+
"''": false,
|
|
276
|
+
"\"": true
|
|
277
|
+
},
|
|
278
|
+
multiline: ["''", "\""],
|
|
279
|
+
commentNeedsSpace: true
|
|
280
|
+
};
|
|
281
|
+
const ini = {
|
|
282
|
+
...none,
|
|
283
|
+
lineComments: ["#", ";"],
|
|
284
|
+
commentNeedsSpace: true
|
|
285
|
+
};
|
|
286
|
+
const sql = {
|
|
287
|
+
lineComments: ["--"],
|
|
288
|
+
blockComments: [["/*", "*/"]],
|
|
289
|
+
strings: {
|
|
290
|
+
"'": false,
|
|
291
|
+
"\"": false
|
|
292
|
+
},
|
|
293
|
+
multiline: ["'", "\""]
|
|
294
|
+
};
|
|
295
|
+
const lua = {
|
|
296
|
+
lineComments: ["--"],
|
|
297
|
+
blockComments: [["--[[", "]]"]],
|
|
298
|
+
strings: {
|
|
299
|
+
"[[": false,
|
|
300
|
+
...QUOTES
|
|
301
|
+
},
|
|
302
|
+
multiline: ["[["]
|
|
303
|
+
};
|
|
304
|
+
const haskell = {
|
|
305
|
+
lineComments: ["--"],
|
|
306
|
+
blockComments: [["{-", "-}"]],
|
|
307
|
+
strings: { "\"": true },
|
|
308
|
+
multiline: [],
|
|
309
|
+
characterLiterals: true
|
|
310
|
+
};
|
|
311
|
+
const lisp = {
|
|
312
|
+
lineComments: [";"],
|
|
313
|
+
blockComments: [["#|", "|#"]],
|
|
314
|
+
strings: { "\"": true },
|
|
315
|
+
multiline: ["\""]
|
|
316
|
+
};
|
|
317
|
+
const erlang = {
|
|
318
|
+
...none,
|
|
319
|
+
lineComments: ["%"],
|
|
320
|
+
strings: { "\"": true },
|
|
321
|
+
characterLiterals: true
|
|
322
|
+
};
|
|
323
|
+
const vim = {
|
|
324
|
+
...none,
|
|
325
|
+
lineComments: ["\""],
|
|
326
|
+
commentNeedsSpace: true
|
|
327
|
+
};
|
|
328
|
+
function group(syntax, extensions) {
|
|
329
|
+
return Object.fromEntries(extensions.split(" ").map((extension) => [extension, syntax]));
|
|
330
|
+
}
|
|
331
|
+
const byExtension = {
|
|
332
|
+
...group(cLike, ".c .h .cpp .hpp .cc .cxx .hh .m .mm .java .cs .rs .zig .proto .glsl .hlsl .wgsl"),
|
|
333
|
+
...group(go, ".go"),
|
|
334
|
+
...group(tripleQuoted, ".kt .kts .swift .scala .sc .groovy .gradle"),
|
|
335
|
+
...group(dart, ".dart"),
|
|
336
|
+
...group(javascript, ".ts .tsx .js .jsx .mjs .cjs .mts .cts .jsonc .json5"),
|
|
337
|
+
...group(css, ".css"),
|
|
338
|
+
...group(scss, ".scss .less .sass"),
|
|
339
|
+
...group(php, ".php"),
|
|
340
|
+
...group(shell, ".sh .bash .zsh .fish .ksh .bats .envrc"),
|
|
341
|
+
...group(hash, ".rb .yaml .yml .toml .r .pl .pm .tf .tfvars .hcl .cmake .mk .mak .dockerfile .containerfile .nu .jl .ex .exs .cr .awk .sed .ps1 .psm1 .gitignore .gitattributes .editorconfig .properties .cfg .conf .coffee .tcl"),
|
|
342
|
+
...group(nix, ".nix"),
|
|
343
|
+
...group(ini, ".ini .desktop .service"),
|
|
344
|
+
...group(sql, ".sql"),
|
|
345
|
+
...group(lua, ".lua"),
|
|
346
|
+
...group(haskell, ".hs .lhs .elm .purs"),
|
|
347
|
+
...group(lisp, ".lisp .lsp .cl .el .scm .ss .rkt .clj .cljs .cljc .edn .fnl .asm .s"),
|
|
348
|
+
...group(erlang, ".erl .hrl"),
|
|
349
|
+
...group(vim, ".vim")
|
|
350
|
+
};
|
|
351
|
+
/** Files known by their whole name. */
|
|
352
|
+
const byName = group(hash, "Makefile makefile GNUmakefile Dockerfile Containerfile Gemfile Rakefile Brewfile Vagrantfile Procfile CODEOWNERS .gitignore .gitattributes .dockerignore .npmrc .editorconfig .bashrc .zshrc .profile .bash_profile .zprofile .zshenv");
|
|
353
|
+
/** Interpreters named on a first line, matched by prefix. */
|
|
354
|
+
const byInterpreter = [
|
|
355
|
+
...[
|
|
356
|
+
"sh",
|
|
357
|
+
"bash",
|
|
358
|
+
"zsh",
|
|
359
|
+
"dash",
|
|
360
|
+
"ksh",
|
|
361
|
+
"fish"
|
|
362
|
+
].map((name) => [name, shell]),
|
|
363
|
+
...[
|
|
364
|
+
"node",
|
|
365
|
+
"deno",
|
|
366
|
+
"bun",
|
|
367
|
+
"tsx"
|
|
368
|
+
].map((name) => [name, javascript]),
|
|
369
|
+
...[
|
|
370
|
+
"ruby",
|
|
371
|
+
"perl",
|
|
372
|
+
"Rscript"
|
|
373
|
+
].map((name) => [name, hash])
|
|
374
|
+
];
|
|
375
|
+
const EM_DASH = String.fromCodePoint(8212);
|
|
376
|
+
const SPACED_DOUBLE_HYPHEN = /(?<=\s)--(?=\s)/gu;
|
|
377
|
+
const FENCE = /^\s*(?:```+|~~~+)/u;
|
|
378
|
+
const INLINE_CODE = /(`+)(?:(?!\1).)+?\1/gu;
|
|
379
|
+
const PROSE_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
380
|
+
".md",
|
|
381
|
+
".markdown",
|
|
382
|
+
".mdx",
|
|
383
|
+
".mdc",
|
|
384
|
+
".txt",
|
|
385
|
+
".rst",
|
|
386
|
+
".adoc",
|
|
387
|
+
".asciidoc",
|
|
388
|
+
".org",
|
|
389
|
+
".tex",
|
|
390
|
+
".textile",
|
|
391
|
+
".wiki"
|
|
392
|
+
]);
|
|
393
|
+
const PROSE_NAMES = /* @__PURE__ */ new Set([
|
|
394
|
+
"README",
|
|
395
|
+
"LICENSE",
|
|
396
|
+
"LICENCE",
|
|
397
|
+
"NOTICE",
|
|
398
|
+
"AUTHORS",
|
|
399
|
+
"CONTRIBUTORS",
|
|
400
|
+
"CHANGELOG",
|
|
401
|
+
"CHANGES",
|
|
402
|
+
"HISTORY",
|
|
403
|
+
"COPYING",
|
|
404
|
+
"TODO",
|
|
405
|
+
"COMMIT_EDITMSG",
|
|
406
|
+
"MERGE_MSG",
|
|
407
|
+
"TAG_EDITMSG",
|
|
408
|
+
"SQUASH_MSG",
|
|
409
|
+
"EDIT_DESCRIPTION"
|
|
410
|
+
]);
|
|
411
|
+
const MARKUP_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
412
|
+
".html",
|
|
413
|
+
".htm",
|
|
414
|
+
".xhtml",
|
|
415
|
+
".xml",
|
|
416
|
+
".svg",
|
|
417
|
+
".vue",
|
|
418
|
+
".svelte",
|
|
419
|
+
".astro",
|
|
420
|
+
".xsl",
|
|
421
|
+
".xslt",
|
|
422
|
+
".plist"
|
|
423
|
+
]);
|
|
424
|
+
const TEMPLATE_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
425
|
+
".tpl",
|
|
426
|
+
".j2",
|
|
427
|
+
".jinja",
|
|
428
|
+
".jinja2",
|
|
429
|
+
".mustache",
|
|
430
|
+
".hbs",
|
|
431
|
+
".ejs",
|
|
432
|
+
".tmpl",
|
|
433
|
+
".erb",
|
|
434
|
+
".liquid"
|
|
435
|
+
]);
|
|
436
|
+
const PYTHON_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
437
|
+
".py",
|
|
438
|
+
".pyi",
|
|
439
|
+
".pyw"
|
|
440
|
+
]);
|
|
441
|
+
const CHARACTER_LITERAL = /'(?:\\(?:u\{[0-9a-fA-F]+\}|x[0-9a-fA-F]{2}|.)|[^\\'\n])'/uy;
|
|
442
|
+
function found(count, line, text, what) {
|
|
443
|
+
return Array.from({ length: count }, () => ({
|
|
444
|
+
line,
|
|
445
|
+
text: text.trim(),
|
|
446
|
+
what
|
|
447
|
+
}));
|
|
448
|
+
}
|
|
449
|
+
const withoutInlineCode = (text) => text.replaceAll(INLINE_CODE, "");
|
|
450
|
+
function hyphens({ line, text }) {
|
|
451
|
+
return found(withoutInlineCode(text).match(SPACED_DOUBLE_HYPHEN)?.length ?? 0, line, text, "spaced double hyphen");
|
|
452
|
+
}
|
|
453
|
+
function emDashes({ line, text }, skipInlineCode) {
|
|
454
|
+
return found((skipInlineCode ? withoutInlineCode(text) : text).split(EM_DASH).length - 1, line, text, "em-dash");
|
|
455
|
+
}
|
|
456
|
+
function numbered(text, first = 1) {
|
|
457
|
+
return text.split(/\r?\n/u).map((line, offset) => ({
|
|
458
|
+
line: first + offset,
|
|
459
|
+
text: line
|
|
460
|
+
}));
|
|
461
|
+
}
|
|
462
|
+
/** Both patterns anywhere in prose, outside code blocks and spans. */
|
|
463
|
+
function checkProse(text) {
|
|
464
|
+
let fenced = false;
|
|
465
|
+
return numbered(text).flatMap((segment) => {
|
|
466
|
+
if (FENCE.test(segment.text)) {
|
|
467
|
+
fenced = !fenced;
|
|
468
|
+
return [];
|
|
469
|
+
}
|
|
470
|
+
return fenced ? [] : [...emDashes(segment, true), ...hyphens(segment)];
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
function lineAt(text, offset) {
|
|
474
|
+
return text.slice(0, offset).split("\n").length;
|
|
475
|
+
}
|
|
476
|
+
function skipString(text, start, quote, syntax) {
|
|
477
|
+
const closer = quote === "[[" ? "]]" : quote;
|
|
478
|
+
let position = start + quote.length;
|
|
479
|
+
while (position < text.length) {
|
|
480
|
+
if (syntax.strings[quote] === true && text[position] === "\\") {
|
|
481
|
+
position += 2;
|
|
482
|
+
continue;
|
|
483
|
+
}
|
|
484
|
+
if (text.startsWith(closer, position)) return position + closer.length;
|
|
485
|
+
if (text[position] === "\n" && !syntax.multiline.includes(quote)) return position;
|
|
486
|
+
position += 1;
|
|
487
|
+
}
|
|
488
|
+
return text.length;
|
|
489
|
+
}
|
|
490
|
+
function escaped(text) {
|
|
491
|
+
return text.replaceAll(/[.*+?^${}()|[\]\\]/gu, "\\$&");
|
|
492
|
+
}
|
|
493
|
+
function lex(text, syntax) {
|
|
494
|
+
const comments = [];
|
|
495
|
+
const strings = [];
|
|
496
|
+
const openers = [
|
|
497
|
+
...Object.keys(syntax.strings),
|
|
498
|
+
...syntax.blockComments.map(([open]) => open),
|
|
499
|
+
...syntax.lineComments
|
|
500
|
+
].toSorted((left, right) => right.length - left.length);
|
|
501
|
+
if (openers.length === 0) return {
|
|
502
|
+
comments,
|
|
503
|
+
strings
|
|
504
|
+
};
|
|
505
|
+
const opening = new RegExp(openers.map(escaped).join("|"), "gu");
|
|
506
|
+
const comment = (start, end) => {
|
|
507
|
+
comments.push(...numbered(text.slice(start, end), lineAt(text, start)));
|
|
508
|
+
};
|
|
509
|
+
let position = 0;
|
|
510
|
+
for (;;) {
|
|
511
|
+
opening.lastIndex = position;
|
|
512
|
+
const match = opening.exec(text);
|
|
513
|
+
if (match === null) return {
|
|
514
|
+
comments,
|
|
515
|
+
strings
|
|
516
|
+
};
|
|
517
|
+
const start = match.index;
|
|
518
|
+
position = start + 1;
|
|
519
|
+
for (const opener of openers.filter((candidate) => text.startsWith(candidate, start))) {
|
|
520
|
+
const closer = syntax.blockComments.find(([open]) => open === opener)?.[1];
|
|
521
|
+
if (closer !== void 0) {
|
|
522
|
+
const close = text.indexOf(closer, start + opener.length);
|
|
523
|
+
const end = close === -1 ? text.length : close;
|
|
524
|
+
comment(start + opener.length, end);
|
|
525
|
+
position = end + closer.length;
|
|
526
|
+
} else if (syntax.lineComments.includes(opener)) {
|
|
527
|
+
if (syntax.commentNeedsSpace === true && start > 0 && !/\s/u.test(text[start - 1] ?? "")) continue;
|
|
528
|
+
const newline = text.indexOf("\n", start);
|
|
529
|
+
const end = newline === -1 ? text.length : newline;
|
|
530
|
+
comment(start + opener.length, end);
|
|
531
|
+
position = end;
|
|
532
|
+
} else if (opener === "'" && syntax.characterLiterals === true) {
|
|
533
|
+
CHARACTER_LITERAL.lastIndex = start;
|
|
534
|
+
position = CHARACTER_LITERAL.test(text) ? CHARACTER_LITERAL.lastIndex : start + 1;
|
|
535
|
+
} else {
|
|
536
|
+
position = skipString(text, start, opener, syntax);
|
|
537
|
+
strings.push([start, position]);
|
|
538
|
+
}
|
|
539
|
+
break;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
function docstrings(text, strings) {
|
|
544
|
+
return strings.flatMap(([start, end]) => {
|
|
545
|
+
const lineStart = text.lastIndexOf("\n", start - 1) + 1;
|
|
546
|
+
const before = text.slice(0, start);
|
|
547
|
+
const code = strings.filter(([, finish]) => finish <= start).reduceRight((rest, [from, to]) => rest.slice(0, from) + rest.slice(to), before).replaceAll(/#[^\n]*/gu, "");
|
|
548
|
+
const depth = (code.match(/[([{]/gu)?.length ?? 0) - (code.match(/[)\]}]/gu)?.length ?? 0);
|
|
549
|
+
const prefix = text.slice(lineStart, start);
|
|
550
|
+
const after = /^[^\n]*/u.exec(text.slice(end))?.[0] ?? "";
|
|
551
|
+
return /^\s*[rRbBuUfF]{0,2}$/u.test(prefix) && depth === 0 && /^\s*(?:#.*)?$/u.test(after) && !/\\\r?\n$/u.test(text.slice(0, lineStart)) ? numbered(text.slice(start, end), lineAt(text, start)) : [];
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
function blank(text, pattern) {
|
|
555
|
+
return text.replaceAll(pattern, (match) => match.replaceAll(/[^\n]/gu, " "));
|
|
556
|
+
}
|
|
557
|
+
function checkMarkup(text) {
|
|
558
|
+
const comments = [...text.matchAll(/<!--([\s\S]*?)-->/gu)].flatMap((match) => numbered(match[1] ?? "", lineAt(text, match.index + 4)));
|
|
559
|
+
const visible = blank(blank(blank(text, /<!--[\s\S]*?-->/gu), /<(script|style|pre|code|textarea)\b[\s\S]*?<\/\1\s*>/giu), /<[^>]*>/gu);
|
|
560
|
+
const segments = [...comments, ...numbered(visible)];
|
|
561
|
+
return [...segments.flatMap((segment) => emDashes(segment, true)), ...segments.flatMap(hyphens)];
|
|
562
|
+
}
|
|
563
|
+
function kindOf(path, text) {
|
|
564
|
+
const name = basename(path);
|
|
565
|
+
let extension = extname(name).toLowerCase();
|
|
566
|
+
if (TEMPLATE_EXTENSIONS.has(extension) && extname(name.slice(0, -extension.length)) !== "") extension = extname(name.slice(0, -extension.length)).toLowerCase();
|
|
567
|
+
if (extension === "" && name.startsWith(".")) extension = name.toLowerCase();
|
|
568
|
+
if (PROSE_EXTENSIONS.has(extension) || PROSE_NAMES.has(name.toUpperCase())) return "prose";
|
|
569
|
+
if (MARKUP_EXTENSIONS.has(extension)) return "markup";
|
|
570
|
+
if (PYTHON_EXTENSIONS.has(extension)) return "python";
|
|
571
|
+
const known = byExtension[extension] ?? byName[name];
|
|
572
|
+
if (known !== void 0) return known;
|
|
573
|
+
const interpreter = /^#!\s*(?:\/usr\/bin\/env\s+(?:-\S+\s+)*)?(\S+)/u.exec(text)?.[1]?.split("/").at(-1);
|
|
574
|
+
if (interpreter === void 0) return "other";
|
|
575
|
+
if (interpreter.startsWith("python")) return "python";
|
|
576
|
+
return byInterpreter.find(([prefix]) => interpreter.startsWith(prefix))?.[1] ?? "other";
|
|
577
|
+
}
|
|
578
|
+
/** The findings in a whole file, by the rules its name, or failing that its first line, selects. */
|
|
579
|
+
function checkFile(path, text) {
|
|
580
|
+
const kind = kindOf(path, text);
|
|
581
|
+
if (kind === "prose") return checkProse(text);
|
|
582
|
+
if (kind === "markup") return checkMarkup(text);
|
|
583
|
+
const everywhere = numbered(text).flatMap((segment) => emDashes(segment, false));
|
|
584
|
+
if (kind === "other") return everywhere;
|
|
585
|
+
const lexed = lex(text, kind === "python" ? python : kind);
|
|
586
|
+
const commented = kind === "python" ? [...lexed.comments, ...docstrings(text, lexed.strings)] : lexed.comments;
|
|
587
|
+
return [...everywhere, ...commented.flatMap(hyphens)];
|
|
588
|
+
}
|
|
589
|
+
const SHOWN_LINES = 10;
|
|
590
|
+
const SHOWN_CHARACTERS = 160;
|
|
591
|
+
function describeFindings(findings) {
|
|
592
|
+
const distinct = [...new Map(findings.map((finding) => [JSON.stringify(finding), finding])).values()];
|
|
593
|
+
return [...distinct.slice(0, SHOWN_LINES).map(({ line, what, text }) => ` line ${String(line)} (${what}): ${text.slice(0, SHOWN_CHARACTERS)}`), ...distinct.length > SHOWN_LINES ? [` ...and ${String(distinct.length - SHOWN_LINES)} more`] : []].join("\n");
|
|
594
|
+
}
|
|
595
|
+
// src/examples/dash/commands.ts
|
|
596
|
+
/**
|
|
597
|
+
* The commit, tag and note messages, and the GitHub titles and bodies, that a shell command would publish.
|
|
598
|
+
*/
|
|
599
|
+
const MESSAGE_SUBCOMMANDS = /* @__PURE__ */ new Set([
|
|
600
|
+
"commit",
|
|
601
|
+
"merge",
|
|
602
|
+
"tag",
|
|
603
|
+
"notes"
|
|
604
|
+
]);
|
|
605
|
+
const GIT_OPTIONS_WITH_VALUE = /* @__PURE__ */ new Set([
|
|
606
|
+
"-C",
|
|
607
|
+
"-c",
|
|
608
|
+
"--git-dir",
|
|
609
|
+
"--work-tree",
|
|
610
|
+
"--namespace",
|
|
611
|
+
"--exec-path",
|
|
612
|
+
"--config-env"
|
|
613
|
+
]);
|
|
614
|
+
const GH_TEXT_GROUPS = /* @__PURE__ */ new Set([
|
|
615
|
+
"issue",
|
|
616
|
+
"pr",
|
|
617
|
+
"release",
|
|
618
|
+
"discussion",
|
|
619
|
+
"gist"
|
|
620
|
+
]);
|
|
621
|
+
const GH_TEXT_OPTIONS = {
|
|
622
|
+
"-b": "text",
|
|
623
|
+
"--body": "text",
|
|
624
|
+
"-t": "text",
|
|
625
|
+
"--title": "text",
|
|
626
|
+
"-n": "text",
|
|
627
|
+
"--notes": "text",
|
|
628
|
+
"-d": "text",
|
|
629
|
+
"--desc": "text",
|
|
630
|
+
"-F": "file",
|
|
631
|
+
"--body-file": "file",
|
|
632
|
+
"--notes-file": "file"
|
|
633
|
+
};
|
|
634
|
+
const GH_API_TEXT_FIELDS = /* @__PURE__ */ new Set([
|
|
635
|
+
"body",
|
|
636
|
+
"title",
|
|
637
|
+
"message",
|
|
638
|
+
"description",
|
|
639
|
+
"notes",
|
|
640
|
+
"name"
|
|
641
|
+
]);
|
|
642
|
+
const PREFIXES = /* @__PURE__ */ new Set([
|
|
643
|
+
"env",
|
|
644
|
+
"exec",
|
|
645
|
+
"sudo"
|
|
646
|
+
]);
|
|
647
|
+
const HEREDOC = /(?<!<)<<(-?)\s*(["']?)([A-Za-z_][A-Za-z0-9_]*)\2/gu;
|
|
648
|
+
const PUBLISHER_ON_LINE = /(?:^|[;&|(`]|\$\()\s*(?:(?:env|command|exec|time|nohup|sudo)\s+)*(?:git|gh)\b/u;
|
|
649
|
+
const HEREDOC_TARGET = /(?:>>?|\btee(?:\s+-a)?)\s*(\S+)/gu;
|
|
650
|
+
function heredocs(command) {
|
|
651
|
+
const lines = command.split("\n");
|
|
652
|
+
const found = [];
|
|
653
|
+
let index = 0;
|
|
654
|
+
while (index < lines.length) {
|
|
655
|
+
const opener = lines[index] ?? "";
|
|
656
|
+
index += 1;
|
|
657
|
+
for (const [, dash, , delimiter] of opener.matchAll(HEREDOC)) {
|
|
658
|
+
const body = [];
|
|
659
|
+
for (; index < lines.length; index += 1) {
|
|
660
|
+
const raw = lines[index] ?? "";
|
|
661
|
+
if ((dash === "-" ? raw.replace(/^\t+/u, "") : raw).trim() === delimiter) break;
|
|
662
|
+
body.push(raw);
|
|
663
|
+
}
|
|
664
|
+
index += 1;
|
|
665
|
+
found.push({
|
|
666
|
+
opener,
|
|
667
|
+
body: body.join("\n")
|
|
668
|
+
});
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
return found;
|
|
672
|
+
}
|
|
673
|
+
function fileText(path, cwd) {
|
|
674
|
+
try {
|
|
675
|
+
return readFileSync(resolve(cwd, path.replace(/^~(?=\/|$)/u, homedir())), "utf8");
|
|
676
|
+
} catch {
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
function option(words, position, wanted) {
|
|
681
|
+
const word = words[position] ?? "";
|
|
682
|
+
const separate = (name) => {
|
|
683
|
+
const value = words[position + 1];
|
|
684
|
+
return value === void 0 ? void 0 : {
|
|
685
|
+
name,
|
|
686
|
+
value,
|
|
687
|
+
next: position + 2
|
|
688
|
+
};
|
|
689
|
+
};
|
|
690
|
+
if (word.startsWith("--")) {
|
|
691
|
+
const [name = "", ...rest] = word.split("=");
|
|
692
|
+
if (!wanted(name)) return void 0;
|
|
693
|
+
return rest.length > 0 ? {
|
|
694
|
+
name,
|
|
695
|
+
value: rest.join("="),
|
|
696
|
+
next: position + 1
|
|
697
|
+
} : separate(name);
|
|
698
|
+
}
|
|
699
|
+
if (!/^-[A-Za-z]/u.test(word)) return void 0;
|
|
700
|
+
const flags = /^-([A-Za-z]+)/u.exec(word)?.[1] ?? "";
|
|
701
|
+
const index = Array.from(flags).findIndex((letter) => wanted(`-${letter}`));
|
|
702
|
+
if (index === -1) return void 0;
|
|
703
|
+
const name = `-${flags[index] ?? ""}`;
|
|
704
|
+
const attached = word.slice(index + name.length);
|
|
705
|
+
return attached === "" ? separate(name) : {
|
|
706
|
+
name,
|
|
707
|
+
value: attached,
|
|
708
|
+
next: position + 1
|
|
709
|
+
};
|
|
710
|
+
}
|
|
711
|
+
function options(words, start, wanted) {
|
|
712
|
+
const found = [];
|
|
713
|
+
for (let position = start; position < words.length && words[position] !== "--";) {
|
|
714
|
+
const matched = option(words, position, wanted);
|
|
715
|
+
if (matched === void 0) position += 1;
|
|
716
|
+
else {
|
|
717
|
+
found.push(matched);
|
|
718
|
+
position = matched.next;
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
return found;
|
|
722
|
+
}
|
|
723
|
+
function fromFile(source, path, cwd) {
|
|
724
|
+
const text = path === "-" ? void 0 : fileText(path, cwd);
|
|
725
|
+
return text === void 0 ? [] : [{
|
|
726
|
+
source,
|
|
727
|
+
text
|
|
728
|
+
}];
|
|
729
|
+
}
|
|
730
|
+
function gitTexts(words, cwd) {
|
|
731
|
+
let index = 0;
|
|
732
|
+
while (words[index]?.startsWith("-") === true) index += GIT_OPTIONS_WITH_VALUE.has(words[index] ?? "") ? 2 : 1;
|
|
733
|
+
const subcommand = words[index];
|
|
734
|
+
if (subcommand === void 0 || !MESSAGE_SUBCOMMANDS.has(subcommand)) return void 0;
|
|
735
|
+
return options(words, index + 1, (name) => [
|
|
736
|
+
"-m",
|
|
737
|
+
"--message",
|
|
738
|
+
"-F",
|
|
739
|
+
"--file"
|
|
740
|
+
].includes(name)).flatMap(({ name, value }) => name === "-F" || name === "--file" ? fromFile(`git ${subcommand} ${name} ${value}`, value, cwd) : [{
|
|
741
|
+
source: `git ${subcommand} message`,
|
|
742
|
+
text: value
|
|
743
|
+
}]);
|
|
744
|
+
}
|
|
745
|
+
function ghTexts(words, cwd) {
|
|
746
|
+
const [group] = words;
|
|
747
|
+
if (group === "api") return options(words, 1, (name) => [
|
|
748
|
+
"-f",
|
|
749
|
+
"-F",
|
|
750
|
+
"--field",
|
|
751
|
+
"--raw-field"
|
|
752
|
+
].includes(name)).flatMap(({ name, value }) => {
|
|
753
|
+
const [key = "", ...rest] = value.split("=");
|
|
754
|
+
if (rest.length === 0 || !GH_API_TEXT_FIELDS.has(key)) return [];
|
|
755
|
+
const text = rest.join("=");
|
|
756
|
+
if ((name === "-F" || name === "--field") && text.startsWith("@")) return fromFile(`gh api field ${key} from ${text.slice(1)}`, text === "@-" ? "-" : text.slice(1), cwd);
|
|
757
|
+
return [{
|
|
758
|
+
source: `gh api field ${key}`,
|
|
759
|
+
text
|
|
760
|
+
}];
|
|
761
|
+
});
|
|
762
|
+
if (group === void 0 || !GH_TEXT_GROUPS.has(group)) return void 0;
|
|
763
|
+
return options(words, 1, (name) => Object.hasOwn(GH_TEXT_OPTIONS, name)).flatMap(({ name, value }) => GH_TEXT_OPTIONS[name] === "file" ? fromFile(`gh ${group} ${name} ${value}`, value, cwd) : [{
|
|
764
|
+
source: `gh ${group} ${name}`,
|
|
765
|
+
text: value
|
|
766
|
+
}]);
|
|
767
|
+
}
|
|
768
|
+
function publishedBy({ opener }, publisherWords) {
|
|
769
|
+
if (PUBLISHER_ON_LINE.test(opener)) return true;
|
|
770
|
+
return [...opener.matchAll(HEREDOC_TARGET)].map(([, target = ""]) => target.replaceAll(/^["']+|["']+$/gu, "")).some((target) => target !== "" && publisherWords.some((word) => word.includes(target)));
|
|
771
|
+
}
|
|
772
|
+
/** The texts a command line would publish, with the heredoc bodies that reach a publishing command; none when the line cannot be split. */
|
|
773
|
+
function publishedTexts(command, cwd) {
|
|
774
|
+
const split = splitCommand(command);
|
|
775
|
+
if (split === void 0) return [];
|
|
776
|
+
const publisherWords = [];
|
|
777
|
+
const texts = split.words.flatMap((words) => {
|
|
778
|
+
const start = words.findIndex((word) => !PREFIXES.has(word) && !/^[A-Za-z_][A-Za-z0-9_]*=/u.test(word));
|
|
779
|
+
const [program = "", ...rest] = start === -1 ? [] : words.slice(start);
|
|
780
|
+
const name = program.split("/").at(-1);
|
|
781
|
+
const found = name === "git" ? gitTexts(rest, cwd) : name === "gh" ? ghTexts(rest, cwd) : void 0;
|
|
782
|
+
if (found !== void 0) publisherWords.push(...rest);
|
|
783
|
+
return found ?? [];
|
|
784
|
+
});
|
|
785
|
+
const bodies = publisherWords.length === 0 ? [] : heredocs(command).filter((heredoc) => publishedBy(heredoc, publisherWords));
|
|
786
|
+
return [...texts, ...bodies.map(({ body }) => ({
|
|
787
|
+
source: "heredoc",
|
|
788
|
+
text: body
|
|
789
|
+
}))].filter(({ text }) => !text.trimStart().startsWith("$("));
|
|
790
|
+
}
|
|
791
|
+
// src/examples/dash-guard.ts
|
|
792
|
+
/**
|
|
793
|
+
* Before a tool call, refuses a change that adds an em-dash, or a spaced double hyphen used as one.
|
|
794
|
+
*
|
|
795
|
+
* A file edit is judged as the whole file before and after, so only an increase is refused: a file already holding either can still be edited, and removing them is always allowed. A shell command is judged by the commit messages and GitHub text it would publish, where any occurrence is new by definition.
|
|
796
|
+
*/
|
|
797
|
+
const ADVICE = "Rewrite the affected sentences as plain prose (a full stop, comma, colon or parentheses) and retry.";
|
|
798
|
+
const NOTEBOOK_EXTENSIONS = {
|
|
799
|
+
python: ".py",
|
|
800
|
+
r: ".r",
|
|
801
|
+
julia: ".jl",
|
|
802
|
+
javascript: ".js",
|
|
803
|
+
typescript: ".ts",
|
|
804
|
+
bash: ".sh"
|
|
805
|
+
};
|
|
806
|
+
function existing(path) {
|
|
807
|
+
return statSync(path, { throwIfNoEntry: false })?.isFile() === true ? readFileSync(path, "utf8") : void 0;
|
|
808
|
+
}
|
|
809
|
+
/** The findings a change adds: those whose line and pattern are new to the file. */
|
|
810
|
+
function addedFindings({ path, before, after }) {
|
|
811
|
+
const now = checkFile(path, after);
|
|
812
|
+
const then = checkFile(path, before);
|
|
813
|
+
if (now.length <= then.length) return [];
|
|
814
|
+
const key = ({ text, what }) => JSON.stringify([text, what]);
|
|
815
|
+
const remaining = /* @__PURE__ */ new Map();
|
|
816
|
+
for (const finding of then) remaining.set(key(finding), (remaining.get(key(finding)) ?? 0) + 1);
|
|
817
|
+
const added = now.filter((finding) => {
|
|
818
|
+
const left = remaining.get(key(finding)) ?? 0;
|
|
819
|
+
remaining.set(key(finding), left - 1);
|
|
820
|
+
return left <= 0;
|
|
821
|
+
});
|
|
822
|
+
return added.length > 0 ? added : now;
|
|
823
|
+
}
|
|
824
|
+
function notebookChange(input, path) {
|
|
825
|
+
const after = typeof input["new_source"] === "string" ? input["new_source"] : "";
|
|
826
|
+
let cellType = typeof input["cell_type"] === "string" ? input["cell_type"] : "";
|
|
827
|
+
let before = "";
|
|
828
|
+
let language = "python";
|
|
829
|
+
let notebook;
|
|
830
|
+
try {
|
|
831
|
+
notebook = JSON.parse(readFileSync(path, "utf8"));
|
|
832
|
+
} catch {
|
|
833
|
+
notebook = void 0;
|
|
834
|
+
}
|
|
835
|
+
if (isRecord(notebook)) {
|
|
836
|
+
const metadata = notebook["metadata"];
|
|
837
|
+
const information = isRecord(metadata) ? metadata["language_info"] : void 0;
|
|
838
|
+
if (isRecord(information) && typeof information["name"] === "string") language = information["name"];
|
|
839
|
+
const cells = notebook["cells"];
|
|
840
|
+
const cell = Array.isArray(cells) && (input["edit_mode"] ?? "replace") === "replace" ? cells.find((candidate) => isRecord(candidate) && candidate["id"] === input["cell_id"]) : void 0;
|
|
841
|
+
if (isRecord(cell)) {
|
|
842
|
+
const source = cell["source"];
|
|
843
|
+
before = Array.isArray(source) ? source.join("") : typeof source === "string" ? source : "";
|
|
844
|
+
if (cellType === "" && typeof cell["cell_type"] === "string") cellType = cell["cell_type"];
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
return {
|
|
848
|
+
path: `cell${cellType === "markdown" ? ".md" : NOTEBOOK_EXTENSIONS[language.toLowerCase()] ?? ".py"}`,
|
|
849
|
+
before,
|
|
850
|
+
after
|
|
851
|
+
};
|
|
852
|
+
}
|
|
853
|
+
function fileChange(tool, input) {
|
|
854
|
+
const text = (name) => typeof input[name] === "string" ? input[name] : "";
|
|
855
|
+
const path = text(tool === "NotebookEdit" ? "notebook_path" : "file_path");
|
|
856
|
+
if (path === "") return void 0;
|
|
857
|
+
if (tool === "Write") return {
|
|
858
|
+
path,
|
|
859
|
+
before: existing(path) ?? "",
|
|
860
|
+
after: text("content")
|
|
861
|
+
};
|
|
862
|
+
if (tool === "NotebookEdit") return input["edit_mode"] === "delete" ? void 0 : notebookChange(input, path);
|
|
863
|
+
if (tool !== "Edit") return void 0;
|
|
864
|
+
const [old, replacement] = [text("old_string"), text("new_string")];
|
|
865
|
+
const content = existing(path);
|
|
866
|
+
if (content?.includes(old) !== true) return {
|
|
867
|
+
path,
|
|
868
|
+
before: old,
|
|
869
|
+
after: replacement
|
|
870
|
+
};
|
|
871
|
+
return {
|
|
872
|
+
path,
|
|
873
|
+
before: content,
|
|
874
|
+
after: input["replace_all"] === true ? content.replaceAll(old, () => replacement) : content.replace(old, () => replacement)
|
|
875
|
+
};
|
|
876
|
+
}
|
|
877
|
+
function refusal(what, details) {
|
|
878
|
+
return deny(`${what} an em-dash or a spaced double hyphen used as a dash, both of which are banned in written output. ${ADVICE}${details}`);
|
|
879
|
+
}
|
|
880
|
+
function judgePatch(input, command) {
|
|
881
|
+
let changes;
|
|
882
|
+
try {
|
|
883
|
+
changes = patchChanges(command, stringField(input, "cwd") ?? ".");
|
|
884
|
+
} catch (error) {
|
|
885
|
+
return deny(`Cannot inspect the patch for prohibited prose: ${error instanceof Error ? error.message : String(error)}`);
|
|
886
|
+
}
|
|
887
|
+
const findings = changes.flatMap(addedFindings);
|
|
888
|
+
return findings.length === 0 ? void 0 : refusal("This change adds", ` Lines involved:\n${describeFindings(findings)}`);
|
|
889
|
+
}
|
|
890
|
+
const hook = (input) => {
|
|
891
|
+
const tool = stringField(input, "tool_name") ?? "";
|
|
892
|
+
const command = shellCommand(input);
|
|
893
|
+
if (tool === "apply_patch") return judgePatch(input, command ?? "");
|
|
894
|
+
if (tool === "Bash") {
|
|
895
|
+
const flagged = publishedTexts(command ?? "", stringField(input, "cwd") ?? ".").flatMap(({ source, text }) => {
|
|
896
|
+
const findings = checkProse(text);
|
|
897
|
+
return findings.length === 0 ? [] : [`${source}:\n${describeFindings(findings)}`];
|
|
898
|
+
});
|
|
899
|
+
return flagged.length === 0 ? void 0 : refusal("This command publishes text containing", `\n${flagged.join("\n")}`);
|
|
900
|
+
}
|
|
901
|
+
const change = fileChange(tool, toolInput(input));
|
|
902
|
+
const added = change === void 0 ? [] : addedFindings(change);
|
|
903
|
+
return added.length === 0 ? void 0 : refusal("This change adds", ` Lines involved:\n${describeFindings(added)}`);
|
|
904
|
+
};
|
|
905
|
+
// src/examples/bin/dash-guard.ts
|
|
906
|
+
await main(hook);
|
|
907
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hookloom",
|
|
3
3
|
"description": "One hook registry for Claude Code and Codex: a plugin that dispatches every lifecycle event to your hooks, with a CLI to set up, migrate and roll back",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.2.0",
|
|
5
5
|
"author": "Joseph Mearman <joseph@mearman.co.uk>",
|
|
6
6
|
"bin": {
|
|
7
7
|
"hookloom": "./dist/cli.mjs"
|