@davidbyoung/software-factory 0.1.4 → 0.1.6
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 +9 -8
- package/dist/cli.mjs +13 -1
- package/dist/code-host/github-setup.mjs +1 -1
- package/dist/config.d.mts +74 -20
- package/dist/config.mjs +326 -76
- package/dist/deps.d.mts +6 -4
- package/dist/deps.mjs +29 -7
- package/dist/doctor.d.mts +3 -0
- package/dist/doctor.mjs +7 -4
- package/dist/explain.d.mts +13 -2
- package/dist/explain.mjs +58 -43
- package/dist/init.mjs +9 -7
- package/dist/lib/default-branch.d.mts +1 -1
- package/dist/lib/default-branch.mjs +2 -2
- package/dist/lib/refined-check.mjs +1 -1
- package/dist/lib/work-status.d.mts +2 -2
- package/dist/lib/work-status.mjs +4 -4
- package/dist/parts.d.mts +1 -1
- package/dist/parts.mjs +2 -2
- package/dist/preset-commands.d.mts +65 -0
- package/dist/preset-commands.mjs +1148 -0
- package/dist/preset.d.mts +169 -0
- package/dist/preset.mjs +550 -0
- package/dist/remove.mjs +34 -1
- package/dist/review-cost.mjs +1 -1
- package/dist/review-tools.mjs +3 -3
- package/dist/session-start.d.mts +3 -0
- package/dist/session-start.mjs +12 -1
- package/dist/sync.d.mts +42 -7
- package/dist/sync.mjs +166 -32
- package/docs/builds.md +12 -11
- package/docs/cli.md +27 -8
- package/docs/configuration.md +723 -714
- package/docs/design.md +1 -1
- package/docs/developing.md +20 -8
- package/docs/existing-repo.md +1 -1
- package/docs/github/by-hand.md +9 -9
- package/docs/github/reference.md +4 -19
- package/docs/github/setup.md +3 -3
- package/docs/glossary.md +18 -9
- package/docs/limitations.md +4 -1
- package/docs/plugins.md +36 -24
- package/docs/presets.md +410 -0
- package/docs/quick-start.md +21 -18
- package/docs/removing.md +1 -1
- package/docs/skills.md +28 -23
- package/docs/updating.md +22 -14
- package/docs/workflow.md +13 -14
- package/package.json +1 -1
- package/rules.md +1 -1
- package/settings.schema.json +60 -11
- package/skills/factory-build/SKILL.md +1 -1
- package/skills/factory-deps/SKILL.md +23 -12
- package/skills/factory-design/SKILL.md +2 -2
- package/skills/factory-refine/SKILL.md +2 -2
- package/skills/factory-resolve-conflicts/SKILL.md +6 -0
- package/workflows/factory-claude-review.yml +8 -3
|
@@ -0,0 +1,1148 @@
|
|
|
1
|
+
// The `factory preset` commands: add and remove a preset, choose between
|
|
2
|
+
// two presets where they disagree, check a preset before publishing it,
|
|
3
|
+
// and say what a preset update changes in this repository.
|
|
4
|
+
//
|
|
5
|
+
// factory preset add NAME [--yes] [--dry-run]
|
|
6
|
+
// factory preset remove NAME [--yes] [--dry-run]
|
|
7
|
+
// factory preset choices
|
|
8
|
+
// factory preset choose KEY PRESET
|
|
9
|
+
// factory preset check
|
|
10
|
+
// factory preset changes REF
|
|
11
|
+
import { execFileSync, spawnSync } from "node:child_process";
|
|
12
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync, } from "node:fs";
|
|
13
|
+
import { STATUS_CODES } from "node:http";
|
|
14
|
+
import { dirname, join, relative, sep } from "node:path";
|
|
15
|
+
import { filesDeps, parseChoices, repoRoot, resolveFiles, SETTINGS_FILE, SettingsError, } from "./config.mjs";
|
|
16
|
+
import { CHOICES_FILE, choicesText, findPackage, LOCK_FILE, OWN, PLUGIN_LISTS, presetFaults, PRESET_FILE, readLock, shipped, shipsText, splitName, stable, staleMessages, } from "./preset.mjs";
|
|
17
|
+
const isObj = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
18
|
+
const DEPENDABOT = ".github/dependabot.yml";
|
|
19
|
+
// ---------------------------------------------------------------- Values
|
|
20
|
+
/** A value as the summaries show it: a list's items joined, a plugin by its
|
|
21
|
+
* name. */
|
|
22
|
+
export function show(v) {
|
|
23
|
+
if (Array.isArray(v))
|
|
24
|
+
return v.map(show).join(", ") || "none";
|
|
25
|
+
if (isObj(v)) {
|
|
26
|
+
if (typeof v.agent === "string")
|
|
27
|
+
return v.agent;
|
|
28
|
+
if (typeof v.skill === "string")
|
|
29
|
+
return v.skill;
|
|
30
|
+
return JSON.stringify(v);
|
|
31
|
+
}
|
|
32
|
+
if (v === null || v === undefined)
|
|
33
|
+
return "none";
|
|
34
|
+
return String(v);
|
|
35
|
+
}
|
|
36
|
+
function getPath(raw, path) {
|
|
37
|
+
let at = raw;
|
|
38
|
+
for (const part of path) {
|
|
39
|
+
if (!isObj(at))
|
|
40
|
+
return undefined;
|
|
41
|
+
at = at[part];
|
|
42
|
+
}
|
|
43
|
+
return at;
|
|
44
|
+
}
|
|
45
|
+
/** The settings in effect with every default filled in, from the lines
|
|
46
|
+
* `factory config explain` prints. */
|
|
47
|
+
async function effective(r) {
|
|
48
|
+
const { explainLines, markerOf } = await import("./explain.mjs");
|
|
49
|
+
const { load } = await import("js-yaml");
|
|
50
|
+
const lines = await explainLines(r.settings, markerOf(r.resolution), r.exists);
|
|
51
|
+
return load(lines.join("\n")) ?? {};
|
|
52
|
+
}
|
|
53
|
+
const VALUE_LISTS = [
|
|
54
|
+
"pr-review.commands",
|
|
55
|
+
"auto-merge.deny",
|
|
56
|
+
"site.pages",
|
|
57
|
+
"site.widths",
|
|
58
|
+
"site.checks",
|
|
59
|
+
"site.hide",
|
|
60
|
+
];
|
|
61
|
+
/** The plugin list a key such as `plugins.build.reviewers.NAME` is in. */
|
|
62
|
+
const pluginListOf = (key) => PLUGIN_LISTS.find((p) => key.startsWith(`${p}.`));
|
|
63
|
+
/** The value a layer gives a key in a resolution: the one in effect, or the
|
|
64
|
+
* one it replaced. */
|
|
65
|
+
function valueOf(r, key, layer) {
|
|
66
|
+
const src = r.resolution.sources.get(key);
|
|
67
|
+
if (src?.from === layer)
|
|
68
|
+
return valueInEffect(r, key);
|
|
69
|
+
return r.resolution.overridden.get(key)?.find((o) => o.from === layer)?.value;
|
|
70
|
+
}
|
|
71
|
+
function valueInEffect(r, key) {
|
|
72
|
+
const list = pluginListOf(key);
|
|
73
|
+
if (list) {
|
|
74
|
+
const name = key.slice(list.length + 1);
|
|
75
|
+
const items = getPath(r.resolution.raw, list.split("."));
|
|
76
|
+
return Array.isArray(items)
|
|
77
|
+
? items.find((i) => isObj(i) && (i.agent ?? i.skill) === name)
|
|
78
|
+
: undefined;
|
|
79
|
+
}
|
|
80
|
+
if (key.startsWith(".claude/"))
|
|
81
|
+
return r.resolution.files.get(key)?.source;
|
|
82
|
+
return getPath(r.resolution.raw, key.split("."));
|
|
83
|
+
}
|
|
84
|
+
function summary(head, groups, files) {
|
|
85
|
+
const shown = groups.filter((g) => g.rows.length);
|
|
86
|
+
const width = Math.max(0, ...shown.flatMap((g) => g.rows.map((r) => r.key.length))) + 3;
|
|
87
|
+
return [
|
|
88
|
+
head,
|
|
89
|
+
"",
|
|
90
|
+
...shown.flatMap((g) => [
|
|
91
|
+
` ${g.title}`,
|
|
92
|
+
...g.rows.map((r) => ` ${r.key.padEnd(width)}${r.text}`),
|
|
93
|
+
]),
|
|
94
|
+
...(files.length ? ["", ...files.map((f) => ` ${f}`)] : []),
|
|
95
|
+
];
|
|
96
|
+
}
|
|
97
|
+
/** A question asked until the answer is one of `options`. */
|
|
98
|
+
async function choose(d, question, options) {
|
|
99
|
+
for (;;) {
|
|
100
|
+
const answer = (await d.ask(question)).trim();
|
|
101
|
+
if (options.includes(answer))
|
|
102
|
+
return answer;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/** `Apply? [y/N]`: anything but `y` is no. */
|
|
106
|
+
async function apply(d) {
|
|
107
|
+
return (await d.ask("Apply? [y/N] ")).trim() === "y";
|
|
108
|
+
}
|
|
109
|
+
// ---------------------------------------------------------------- Files
|
|
110
|
+
async function yamlDoc(text) {
|
|
111
|
+
const { parseDocument } = await import("yaml");
|
|
112
|
+
return parseDocument(text);
|
|
113
|
+
}
|
|
114
|
+
/** `.claude/factory.yaml` with NAME added, quoted, at the end of
|
|
115
|
+
* `presets`, which goes after `repository` when it is new. */
|
|
116
|
+
async function withPreset(text, name) {
|
|
117
|
+
const { Scalar, isMap, isSeq, YAMLSeq, Pair } = await import("yaml");
|
|
118
|
+
const doc = await yamlDoc(text);
|
|
119
|
+
const item = new Scalar(name);
|
|
120
|
+
item.type = Scalar.QUOTE_DOUBLE;
|
|
121
|
+
const list = doc.get("presets");
|
|
122
|
+
if (isSeq(list))
|
|
123
|
+
list.items.push(item);
|
|
124
|
+
else if (isMap(doc.contents)) {
|
|
125
|
+
const seq = new YAMLSeq();
|
|
126
|
+
seq.items.push(item);
|
|
127
|
+
const at = doc.contents.items.findIndex((p) => String(p.key?.value) === "repository");
|
|
128
|
+
doc.contents.items.splice(at + 1, 0, new Pair(new Scalar("presets"), seq));
|
|
129
|
+
}
|
|
130
|
+
return doc.toString({ flowCollectionPadding: false, lineWidth: 0 });
|
|
131
|
+
}
|
|
132
|
+
/** `.claude/factory.yaml` without NAME in `presets`, and without `presets`
|
|
133
|
+
* once it is empty. */
|
|
134
|
+
async function withoutPreset(text, name) {
|
|
135
|
+
const { isSeq, isScalar } = await import("yaml");
|
|
136
|
+
const doc = await yamlDoc(text);
|
|
137
|
+
const list = doc.get("presets");
|
|
138
|
+
if (isSeq(list)) {
|
|
139
|
+
list.items = list.items.filter((i) => !(isScalar(i) && i.value === name));
|
|
140
|
+
if (!list.items.length)
|
|
141
|
+
doc.delete("presets");
|
|
142
|
+
}
|
|
143
|
+
return doc.toString({ flowCollectionPadding: false, lineWidth: 0 });
|
|
144
|
+
}
|
|
145
|
+
/** `.claude/factory.yaml` without its own value for `key`, and without a
|
|
146
|
+
* block that leaves empty. */
|
|
147
|
+
async function withoutKey(text, key) {
|
|
148
|
+
const { isMap, isSeq } = await import("yaml");
|
|
149
|
+
const doc = await yamlDoc(text);
|
|
150
|
+
const list = pluginListOf(key);
|
|
151
|
+
if (list) {
|
|
152
|
+
const name = key.slice(list.length + 1);
|
|
153
|
+
const seq = doc.getIn(list.split("."));
|
|
154
|
+
if (isSeq(seq))
|
|
155
|
+
seq.items = seq.items.filter((i) => {
|
|
156
|
+
const v = i.toJSON?.();
|
|
157
|
+
return !(isObj(v) && (v.agent ?? v.skill) === name);
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
else
|
|
161
|
+
doc.deleteIn(key.split("."));
|
|
162
|
+
const path = (list ?? key).split(".");
|
|
163
|
+
for (let n = path.length - 1; n > 0; n--) {
|
|
164
|
+
const parent = doc.getIn(path.slice(0, n));
|
|
165
|
+
if (isMap(parent) && parent.items.length === 0)
|
|
166
|
+
doc.deleteIn(path.slice(0, n));
|
|
167
|
+
else
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
return doc.toString({ flowCollectionPadding: false, lineWidth: 0 });
|
|
171
|
+
}
|
|
172
|
+
/** `.claude/factory.yaml` with `value` as its own for `key`. */
|
|
173
|
+
async function withKey(text, key, value) {
|
|
174
|
+
const { isSeq } = await import("yaml");
|
|
175
|
+
const doc = await yamlDoc(text);
|
|
176
|
+
const list = pluginListOf(key);
|
|
177
|
+
if (list) {
|
|
178
|
+
const seq = doc.getIn(list.split("."));
|
|
179
|
+
if (isSeq(seq))
|
|
180
|
+
seq.items.push(doc.createNode(value));
|
|
181
|
+
else
|
|
182
|
+
doc.setIn(list.split("."), doc.createNode([value]));
|
|
183
|
+
}
|
|
184
|
+
else
|
|
185
|
+
doc.setIn(key.split("."), doc.createNode(value));
|
|
186
|
+
return doc.toString({ flowCollectionPadding: false, lineWidth: 0 });
|
|
187
|
+
}
|
|
188
|
+
// ---------------------------------------------------------------- npm
|
|
189
|
+
/** The registry npm installs a package from. */
|
|
190
|
+
function registryOf(d, pkg) {
|
|
191
|
+
const scope = pkg.startsWith("@") ? pkg.split("/")[0] : null;
|
|
192
|
+
const get = (key) => {
|
|
193
|
+
const r = d.npm(["config", "get", key]);
|
|
194
|
+
const v = r.stdout.trim();
|
|
195
|
+
return r.status === 0 && v && v !== "undefined" ? v : null;
|
|
196
|
+
};
|
|
197
|
+
return (scope && get(`${scope}:registry`)) ?? get("registry") ?? "";
|
|
198
|
+
}
|
|
199
|
+
/** npm's failure in the factory's words. */
|
|
200
|
+
export function installError(name, stderr, registry) {
|
|
201
|
+
const code = /code E(\d{3})\b/.exec(stderr)?.[1];
|
|
202
|
+
if (!code) {
|
|
203
|
+
const last = stderr
|
|
204
|
+
.split("\n")
|
|
205
|
+
.filter((l) => /^npm (error|ERR!)/.test(l))
|
|
206
|
+
.at(-1) ??
|
|
207
|
+
stderr.trim().split("\n").at(-1) ??
|
|
208
|
+
"";
|
|
209
|
+
return [`npm couldn't install ${name}:`, last];
|
|
210
|
+
}
|
|
211
|
+
const lines = [
|
|
212
|
+
`npm couldn't install ${name}: ${registry} answered ${code} ${STATUS_CODES[Number(code)] ?? ""}.`.replace(" .", "."),
|
|
213
|
+
];
|
|
214
|
+
if (["401", "403", "404"].includes(code))
|
|
215
|
+
lines.push(`If it's a private package, give this machine a token for ${registry}, as .npmrc expects.`);
|
|
216
|
+
return lines;
|
|
217
|
+
}
|
|
218
|
+
function inPackageJson(d, pkg) {
|
|
219
|
+
const text = d.read("package.json");
|
|
220
|
+
if (!text)
|
|
221
|
+
return false;
|
|
222
|
+
const m = JSON.parse(text);
|
|
223
|
+
return Boolean(m.dependencies?.[pkg] ?? m.devDependencies?.[pkg]);
|
|
224
|
+
}
|
|
225
|
+
// ---------------------------------------------------------------- Dependabot
|
|
226
|
+
async function loadYaml(text) {
|
|
227
|
+
if (text === null)
|
|
228
|
+
return null;
|
|
229
|
+
const { load } = await import("js-yaml");
|
|
230
|
+
try {
|
|
231
|
+
return load(text);
|
|
232
|
+
}
|
|
233
|
+
catch {
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
/** Whether a group in `.github/dependabot.yml` has the package as its only
|
|
238
|
+
* pattern. */
|
|
239
|
+
export async function hasOwnGroup(text, pkg) {
|
|
240
|
+
const o = await loadYaml(text);
|
|
241
|
+
const updates = isObj(o) && Array.isArray(o.updates) ? o.updates : [];
|
|
242
|
+
return updates.some((u) => isObj(u) &&
|
|
243
|
+
isObj(u.groups) &&
|
|
244
|
+
Object.values(u.groups).some((g) => isObj(g) &&
|
|
245
|
+
Array.isArray(g.patterns) &&
|
|
246
|
+
g.patterns.length === 1 &&
|
|
247
|
+
g.patterns[0] === pkg));
|
|
248
|
+
}
|
|
249
|
+
/** The registry `.npmrc` sends a package's scope to, or null. */
|
|
250
|
+
export function npmrcRegistry(text, pkg) {
|
|
251
|
+
if (!text || !pkg.startsWith("@"))
|
|
252
|
+
return null;
|
|
253
|
+
const scope = pkg.split("/")[0];
|
|
254
|
+
for (const line of text.split("\n")) {
|
|
255
|
+
const m = /^\s*(@[^:\s]+):registry\s*=\s*(\S+)\s*$/.exec(line);
|
|
256
|
+
if (m && m[1] === scope)
|
|
257
|
+
return m[2];
|
|
258
|
+
}
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
const bare = (url) => url.replace(/\/+$/, "");
|
|
262
|
+
/** The lines `preset add` ends with for what Dependabot still needs. */
|
|
263
|
+
export async function nextLines(dependabot, npmrc, pkg) {
|
|
264
|
+
if (dependabot === null)
|
|
265
|
+
return [];
|
|
266
|
+
const out = [];
|
|
267
|
+
if (!(await hasOwnGroup(dependabot, pkg)))
|
|
268
|
+
out.push(`Next: in ${DEPENDABOT}, give ${pkg} a group of its own, so its updates arrive as their own pull requests.`);
|
|
269
|
+
const registry = npmrcRegistry(npmrc, pkg);
|
|
270
|
+
if (registry) {
|
|
271
|
+
const o = await loadYaml(dependabot);
|
|
272
|
+
const known = isObj(o) && isObj(o.registries)
|
|
273
|
+
? Object.values(o.registries).map((r) => isObj(r) && typeof r.url === "string" ? bare(r.url) : "")
|
|
274
|
+
: [];
|
|
275
|
+
if (!known.includes(bare(registry)))
|
|
276
|
+
out.push(`Next: in ${DEPENDABOT}, add ${registry} under registries, so Dependabot sees new versions of ${pkg}.`);
|
|
277
|
+
}
|
|
278
|
+
return out;
|
|
279
|
+
}
|
|
280
|
+
/** Runs the sync, saying the choices file was written, where it was, just
|
|
281
|
+
* before the lock, as the sync would have said it. */
|
|
282
|
+
async function syncing(d, wroteChoices) {
|
|
283
|
+
const lines = [];
|
|
284
|
+
const code = await d.sync((l) => lines.push(l));
|
|
285
|
+
if (wroteChoices) {
|
|
286
|
+
const at = lines.indexOf(`wrote ${LOCK_FILE}`);
|
|
287
|
+
lines.splice(at < 0 ? lines.length : at, 0, `wrote ${CHOICES_FILE}`);
|
|
288
|
+
}
|
|
289
|
+
for (const line of lines)
|
|
290
|
+
d.out(line);
|
|
291
|
+
return code;
|
|
292
|
+
}
|
|
293
|
+
// ---------------------------------------------------------------- add
|
|
294
|
+
const PAREN = "this preset";
|
|
295
|
+
export async function add(argv, d) {
|
|
296
|
+
const name = argv.find((a) => !a.startsWith("--"));
|
|
297
|
+
const yes = argv.includes("--yes");
|
|
298
|
+
const dry = argv.includes("--dry-run");
|
|
299
|
+
if (!name) {
|
|
300
|
+
d.err("Usage: factory preset add NAME [--yes] [--dry-run]");
|
|
301
|
+
return 2;
|
|
302
|
+
}
|
|
303
|
+
const ownText = d.read(SETTINGS_FILE);
|
|
304
|
+
if (ownText === null) {
|
|
305
|
+
d.err(`${SETTINGS_FILE} is missing. Run: npx --no-install factory init`);
|
|
306
|
+
return 1;
|
|
307
|
+
}
|
|
308
|
+
const own = (await loadYaml(ownText));
|
|
309
|
+
const listed = Array.isArray(own?.presets) ? own.presets.map(String) : [];
|
|
310
|
+
if (listed.includes(name)) {
|
|
311
|
+
d.err(`${name} is already in presets`);
|
|
312
|
+
return 1;
|
|
313
|
+
}
|
|
314
|
+
const { pkg } = splitName(name);
|
|
315
|
+
const before = inPackageJson(d, pkg);
|
|
316
|
+
const undo = () => {
|
|
317
|
+
if (!before)
|
|
318
|
+
d.npm(["uninstall", pkg]);
|
|
319
|
+
};
|
|
320
|
+
if (!before) {
|
|
321
|
+
const r = d.npm(["install", "--save-dev", pkg]);
|
|
322
|
+
if (r.status !== 0) {
|
|
323
|
+
for (const line of installError(name, r.stderr, registryOf(d, pkg)))
|
|
324
|
+
d.err(line);
|
|
325
|
+
return 1;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
const choicesBefore = d.read(CHOICES_FILE);
|
|
329
|
+
let now;
|
|
330
|
+
let after;
|
|
331
|
+
try {
|
|
332
|
+
now = await d.resolve(ownText, choicesBefore);
|
|
333
|
+
after = await d.resolve(await withPreset(ownText, name), choicesBefore);
|
|
334
|
+
}
|
|
335
|
+
catch (e) {
|
|
336
|
+
undo();
|
|
337
|
+
d.err(e instanceof Error ? e.message : String(e));
|
|
338
|
+
return 1;
|
|
339
|
+
}
|
|
340
|
+
const version = after.presets.find((p) => p.name === name).version;
|
|
341
|
+
const [effNow, effAfter] = await Promise.all([
|
|
342
|
+
effective(now),
|
|
343
|
+
effective(after),
|
|
344
|
+
]);
|
|
345
|
+
const added = [];
|
|
346
|
+
const replacesDefault = [];
|
|
347
|
+
const disagrees = new Map();
|
|
348
|
+
const yours = [];
|
|
349
|
+
const questions = [];
|
|
350
|
+
for (const [key, setters] of after.resolution.setBy) {
|
|
351
|
+
if (!setters.has(name) || key.startsWith(".claude/"))
|
|
352
|
+
continue;
|
|
353
|
+
const mine = valueOf(after, key, name);
|
|
354
|
+
const src = after.resolution.sources.get(key);
|
|
355
|
+
const list = pluginListOf(key);
|
|
356
|
+
if (src?.from === OWN) {
|
|
357
|
+
const theirs = valueInEffect(after, key);
|
|
358
|
+
if (stable(theirs) === stable(mine))
|
|
359
|
+
continue;
|
|
360
|
+
yours.push({
|
|
361
|
+
key: list ?? key,
|
|
362
|
+
text: `yours: ${show(theirs)} ${PAREN}'s: ${show(mine)}`,
|
|
363
|
+
});
|
|
364
|
+
questions.push({
|
|
365
|
+
key,
|
|
366
|
+
kind: "yours",
|
|
367
|
+
other: OWN,
|
|
368
|
+
mine: show(mine),
|
|
369
|
+
theirs: show(theirs),
|
|
370
|
+
});
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
const before = now.resolution.sources.get(key);
|
|
374
|
+
if (before && before.from !== OWN && setters.size > 1) {
|
|
375
|
+
const theirs = valueOf(now, key, before.from);
|
|
376
|
+
if (stable(theirs) === stable(mine))
|
|
377
|
+
continue;
|
|
378
|
+
const rows = disagrees.get(before.from) ?? [];
|
|
379
|
+
rows.push({
|
|
380
|
+
key: list ?? key,
|
|
381
|
+
text: `${before.from}: ${show(theirs)} ${PAREN}: ${show(mine)}`,
|
|
382
|
+
});
|
|
383
|
+
disagrees.set(before.from, rows);
|
|
384
|
+
questions.push({
|
|
385
|
+
key,
|
|
386
|
+
kind: "disagree",
|
|
387
|
+
other: before.from,
|
|
388
|
+
mine: show(mine),
|
|
389
|
+
theirs: show(theirs),
|
|
390
|
+
});
|
|
391
|
+
continue;
|
|
392
|
+
}
|
|
393
|
+
if (list) {
|
|
394
|
+
added.push({ key: list, text: `+ ${show(mine)}` });
|
|
395
|
+
continue;
|
|
396
|
+
}
|
|
397
|
+
const was = getPath(effNow, key.split("."));
|
|
398
|
+
if (stable(was) === stable(mine))
|
|
399
|
+
continue;
|
|
400
|
+
replacesDefault.push({ key, text: `${show(was)} → ${show(mine)}` });
|
|
401
|
+
}
|
|
402
|
+
for (const path of VALUE_LISTS) {
|
|
403
|
+
const was = getPath(effNow, path.split("."));
|
|
404
|
+
const is = getPath(effAfter, path.split("."));
|
|
405
|
+
if (!Array.isArray(is))
|
|
406
|
+
continue;
|
|
407
|
+
const had = new Set((Array.isArray(was) ? was : []).map(stable));
|
|
408
|
+
for (const item of is)
|
|
409
|
+
if (!had.has(stable(item)) &&
|
|
410
|
+
after.resolution.sources.get(`${path}[${stable(item)}]`)?.from === name)
|
|
411
|
+
added.push({ key: path, text: `+ ${show(item)}` });
|
|
412
|
+
}
|
|
413
|
+
const locked = await lockFiles(d);
|
|
414
|
+
const writes = [...after.resolution.files]
|
|
415
|
+
.filter(([to, f]) => f.from === name && !now.resolution.files.has(to))
|
|
416
|
+
.filter(([to]) => !existsSync(join(d.root, to)) || locked.includes(to))
|
|
417
|
+
.map(([to]) => `writes ${to}`);
|
|
418
|
+
for (const line of summary(`${name} ${version} would change these settings here:`, [
|
|
419
|
+
{ title: "added", rows: added },
|
|
420
|
+
{ title: "replaces the default", rows: replacesDefault },
|
|
421
|
+
...[...disagrees].map(([other, rows]) => ({
|
|
422
|
+
title: `disagrees with ${other}`,
|
|
423
|
+
rows,
|
|
424
|
+
})),
|
|
425
|
+
{ title: `replaced by your ${SETTINGS_FILE}`, rows: yours },
|
|
426
|
+
], writes))
|
|
427
|
+
d.out(line);
|
|
428
|
+
if (dry) {
|
|
429
|
+
undo();
|
|
430
|
+
return 0;
|
|
431
|
+
}
|
|
432
|
+
if (!yes && !d.terminal) {
|
|
433
|
+
undo();
|
|
434
|
+
d.out("");
|
|
435
|
+
d.out("Run it in a terminal to choose, or add --yes to apply it.");
|
|
436
|
+
return 1;
|
|
437
|
+
}
|
|
438
|
+
const choices = parseChoices(choicesBefore, (await import("js-yaml")).load);
|
|
439
|
+
let text = await withPreset(ownText, name);
|
|
440
|
+
if (!yes) {
|
|
441
|
+
if (questions.length) {
|
|
442
|
+
d.out("");
|
|
443
|
+
d.out("Choose:");
|
|
444
|
+
}
|
|
445
|
+
for (const q of questions) {
|
|
446
|
+
const k = q.key;
|
|
447
|
+
if (q.kind === "disagree") {
|
|
448
|
+
const options = q.mine === q.theirs ? ["1", "2"] : [q.mine, q.theirs];
|
|
449
|
+
const a = await choose(d, ` ${k}: ${q.mine} from ${PAREN}, or ${q.theirs} from ${q.other}? [${options.join("/")}] `, options);
|
|
450
|
+
choices[k] = a === options[0] ? name : q.other;
|
|
451
|
+
}
|
|
452
|
+
else {
|
|
453
|
+
const a = await choose(d, ` ${k}: keep yours, or drop it and use ${PAREN}'s? [keep/drop] `, ["keep", "drop"]);
|
|
454
|
+
if (a === "drop")
|
|
455
|
+
text = await withoutKey(text, k);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
d.out("");
|
|
459
|
+
if (!(await apply(d))) {
|
|
460
|
+
undo();
|
|
461
|
+
d.out("Nothing changed.");
|
|
462
|
+
return 0;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
d.write(SETTINGS_FILE, text);
|
|
466
|
+
d.out(`added ${name} to presets in ${SETTINGS_FILE}`);
|
|
467
|
+
const chosen = choicesText(choices);
|
|
468
|
+
if (chosen !== choicesBefore)
|
|
469
|
+
d.write(CHOICES_FILE, chosen);
|
|
470
|
+
const code = await syncing(d, chosen !== choicesBefore && chosen !== null);
|
|
471
|
+
for (const line of await nextLines(d.read(DEPENDABOT), d.read(".npmrc"), pkg))
|
|
472
|
+
d.out(line);
|
|
473
|
+
return code;
|
|
474
|
+
}
|
|
475
|
+
/** The files the lock lists. */
|
|
476
|
+
async function lockFiles(d) {
|
|
477
|
+
const text = d.read(LOCK_FILE);
|
|
478
|
+
if (!text)
|
|
479
|
+
return [];
|
|
480
|
+
const { load } = await import("js-yaml");
|
|
481
|
+
return readLock(text, load).files;
|
|
482
|
+
}
|
|
483
|
+
// ---------------------------------------------------------------- remove
|
|
484
|
+
export async function remove(argv, d) {
|
|
485
|
+
const name = argv.find((a) => !a.startsWith("--"));
|
|
486
|
+
const yes = argv.includes("--yes");
|
|
487
|
+
const dry = argv.includes("--dry-run");
|
|
488
|
+
if (!name) {
|
|
489
|
+
d.err("Usage: factory preset remove NAME [--yes] [--dry-run]");
|
|
490
|
+
return 2;
|
|
491
|
+
}
|
|
492
|
+
const ownText = d.read(SETTINGS_FILE) ?? "";
|
|
493
|
+
const own = (await loadYaml(ownText));
|
|
494
|
+
const listed = Array.isArray(own?.presets) ? own.presets.map(String) : [];
|
|
495
|
+
if (!listed.includes(name)) {
|
|
496
|
+
d.err(`${name} is not in presets`);
|
|
497
|
+
return 1;
|
|
498
|
+
}
|
|
499
|
+
const { load } = await import("js-yaml");
|
|
500
|
+
const choicesBefore = d.read(CHOICES_FILE);
|
|
501
|
+
const choices = parseChoices(choicesBefore, load);
|
|
502
|
+
const kept = Object.fromEntries(Object.entries(choices).filter(([, v]) => v !== name));
|
|
503
|
+
const dropped = Object.keys(choices).length - Object.keys(kept).length;
|
|
504
|
+
let now;
|
|
505
|
+
let after;
|
|
506
|
+
const without = await withoutPreset(ownText, name);
|
|
507
|
+
try {
|
|
508
|
+
now = await d.resolve(ownText, choicesBefore);
|
|
509
|
+
after = await d.resolve(without, choicesText(kept));
|
|
510
|
+
}
|
|
511
|
+
catch (e) {
|
|
512
|
+
d.err(e instanceof Error ? e.message : String(e));
|
|
513
|
+
return 1;
|
|
514
|
+
}
|
|
515
|
+
const effAfter = await effective(after);
|
|
516
|
+
const removed = [];
|
|
517
|
+
const toDefault = [];
|
|
518
|
+
const toOther = [];
|
|
519
|
+
const chosenAgainst = [];
|
|
520
|
+
const questions = [];
|
|
521
|
+
for (const [key, src] of now.resolution.sources) {
|
|
522
|
+
// A list's own source is its items', which have lines of their own.
|
|
523
|
+
if (VALUE_LISTS.includes(key) || PLUGIN_LISTS.includes(key))
|
|
524
|
+
continue;
|
|
525
|
+
if (src.from !== name || key.startsWith(".claude/"))
|
|
526
|
+
continue;
|
|
527
|
+
const list = pluginListOf(key);
|
|
528
|
+
const old = valueInEffect(now, key);
|
|
529
|
+
const nextSrc = after.resolution.sources.get(key);
|
|
530
|
+
if (key.includes("[")) {
|
|
531
|
+
const path = key.slice(0, key.indexOf("["));
|
|
532
|
+
if (!after.resolution.sources.has(key))
|
|
533
|
+
removed.push({
|
|
534
|
+
key: path,
|
|
535
|
+
text: `− ${show(JSON.parse(key.slice(path.length + 1, -1)))}`,
|
|
536
|
+
});
|
|
537
|
+
continue;
|
|
538
|
+
}
|
|
539
|
+
if (!nextSrc) {
|
|
540
|
+
if (list)
|
|
541
|
+
removed.push({ key: list, text: `− ${show(old)}` });
|
|
542
|
+
else
|
|
543
|
+
toDefault.push({
|
|
544
|
+
key,
|
|
545
|
+
text: `${show(old)} → ${show(getPath(effAfter, key.split(".")))}`,
|
|
546
|
+
});
|
|
547
|
+
continue;
|
|
548
|
+
}
|
|
549
|
+
const next = valueInEffect(after, key);
|
|
550
|
+
if (stable(next) === stable(old))
|
|
551
|
+
continue;
|
|
552
|
+
const row = {
|
|
553
|
+
key: list ?? key,
|
|
554
|
+
text: `${show(old)} → ${show(next)}, from ${nextSrc.from}`,
|
|
555
|
+
};
|
|
556
|
+
if (src.state === "chosen") {
|
|
557
|
+
chosenAgainst.push(row);
|
|
558
|
+
questions.push({ key, old, next, from: nextSrc.from });
|
|
559
|
+
}
|
|
560
|
+
else
|
|
561
|
+
toOther.push(row);
|
|
562
|
+
}
|
|
563
|
+
const locked = await lockFiles(d);
|
|
564
|
+
const deletes = [...now.resolution.files]
|
|
565
|
+
.filter(([to, f]) => f.from === name && !after.resolution.files.has(to))
|
|
566
|
+
.filter(([to]) => locked.includes(to))
|
|
567
|
+
.map(([to]) => `deletes ${to}`);
|
|
568
|
+
for (const line of summary(`Removing ${name} would change these settings here:`, [
|
|
569
|
+
{ title: "removed", rows: removed },
|
|
570
|
+
{ title: "back to the default", rows: toDefault },
|
|
571
|
+
{ title: "back to another preset's value", rows: toOther },
|
|
572
|
+
{ title: "back to a value you chose against", rows: chosenAgainst },
|
|
573
|
+
], deletes))
|
|
574
|
+
d.out(line);
|
|
575
|
+
if (dry)
|
|
576
|
+
return 0;
|
|
577
|
+
if (!yes && !d.terminal) {
|
|
578
|
+
d.out("");
|
|
579
|
+
d.out("Run it in a terminal to choose, or add --yes to apply it.");
|
|
580
|
+
return 1;
|
|
581
|
+
}
|
|
582
|
+
let text = without;
|
|
583
|
+
if (!yes) {
|
|
584
|
+
if (questions.length) {
|
|
585
|
+
d.out("");
|
|
586
|
+
d.out("Choose:");
|
|
587
|
+
}
|
|
588
|
+
for (const q of questions) {
|
|
589
|
+
const [use, keep] = [show(q.next), show(q.old)];
|
|
590
|
+
const options = use === keep ? ["1", "2"] : [use, keep];
|
|
591
|
+
const a = await choose(d, ` ${q.key}: use ${use} from ${q.from}, or keep ${keep} in your ${SETTINGS_FILE}? [${options.join("/")}] `, options);
|
|
592
|
+
if (a === options[1])
|
|
593
|
+
text = await withKey(text, q.key, q.old);
|
|
594
|
+
}
|
|
595
|
+
d.out("");
|
|
596
|
+
if (!(await apply(d))) {
|
|
597
|
+
d.out("Nothing changed.");
|
|
598
|
+
return 0;
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
d.write(SETTINGS_FILE, text);
|
|
602
|
+
d.out(`removed ${name} from presets in ${SETTINGS_FILE}`);
|
|
603
|
+
if (dropped > 0) {
|
|
604
|
+
d.write(CHOICES_FILE, choicesText(kept));
|
|
605
|
+
d.out(`removed ${dropped} choice${dropped === 1 ? "" : "s"} from ${CHOICES_FILE}`);
|
|
606
|
+
}
|
|
607
|
+
const { pkg } = splitName(name);
|
|
608
|
+
if (!listed.some((n) => n !== name && splitName(n).pkg === pkg))
|
|
609
|
+
d.npm(["uninstall", pkg]);
|
|
610
|
+
return syncing(d, false);
|
|
611
|
+
}
|
|
612
|
+
// ---------------------------------------------------------------- choices
|
|
613
|
+
export async function choices(_argv, d) {
|
|
614
|
+
const ownText = d.read(SETTINGS_FILE) ?? "";
|
|
615
|
+
const before = d.read(CHOICES_FILE);
|
|
616
|
+
let r;
|
|
617
|
+
try {
|
|
618
|
+
r = await d.resolve(ownText, before);
|
|
619
|
+
}
|
|
620
|
+
catch (e) {
|
|
621
|
+
d.err(e instanceof Error ? e.message : String(e));
|
|
622
|
+
return 1;
|
|
623
|
+
}
|
|
624
|
+
// What is not chosen yet comes first: it is what a choice is wanted for.
|
|
625
|
+
const list = [
|
|
626
|
+
...r.resolution.disagreements.filter((g) => !g.chosen),
|
|
627
|
+
...r.resolution.disagreements.filter((g) => g.chosen),
|
|
628
|
+
];
|
|
629
|
+
if (!list.length) {
|
|
630
|
+
d.out("No two presets disagree here.");
|
|
631
|
+
return 0;
|
|
632
|
+
}
|
|
633
|
+
const valueWidth = Math.max(...list.flatMap((g) => g.values.map((v) => show(v.value).length))) + 2;
|
|
634
|
+
const item = (i, v) => ` ${i + 1}) ${show(v.value).padEnd(valueWidth)}${v.from}`;
|
|
635
|
+
const column = Math.max(...list.map((g) => g.key.length), ...list.flatMap((g) => g.values.map((v, i) => item(i, v).length))) + 5;
|
|
636
|
+
const next = { ...r.choices };
|
|
637
|
+
for (const [n, g] of list.entries()) {
|
|
638
|
+
if (n)
|
|
639
|
+
d.out("");
|
|
640
|
+
d.out(`${g.key.padEnd(column)}${g.chosen ? "chosen" : "not chosen yet"}`);
|
|
641
|
+
g.values.forEach((v, i) => d.out(v.from === g.applies
|
|
642
|
+
? `${item(i, v).padEnd(column)}← applies now`
|
|
643
|
+
: item(i, v)));
|
|
644
|
+
if (!d.terminal)
|
|
645
|
+
continue;
|
|
646
|
+
const numbers = g.values.map((_, i) => String(i + 1));
|
|
647
|
+
const a = await choose(d, ` choose [${numbers.join("/")}, Enter keeps it]: `, ["", ...numbers]);
|
|
648
|
+
if (a)
|
|
649
|
+
next[g.key] = g.values[Number(a) - 1].from;
|
|
650
|
+
}
|
|
651
|
+
if (!d.terminal)
|
|
652
|
+
return 0;
|
|
653
|
+
d.out("");
|
|
654
|
+
const text = choicesText(next);
|
|
655
|
+
if (stable(next) === stable(r.choices)) {
|
|
656
|
+
d.out("Nothing changed.");
|
|
657
|
+
return 0;
|
|
658
|
+
}
|
|
659
|
+
d.write(CHOICES_FILE, text);
|
|
660
|
+
return syncing(d, text !== null);
|
|
661
|
+
}
|
|
662
|
+
export async function chooseOne(argv, d) {
|
|
663
|
+
const [key, preset] = argv;
|
|
664
|
+
if (!key || !preset) {
|
|
665
|
+
d.err("Usage: factory preset choose KEY PRESET");
|
|
666
|
+
return 2;
|
|
667
|
+
}
|
|
668
|
+
const before = d.read(CHOICES_FILE);
|
|
669
|
+
let r;
|
|
670
|
+
try {
|
|
671
|
+
r = await d.resolve(d.read(SETTINGS_FILE) ?? "", before);
|
|
672
|
+
}
|
|
673
|
+
catch (e) {
|
|
674
|
+
d.err(e instanceof Error ? e.message : String(e));
|
|
675
|
+
return 1;
|
|
676
|
+
}
|
|
677
|
+
const g = r.resolution.disagreements.find((x) => x.key === key);
|
|
678
|
+
if (!g) {
|
|
679
|
+
d.err(`${key}: no two presets disagree on this`);
|
|
680
|
+
return 1;
|
|
681
|
+
}
|
|
682
|
+
if (!g.values.some((v) => v.from === preset)) {
|
|
683
|
+
d.err(`${key}: ${preset} doesn't set this`);
|
|
684
|
+
return 1;
|
|
685
|
+
}
|
|
686
|
+
const text = choicesText({ ...r.choices, [key]: preset });
|
|
687
|
+
if (text !== before)
|
|
688
|
+
d.write(CHOICES_FILE, text);
|
|
689
|
+
return syncing(d, text !== before);
|
|
690
|
+
}
|
|
691
|
+
// ---------------------------------------------------------------- check
|
|
692
|
+
const ADVICE = {
|
|
693
|
+
"own-only": ". Remove it, and set it in each repository that wants it",
|
|
694
|
+
presets: ". Remove it, and list each preset in the repositories that use this one",
|
|
695
|
+
off: ". Remove it: only a repository can leave a plugin out",
|
|
696
|
+
unknown: "",
|
|
697
|
+
};
|
|
698
|
+
/** Each folder under `dir` with a preset file, the package's own first. */
|
|
699
|
+
function presetFolders(dir, base = dir) {
|
|
700
|
+
const here = existsSync(join(dir, PRESET_FILE))
|
|
701
|
+
? [relative(base, dir).split(sep).join("/")]
|
|
702
|
+
: [];
|
|
703
|
+
const subs = readdirSync(dir)
|
|
704
|
+
.filter((f) => !["node_modules", "agents", "skills", ".git"].includes(f) &&
|
|
705
|
+
statSync(join(dir, f)).isDirectory())
|
|
706
|
+
.sort()
|
|
707
|
+
.flatMap((f) => presetFolders(join(dir, f), base));
|
|
708
|
+
return [...here, ...subs];
|
|
709
|
+
}
|
|
710
|
+
export async function check(_argv, d) {
|
|
711
|
+
const none = "No preset here: no factory-preset.yaml beside a package.json";
|
|
712
|
+
const manifest = join(d.cwd, "package.json");
|
|
713
|
+
if (!existsSync(manifest)) {
|
|
714
|
+
d.err(none);
|
|
715
|
+
return 1;
|
|
716
|
+
}
|
|
717
|
+
const folders = presetFolders(d.cwd);
|
|
718
|
+
if (!folders.length) {
|
|
719
|
+
d.err(none);
|
|
720
|
+
return 1;
|
|
721
|
+
}
|
|
722
|
+
const pkg = String(JSON.parse(readFileSync(manifest, "utf8")).name ?? "");
|
|
723
|
+
const { load } = await import("js-yaml");
|
|
724
|
+
const names = folders.map((f) => (f ? `${pkg}/${f}` : pkg));
|
|
725
|
+
const shippedNames = new Set(folders.flatMap((f) => shipped(join(d.cwd, f)).map(([to]) => to.startsWith(".claude/agents/") ? to.slice(15, -3) : to.split("/")[2])));
|
|
726
|
+
const problems = [];
|
|
727
|
+
const notes = [];
|
|
728
|
+
folders.forEach((f) => {
|
|
729
|
+
const file = f ? `${f}/${PRESET_FILE}` : PRESET_FILE;
|
|
730
|
+
let raw;
|
|
731
|
+
try {
|
|
732
|
+
raw = load(readFileSync(join(d.cwd, file), "utf8"));
|
|
733
|
+
}
|
|
734
|
+
catch (e) {
|
|
735
|
+
problems.push(`${file}: ${e.message.split("\n")[0]}`);
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
for (const fault of presetFaults(raw))
|
|
739
|
+
problems.push(`${file}: ${fault.key}: ${fault.message}${ADVICE[fault.kind]}`);
|
|
740
|
+
const plugins = isObj(raw) ? raw.plugins : undefined;
|
|
741
|
+
for (const path of [
|
|
742
|
+
...PLUGIN_LISTS,
|
|
743
|
+
"plugins.refine.refiner",
|
|
744
|
+
"plugins.refine.mockups",
|
|
745
|
+
"plugins.test.prover",
|
|
746
|
+
]) {
|
|
747
|
+
const at = getPath({ plugins }, path.split("."));
|
|
748
|
+
for (const item of Array.isArray(at)
|
|
749
|
+
? at
|
|
750
|
+
: at === undefined
|
|
751
|
+
? []
|
|
752
|
+
: [at]) {
|
|
753
|
+
const n = isObj(item) ? (item.agent ?? item.skill) : undefined;
|
|
754
|
+
if (typeof n !== "string" || n.startsWith("factory-"))
|
|
755
|
+
continue;
|
|
756
|
+
if (!shippedNames.has(n))
|
|
757
|
+
notes.push(`${file}: ${path}: ${n}: not shipped here, so each repository that uses this preset must provide it`);
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
});
|
|
761
|
+
if (problems.length) {
|
|
762
|
+
for (const line of [...problems, ...notes])
|
|
763
|
+
d.out(line);
|
|
764
|
+
return 1;
|
|
765
|
+
}
|
|
766
|
+
const width = Math.max(...names.map((n) => n.length)) + 3;
|
|
767
|
+
folders.forEach((f, i) => d.out(`${names[i].padEnd(width)}${shipsText(join(d.cwd, f))}`));
|
|
768
|
+
for (const line of notes)
|
|
769
|
+
d.out(line);
|
|
770
|
+
return 0;
|
|
771
|
+
}
|
|
772
|
+
/** A mark's text read back: who a value came from, and whether it was
|
|
773
|
+
* chosen. No mark is `.claude/factory.yaml`'s own. */
|
|
774
|
+
export function readMark(text) {
|
|
775
|
+
const t = text?.trim();
|
|
776
|
+
if (!t)
|
|
777
|
+
return { from: OWN };
|
|
778
|
+
if (t === "default")
|
|
779
|
+
return { from: "default" };
|
|
780
|
+
if (t.startsWith("over "))
|
|
781
|
+
return { from: OWN };
|
|
782
|
+
let m = /^(.+), chosen over .+$/.exec(t);
|
|
783
|
+
if (m)
|
|
784
|
+
return { from: m[1], state: "chosen" };
|
|
785
|
+
m = /^(.+), not chosen yet$/.exec(t);
|
|
786
|
+
if (m)
|
|
787
|
+
return { from: m[1], state: "not-chosen" };
|
|
788
|
+
return { from: t };
|
|
789
|
+
}
|
|
790
|
+
/** Each setting's mark in a lock, by the key `combine` gives it. */
|
|
791
|
+
export async function lockMarks(text) {
|
|
792
|
+
const { parseDocument, isMap, isSeq, isScalar } = await import("yaml");
|
|
793
|
+
const doc = parseDocument(text);
|
|
794
|
+
const out = new Map();
|
|
795
|
+
const settings = doc.get("settings");
|
|
796
|
+
const visit = (node, path) => {
|
|
797
|
+
const key = path.join(".");
|
|
798
|
+
if (isMap(node)) {
|
|
799
|
+
for (const pair of node.items) {
|
|
800
|
+
const k = String(pair.key.value);
|
|
801
|
+
visit(pair.value, [...path, k]);
|
|
802
|
+
}
|
|
803
|
+
// A role that takes one plugin is marked on its name's line.
|
|
804
|
+
const name = node.items.find((p) => ["agent", "skill"].includes(String(p.key.value)));
|
|
805
|
+
if (name && isScalar(name.value) && !out.has(key))
|
|
806
|
+
out.set(key, readMark(name.value.comment));
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
if (isSeq(node)) {
|
|
810
|
+
const list = PLUGIN_LISTS.includes(key);
|
|
811
|
+
node.items.forEach((item) => {
|
|
812
|
+
if (list && isMap(item)) {
|
|
813
|
+
const name = item.items.find((p) => ["agent", "skill"].includes(String(p.key.value)));
|
|
814
|
+
if (name && isScalar(name.value))
|
|
815
|
+
out.set(`${key}.${name.value.value}`, readMark(name.value.comment));
|
|
816
|
+
}
|
|
817
|
+
else if (isScalar(item)) {
|
|
818
|
+
out.set(`${key}[${stable(item.value)}]`, readMark(item.comment));
|
|
819
|
+
if (!out.has(key))
|
|
820
|
+
out.set(key, readMark(item.comment));
|
|
821
|
+
}
|
|
822
|
+
});
|
|
823
|
+
return;
|
|
824
|
+
}
|
|
825
|
+
if (isScalar(node))
|
|
826
|
+
out.set(key, readMark(node.comment));
|
|
827
|
+
};
|
|
828
|
+
visit(settings, []);
|
|
829
|
+
return out;
|
|
830
|
+
}
|
|
831
|
+
/** Every leaf of a block, by its dotted path, a list as one value. */
|
|
832
|
+
function leaves(o, path = [], out = new Map()) {
|
|
833
|
+
if (isObj(o))
|
|
834
|
+
for (const [k, v] of Object.entries(o))
|
|
835
|
+
leaves(v, [...path, k], out);
|
|
836
|
+
else
|
|
837
|
+
out.set(path.join("."), o);
|
|
838
|
+
return out;
|
|
839
|
+
}
|
|
840
|
+
/** `factory preset changes REF`: what the presets' update changed here,
|
|
841
|
+
* as `factory-deps` comments it. */
|
|
842
|
+
export async function changes(argv, d) {
|
|
843
|
+
const ref = argv[0];
|
|
844
|
+
if (!ref) {
|
|
845
|
+
d.err("Usage: factory preset changes REF");
|
|
846
|
+
return 2;
|
|
847
|
+
}
|
|
848
|
+
const { load } = await import("js-yaml");
|
|
849
|
+
const oldText = d.show(ref, LOCK_FILE);
|
|
850
|
+
const newText = d.read(LOCK_FILE);
|
|
851
|
+
if (newText === null) {
|
|
852
|
+
d.err(staleMessages.missing);
|
|
853
|
+
return 1;
|
|
854
|
+
}
|
|
855
|
+
const empty = {
|
|
856
|
+
version: "",
|
|
857
|
+
presets: [],
|
|
858
|
+
hash: null,
|
|
859
|
+
settings: {},
|
|
860
|
+
overridden: {},
|
|
861
|
+
files: [],
|
|
862
|
+
};
|
|
863
|
+
const a = oldText ? readLock(oldText, load) : empty;
|
|
864
|
+
const b = readLock(newText, load);
|
|
865
|
+
const [ma, mb] = [
|
|
866
|
+
oldText ? await lockMarks(oldText) : new Map(),
|
|
867
|
+
await lockMarks(newText),
|
|
868
|
+
];
|
|
869
|
+
const out = [];
|
|
870
|
+
const versions = (l) => new Map(l.presets.map((p) => [splitName(p.name).pkg, p.version]));
|
|
871
|
+
const [va, vb] = [versions(a), versions(b)];
|
|
872
|
+
for (const [pkg, v] of vb) {
|
|
873
|
+
const was = va.get(pkg);
|
|
874
|
+
if (was !== undefined && was !== v)
|
|
875
|
+
out.push(`${pkg} ${was} → ${v}`);
|
|
876
|
+
}
|
|
877
|
+
const here = [];
|
|
878
|
+
const added = b.files.filter((f) => !a.files.includes(f));
|
|
879
|
+
const deleted = a.files.filter((f) => !b.files.includes(f));
|
|
880
|
+
const explained = new Set();
|
|
881
|
+
const fileOf = (name, files) => files.find((f) => f === `.claude/agents/${name}.md` ||
|
|
882
|
+
f.startsWith(`.claude/skills/${name}/`));
|
|
883
|
+
const [la, lb] = [leaves(a.settings), leaves(b.settings)];
|
|
884
|
+
for (const key of new Set([...la.keys(), ...lb.keys()])) {
|
|
885
|
+
const [x, y] = [la.get(key), lb.get(key)];
|
|
886
|
+
if (stable(x) === stable(y))
|
|
887
|
+
continue;
|
|
888
|
+
if (PLUGIN_LISTS.includes(key) || VALUE_LISTS.includes(key)) {
|
|
889
|
+
const plug = PLUGIN_LISTS.includes(key);
|
|
890
|
+
const id = (i) => (plug ? show(i) : stable(i));
|
|
891
|
+
const xs = Array.isArray(x) ? x : [];
|
|
892
|
+
const ys = Array.isArray(y) ? y : [];
|
|
893
|
+
for (const item of ys)
|
|
894
|
+
if (!xs.some((i) => id(i) === id(item))) {
|
|
895
|
+
const file = plug ? fileOf(show(item), added) : undefined;
|
|
896
|
+
if (file)
|
|
897
|
+
explained.add(file);
|
|
898
|
+
here.push(...(file
|
|
899
|
+
? [`- ${key}: + ${show(item)},`, ` which wrote ${file}`]
|
|
900
|
+
: [`- ${key}: + ${show(item)}`]));
|
|
901
|
+
}
|
|
902
|
+
for (const item of xs)
|
|
903
|
+
if (!ys.some((i) => id(i) === id(item))) {
|
|
904
|
+
const file = plug ? fileOf(show(item), deleted) : undefined;
|
|
905
|
+
if (file)
|
|
906
|
+
explained.add(file);
|
|
907
|
+
here.push(`- ${key}: − ${show(item)}${file ? `, which deleted ${file}` : ""}`);
|
|
908
|
+
}
|
|
909
|
+
continue;
|
|
910
|
+
}
|
|
911
|
+
const [sa, sb] = [ma.get(key)?.from, mb.get(key)?.from];
|
|
912
|
+
here.push(`- ${key}: ${show(x)} → ${show(y)}${sa !== sb && sb && sb !== OWN && sb !== "default" ? `, now from ${sb}` : ""}`);
|
|
913
|
+
}
|
|
914
|
+
for (const f of added)
|
|
915
|
+
if (!explained.has(f))
|
|
916
|
+
here.push(`- wrote ${f}`);
|
|
917
|
+
for (const f of deleted)
|
|
918
|
+
if (!explained.has(f))
|
|
919
|
+
here.push(`- deleted ${f}`);
|
|
920
|
+
const choice = [];
|
|
921
|
+
for (const [key, mark] of mb) {
|
|
922
|
+
if (mark.state !== "not-chosen")
|
|
923
|
+
continue;
|
|
924
|
+
if (ma.get(key)?.state === "not-chosen")
|
|
925
|
+
continue;
|
|
926
|
+
const others = (b.overridden[key] ?? []).filter((o) => o.from !== OWN);
|
|
927
|
+
const now = valueAt(b, key);
|
|
928
|
+
const parts = others.map((o) => `${o.from} now sets ${show(o.value)}`);
|
|
929
|
+
const all = [...parts, `${mark.from} sets ${show(now)}`];
|
|
930
|
+
choice.push(`- ${key}: ${all.length > 2 ? `${all.slice(0, -1).join(", ")}, and ${all.at(-1)}` : all.join(", and ")}. ${show(now)} applies, from the later preset, until you choose.`);
|
|
931
|
+
}
|
|
932
|
+
const ignored = [];
|
|
933
|
+
const needsChoice = new Set([...mb]
|
|
934
|
+
.filter(([k, m]) => m.state === "not-chosen" && ma.get(k)?.state !== "not-chosen")
|
|
935
|
+
.map(([k]) => k));
|
|
936
|
+
for (const [key, entries] of Object.entries(b.overridden)) {
|
|
937
|
+
if (needsChoice.has(key))
|
|
938
|
+
continue;
|
|
939
|
+
for (const e of entries) {
|
|
940
|
+
if (e.from === OWN || key.startsWith(".claude/"))
|
|
941
|
+
continue;
|
|
942
|
+
const was = a.overridden[key]?.find((o) => o.from === e.from);
|
|
943
|
+
const wasInEffect = ma.get(key)?.from === e.from ? valueAt(a, key) : was?.value;
|
|
944
|
+
if (stable(wasInEffect) === stable(e.value))
|
|
945
|
+
continue;
|
|
946
|
+
if (stable(valueAt(a, key)) !== stable(valueAt(b, key)))
|
|
947
|
+
continue;
|
|
948
|
+
const mark = mb.get(key);
|
|
949
|
+
if (!mark)
|
|
950
|
+
continue;
|
|
951
|
+
if (mark.from === OWN)
|
|
952
|
+
ignored.push(`- ${key}, which your ${SETTINGS_FILE} sets`);
|
|
953
|
+
else if (mark.state === "chosen")
|
|
954
|
+
ignored.push(`- ${key}: ${show(wasInEffect)} → ${show(e.value)}, but you chose ${mark.from}'s ${show(valueAt(b, key))}`);
|
|
955
|
+
else
|
|
956
|
+
ignored.push(`- ${key}: ${show(wasInEffect)} → ${show(e.value)}, but ${mark.from}'s ${show(valueAt(b, key))} applies`);
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
const sections = [
|
|
960
|
+
["Changes here:", here],
|
|
961
|
+
["Needs a choice:", choice],
|
|
962
|
+
["Doesn't apply here:", ignored],
|
|
963
|
+
];
|
|
964
|
+
const shown = sections.filter(([, lines]) => lines.length);
|
|
965
|
+
if (out.length)
|
|
966
|
+
out.push("");
|
|
967
|
+
if (!shown.length)
|
|
968
|
+
out.push("Nothing changes here.");
|
|
969
|
+
shown.forEach(([title, lines], i) => {
|
|
970
|
+
if (i)
|
|
971
|
+
out.push("");
|
|
972
|
+
out.push(title, ...lines);
|
|
973
|
+
});
|
|
974
|
+
if (choice.length)
|
|
975
|
+
out.push("", "To choose, on this branch:", " npx --no-install factory preset choices");
|
|
976
|
+
for (const line of out)
|
|
977
|
+
d.out(line);
|
|
978
|
+
return 0;
|
|
979
|
+
}
|
|
980
|
+
/** A setting's value in a lock: a list's whole value, or a plugin by its
|
|
981
|
+
* key's name. */
|
|
982
|
+
function valueAt(l, key) {
|
|
983
|
+
const list = pluginListOf(key);
|
|
984
|
+
if (list) {
|
|
985
|
+
const items = getPath(l.settings, list.split("."));
|
|
986
|
+
const name = key.slice(list.length + 1);
|
|
987
|
+
return Array.isArray(items)
|
|
988
|
+
? items.find((i) => isObj(i) && (i.agent ?? i.skill) === name)
|
|
989
|
+
: undefined;
|
|
990
|
+
}
|
|
991
|
+
return getPath(l.settings, key.split("."));
|
|
992
|
+
}
|
|
993
|
+
// ---------------------------------------------------------------- doctor
|
|
994
|
+
/** Doctor's lines for the lock and the presets it records. */
|
|
995
|
+
export async function presetChecks(root, stale) {
|
|
996
|
+
const out = [];
|
|
997
|
+
const read = (p) => {
|
|
998
|
+
try {
|
|
999
|
+
return readFileSync(join(root, p), "utf8");
|
|
1000
|
+
}
|
|
1001
|
+
catch {
|
|
1002
|
+
return null;
|
|
1003
|
+
}
|
|
1004
|
+
};
|
|
1005
|
+
const text = read(LOCK_FILE);
|
|
1006
|
+
const { load } = await import("js-yaml");
|
|
1007
|
+
const lock = text ? readLock(text, load) : null;
|
|
1008
|
+
const shownByPreset = new Set();
|
|
1009
|
+
const hasModules = existsSync(join(root, "node_modules"));
|
|
1010
|
+
for (const p of lock?.presets ?? []) {
|
|
1011
|
+
const pkg = splitName(p.name).pkg;
|
|
1012
|
+
const v = hasModules
|
|
1013
|
+
? (findPackage(root, pkg)?.version ?? null)
|
|
1014
|
+
: undefined;
|
|
1015
|
+
if (v === null) {
|
|
1016
|
+
const m = staleMessages.notInstalled(p.name);
|
|
1017
|
+
shownByPreset.add(m);
|
|
1018
|
+
out.push({ state: "fail", text: m });
|
|
1019
|
+
}
|
|
1020
|
+
else if (v !== undefined && v !== p.version) {
|
|
1021
|
+
const m = staleMessages.version(p.name, v, p.version);
|
|
1022
|
+
shownByPreset.add(m);
|
|
1023
|
+
out.push({ state: "fail", text: m });
|
|
1024
|
+
}
|
|
1025
|
+
else
|
|
1026
|
+
out.push({
|
|
1027
|
+
state: "pass",
|
|
1028
|
+
text: `${p.name} ${p.version} is installed, as the lock records`,
|
|
1029
|
+
});
|
|
1030
|
+
}
|
|
1031
|
+
if (stale && !shownByPreset.has(stale))
|
|
1032
|
+
out.unshift({ state: "fail", text: stale });
|
|
1033
|
+
const dependabot = read(DEPENDABOT);
|
|
1034
|
+
if (dependabot !== null)
|
|
1035
|
+
for (const pkg of new Set((lock?.presets ?? []).map((p) => splitName(p.name).pkg)))
|
|
1036
|
+
out.push((await hasOwnGroup(dependabot, pkg))
|
|
1037
|
+
? { state: "pass", text: `${pkg} has a Dependabot group of its own` }
|
|
1038
|
+
: {
|
|
1039
|
+
state: "fail",
|
|
1040
|
+
text: `${pkg} has no Dependabot group of its own, so its updates may arrive with other packages`,
|
|
1041
|
+
fix: `in ${DEPENDABOT}, give ${pkg} a group of its own`,
|
|
1042
|
+
});
|
|
1043
|
+
return out;
|
|
1044
|
+
}
|
|
1045
|
+
// ---------------------------------------------------------------- running
|
|
1046
|
+
export async function main(argv, d) {
|
|
1047
|
+
const [sub, ...rest] = argv;
|
|
1048
|
+
const commands = {
|
|
1049
|
+
add,
|
|
1050
|
+
remove,
|
|
1051
|
+
choices,
|
|
1052
|
+
choose: chooseOne,
|
|
1053
|
+
check,
|
|
1054
|
+
changes,
|
|
1055
|
+
};
|
|
1056
|
+
const run = sub ? commands[sub] : undefined;
|
|
1057
|
+
if (!run) {
|
|
1058
|
+
d.err("Usage: factory preset add NAME | remove NAME | choices | choose KEY PRESET | check | changes REF");
|
|
1059
|
+
return 2;
|
|
1060
|
+
}
|
|
1061
|
+
try {
|
|
1062
|
+
return await run(rest, d);
|
|
1063
|
+
}
|
|
1064
|
+
catch (e) {
|
|
1065
|
+
if (!(e instanceof SettingsError))
|
|
1066
|
+
throw e;
|
|
1067
|
+
d.err(e.message);
|
|
1068
|
+
return 1;
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
/** The preset commands' dependencies in the repository at `root`. */
|
|
1072
|
+
export function presetDeps(root = repoRoot()) {
|
|
1073
|
+
const files = filesDeps(root);
|
|
1074
|
+
const readAt = (p) => {
|
|
1075
|
+
try {
|
|
1076
|
+
return readFileSync(join(root, p), "utf8");
|
|
1077
|
+
}
|
|
1078
|
+
catch {
|
|
1079
|
+
return null;
|
|
1080
|
+
}
|
|
1081
|
+
};
|
|
1082
|
+
return {
|
|
1083
|
+
root,
|
|
1084
|
+
cwd: process.cwd(),
|
|
1085
|
+
out: (l) => console.log(l),
|
|
1086
|
+
err: (l) => console.error(l),
|
|
1087
|
+
terminal: Boolean(process.stdin.isTTY && process.stdout.isTTY),
|
|
1088
|
+
ask: async (question) => {
|
|
1089
|
+
const { createInterface } = await import("node:readline/promises");
|
|
1090
|
+
const rl = createInterface({
|
|
1091
|
+
input: process.stdin,
|
|
1092
|
+
output: process.stdout,
|
|
1093
|
+
});
|
|
1094
|
+
try {
|
|
1095
|
+
return await rl.question(question);
|
|
1096
|
+
}
|
|
1097
|
+
finally {
|
|
1098
|
+
rl.close();
|
|
1099
|
+
}
|
|
1100
|
+
},
|
|
1101
|
+
npm: (args) => {
|
|
1102
|
+
const r = spawnSync("npm", args, { cwd: root, encoding: "utf8" });
|
|
1103
|
+
return {
|
|
1104
|
+
status: r.status ?? 1,
|
|
1105
|
+
stdout: r.stdout ?? "",
|
|
1106
|
+
stderr: r.stderr ?? "",
|
|
1107
|
+
};
|
|
1108
|
+
},
|
|
1109
|
+
sync: async (out) => {
|
|
1110
|
+
const sync = await import("./sync.mjs");
|
|
1111
|
+
return sync.main([], {
|
|
1112
|
+
...sync.syncDeps(root),
|
|
1113
|
+
out,
|
|
1114
|
+
});
|
|
1115
|
+
},
|
|
1116
|
+
show: (ref, path) => {
|
|
1117
|
+
try {
|
|
1118
|
+
return execFileSync("git", ["show", `${ref}:${path}`], {
|
|
1119
|
+
cwd: root,
|
|
1120
|
+
encoding: "utf8",
|
|
1121
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
1122
|
+
});
|
|
1123
|
+
}
|
|
1124
|
+
catch {
|
|
1125
|
+
return null;
|
|
1126
|
+
}
|
|
1127
|
+
},
|
|
1128
|
+
resolve: (own, choicesFile) => resolveFiles({
|
|
1129
|
+
...files,
|
|
1130
|
+
readFile: () => own,
|
|
1131
|
+
readChoices: () => choicesFile,
|
|
1132
|
+
}),
|
|
1133
|
+
write: (path, text) => {
|
|
1134
|
+
const at = join(root, path);
|
|
1135
|
+
if (text === null)
|
|
1136
|
+
rmSync(at, { force: true });
|
|
1137
|
+
else {
|
|
1138
|
+
mkdirSync(dirname(at), { recursive: true });
|
|
1139
|
+
writeFileSync(at, text);
|
|
1140
|
+
}
|
|
1141
|
+
},
|
|
1142
|
+
read: readAt,
|
|
1143
|
+
};
|
|
1144
|
+
}
|
|
1145
|
+
/** `factory preset ...`. */
|
|
1146
|
+
export async function run(argv) {
|
|
1147
|
+
return main(argv, presetDeps());
|
|
1148
|
+
}
|