@davidbyoung/software-factory 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. package/.claude-plugin/plugin.json +5 -0
  2. package/LICENSE +21 -0
  3. package/README.md +135 -2
  4. package/actions/factory-agent/action.yml +218 -0
  5. package/agents/factory-test-reviewer.md +129 -0
  6. package/agents/factory-ticket-readiness.md +107 -0
  7. package/agents/factory-ui-reviewer.md +160 -0
  8. package/bin/factory.js +45 -0
  9. package/bin/launch.js +67 -0
  10. package/dist/board.d.mts +128 -0
  11. package/dist/board.mjs +456 -0
  12. package/dist/build.d.mts +18 -0
  13. package/dist/build.mjs +60 -0
  14. package/dist/cli.d.mts +17 -0
  15. package/dist/cli.mjs +264 -0
  16. package/dist/code-host/github.d.mts +118 -0
  17. package/dist/code-host/github.mjs +419 -0
  18. package/dist/code-host/index.d.mts +9 -0
  19. package/dist/code-host/index.mjs +18 -0
  20. package/dist/code-host/types.d.mts +205 -0
  21. package/dist/code-host/types.mjs +12 -0
  22. package/dist/config.d.mts +166 -0
  23. package/dist/config.mjs +548 -0
  24. package/dist/deps.d.mts +32 -0
  25. package/dist/deps.mjs +126 -0
  26. package/dist/doctor.d.mts +34 -0
  27. package/dist/doctor.mjs +188 -0
  28. package/dist/guard-commit.d.mts +41 -0
  29. package/dist/guard-commit.mjs +205 -0
  30. package/dist/guard-permission.d.mts +15 -0
  31. package/dist/guard-permission.mjs +52 -0
  32. package/dist/guard-ticket-text.d.mts +27 -0
  33. package/dist/guard-ticket-text.mjs +288 -0
  34. package/dist/guard.d.mts +4 -0
  35. package/dist/guard.mjs +37 -0
  36. package/dist/init.d.mts +58 -0
  37. package/dist/init.mjs +556 -0
  38. package/dist/lib/clock.d.mts +14 -0
  39. package/dist/lib/clock.mjs +36 -0
  40. package/dist/lib/gh.d.mts +28 -0
  41. package/dist/lib/gh.mjs +92 -0
  42. package/dist/lib/glob.d.mts +2 -0
  43. package/dist/lib/glob.mjs +41 -0
  44. package/dist/lib/pricing.d.mts +25 -0
  45. package/dist/lib/pricing.mjs +77 -0
  46. package/dist/lib/usage.d.mts +34 -0
  47. package/dist/lib/usage.mjs +128 -0
  48. package/dist/lib/work-status.d.mts +251 -0
  49. package/dist/lib/work-status.mjs +510 -0
  50. package/dist/merge-policy.d.mts +66 -0
  51. package/dist/merge-policy.mjs +146 -0
  52. package/dist/parts.d.mts +39 -0
  53. package/dist/parts.mjs +138 -0
  54. package/dist/poller.d.mts +61 -0
  55. package/dist/poller.mjs +247 -0
  56. package/dist/pr-watch.d.mts +45 -0
  57. package/dist/pr-watch.mjs +244 -0
  58. package/dist/pr.d.mts +65 -0
  59. package/dist/pr.mjs +571 -0
  60. package/dist/review-cost.d.mts +18 -0
  61. package/dist/review-cost.mjs +95 -0
  62. package/dist/review-tools.d.mts +5 -0
  63. package/dist/review-tools.mjs +46 -0
  64. package/dist/review-verdict.d.mts +50 -0
  65. package/dist/review-verdict.mjs +171 -0
  66. package/dist/reviewers.d.mts +54 -0
  67. package/dist/reviewers.mjs +312 -0
  68. package/dist/runner/github-actions.d.mts +12 -0
  69. package/dist/runner/github-actions.mjs +97 -0
  70. package/dist/runner/index.d.mts +9 -0
  71. package/dist/runner/index.mjs +22 -0
  72. package/dist/runner/types.d.mts +60 -0
  73. package/dist/runner/types.mjs +7 -0
  74. package/dist/session-start.d.mts +20 -0
  75. package/dist/session-start.mjs +115 -0
  76. package/dist/site/capture.d.mts +44 -0
  77. package/dist/site/capture.mjs +114 -0
  78. package/dist/site/measure.d.mts +83 -0
  79. package/dist/site/measure.mjs +225 -0
  80. package/dist/site/pages.d.mts +10 -0
  81. package/dist/site/pages.mjs +71 -0
  82. package/dist/site/pixels.d.mts +47 -0
  83. package/dist/site/pixels.mjs +157 -0
  84. package/dist/site/servers.d.mts +47 -0
  85. package/dist/site/servers.mjs +141 -0
  86. package/dist/site.d.mts +60 -0
  87. package/dist/site.mjs +341 -0
  88. package/dist/status.d.mts +100 -0
  89. package/dist/status.mjs +514 -0
  90. package/dist/sync.d.mts +59 -0
  91. package/dist/sync.mjs +292 -0
  92. package/dist/ticket.d.mts +61 -0
  93. package/dist/ticket.mjs +562 -0
  94. package/dist/tickets/github.d.mts +128 -0
  95. package/dist/tickets/github.mjs +491 -0
  96. package/dist/tickets/index.d.mts +15 -0
  97. package/dist/tickets/index.mjs +27 -0
  98. package/dist/tickets/transport.d.mts +42 -0
  99. package/dist/tickets/transport.mjs +158 -0
  100. package/dist/tickets/types.d.mts +191 -0
  101. package/dist/tickets/types.mjs +76 -0
  102. package/docs/builds.md +227 -0
  103. package/docs/cli.md +221 -0
  104. package/docs/configuration.md +809 -0
  105. package/docs/design.md +106 -0
  106. package/docs/developing.md +400 -0
  107. package/docs/existing-repo.md +138 -0
  108. package/docs/github/reference.md +288 -0
  109. package/docs/github/setup.md +312 -0
  110. package/docs/glossary.md +106 -0
  111. package/docs/limitations.md +115 -0
  112. package/docs/plugins.md +192 -0
  113. package/docs/quick-start.md +222 -0
  114. package/docs/removing.md +76 -0
  115. package/docs/skills.md +270 -0
  116. package/docs/updating.md +108 -0
  117. package/docs/workflow.md +195 -0
  118. package/package.json +67 -4
  119. package/poller.Dockerfile +21 -0
  120. package/review.md +170 -0
  121. package/rules.md +313 -0
  122. package/settings.schema.json +411 -0
  123. package/skills/factory-address-review/SKILL.md +547 -0
  124. package/skills/factory-address-review/patch-coverage.py +68 -0
  125. package/skills/factory-build/SKILL.md +412 -0
  126. package/skills/factory-deps/SKILL.md +185 -0
  127. package/skills/factory-design/SKILL.md +159 -0
  128. package/skills/factory-diagnose/SKILL.md +93 -0
  129. package/skills/factory-mutate/SKILL.md +76 -0
  130. package/skills/factory-refine/SKILL.md +417 -0
  131. package/skills/factory-resolve-conflicts/SKILL.md +235 -0
  132. package/workflows/factory-board-sync.yml +91 -0
  133. package/workflows/factory-build.yml +72 -0
  134. package/workflows/factory-check.yml +37 -0
  135. package/workflows/factory-claude-review.yml +133 -0
  136. package/workflows/factory-conflicts.yml +69 -0
  137. package/workflows/factory-deps-auto-merge.yml +43 -0
  138. package/workflows/factory-deps.yml +59 -0
  139. package/workflows/factory-merge-policy.yml +121 -0
  140. package/workflows/factory-review-verdict.yml +54 -0
  141. package/workflows/factory-review.yml +75 -0
@@ -0,0 +1,548 @@
1
+ #!/usr/bin/env node
2
+ // The factory's settings, from `.claude/factory.yaml`. This is the one place
3
+ // they are read: a command loads them once when it starts and passes down
4
+ // what its functions need, and nothing under src/lib/ loads them.
5
+ //
6
+ // factory config get KEY # e.g. repository, tickets.board
7
+ // factory plugins [ROLE] # kind, name, role, files or labels
8
+ // factory config check # prints nothing when sound
9
+ //
10
+ // A list prints one item per line. A failure names the key and exits 1.
11
+ import { execFileSync } from "node:child_process";
12
+ import { existsSync, readFileSync } from "node:fs";
13
+ import { createRequire } from "node:module";
14
+ import { dirname, join } from "node:path";
15
+ import { DEFAULT_CODE_HOST, unsupportedCodeHost } from "./code-host/index.mjs";
16
+ import { matchesAny } from "./lib/glob.mjs";
17
+ import { DEFAULT_RUNNER, unsupportedRunner } from "./runner/index.mjs";
18
+ import { DEFAULT_COLUMNS, STATE_KEYS, sameName, } from "./lib/work-status.mjs";
19
+ export class SettingsError extends Error {
20
+ }
21
+ export const ROLES = ["mockups", "builder", "reviewer"];
22
+ export const TRIGGERS = ["own-server", "github-action", "manual"];
23
+ export const SPECS = ["none", "openspec"];
24
+ const FIELD_DEFAULTS = {
25
+ session: "Session",
26
+ "runs-in": "Runs in",
27
+ cost: "Cost",
28
+ };
29
+ /** Each kind of unattended session, with how long it may run by default. */
30
+ export const SESSION_MINUTES = {
31
+ build: 230,
32
+ conflicts: 110,
33
+ review: 50,
34
+ dependencies: 50,
35
+ };
36
+ export const ACCESSIBILITY = [
37
+ "wcag22aa",
38
+ "wcag21aa",
39
+ "wcag2aa",
40
+ "none",
41
+ ];
42
+ /** The agent that needs `site`. */
43
+ export const SITE_AGENT = "factory-ui-reviewer";
44
+ export const NO_PLAYWRIGHT = "site needs Playwright: npm install -D playwright";
45
+ /** The keys each block allows, by the block's path: `""` is the top of the
46
+ * file and `plugins[]` is one plug-in. `settings.schema.json` lists the same
47
+ * keys, and a test fails when the two differ. A key the code accepts goes
48
+ * here, in the schema, and in docs/configuration.md. */
49
+ export const SETTINGS_KEYS = {
50
+ "": [
51
+ "version",
52
+ "repository",
53
+ "tickets",
54
+ "code-host",
55
+ "builds",
56
+ "specs",
57
+ "before-commit",
58
+ "build-setup",
59
+ "plugins",
60
+ "auto-merge",
61
+ "ci-review",
62
+ "site",
63
+ "session-minutes",
64
+ ],
65
+ tickets: ["type", "board", "columns", "fields"],
66
+ "tickets.columns": Object.keys(STATE_KEYS),
67
+ "tickets.fields": Object.keys(FIELD_DEFAULTS),
68
+ "code-host": ["type"],
69
+ builds: ["trigger", "runner"],
70
+ specs: ["type"],
71
+ "auto-merge": ["allow", "deny", "commands"],
72
+ "ci-review": ["commands"],
73
+ site: [
74
+ "start",
75
+ "url",
76
+ "design-guide",
77
+ "pages",
78
+ "widths",
79
+ "hide",
80
+ "checks",
81
+ "max-pages",
82
+ "start-minutes",
83
+ "accessibility",
84
+ "new-values",
85
+ ],
86
+ "plugins[]": ["skill", "agent", "role", "files", "labels"],
87
+ "session-minutes": Object.keys(SESSION_MINUTES),
88
+ };
89
+ function wholeNumber(v, key, fallback) {
90
+ if (v === undefined)
91
+ return fallback;
92
+ return Number.isInteger(v) && v > 0
93
+ ? v
94
+ : fail(key, "must be a whole number above zero");
95
+ }
96
+ function parseSite(v, exists) {
97
+ if (v === undefined)
98
+ return null;
99
+ const o = object(v, "site");
100
+ for (const k of ["start", "url"]) {
101
+ if (typeof o[k] !== "string" || !o[k])
102
+ fail(`site.${k}`, "is required when site is written");
103
+ }
104
+ const guide = o["design-guide"];
105
+ if (guide !== undefined && (typeof guide !== "string" || !exists(guide)))
106
+ fail("site.design-guide", "must be a file that exists");
107
+ let widths = [390, 1280];
108
+ if (o.widths !== undefined) {
109
+ if (!Array.isArray(o.widths) || o.widths.length === 0)
110
+ fail("site.widths", "must be a list of widths in pixels");
111
+ widths = o.widths.map((w, i) => wholeNumber(w, `site.widths[${i}]`, 0));
112
+ }
113
+ const acc = o.accessibility ?? "wcag22aa";
114
+ if (!ACCESSIBILITY.includes(acc))
115
+ fail("site.accessibility", `must be one of ${ACCESSIBILITY.join(", ")}`);
116
+ const nv = o["new-values"] ?? true;
117
+ if (typeof nv !== "boolean")
118
+ fail("site.new-values", "must be true or false");
119
+ return {
120
+ start: o.start,
121
+ url: o.url,
122
+ designGuide: typeof guide === "string" ? guide : null,
123
+ pages: o.pages === undefined ? null : strings(o.pages, "site.pages"),
124
+ widths,
125
+ hide: strings(o.hide, "site.hide"),
126
+ checks: strings(o.checks, "site.checks"),
127
+ maxPages: wholeNumber(o["max-pages"], "site.max-pages", 4),
128
+ startMinutes: wholeNumber(o["start-minutes"], "site.start-minutes", 5),
129
+ accessibility: acc,
130
+ newValues: nv,
131
+ };
132
+ }
133
+ /** `factory config get site`: every key in the table's order. */
134
+ export function siteLines(site) {
135
+ const show = (v) => Array.isArray(v) ? v.join(", ") || "none" : v === null ? "none" : String(v);
136
+ return [
137
+ ["start", site.start],
138
+ ["url", site.url],
139
+ ["design-guide", site.designGuide],
140
+ ["pages", site.pages === null ? "found" : site.pages],
141
+ ["widths", site.widths.map(String)],
142
+ ["hide", site.hide],
143
+ ["checks", site.checks],
144
+ ["max-pages", site.maxPages],
145
+ ["start-minutes", site.startMinutes],
146
+ ["accessibility", site.accessibility],
147
+ ["new-values", site.newValues],
148
+ ].map(([k, v]) => `${k}: ${show(v)}`);
149
+ }
150
+ /** The root of the repository a command runs in: the nearest folder at or
151
+ * above `from` that holds `.git`, or `from` itself when none does. */
152
+ export function repoRoot(from = process.cwd()) {
153
+ for (let dir = from;; dir = dirname(dir)) {
154
+ if (existsSync(join(dir, ".git")))
155
+ return dir;
156
+ if (dirname(dir) === dir)
157
+ return from;
158
+ }
159
+ }
160
+ export const SETTINGS_FILE = ".claude/factory.yaml";
161
+ const fail = (key, message) => {
162
+ throw new SettingsError(`${SETTINGS_FILE}: ${key}: ${message}`);
163
+ };
164
+ const isObj = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
165
+ /** A block of settings. Its keys are `SETTINGS_KEYS[path]`, and `path` is
166
+ * the block's `key` unless a list's item names it otherwise. */
167
+ function object(v, key, path = key) {
168
+ if (v === undefined)
169
+ return {};
170
+ if (!isObj(v))
171
+ return fail(key, "must be a block of settings");
172
+ const allowed = SETTINGS_KEYS[path];
173
+ for (const k of Object.keys(v)) {
174
+ if (!allowed.includes(k))
175
+ fail(key ? `${key}.${k}` : k, "unknown key");
176
+ }
177
+ return v;
178
+ }
179
+ function strings(v, key) {
180
+ if (v === undefined)
181
+ return [];
182
+ if (!Array.isArray(v))
183
+ return fail(key, "must be a list");
184
+ return v.map((item, i) => typeof item === "string" && item
185
+ ? item
186
+ : fail(`${key}[${i}]`, "must be text"));
187
+ }
188
+ /** The CI review's commands: each becomes `Bash(COMMAND:*)` in a list
189
+ * that commas separate, so none may hold a comma, a parenthesis, or a
190
+ * quote. */
191
+ function reviewCommands(v) {
192
+ const key = "ci-review.commands";
193
+ return strings(v, key).map((command, i) => /[,()"]/.test(command)
194
+ ? fail(`${key}[${i}]`, "must hold no comma, parenthesis, or quote")
195
+ : command);
196
+ }
197
+ function oneOf(v, key, allowed, fallback) {
198
+ if (v === undefined)
199
+ return fallback;
200
+ return allowed.includes(v)
201
+ ? v
202
+ : fail(key, `must be one of ${allowed.join(", ")}`);
203
+ }
204
+ /** A block of names for the keys of `defaults`: each left out keeps its
205
+ * default, and "none" is null. */
206
+ function names(v, key, defaults) {
207
+ const o = object(v, key);
208
+ return Object.fromEntries(Object.entries(defaults).map(([k, fallback]) => {
209
+ const value = o[k];
210
+ if (value === undefined)
211
+ return [k, fallback];
212
+ if (value === "none")
213
+ return [k, null];
214
+ if (typeof value !== "string" || !value.trim())
215
+ return fail(`${key}.${k}`, "must be a name, or none");
216
+ return [k, value.trim()];
217
+ }));
218
+ }
219
+ /** A part you can swap, written as a block with a `type` or as one word. */
220
+ function typed(v, key) {
221
+ if (typeof v === "string")
222
+ return { type: v };
223
+ return object(v, key);
224
+ }
225
+ function plugin(v, i, exists) {
226
+ const key = `plugins[${i}]`;
227
+ const o = object(v, key, "plugins[]");
228
+ const kind = "skill" in o && !("agent" in o)
229
+ ? "skill"
230
+ : "agent" in o && !("skill" in o)
231
+ ? "agent"
232
+ : fail(key, "names one skill or one agent");
233
+ const name = o[kind];
234
+ if (typeof name !== "string" || !/^[\w-]+$/.test(name))
235
+ fail(`${key}.${kind}`, "must be a name");
236
+ const path = kind === "skill"
237
+ ? `.claude/skills/${name}/SKILL.md`
238
+ : `.claude/agents/${name}.md`;
239
+ if (!exists(path))
240
+ fail(`${key}.${kind}`, `${path} does not exist`);
241
+ return {
242
+ kind,
243
+ name: name,
244
+ role: o.role === undefined
245
+ ? fail(`${key}.role`, `must be one of ${ROLES.join(", ")}`)
246
+ : oneOf(o.role, `${key}.role`, ROLES, "reviewer"),
247
+ files: strings(o.files, `${key}.files`),
248
+ labels: strings(o.labels, `${key}.labels`),
249
+ };
250
+ }
251
+ /** The settings with every default filled in. Throws a SettingsError naming
252
+ * the key on anything unsound. `exists` says whether a path from the root
253
+ * exists, for the plug-ins' files. */
254
+ export function parseSettings(raw, exists) {
255
+ const top = object(raw ?? {}, "");
256
+ if (top.version !== 1)
257
+ fail("version", "must be 1");
258
+ if (typeof top.repository !== "string" ||
259
+ !/^[\w.-]+\/[\w.-]+$/.test(top.repository))
260
+ fail("repository", "must be owner/name");
261
+ const tickets = typed(top.tickets, "tickets");
262
+ if (tickets.type !== undefined && tickets.type !== "github")
263
+ // The same words every ticket command fails with (`tickets/index.mts`).
264
+ throw new SettingsError(`${SETTINGS_FILE}: tickets.type "${String(tickets.type)}" is not supported. "github" is the only ticket system built.`);
265
+ const board = tickets.board;
266
+ if (board !== undefined &&
267
+ board !== "none" &&
268
+ !(Number.isInteger(board) && board > 0))
269
+ fail("tickets.board", "must be a whole number above zero, or none");
270
+ const codeHost = typed(top["code-host"], "code-host").type;
271
+ if (codeHost !== undefined && codeHost !== DEFAULT_CODE_HOST)
272
+ throw new SettingsError(`${SETTINGS_FILE}: ${unsupportedCodeHost(String(codeHost))}`);
273
+ const builds = object(top.builds, "builds");
274
+ const trigger = oneOf(builds.trigger, "builds.trigger", TRIGGERS, "manual");
275
+ if (builds.runner !== undefined && builds.runner !== DEFAULT_RUNNER)
276
+ throw new SettingsError(`${SETTINGS_FILE}: ${unsupportedRunner(String(builds.runner))}`);
277
+ const hasBoard = typeof board === "number";
278
+ if (tickets.columns !== undefined && !hasBoard)
279
+ fail("tickets.columns", "needs tickets.board");
280
+ if (tickets.fields !== undefined && !hasBoard)
281
+ fail("tickets.fields", "needs tickets.board");
282
+ const columns = names(tickets.columns, "tickets.columns", Object.fromEntries(Object.entries(STATE_KEYS).map(([k, s]) => [k, DEFAULT_COLUMNS[s]])));
283
+ if (columns.backlog === null)
284
+ fail("tickets.columns.backlog", "cannot be none: every ticket starts there");
285
+ const seen = new Map();
286
+ for (const [key, name] of Object.entries(columns)) {
287
+ if (name === null)
288
+ continue;
289
+ const other = [...seen].find(([n]) => sameName(n, name));
290
+ if (other)
291
+ fail(`tickets.columns.${key}`, `names the same column as ${other[1]}`);
292
+ seen.set(name, key);
293
+ }
294
+ if (columns.ready === null && trigger !== "manual")
295
+ fail("tickets.columns.ready", "no column shows ready, so nothing would start a build: name one, or set builds.trigger to manual");
296
+ const fieldNames = names(tickets.fields, "tickets.fields", FIELD_DEFAULTS);
297
+ const specs = typed(top.specs ?? "none", "specs");
298
+ const autoMerge = object(top["auto-merge"], "auto-merge");
299
+ const ciReview = top["ci-review"] === undefined
300
+ ? null
301
+ : object(top["ci-review"] ?? {}, "ci-review");
302
+ if (top.plugins !== undefined && !Array.isArray(top.plugins))
303
+ fail("plugins", "must be a list");
304
+ const site = parseSite(top.site, exists);
305
+ const plugins = (top.plugins ?? []).map((p, i) => plugin(p, i, exists));
306
+ if (!site && plugins.some((p) => p.kind === "agent" && p.name === SITE_AGENT))
307
+ fail("site", `is required to list ${SITE_AGENT}`);
308
+ return {
309
+ version: 1,
310
+ repository: top.repository,
311
+ tickets: {
312
+ type: "github",
313
+ board: typeof board === "number" ? board : null,
314
+ columns,
315
+ fields: fieldNames,
316
+ },
317
+ codeHost: { type: DEFAULT_CODE_HOST },
318
+ builds: { trigger, runner: DEFAULT_RUNNER },
319
+ specs: oneOf(specs.type, "specs", SPECS, "none"),
320
+ beforeCommit: strings(top["before-commit"], "before-commit"),
321
+ buildSetup: strings(top["build-setup"], "build-setup"),
322
+ plugins,
323
+ autoMerge: {
324
+ allow: strings(autoMerge.allow, "auto-merge.allow"),
325
+ deny: strings(autoMerge.deny, "auto-merge.deny"),
326
+ commands: strings(autoMerge.commands, "auto-merge.commands"),
327
+ },
328
+ ciReview: ciReview && { commands: reviewCommands(ciReview.commands) },
329
+ site,
330
+ sessionMinutes: sessionMinutesOf(top["session-minutes"]),
331
+ };
332
+ }
333
+ function sessionMinutesOf(v) {
334
+ const o = object(v, "session-minutes");
335
+ return Object.fromEntries(Object.entries(SESSION_MINUTES).map(([kind, fallback]) => [
336
+ kind,
337
+ wholeNumber(o[kind], `session-minutes.${kind}`, fallback),
338
+ ]));
339
+ }
340
+ /** One line for each pattern that matches none of `files`: a stale pattern
341
+ * is how a policy rots. */
342
+ export function patternWarnings(s, files) {
343
+ const lists = [
344
+ ...s.plugins.map((p, i) => [
345
+ `plugins[${i}].files`,
346
+ p.files,
347
+ ]),
348
+ ["auto-merge.allow", s.autoMerge.allow],
349
+ ["auto-merge.deny", s.autoMerge.deny],
350
+ ];
351
+ return lists.flatMap(([key, patterns]) => patterns
352
+ .filter((p) => !files.some((f) => matchesAny(f, [p])))
353
+ .map((p) => `${key}: ${p} matches no file`));
354
+ }
355
+ export const NO_DEPENDENCIES = "The factory's settings cannot be read until dependencies are installed: run npm ci.";
356
+ /** js-yaml is loaded when needed, so a checkout without dependencies gets
357
+ * one line saying what to do rather than a stack trace. */
358
+ async function read(d) {
359
+ let load;
360
+ try {
361
+ load = await d.loadYaml();
362
+ }
363
+ catch {
364
+ throw new SettingsError(NO_DEPENDENCIES);
365
+ }
366
+ let raw;
367
+ try {
368
+ raw = load(d.readFile());
369
+ }
370
+ catch (e) {
371
+ throw new SettingsError(`${SETTINGS_FILE}: ${e.message}`);
372
+ }
373
+ return parseSettings(raw, d.exists);
374
+ }
375
+ /** The settings file: FACTORY_SETTINGS where it is set, for trying settings
376
+ * against a board without changing the repository's, and otherwise the
377
+ * repository's own. */
378
+ export const settingsPath = (root, env = process.env) => env.FACTORY_SETTINGS || `${root}/${SETTINGS_FILE}`;
379
+ export const defaultDeps = (root = repoRoot()) => ({
380
+ readFile: () => readFileSync(settingsPath(root), "utf8"),
381
+ exists: (path) => existsSync(`${root}/${path}`),
382
+ hasPackage: (name) => {
383
+ try {
384
+ createRequire(`${root}/package.json`).resolve(`${name}/package.json`);
385
+ return true;
386
+ }
387
+ catch {
388
+ return false;
389
+ }
390
+ },
391
+ listFiles: () => execFileSync("git", ["ls-files", "--cached", "--others", "--exclude-standard"], { cwd: root, encoding: "utf8" })
392
+ .split("\n")
393
+ .filter(Boolean),
394
+ loadYaml: async () => (await import("js-yaml")).load,
395
+ out: (line) => console.log(line),
396
+ err: (line) => console.error(line),
397
+ });
398
+ /** The column each state shows in on the board, null for none. */
399
+ export function columnsOf(s) {
400
+ const out = { ...DEFAULT_COLUMNS };
401
+ for (const [key, status] of Object.entries(STATE_KEYS))
402
+ out[status] = s.tickets.columns[key];
403
+ return out;
404
+ }
405
+ /** The board's names for the fields the sync writes, null for none. */
406
+ export function fieldNamesOf(s) {
407
+ return {
408
+ session: s.tickets.fields.session,
409
+ runsIn: s.tickets.fields["runs-in"],
410
+ cost: s.tickets.fields.cost,
411
+ };
412
+ }
413
+ /** The settings of the checkout the command runs in. */
414
+ export const loadSettings = (d = defaultDeps()) => read(d);
415
+ function lookup(s, key) {
416
+ const camel = {
417
+ "before-commit": "beforeCommit",
418
+ "build-setup": "buildSetup",
419
+ "auto-merge": "autoMerge",
420
+ "ci-review": "ciReview",
421
+ "code-host": "codeHost",
422
+ "design-guide": "designGuide",
423
+ "max-pages": "maxPages",
424
+ "start-minutes": "startMinutes",
425
+ "session-minutes": "sessionMinutes",
426
+ };
427
+ let v = s;
428
+ for (const part of key.split(".")) {
429
+ if (!isObj(v))
430
+ return undefined;
431
+ v = v[camel[part] ?? part];
432
+ }
433
+ return v;
434
+ }
435
+ /** The factory's design skill, which `factory sync` writes where there is a
436
+ * site. */
437
+ const DESIGN_SKILL = ".claude/skills/factory-design/SKILL.md";
438
+ /** The settings' plug-ins, and the factory's design skill as the mockups
439
+ * plug-in of a site that lists none, once it is synced. */
440
+ export function pluginsOf(s, exists) {
441
+ if (!s.site || s.plugins.some((p) => p.role === "mockups"))
442
+ return s.plugins;
443
+ if (!exists(DESIGN_SKILL))
444
+ return s.plugins;
445
+ return [
446
+ {
447
+ kind: "skill",
448
+ name: "factory-design",
449
+ role: "mockups",
450
+ files: [],
451
+ labels: [],
452
+ },
453
+ ...s.plugins,
454
+ ];
455
+ }
456
+ export async function main(argv, d) {
457
+ const [command, arg] = argv;
458
+ if (!command) {
459
+ d.err("Usage: factory config get KEY | config check | plugins [ROLE]");
460
+ return 2;
461
+ }
462
+ let s;
463
+ try {
464
+ s = await read(d);
465
+ }
466
+ catch (e) {
467
+ if (!(e instanceof SettingsError))
468
+ throw e;
469
+ d.err(e.message);
470
+ return 1;
471
+ }
472
+ if (command === "check") {
473
+ if (s.site && !d.hasPackage("playwright")) {
474
+ d.err(NO_PLAYWRIGHT);
475
+ return 1;
476
+ }
477
+ for (const w of patternWarnings(s, d.listFiles()))
478
+ d.err(`warning: ${w}`);
479
+ if (s.site && !s.plugins.some((p) => p.role === "mockups"))
480
+ if (!d.exists(DESIGN_SKILL))
481
+ d.err(`warning: ${DESIGN_SKILL} is missing: run \`npx --no-install factory sync\` to give the site its design skill`);
482
+ return 0;
483
+ }
484
+ if (command === "plugins") {
485
+ for (const p of pluginsOf(s, d.exists)) {
486
+ if (arg && p.role !== arg)
487
+ continue;
488
+ d.out([p.kind, p.name, p.role, ...p.files, ...p.labels].join(" ").trim());
489
+ }
490
+ return 0;
491
+ }
492
+ if (command === "site" && s.site) {
493
+ for (const line of siteLines(s.site))
494
+ d.out(line);
495
+ return 0;
496
+ }
497
+ const v = command.startsWith("site") && !s.site ? undefined : lookup(s, command);
498
+ if (command === "site.pages" && s.site?.pages === null) {
499
+ d.out("found");
500
+ return 0;
501
+ }
502
+ if (v === undefined || isObj(v)) {
503
+ d.err(`${SETTINGS_FILE}: ${command}: not a setting that prints`);
504
+ return 1;
505
+ }
506
+ // An unset key of the site's settings prints `none`; elsewhere, nothing.
507
+ const empty = Array.isArray(v) && v.length === 0;
508
+ for (const line of Array.isArray(v) && !(empty && command.startsWith("site"))
509
+ ? v
510
+ : [empty ? "none" : (v ?? "none")])
511
+ d.out(String(line));
512
+ return 0;
513
+ }
514
+ /** `factory config get KEY`, `factory config check`, and
515
+ * `factory plugins [ROLE]`. */
516
+ export async function run(argv) {
517
+ return main(argv, defaultDeps());
518
+ }
519
+ /** The settings' answer, with GITHUB_REPOSITORY and FACTORY_BOARD_NUMBER
520
+ * winning where they are set. */
521
+ export function targetOf(s, env) {
522
+ return {
523
+ repository: env.GITHUB_REPOSITORY || s.repository,
524
+ board: env.FACTORY_BOARD_NUMBER
525
+ ? Number(env.FACTORY_BOARD_NUMBER)
526
+ : s.tickets.board,
527
+ watched: s.builds.trigger !== "manual",
528
+ columns: columnsOf(s),
529
+ };
530
+ }
531
+ /** The repository, code host, and runner for a command a workflow runs
532
+ * too. There GITHUB_REPOSITORY names the repository and the settings are
533
+ * not read, so an unreadable settings file cannot fail the workflow's step,
534
+ * and the code host and the runner are the defaults. */
535
+ export async function reachOf(env, settings = loadSettings) {
536
+ if (env.GITHUB_REPOSITORY)
537
+ return {
538
+ repository: env.GITHUB_REPOSITORY,
539
+ codeHost: DEFAULT_CODE_HOST,
540
+ runner: DEFAULT_RUNNER,
541
+ };
542
+ const s = await settings();
543
+ return {
544
+ repository: s.repository,
545
+ codeHost: s.codeHost.type,
546
+ runner: s.builds.runner,
547
+ };
548
+ }
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env node
2
+ import { type CodeHost } from "./code-host/index.mts";
3
+ type CheckRun = {
4
+ id: number;
5
+ name: string;
6
+ status: string;
7
+ conclusion: string | null;
8
+ };
9
+ export type Bump = {
10
+ number: number;
11
+ title: string;
12
+ head: string;
13
+ sha: string;
14
+ major: boolean;
15
+ mergeableState: string;
16
+ autoMerge: boolean;
17
+ checks: CheckRun[];
18
+ shepherdComments: number;
19
+ };
20
+ /** A major bump changes the leading number. A grouped bump is minor and
21
+ * patch only. */
22
+ export declare function isMajor(title: string): boolean;
23
+ /** What the skill does with a bump, in the order its steps take them. */
24
+ export declare function nextStep(bump: Bump): string;
25
+ /** Every check on the bump's commit, not a fixed list of names: passed, or
26
+ * the names that failed and the names still running. */
27
+ export declare function checksLine(bump: Bump): string;
28
+ export declare function formatBump(bump: Bump): string[];
29
+ export declare function main(p: CodeHost, out: (line: string) => void, argv?: string[]): Promise<number>;
30
+ /** `factory deps`. */
31
+ export declare function run(argv?: string[]): Promise<number>;
32
+ export {};