loopwright 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.
- package/LICENSE +21 -0
- package/README.md +174 -0
- package/content/.gitkeep +0 -0
- package/content/agents/implementer.md +41 -0
- package/content/agents/reviewer.md +15 -0
- package/content/agents/scout.md +36 -0
- package/content/agents/tester.md +39 -0
- package/content/hooks/default-config.json +23 -0
- package/content/hooks/guard-locked-tests.mjs +35 -0
- package/content/hooks/lib.mjs +147 -0
- package/content/hooks/lw.mjs +215 -0
- package/content/hooks/reinject-context.mjs +66 -0
- package/content/hooks/stop-gate.mjs +37 -0
- package/content/prompts/reviewer.md +66 -0
- package/content/schemas/verdict.json +26 -0
- package/content/skills/loopwright-build/SKILL.md +35 -0
- package/content/skills/loopwright-done/SKILL.md +40 -0
- package/content/skills/loopwright-flow/SKILL.md +40 -0
- package/content/skills/loopwright-plan/SKILL.md +106 -0
- package/content/skills/loopwright-review/SKILL.md +49 -0
- package/content/skills/loopwright-test/SKILL.md +33 -0
- package/dist/cli.js +1013 -0
- package/package.json +44 -0
package/dist/cli.js
ADDED
|
@@ -0,0 +1,1013 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { parseArgs } from "node:util";
|
|
3
|
+
import { cpSync, existsSync, existsSync as existsSync$1, lstatSync, mkdirSync, readFileSync, readdirSync, readdirSync as readdirSync$1, readlinkSync, rmSync, rmdirSync, symlinkSync, unlinkSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import { basename, dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
6
|
+
import { execFileSync } from "node:child_process";
|
|
7
|
+
import { createHash } from "node:crypto";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
import { createInterface } from "node:readline";
|
|
10
|
+
// src/core/git.ts
|
|
11
|
+
function git(cwd, args) {
|
|
12
|
+
return execFileSync("git", args, {
|
|
13
|
+
cwd,
|
|
14
|
+
stdio: [
|
|
15
|
+
"ignore",
|
|
16
|
+
"pipe",
|
|
17
|
+
"pipe"
|
|
18
|
+
],
|
|
19
|
+
encoding: "utf8"
|
|
20
|
+
}).trim();
|
|
21
|
+
}
|
|
22
|
+
function gitTopLevel(cwd) {
|
|
23
|
+
return git(cwd, ["rev-parse", "--show-toplevel"]);
|
|
24
|
+
}
|
|
25
|
+
function mainWorktreeRoot(cwd) {
|
|
26
|
+
const common = git(cwd, [
|
|
27
|
+
"rev-parse",
|
|
28
|
+
"--path-format=absolute",
|
|
29
|
+
"--git-common-dir"
|
|
30
|
+
]);
|
|
31
|
+
if (basename(common) === ".git") return dirname(common);
|
|
32
|
+
return gitTopLevel(cwd);
|
|
33
|
+
}
|
|
34
|
+
// src/core/targets.ts
|
|
35
|
+
var NotAGitRepoError = class extends Error {
|
|
36
|
+
constructor(cwd) {
|
|
37
|
+
super(`Not inside a git repository: ${cwd}`);
|
|
38
|
+
this.name = "NotAGitRepoError";
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
function resolveTargets(opts) {
|
|
42
|
+
if (opts.mode === "global") {
|
|
43
|
+
const h = opts.home;
|
|
44
|
+
const lw = join(h, ".config/loopwright");
|
|
45
|
+
return {
|
|
46
|
+
mode: "global",
|
|
47
|
+
root: h,
|
|
48
|
+
skillsDir: join(h, ".agents/skills"),
|
|
49
|
+
claudeSkillsDir: join(h, ".claude/skills"),
|
|
50
|
+
claudeSkillsMode: "copy",
|
|
51
|
+
claudeAgentsDir: join(h, ".claude/agents"),
|
|
52
|
+
codexAgentsDir: join(h, ".codex/agents"),
|
|
53
|
+
lwHome: lw,
|
|
54
|
+
lwHomeRef: lw,
|
|
55
|
+
claudeSettingsPath: join(h, ".claude/settings.json"),
|
|
56
|
+
codexConfigPath: join(h, ".codex/config.toml"),
|
|
57
|
+
manifestPath: join(lw, "manifest.json"),
|
|
58
|
+
configPath: join(lw, "config.json"),
|
|
59
|
+
stateDir: null
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
let root;
|
|
63
|
+
let main;
|
|
64
|
+
try {
|
|
65
|
+
root = gitTopLevel(opts.cwd);
|
|
66
|
+
main = mainWorktreeRoot(opts.cwd);
|
|
67
|
+
} catch {
|
|
68
|
+
throw new NotAGitRepoError(opts.cwd);
|
|
69
|
+
}
|
|
70
|
+
const stateDir = join(main, ".loopwright");
|
|
71
|
+
return {
|
|
72
|
+
mode: "repo",
|
|
73
|
+
root,
|
|
74
|
+
skillsDir: join(root, ".agents/skills"),
|
|
75
|
+
claudeSkillsDir: join(root, ".claude/skills"),
|
|
76
|
+
claudeSkillsMode: opts.symlink ? "symlink" : "none",
|
|
77
|
+
claudeAgentsDir: join(root, ".claude/agents"),
|
|
78
|
+
codexAgentsDir: join(root, ".codex/agents"),
|
|
79
|
+
lwHome: join(root, ".agents/loopwright"),
|
|
80
|
+
lwHomeRef: ".agents/loopwright",
|
|
81
|
+
claudeSettingsPath: join(root, ".claude/settings.json"),
|
|
82
|
+
codexConfigPath: join(root, ".codex/config.toml"),
|
|
83
|
+
manifestPath: join(stateDir, "manifest.json"),
|
|
84
|
+
configPath: join(stateDir, "config.json"),
|
|
85
|
+
stateDir
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
// src/core/config.ts
|
|
89
|
+
const DEFAULT_CONFIG = {
|
|
90
|
+
maxFixRounds: 3,
|
|
91
|
+
crossModelReview: "off",
|
|
92
|
+
docsDir: "docs/agents",
|
|
93
|
+
models: {
|
|
94
|
+
claude: {
|
|
95
|
+
scout: "opus",
|
|
96
|
+
tester: "sonnet",
|
|
97
|
+
implementer: "sonnet",
|
|
98
|
+
reviewer: "opus"
|
|
99
|
+
},
|
|
100
|
+
codex: {
|
|
101
|
+
scout: "inherit",
|
|
102
|
+
tester: "inherit",
|
|
103
|
+
implementer: "inherit",
|
|
104
|
+
reviewer: "inherit"
|
|
105
|
+
},
|
|
106
|
+
crossModelReviewer: {
|
|
107
|
+
claude: "opus",
|
|
108
|
+
codex: "inherit"
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
function isPlainObject(v) {
|
|
113
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
114
|
+
}
|
|
115
|
+
function merge(base, over) {
|
|
116
|
+
if (!isPlainObject(base) || !isPlainObject(over)) return over === void 0 ? base : over;
|
|
117
|
+
const out = { ...base };
|
|
118
|
+
for (const [k, v] of Object.entries(over)) out[k] = merge(base[k], v);
|
|
119
|
+
return out;
|
|
120
|
+
}
|
|
121
|
+
function readConfig(path) {
|
|
122
|
+
if (!existsSync(path)) return structuredClone(DEFAULT_CONFIG);
|
|
123
|
+
let parsed;
|
|
124
|
+
try {
|
|
125
|
+
parsed = JSON.parse(readFileSync(path, "utf8"));
|
|
126
|
+
} catch (e) {
|
|
127
|
+
throw new Error(`Invalid JSON in ${path}: ${e.message}`);
|
|
128
|
+
}
|
|
129
|
+
return merge(structuredClone(DEFAULT_CONFIG), parsed);
|
|
130
|
+
}
|
|
131
|
+
// src/core/manifest.ts
|
|
132
|
+
function sha256(text) {
|
|
133
|
+
return createHash("sha256").update(text).digest("hex");
|
|
134
|
+
}
|
|
135
|
+
function readManifest(path) {
|
|
136
|
+
if (!existsSync(path)) return null;
|
|
137
|
+
try {
|
|
138
|
+
const m = JSON.parse(readFileSync(path, "utf-8"));
|
|
139
|
+
if (m && m.version === 1 && Array.isArray(m.files)) return {
|
|
140
|
+
...m,
|
|
141
|
+
config: Array.isArray(m.config) ? m.config : []
|
|
142
|
+
};
|
|
143
|
+
return null;
|
|
144
|
+
} catch {
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function writeManifest(path, m) {
|
|
149
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
150
|
+
writeFileSync(path, JSON.stringify(m, null, 2) + "\n", "utf-8");
|
|
151
|
+
}
|
|
152
|
+
// src/core/writer.ts
|
|
153
|
+
var InstallWriter = class {
|
|
154
|
+
opts;
|
|
155
|
+
recorded = /* @__PURE__ */ new Map();
|
|
156
|
+
config = [];
|
|
157
|
+
constructor(opts) {
|
|
158
|
+
this.opts = opts;
|
|
159
|
+
}
|
|
160
|
+
async writeFile(abs, content) {
|
|
161
|
+
const key = this.opts.toKey(abs);
|
|
162
|
+
const newHash = sha256(content);
|
|
163
|
+
if (!existsSync(abs)) {
|
|
164
|
+
this.write(abs, content);
|
|
165
|
+
this.recorded.set(key, newHash);
|
|
166
|
+
return "created";
|
|
167
|
+
}
|
|
168
|
+
const current = readFileSync(abs, "utf-8");
|
|
169
|
+
const currentHash = sha256(current);
|
|
170
|
+
if (current === content) {
|
|
171
|
+
this.recorded.set(key, currentHash);
|
|
172
|
+
return "unchanged";
|
|
173
|
+
}
|
|
174
|
+
const prior = this.opts.manifest?.files.find((f) => f.path === key)?.sha256;
|
|
175
|
+
let overwrite = prior !== void 0 && prior === currentHash;
|
|
176
|
+
if (!overwrite) {
|
|
177
|
+
if (this.opts.force) overwrite = true;
|
|
178
|
+
else if (this.opts.dryRun) overwrite = false;
|
|
179
|
+
else overwrite = await this.opts.confirm(`${abs} was modified. Overwrite?`);
|
|
180
|
+
}
|
|
181
|
+
if (overwrite) {
|
|
182
|
+
this.write(abs, content);
|
|
183
|
+
this.recorded.set(key, newHash);
|
|
184
|
+
return "updated";
|
|
185
|
+
}
|
|
186
|
+
if (prior !== void 0) this.recorded.set(key, prior);
|
|
187
|
+
return "kept";
|
|
188
|
+
}
|
|
189
|
+
/** Record a wired config file (absolute path); stored under the same key scheme as files. */
|
|
190
|
+
recordConfig(entry) {
|
|
191
|
+
this.addConfig({
|
|
192
|
+
file: this.opts.toKey(entry.file),
|
|
193
|
+
kind: entry.kind
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
/** Hooks skipped this run: existing wiring stays in place, so its manifest record must too. */
|
|
197
|
+
carryPriorConfig() {
|
|
198
|
+
for (const c of this.opts.manifest?.config ?? []) this.addConfig(c);
|
|
199
|
+
}
|
|
200
|
+
addConfig(entry) {
|
|
201
|
+
if (!this.config.some((c) => c.file === entry.file && c.kind === entry.kind)) this.config.push({
|
|
202
|
+
file: entry.file,
|
|
203
|
+
kind: entry.kind
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
result() {
|
|
207
|
+
const all = new Map(this.recorded);
|
|
208
|
+
for (const f of this.opts.manifest?.files ?? []) if (!all.has(f.path)) all.set(f.path, f.sha256);
|
|
209
|
+
const files = [...all.entries()].map(([path, hash]) => ({
|
|
210
|
+
path,
|
|
211
|
+
sha256: hash
|
|
212
|
+
})).sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
|
|
213
|
+
return {
|
|
214
|
+
version: 1,
|
|
215
|
+
mode: this.opts.mode ?? this.opts.manifest?.mode ?? "repo",
|
|
216
|
+
files,
|
|
217
|
+
config: [...this.config]
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
write(abs, content) {
|
|
221
|
+
if (this.opts.dryRun) return;
|
|
222
|
+
mkdirSync(dirname(abs), { recursive: true });
|
|
223
|
+
writeFileSync(abs, content, "utf-8");
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
// src/core/content.ts
|
|
227
|
+
/** Walk up from this module to the package root so it works from src/ and from the bundled dist/. */
|
|
228
|
+
function contentRoot() {
|
|
229
|
+
let dir = dirname(fileURLToPath(import.meta.url));
|
|
230
|
+
for (;;) {
|
|
231
|
+
if (existsSync(join(dir, "package.json"))) return join(dir, "content") + "/";
|
|
232
|
+
const parent = dirname(dir);
|
|
233
|
+
if (parent === dir) throw new Error("Could not locate package root for content/");
|
|
234
|
+
dir = parent;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
function listContent(subdir, root = contentRoot()) {
|
|
238
|
+
const base = join(root, subdir);
|
|
239
|
+
if (!existsSync(base)) return [];
|
|
240
|
+
const out = [];
|
|
241
|
+
const walk = (d) => {
|
|
242
|
+
for (const e of readdirSync(d, { withFileTypes: true })) {
|
|
243
|
+
const p = join(d, e.name);
|
|
244
|
+
if (e.isDirectory()) walk(p);
|
|
245
|
+
else out.push(relative(base, p).split("\\").join("/"));
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
walk(base);
|
|
249
|
+
return out.sort();
|
|
250
|
+
}
|
|
251
|
+
function readContent(rel, root = contentRoot()) {
|
|
252
|
+
return readFileSync(join(root, rel), "utf8");
|
|
253
|
+
}
|
|
254
|
+
function renderTemplate(text, vars) {
|
|
255
|
+
return text.replace(/\{\{([^{}]*)\}\}/g, (_m, key) => {
|
|
256
|
+
if (!Object.hasOwn(vars, key)) throw new Error(`Unknown template placeholder: {{${key}}}`);
|
|
257
|
+
return vars[key];
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
// src/generate/agents.ts
|
|
261
|
+
function parseAgentSource(role, md) {
|
|
262
|
+
const m = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/.exec(md);
|
|
263
|
+
if (!m) throw new Error(`Agent source "${role}" is missing frontmatter`);
|
|
264
|
+
const fm = {};
|
|
265
|
+
for (const line of m[1].split(/\r?\n/)) {
|
|
266
|
+
const i = line.indexOf(":");
|
|
267
|
+
if (i < 0) continue;
|
|
268
|
+
fm[line.slice(0, i).trim()] = line.slice(i + 1).trim();
|
|
269
|
+
}
|
|
270
|
+
if (!fm.name) throw new Error(`Agent source "${role}" is missing name`);
|
|
271
|
+
if (!fm.description) throw new Error(`Agent source "${role}" is missing description`);
|
|
272
|
+
return {
|
|
273
|
+
role,
|
|
274
|
+
name: fm.name,
|
|
275
|
+
description: fm.description,
|
|
276
|
+
...fm.tools ? { tools: fm.tools } : {},
|
|
277
|
+
body: m[2].trim()
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
function renderClaudeAgent(src, model) {
|
|
281
|
+
const lines = [
|
|
282
|
+
"---",
|
|
283
|
+
`name: ${src.name}`,
|
|
284
|
+
`description: ${src.description}`
|
|
285
|
+
];
|
|
286
|
+
if (src.tools) lines.push(`tools: ${src.tools}`);
|
|
287
|
+
if (model !== "inherit") lines.push(`model: ${model}`);
|
|
288
|
+
lines.push("---", "", src.body, "");
|
|
289
|
+
return lines.join("\n");
|
|
290
|
+
}
|
|
291
|
+
function basicString(s) {
|
|
292
|
+
return `"${s.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}"`;
|
|
293
|
+
}
|
|
294
|
+
function multilineBasic(s) {
|
|
295
|
+
return `"""\n${s.replace(/\\/g, "\\\\").replace(/"""/g, "\\\"\\\"\\\"").replace(/[\u0000-\u0008\u000b-\u001f\u007f]/g, (c) => `\\u${c.charCodeAt(0).toString(16).padStart(4, "0")}`)}\n"""`;
|
|
296
|
+
}
|
|
297
|
+
function renderCodexAgent(src, model, effort) {
|
|
298
|
+
const instructions = src.body.includes("'''") ? multilineBasic(src.body) : `'''\n${src.body}\n'''`;
|
|
299
|
+
const lines = [`name = ${basicString(src.name)}`, `description = ${basicString(src.description)}`];
|
|
300
|
+
if (model !== "inherit") lines.push(`model = ${basicString(model)}`);
|
|
301
|
+
if (effort) lines.push(`model_reasoning_effort = ${basicString(effort)}`);
|
|
302
|
+
lines.push(`developer_instructions = ${instructions}`, "");
|
|
303
|
+
return lines.join("\n");
|
|
304
|
+
}
|
|
305
|
+
// src/install/hook-wiring.ts
|
|
306
|
+
/** Substring identifying commands managed by loopwright. */
|
|
307
|
+
const MANAGED_HOOK_HINT = "loopwright/hooks/";
|
|
308
|
+
function hookSpecs(tool) {
|
|
309
|
+
return [
|
|
310
|
+
{
|
|
311
|
+
event: "PreToolUse",
|
|
312
|
+
matcher: tool === "claude" ? "Edit|Write|MultiEdit" : "^apply_patch$",
|
|
313
|
+
script: "guard-locked-tests.mjs"
|
|
314
|
+
},
|
|
315
|
+
{
|
|
316
|
+
event: "Stop",
|
|
317
|
+
script: "stop-gate.mjs"
|
|
318
|
+
},
|
|
319
|
+
{
|
|
320
|
+
event: "SessionStart",
|
|
321
|
+
matcher: "compact",
|
|
322
|
+
script: "reinject-context.mjs"
|
|
323
|
+
}
|
|
324
|
+
];
|
|
325
|
+
}
|
|
326
|
+
function hookCommand(tool, targets, script) {
|
|
327
|
+
if (targets.mode === "global") return `node "${targets.lwHome}/hooks/${script}" --tool ${tool}`;
|
|
328
|
+
return `node "${tool === "claude" ? "$CLAUDE_PROJECT_DIR" : "$(git rev-parse --show-toplevel)"}/.agents/loopwright/hooks/${script}" --tool ${tool}`;
|
|
329
|
+
}
|
|
330
|
+
// src/install/claude-settings.ts
|
|
331
|
+
var SettingsParseError = class extends Error {
|
|
332
|
+
constructor(message, filePath) {
|
|
333
|
+
super(filePath ? `Could not parse ${filePath}: ${message}` : `Could not parse settings: ${message}`);
|
|
334
|
+
this.name = "SettingsParseError";
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
const isObj = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
338
|
+
function parseObject(text, filePath) {
|
|
339
|
+
if (!text.trim()) return {};
|
|
340
|
+
let parsed;
|
|
341
|
+
try {
|
|
342
|
+
parsed = JSON.parse(text);
|
|
343
|
+
} catch (e) {
|
|
344
|
+
throw new SettingsParseError(e instanceof Error ? e.message : String(e), filePath);
|
|
345
|
+
}
|
|
346
|
+
if (!isObj(parsed)) throw new SettingsParseError("expected a JSON object at the top level", filePath);
|
|
347
|
+
return parsed;
|
|
348
|
+
}
|
|
349
|
+
function isManagedGroup(group, hint) {
|
|
350
|
+
if (!isObj(group) || !Array.isArray(group["hooks"]) || group["hooks"].length === 0) return false;
|
|
351
|
+
return group["hooks"].every((h) => isObj(h) && typeof h["command"] === "string" && h["command"].includes(hint));
|
|
352
|
+
}
|
|
353
|
+
function hooksOf(obj, filePath) {
|
|
354
|
+
const h = obj["hooks"];
|
|
355
|
+
if (h === void 0) return void 0;
|
|
356
|
+
if (!isObj(h)) throw new SettingsParseError("expected \"hooks\" to be a JSON object", filePath);
|
|
357
|
+
return h;
|
|
358
|
+
}
|
|
359
|
+
function mergeClaudeHooks(settingsText, specs, commandFor, filePath) {
|
|
360
|
+
const settings = parseObject(settingsText, filePath);
|
|
361
|
+
const hooks = { ...hooksOf(settings, filePath) ?? {} };
|
|
362
|
+
const events = [...new Set(specs.map((s) => s.event))];
|
|
363
|
+
for (const ev of events) {
|
|
364
|
+
const cur = hooks[ev];
|
|
365
|
+
if (cur !== void 0 && !Array.isArray(cur)) throw new SettingsParseError(`expected hooks.${ev} to be an array`, filePath);
|
|
366
|
+
hooks[ev] = (cur ?? []).filter((g) => !isManagedGroup(g, MANAGED_HOOK_HINT));
|
|
367
|
+
}
|
|
368
|
+
for (const s of specs) {
|
|
369
|
+
const group = {};
|
|
370
|
+
if (s.matcher) group["matcher"] = s.matcher;
|
|
371
|
+
group["hooks"] = [{
|
|
372
|
+
type: "command",
|
|
373
|
+
command: commandFor(s)
|
|
374
|
+
}];
|
|
375
|
+
hooks[s.event].push(group);
|
|
376
|
+
}
|
|
377
|
+
return JSON.stringify({
|
|
378
|
+
...settings,
|
|
379
|
+
hooks
|
|
380
|
+
}, null, 2) + "\n";
|
|
381
|
+
}
|
|
382
|
+
function removeClaudeHooks(settingsText, hint, filePath) {
|
|
383
|
+
const settings = parseObject(settingsText, filePath);
|
|
384
|
+
const hooksIn = hooksOf(settings, filePath);
|
|
385
|
+
if (!hooksIn) return settingsText;
|
|
386
|
+
const hooks = { ...hooksIn };
|
|
387
|
+
let touched = false;
|
|
388
|
+
for (const [ev, groups] of Object.entries(hooks)) {
|
|
389
|
+
if (!Array.isArray(groups)) continue;
|
|
390
|
+
const kept = groups.filter((g) => !isManagedGroup(g, hint));
|
|
391
|
+
if (kept.length === groups.length) continue;
|
|
392
|
+
touched = true;
|
|
393
|
+
if (kept.length === 0) delete hooks[ev];
|
|
394
|
+
else hooks[ev] = kept;
|
|
395
|
+
}
|
|
396
|
+
if (!touched) return settingsText;
|
|
397
|
+
const next = { ...settings };
|
|
398
|
+
if (Object.keys(hooks).length === 0) delete next["hooks"];
|
|
399
|
+
else next["hooks"] = hooks;
|
|
400
|
+
return JSON.stringify(next, null, 2) + "\n";
|
|
401
|
+
}
|
|
402
|
+
// src/install/codex-config.ts
|
|
403
|
+
const CODEX_BEGIN = "# BEGIN loopwright";
|
|
404
|
+
const CODEX_END = "# END loopwright";
|
|
405
|
+
const BLOCK_RE = /^# BEGIN loopwright[ \t]*\r?\n[\s\S]*?^# END loopwright[ \t]*(?:\r?\n|$)/m;
|
|
406
|
+
function tomlString(value) {
|
|
407
|
+
if (!value.includes("'") && !/[\r\n]/.test(value)) return `'${value}'`;
|
|
408
|
+
return `"${value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"").replace(/\n/g, "\\n").replace(/\r/g, "\\r")}"`;
|
|
409
|
+
}
|
|
410
|
+
function renderCodexHookBlock(specs, commandFor) {
|
|
411
|
+
const lines = [CODEX_BEGIN];
|
|
412
|
+
for (const s of specs) {
|
|
413
|
+
lines.push(`[[hooks.${s.event}]]`);
|
|
414
|
+
if (s.matcher) lines.push(`matcher = ${tomlString(s.matcher)}`);
|
|
415
|
+
lines.push(`[[hooks.${s.event}.hooks]]`, "type = \"command\"", `command = ${tomlString(commandFor(s))}`);
|
|
416
|
+
}
|
|
417
|
+
lines.push(CODEX_END);
|
|
418
|
+
return lines.join("\n") + "\n";
|
|
419
|
+
}
|
|
420
|
+
function upsertCodexBlock(text, block) {
|
|
421
|
+
const b = block.replace(/\r\n/g, "\n");
|
|
422
|
+
const normalized = b.endsWith("\n") ? b : b + "\n";
|
|
423
|
+
if (BLOCK_RE.test(text)) return text.replace(BLOCK_RE, () => normalized);
|
|
424
|
+
if (text.trim() === "") return normalized;
|
|
425
|
+
return `${text.endsWith("\n") ? text : text + "\n"}\n${normalized}`;
|
|
426
|
+
}
|
|
427
|
+
function removeCodexBlock(text) {
|
|
428
|
+
const m = BLOCK_RE.exec(text);
|
|
429
|
+
if (!m) return text;
|
|
430
|
+
let before = text.slice(0, m.index);
|
|
431
|
+
const after = text.slice(m.index + m[0].length);
|
|
432
|
+
before = before.replace(/\r?\n(?=\r?\n$)/, "");
|
|
433
|
+
return before + after;
|
|
434
|
+
}
|
|
435
|
+
// src/utils/fs.ts
|
|
436
|
+
function ensureDir(path) {
|
|
437
|
+
mkdirSync(path, { recursive: true });
|
|
438
|
+
}
|
|
439
|
+
function isSymlink(path) {
|
|
440
|
+
try {
|
|
441
|
+
return lstatSync(path).isSymbolicLink();
|
|
442
|
+
} catch {
|
|
443
|
+
return false;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
function symlinkTarget(path) {
|
|
447
|
+
try {
|
|
448
|
+
return readlinkSync(path);
|
|
449
|
+
} catch {
|
|
450
|
+
return null;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
function isEmptyDir(path) {
|
|
454
|
+
try {
|
|
455
|
+
return lstatSync(path).isDirectory() && readdirSync$1(path).length === 0;
|
|
456
|
+
} catch {
|
|
457
|
+
return false;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
function dirExists(path) {
|
|
461
|
+
try {
|
|
462
|
+
return lstatSync(path).isDirectory();
|
|
463
|
+
} catch {
|
|
464
|
+
return false;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
function createSymlink(target, linkPath) {
|
|
468
|
+
ensureDir(dirname(linkPath));
|
|
469
|
+
symlinkSync(target, linkPath, "dir");
|
|
470
|
+
}
|
|
471
|
+
function removeDir(path) {
|
|
472
|
+
rmSync(path, {
|
|
473
|
+
recursive: true,
|
|
474
|
+
force: true
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
function removePath(path) {
|
|
478
|
+
try {
|
|
479
|
+
unlinkSync(path);
|
|
480
|
+
} catch {
|
|
481
|
+
rmSync(path, {
|
|
482
|
+
recursive: true,
|
|
483
|
+
force: true
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
// src/utils/log.ts
|
|
488
|
+
const noColor = !!process.env["NO_COLOR"];
|
|
489
|
+
const fmt = (code, text) => noColor ? text : `\x1b[${code}m${text}\x1b[0m`;
|
|
490
|
+
const log = {
|
|
491
|
+
info: (msg) => console.log(fmt("36", "ℹ") + " " + msg),
|
|
492
|
+
success: (msg) => console.log(fmt("32", "✔") + " " + msg),
|
|
493
|
+
warn: (msg) => console.log(fmt("33", "⚠") + " " + msg),
|
|
494
|
+
error: (msg) => console.error(fmt("31", "✖") + " " + msg),
|
|
495
|
+
dim: (msg) => console.log(fmt("2", msg)),
|
|
496
|
+
bold: (text) => fmt("1", text)
|
|
497
|
+
};
|
|
498
|
+
// src/utils/prompt.ts
|
|
499
|
+
async function ask(question) {
|
|
500
|
+
const rl = createInterface({
|
|
501
|
+
input: process.stdin,
|
|
502
|
+
output: process.stdout
|
|
503
|
+
});
|
|
504
|
+
return new Promise((resolve) => {
|
|
505
|
+
rl.question(question, (answer) => {
|
|
506
|
+
rl.close();
|
|
507
|
+
resolve(answer.trim());
|
|
508
|
+
});
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
async function confirm(question, defaultYes = true) {
|
|
512
|
+
const answer = await ask(`${question} ${defaultYes ? "[Y/n]" : "[y/N]"} `);
|
|
513
|
+
if (answer === "") return defaultYes;
|
|
514
|
+
return answer.toLowerCase().startsWith("y");
|
|
515
|
+
}
|
|
516
|
+
async function choose(question, options) {
|
|
517
|
+
console.log(question);
|
|
518
|
+
options.forEach((opt, i) => console.log(` ${i + 1}) ${opt}`));
|
|
519
|
+
const answer = await ask("Choice: ");
|
|
520
|
+
const idx = parseInt(answer, 10) - 1;
|
|
521
|
+
if (idx >= 0 && idx < options.length) return options[idx];
|
|
522
|
+
return options[0];
|
|
523
|
+
}
|
|
524
|
+
// src/install/symlink.ts
|
|
525
|
+
function detectConflict(claudeSkillsPath, expectedTarget) {
|
|
526
|
+
if (!existsSync$1(claudeSkillsPath) && !isSymlink(claudeSkillsPath)) return { kind: "none" };
|
|
527
|
+
if (isSymlink(claudeSkillsPath)) {
|
|
528
|
+
const target = symlinkTarget(claudeSkillsPath);
|
|
529
|
+
if (target === expectedTarget) return {
|
|
530
|
+
kind: "correct-symlink",
|
|
531
|
+
currentTarget: target ?? void 0
|
|
532
|
+
};
|
|
533
|
+
return {
|
|
534
|
+
kind: "wrong-symlink",
|
|
535
|
+
currentTarget: target ?? void 0
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
if (dirExists(claudeSkillsPath)) {
|
|
539
|
+
if (isEmptyDir(claudeSkillsPath)) return { kind: "empty-dir" };
|
|
540
|
+
return {
|
|
541
|
+
kind: "dir-with-content",
|
|
542
|
+
files: readdirSync$1(claudeSkillsPath)
|
|
543
|
+
};
|
|
544
|
+
}
|
|
545
|
+
return { kind: "none" };
|
|
546
|
+
}
|
|
547
|
+
const RELATIVE_TARGET = "../.agents/skills";
|
|
548
|
+
/**
|
|
549
|
+
* Pre-flight: inspect .claude/skills and resolve any prompt before anything is written.
|
|
550
|
+
* Dry-run never prompts and returns the action it would take ('skip' when it would ask).
|
|
551
|
+
*/
|
|
552
|
+
async function planLink(opts) {
|
|
553
|
+
const conflict = detectConflict(join(opts.root, ".claude", "skills"), RELATIVE_TARGET);
|
|
554
|
+
switch (conflict.kind) {
|
|
555
|
+
case "none": return "create";
|
|
556
|
+
case "correct-symlink": return "keep";
|
|
557
|
+
case "empty-dir": return "replace-empty";
|
|
558
|
+
case "wrong-symlink":
|
|
559
|
+
if (opts.dryRun || opts.force) return "repoint";
|
|
560
|
+
log.warn(`.claude/skills currently points to ${conflict.currentTarget}`);
|
|
561
|
+
return await confirm("Re-point to .agents/skills?") ? "repoint" : "skip";
|
|
562
|
+
case "dir-with-content": {
|
|
563
|
+
if (opts.dryRun) {
|
|
564
|
+
log.dim(` .claude/skills/ exists with files: ${conflict.files?.join(", ")}`);
|
|
565
|
+
log.dim(" Would prompt for action");
|
|
566
|
+
return "skip";
|
|
567
|
+
}
|
|
568
|
+
log.warn(`.claude/skills/ is a directory with: ${conflict.files?.join(", ")}`);
|
|
569
|
+
if (opts.force) return "merge";
|
|
570
|
+
const action = await choose("How to handle existing .claude/skills/?", [
|
|
571
|
+
"merge — copy files into .agents/skills/, then symlink",
|
|
572
|
+
"replace — delete .claude/skills/, create symlink",
|
|
573
|
+
"skip — leave .claude/skills/ as-is",
|
|
574
|
+
"abort — stop install"
|
|
575
|
+
]);
|
|
576
|
+
if (action.startsWith("merge")) return "merge";
|
|
577
|
+
if (action.startsWith("replace")) return "replace";
|
|
578
|
+
if (action.startsWith("abort")) return "abort";
|
|
579
|
+
return "skip";
|
|
580
|
+
}
|
|
581
|
+
default: return "skip";
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
/** Carry out a planned action. Returns whether anything changed. */
|
|
585
|
+
function applyLink(root, action, dryRun) {
|
|
586
|
+
const claudeSkillsPath = join(root, ".claude", "skills");
|
|
587
|
+
const agentsSkillsPath = join(root, ".agents", "skills");
|
|
588
|
+
switch (action) {
|
|
589
|
+
case "create":
|
|
590
|
+
if (dryRun) {
|
|
591
|
+
log.dim(` Would create symlink .claude/skills → ${RELATIVE_TARGET}`);
|
|
592
|
+
return false;
|
|
593
|
+
}
|
|
594
|
+
ensureDir(join(root, ".claude"));
|
|
595
|
+
createSymlink(RELATIVE_TARGET, claudeSkillsPath);
|
|
596
|
+
log.success(`Created symlink .claude/skills → ${RELATIVE_TARGET}`);
|
|
597
|
+
return true;
|
|
598
|
+
case "keep":
|
|
599
|
+
log.dim(" .claude/skills already linked correctly");
|
|
600
|
+
return false;
|
|
601
|
+
case "repoint":
|
|
602
|
+
if (dryRun) {
|
|
603
|
+
log.dim(` Would re-point symlink .claude/skills → ${RELATIVE_TARGET}`);
|
|
604
|
+
return false;
|
|
605
|
+
}
|
|
606
|
+
removePath(claudeSkillsPath);
|
|
607
|
+
createSymlink(RELATIVE_TARGET, claudeSkillsPath);
|
|
608
|
+
log.success(`Re-pointed symlink .claude/skills → ${RELATIVE_TARGET}`);
|
|
609
|
+
return true;
|
|
610
|
+
case "replace-empty":
|
|
611
|
+
if (dryRun) {
|
|
612
|
+
log.dim(" Would replace empty .claude/skills/ with symlink");
|
|
613
|
+
return false;
|
|
614
|
+
}
|
|
615
|
+
removeDir(claudeSkillsPath);
|
|
616
|
+
createSymlink(RELATIVE_TARGET, claudeSkillsPath);
|
|
617
|
+
log.success("Replaced empty .claude/skills/ with symlink");
|
|
618
|
+
return true;
|
|
619
|
+
case "merge":
|
|
620
|
+
case "replace":
|
|
621
|
+
if (dryRun) return false;
|
|
622
|
+
if (action === "merge") mergeIntoAgents(claudeSkillsPath, agentsSkillsPath);
|
|
623
|
+
removeDir(claudeSkillsPath);
|
|
624
|
+
createSymlink(RELATIVE_TARGET, claudeSkillsPath);
|
|
625
|
+
log.success(action === "merge" ? "Merged existing skills and created symlink" : "Replaced .claude/skills/ with symlink");
|
|
626
|
+
return true;
|
|
627
|
+
default:
|
|
628
|
+
if (action === "skip") log.dim(" Skipped symlink");
|
|
629
|
+
return false;
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
function mergeIntoAgents(source, dest) {
|
|
633
|
+
ensureDir(dest);
|
|
634
|
+
for (const entry of readdirSync$1(source)) {
|
|
635
|
+
const srcPath = join(source, entry);
|
|
636
|
+
const destPath = join(dest, entry);
|
|
637
|
+
if (!existsSync$1(destPath)) {
|
|
638
|
+
cpSync(srcPath, destPath, { recursive: true });
|
|
639
|
+
log.dim(` Merged ${entry}`);
|
|
640
|
+
} else log.dim(` Skipped ${entry} (already exists in .agents/skills/)`);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
// src/install/install.ts
|
|
644
|
+
/** Compute hook wiring results without writing. Throws SettingsParseError on malformed files. */
|
|
645
|
+
function planWiring(targets, opts) {
|
|
646
|
+
if (!opts.hook) return [];
|
|
647
|
+
const read = (f) => existsSync(f) ? readFileSync(f, "utf8") : "";
|
|
648
|
+
const cs = read(targets.claudeSettingsPath);
|
|
649
|
+
const cc = read(targets.codexConfigPath);
|
|
650
|
+
return [{
|
|
651
|
+
file: targets.claudeSettingsPath,
|
|
652
|
+
kind: "claude-hooks",
|
|
653
|
+
cur: cs,
|
|
654
|
+
out: mergeClaudeHooks(cs, hookSpecs("claude"), (s) => hookCommand("claude", targets, s.script), targets.claudeSettingsPath)
|
|
655
|
+
}, {
|
|
656
|
+
file: targets.codexConfigPath,
|
|
657
|
+
kind: "codex-block",
|
|
658
|
+
cur: cc,
|
|
659
|
+
out: upsertCodexBlock(cc, renderCodexHookBlock(hookSpecs("codex"), (s) => hookCommand("codex", targets, s.script)))
|
|
660
|
+
}];
|
|
661
|
+
}
|
|
662
|
+
/** Install every file under content/ plus agents and hook wiring. Returns whether anything changed. */
|
|
663
|
+
async function installAll(targets, writer, config, opts, link = "skip") {
|
|
664
|
+
const wiring = planWiring(targets, opts);
|
|
665
|
+
let changed = false;
|
|
666
|
+
const vars = { LW_HOME: targets.lwHomeRef };
|
|
667
|
+
const put = async (abs, content) => {
|
|
668
|
+
const outcome = await writer.writeFile(abs, content);
|
|
669
|
+
if (outcome === "created" || outcome === "updated") {
|
|
670
|
+
changed = true;
|
|
671
|
+
log.dim(` ${opts.dryRun ? "Would " : ""}${outcome === "created" ? "create" : "update"} ${abs}`);
|
|
672
|
+
} else if (outcome === "kept") log.warn(`Kept your modified ${abs}`);
|
|
673
|
+
};
|
|
674
|
+
for (const f of listContent("skills")) {
|
|
675
|
+
const text = renderTemplate(readContent(`skills/${f}`), vars);
|
|
676
|
+
await put(join(targets.skillsDir, f), text);
|
|
677
|
+
if (targets.claudeSkillsMode === "copy") await put(join(targets.claudeSkillsDir, f), text);
|
|
678
|
+
}
|
|
679
|
+
for (const f of listContent("agents")) {
|
|
680
|
+
if (!f.endsWith(".md")) continue;
|
|
681
|
+
const role = basename(f, ".md");
|
|
682
|
+
const src = parseAgentSource(role, renderTemplate(readContent(`agents/${f}`), vars));
|
|
683
|
+
await put(join(targets.claudeAgentsDir, `${src.name}.md`), renderClaudeAgent(src, config.models.claude[role] ?? "inherit"));
|
|
684
|
+
await put(join(targets.codexAgentsDir, `${src.name}.toml`), renderCodexAgent(src, config.models.codex[role] ?? "inherit", config.models.codex.effort?.[role]));
|
|
685
|
+
}
|
|
686
|
+
for (const sub of [
|
|
687
|
+
"hooks",
|
|
688
|
+
"prompts",
|
|
689
|
+
"schemas"
|
|
690
|
+
]) for (const f of listContent(sub)) {
|
|
691
|
+
const raw = readContent(`${sub}/${f}`);
|
|
692
|
+
await put(join(targets.lwHome, sub, f), sub === "hooks" ? raw : renderTemplate(raw, vars));
|
|
693
|
+
}
|
|
694
|
+
if (targets.claudeSkillsMode === "symlink") {
|
|
695
|
+
if (applyLink(targets.root, link, opts.dryRun)) changed = true;
|
|
696
|
+
}
|
|
697
|
+
if (!opts.hook) writer.carryPriorConfig();
|
|
698
|
+
for (const w of wiring) {
|
|
699
|
+
writer.recordConfig({
|
|
700
|
+
file: w.file,
|
|
701
|
+
kind: w.kind
|
|
702
|
+
});
|
|
703
|
+
if (w.out === w.cur) continue;
|
|
704
|
+
changed = true;
|
|
705
|
+
log.dim(` ${opts.dryRun ? "Would update" : "Updated"} ${w.file}`);
|
|
706
|
+
if (opts.dryRun) continue;
|
|
707
|
+
mkdirSync(dirname(w.file), { recursive: true });
|
|
708
|
+
writeFileSync(w.file, w.out, "utf8");
|
|
709
|
+
}
|
|
710
|
+
return changed;
|
|
711
|
+
}
|
|
712
|
+
// src/install/loopwright-dir.ts
|
|
713
|
+
/** Create `.loopwright/` with a self-ignoring .gitignore, a default config (only if absent), tasks/ and state/. */
|
|
714
|
+
function scaffoldLoopwrightDir(stateDir, dryRun) {
|
|
715
|
+
if (dryRun) return;
|
|
716
|
+
mkdirSync(join(stateDir, "tasks"), { recursive: true });
|
|
717
|
+
mkdirSync(join(stateDir, "state"), { recursive: true });
|
|
718
|
+
const ignore = join(stateDir, ".gitignore");
|
|
719
|
+
if (!existsSync(ignore)) writeFileSync(ignore, "*\n", "utf8");
|
|
720
|
+
const cfg = join(stateDir, "config.json");
|
|
721
|
+
if (!existsSync(cfg)) writeFileSync(cfg, JSON.stringify(DEFAULT_CONFIG, null, 2) + "\n", "utf8");
|
|
722
|
+
}
|
|
723
|
+
// src/commands/init.ts
|
|
724
|
+
function makeWriter(targets, opts) {
|
|
725
|
+
return new InstallWriter({
|
|
726
|
+
manifest: targets.manifestPath ? readManifest(targets.manifestPath) : null,
|
|
727
|
+
dryRun: opts.dryRun,
|
|
728
|
+
force: opts.force,
|
|
729
|
+
confirm: (q) => confirm(q, false),
|
|
730
|
+
toKey: (abs) => targets.mode === "repo" ? relative(targets.root, abs).split("\\").join("/") : abs,
|
|
731
|
+
mode: targets.mode
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
function runInit(opts) {
|
|
735
|
+
return runInstall(opts, "installed", true);
|
|
736
|
+
}
|
|
737
|
+
/** For update: reuse the hook/symlink options recorded in the manifest unless the flag was passed explicitly. */
|
|
738
|
+
function withRecordedOptions(opts, recorded) {
|
|
739
|
+
if (!recorded) return opts;
|
|
740
|
+
return {
|
|
741
|
+
...opts,
|
|
742
|
+
hook: opts.explicit?.hook ? opts.hook : recorded.hook,
|
|
743
|
+
symlink: opts.explicit?.symlink ? opts.symlink : recorded.symlink
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
/** Shared install flow for `init` and `update`. */
|
|
747
|
+
async function runInstall(opts, doneVerb, isInit = false) {
|
|
748
|
+
let targets;
|
|
749
|
+
try {
|
|
750
|
+
targets = resolveTargets(opts);
|
|
751
|
+
if (!isInit) {
|
|
752
|
+
opts = withRecordedOptions(opts, readManifest(targets.manifestPath)?.options);
|
|
753
|
+
targets = resolveTargets(opts);
|
|
754
|
+
}
|
|
755
|
+
} catch (e) {
|
|
756
|
+
if (e instanceof NotAGitRepoError) {
|
|
757
|
+
console.error("loopwright: not a git repository (run inside a repo, or use --global)");
|
|
758
|
+
return 1;
|
|
759
|
+
}
|
|
760
|
+
throw e;
|
|
761
|
+
}
|
|
762
|
+
try {
|
|
763
|
+
planWiring(targets, opts);
|
|
764
|
+
const link = targets.claudeSkillsMode === "symlink" ? await planLink({
|
|
765
|
+
root: targets.root,
|
|
766
|
+
dryRun: opts.dryRun,
|
|
767
|
+
force: opts.force
|
|
768
|
+
}) : "skip";
|
|
769
|
+
if (link === "abort") {
|
|
770
|
+
console.error("loopwright: aborted; nothing was written.");
|
|
771
|
+
return 1;
|
|
772
|
+
}
|
|
773
|
+
if (targets.mode === "repo" && targets.stateDir) scaffoldLoopwrightDir(targets.stateDir, opts.dryRun);
|
|
774
|
+
const config = readConfig(targets.configPath);
|
|
775
|
+
if (targets.mode === "global" && !opts.dryRun && !opts.force) {
|
|
776
|
+
log.info("The following changes would be made to your user configuration:");
|
|
777
|
+
const preview = {
|
|
778
|
+
...opts,
|
|
779
|
+
dryRun: true
|
|
780
|
+
};
|
|
781
|
+
if (!await installAll(targets, makeWriter(targets, preview), config, preview)) log.dim(" (no changes)");
|
|
782
|
+
if (!await confirm("Apply these changes to your user configuration?", false)) {
|
|
783
|
+
log.dim("No changes made.");
|
|
784
|
+
return 0;
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
const writer = makeWriter(targets, opts);
|
|
788
|
+
const changed = await installAll(targets, writer, config, opts, link);
|
|
789
|
+
if (!opts.dryRun) writeManifest(targets.manifestPath, {
|
|
790
|
+
...writer.result(),
|
|
791
|
+
options: {
|
|
792
|
+
hook: opts.hook,
|
|
793
|
+
symlink: opts.symlink
|
|
794
|
+
}
|
|
795
|
+
});
|
|
796
|
+
if (!changed) log.success("loopwright is already up to date.");
|
|
797
|
+
else if (!opts.dryRun) log.success(`loopwright ${doneVerb}.`);
|
|
798
|
+
if (targets.mode === "repo" && opts.hook) log.info("Codex: trust this project and approve loopwright hooks via /hooks.");
|
|
799
|
+
return 0;
|
|
800
|
+
} catch (e) {
|
|
801
|
+
if (e instanceof SettingsParseError) {
|
|
802
|
+
console.error(e.message);
|
|
803
|
+
return 1;
|
|
804
|
+
}
|
|
805
|
+
throw e;
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
// src/commands/update.ts
|
|
809
|
+
/** Re-apply the current version, honoring the existing manifest. */
|
|
810
|
+
function runUpdate(opts) {
|
|
811
|
+
return runInstall(opts, "updated");
|
|
812
|
+
}
|
|
813
|
+
// src/commands/uninstall.ts
|
|
814
|
+
function pruneEmptyDirs(start, stops) {
|
|
815
|
+
let dir = resolve(start);
|
|
816
|
+
while (!stops.has(dir)) {
|
|
817
|
+
try {
|
|
818
|
+
if (readdirSync(dir).length > 0) return;
|
|
819
|
+
rmdirSync(dir);
|
|
820
|
+
} catch {
|
|
821
|
+
return;
|
|
822
|
+
}
|
|
823
|
+
dir = dirname(dir);
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
async function runUninstall(opts) {
|
|
827
|
+
let targets;
|
|
828
|
+
try {
|
|
829
|
+
targets = resolveTargets(opts);
|
|
830
|
+
} catch (e) {
|
|
831
|
+
if (e instanceof NotAGitRepoError) {
|
|
832
|
+
console.error("loopwright: not a git repository (run inside a repo, or use --global)");
|
|
833
|
+
return 1;
|
|
834
|
+
}
|
|
835
|
+
throw e;
|
|
836
|
+
}
|
|
837
|
+
const manifest = readManifest(targets.manifestPath);
|
|
838
|
+
if (!manifest) {
|
|
839
|
+
console.log("loopwright: nothing to uninstall (no manifest found)");
|
|
840
|
+
return 0;
|
|
841
|
+
}
|
|
842
|
+
if (targets.mode === "global" && !opts.dryRun && !opts.force) {
|
|
843
|
+
if (!await confirm("Remove loopwright from your user configuration?", false)) {
|
|
844
|
+
log.dim("No changes made.");
|
|
845
|
+
return 0;
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
const dry = opts.dryRun;
|
|
849
|
+
const would = dry ? "Would remove" : "Removed";
|
|
850
|
+
const abs = (k) => isAbsolute(k) ? k : join(targets.root, k);
|
|
851
|
+
const dirsToPrune = /* @__PURE__ */ new Set();
|
|
852
|
+
for (const f of manifest.files) {
|
|
853
|
+
const p = abs(f.path);
|
|
854
|
+
if (!existsSync(p)) continue;
|
|
855
|
+
if (sha256(readFileSync(p, "utf-8")) !== f.sha256) {
|
|
856
|
+
log.warn(`kept modified file ${p}`);
|
|
857
|
+
continue;
|
|
858
|
+
}
|
|
859
|
+
log.dim(` ${would} ${p}`);
|
|
860
|
+
if (!dry) unlinkSync(p);
|
|
861
|
+
dirsToPrune.add(dirname(p));
|
|
862
|
+
}
|
|
863
|
+
const configs = /* @__PURE__ */ new Map();
|
|
864
|
+
for (const c of manifest.config) configs.set(resolve(abs(c.file)), c.kind);
|
|
865
|
+
configs.set(resolve(targets.claudeSettingsPath), "claude-hooks");
|
|
866
|
+
configs.set(resolve(targets.codexConfigPath), "codex-block");
|
|
867
|
+
for (const [file, kind] of configs) {
|
|
868
|
+
if (!existsSync(file)) continue;
|
|
869
|
+
const cur = readFileSync(file, "utf-8");
|
|
870
|
+
let out;
|
|
871
|
+
try {
|
|
872
|
+
out = kind === "claude-hooks" ? removeClaudeHooks(cur, "loopwright/hooks/", file) : removeCodexBlock(cur);
|
|
873
|
+
} catch (e) {
|
|
874
|
+
if (e instanceof SettingsParseError) {
|
|
875
|
+
log.warn(`left ${file} untouched: ${e.message}`);
|
|
876
|
+
continue;
|
|
877
|
+
}
|
|
878
|
+
throw e;
|
|
879
|
+
}
|
|
880
|
+
if (out === cur) continue;
|
|
881
|
+
log.dim(` ${dry ? "Would update" : "Updated"} ${file}`);
|
|
882
|
+
if (dry) continue;
|
|
883
|
+
writeFileSync(file, kind === "claude-hooks" && out.trim() === "{}" ? "{}\n" : out, "utf-8");
|
|
884
|
+
}
|
|
885
|
+
const link = targets.claudeSkillsDir;
|
|
886
|
+
if (lstatSync(link, { throwIfNoEntry: false })?.isSymbolicLink() && readlinkSync(link) === "../.agents/skills") {
|
|
887
|
+
log.dim(` ${would} ${link}`);
|
|
888
|
+
if (!dry) {
|
|
889
|
+
unlinkSync(link);
|
|
890
|
+
dirsToPrune.add(dirname(link));
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
if (!dry) {
|
|
894
|
+
rmSync(targets.manifestPath, { force: true });
|
|
895
|
+
dirsToPrune.add(dirname(targets.manifestPath));
|
|
896
|
+
const r = targets.root;
|
|
897
|
+
const stops = new Set([
|
|
898
|
+
r,
|
|
899
|
+
join(r, ".agents"),
|
|
900
|
+
join(r, ".claude"),
|
|
901
|
+
join(r, ".codex"),
|
|
902
|
+
join(r, ".config")
|
|
903
|
+
].map((p) => resolve(p)));
|
|
904
|
+
if (targets.stateDir) stops.add(resolve(targets.stateDir));
|
|
905
|
+
for (const d of dirsToPrune) pruneEmptyDirs(d, stops);
|
|
906
|
+
log.success("loopwright uninstalled.");
|
|
907
|
+
} else log.dim(` Would remove ${targets.manifestPath}`);
|
|
908
|
+
return 0;
|
|
909
|
+
}
|
|
910
|
+
// src/cli.ts
|
|
911
|
+
const HELP = `loopwright - agent workflow installer for Claude Code and Codex
|
|
912
|
+
|
|
913
|
+
Usage:
|
|
914
|
+
loopwright init Install the workflow into the current repo (or globally)
|
|
915
|
+
loopwright update Refresh installed files to the current version
|
|
916
|
+
(reuses init's --no-hook/--no-symlink unless passed again)
|
|
917
|
+
loopwright uninstall Remove installed files
|
|
918
|
+
|
|
919
|
+
Options:
|
|
920
|
+
--global Install into the user home instead of the current repo
|
|
921
|
+
--no-hook Skip hook wiring
|
|
922
|
+
--no-symlink Skip the .claude/skills symlink
|
|
923
|
+
--dry-run Show what would change without writing
|
|
924
|
+
--force Overwrite conflicting files without prompting
|
|
925
|
+
-v, --version Print version
|
|
926
|
+
-h, --help Show this help
|
|
927
|
+
`;
|
|
928
|
+
function readVersion() {
|
|
929
|
+
return JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version;
|
|
930
|
+
}
|
|
931
|
+
async function main(argv) {
|
|
932
|
+
let parsed;
|
|
933
|
+
try {
|
|
934
|
+
parsed = parseArgs({
|
|
935
|
+
args: argv,
|
|
936
|
+
allowPositionals: true,
|
|
937
|
+
strict: true,
|
|
938
|
+
options: {
|
|
939
|
+
global: {
|
|
940
|
+
type: "boolean",
|
|
941
|
+
default: false
|
|
942
|
+
},
|
|
943
|
+
"no-hook": {
|
|
944
|
+
type: "boolean",
|
|
945
|
+
default: false
|
|
946
|
+
},
|
|
947
|
+
"no-symlink": {
|
|
948
|
+
type: "boolean",
|
|
949
|
+
default: false
|
|
950
|
+
},
|
|
951
|
+
"dry-run": {
|
|
952
|
+
type: "boolean",
|
|
953
|
+
default: false
|
|
954
|
+
},
|
|
955
|
+
force: {
|
|
956
|
+
type: "boolean",
|
|
957
|
+
default: false
|
|
958
|
+
},
|
|
959
|
+
version: {
|
|
960
|
+
type: "boolean",
|
|
961
|
+
short: "v",
|
|
962
|
+
default: false
|
|
963
|
+
},
|
|
964
|
+
help: {
|
|
965
|
+
type: "boolean",
|
|
966
|
+
short: "h",
|
|
967
|
+
default: false
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
});
|
|
971
|
+
} catch (err) {
|
|
972
|
+
console.error(err.message);
|
|
973
|
+
return 1;
|
|
974
|
+
}
|
|
975
|
+
const { values, positionals } = parsed;
|
|
976
|
+
if (values.version) {
|
|
977
|
+
console.log(readVersion());
|
|
978
|
+
return 0;
|
|
979
|
+
}
|
|
980
|
+
const command = positionals[0];
|
|
981
|
+
if (values.help || !command) {
|
|
982
|
+
console.log(HELP);
|
|
983
|
+
return 0;
|
|
984
|
+
}
|
|
985
|
+
const opts = {
|
|
986
|
+
mode: values.global ? "global" : "repo",
|
|
987
|
+
cwd: process.cwd(),
|
|
988
|
+
home: homedir(),
|
|
989
|
+
dryRun: values["dry-run"],
|
|
990
|
+
force: values.force,
|
|
991
|
+
hook: !values["no-hook"],
|
|
992
|
+
symlink: !values["no-symlink"],
|
|
993
|
+
explicit: {
|
|
994
|
+
hook: values["no-hook"],
|
|
995
|
+
symlink: values["no-symlink"]
|
|
996
|
+
}
|
|
997
|
+
};
|
|
998
|
+
switch (command) {
|
|
999
|
+
case "init": return runInit(opts);
|
|
1000
|
+
case "update": return runUpdate(opts);
|
|
1001
|
+
case "uninstall": return runUninstall(opts);
|
|
1002
|
+
default:
|
|
1003
|
+
console.error(`Unknown command: ${command}\n\n${HELP}`);
|
|
1004
|
+
return 1;
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
main(process.argv.slice(2)).then((code) => {
|
|
1008
|
+
process.exitCode = code;
|
|
1009
|
+
}, (err) => {
|
|
1010
|
+
console.error(err instanceof Error ? err.message : err);
|
|
1011
|
+
process.exitCode = 1;
|
|
1012
|
+
});
|
|
1013
|
+
export { main };
|