@davidbyoung/software-factory 0.0.0-stage → 0.1.1
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/.claude-plugin/plugin.json +5 -0
- package/LICENSE +21 -0
- package/README.md +135 -2
- package/actions/factory-agent/action.yml +218 -0
- package/agents/factory-test-reviewer.md +129 -0
- package/agents/factory-ticket-readiness.md +107 -0
- package/agents/factory-ui-reviewer.md +160 -0
- package/bin/factory.js +45 -0
- package/bin/launch.js +67 -0
- package/dist/board.d.mts +128 -0
- package/dist/board.mjs +456 -0
- package/dist/build.d.mts +18 -0
- package/dist/build.mjs +60 -0
- package/dist/cli.d.mts +17 -0
- package/dist/cli.mjs +264 -0
- package/dist/code-host/github.d.mts +118 -0
- package/dist/code-host/github.mjs +419 -0
- package/dist/code-host/index.d.mts +9 -0
- package/dist/code-host/index.mjs +18 -0
- package/dist/code-host/types.d.mts +205 -0
- package/dist/code-host/types.mjs +12 -0
- package/dist/config.d.mts +166 -0
- package/dist/config.mjs +548 -0
- package/dist/deps.d.mts +32 -0
- package/dist/deps.mjs +126 -0
- package/dist/doctor.d.mts +34 -0
- package/dist/doctor.mjs +188 -0
- package/dist/guard-commit.d.mts +41 -0
- package/dist/guard-commit.mjs +209 -0
- package/dist/guard-permission.d.mts +15 -0
- package/dist/guard-permission.mjs +52 -0
- package/dist/guard-ticket-text.d.mts +27 -0
- package/dist/guard-ticket-text.mjs +288 -0
- package/dist/guard.d.mts +4 -0
- package/dist/guard.mjs +37 -0
- package/dist/init.d.mts +58 -0
- package/dist/init.mjs +556 -0
- package/dist/lib/clock.d.mts +14 -0
- package/dist/lib/clock.mjs +36 -0
- package/dist/lib/gh.d.mts +28 -0
- package/dist/lib/gh.mjs +92 -0
- package/dist/lib/glob.d.mts +2 -0
- package/dist/lib/glob.mjs +41 -0
- package/dist/lib/pricing.d.mts +25 -0
- package/dist/lib/pricing.mjs +77 -0
- package/dist/lib/usage.d.mts +34 -0
- package/dist/lib/usage.mjs +128 -0
- package/dist/lib/work-status.d.mts +251 -0
- package/dist/lib/work-status.mjs +510 -0
- package/dist/merge-policy.d.mts +66 -0
- package/dist/merge-policy.mjs +146 -0
- package/dist/parts.d.mts +39 -0
- package/dist/parts.mjs +138 -0
- package/dist/poller.d.mts +61 -0
- package/dist/poller.mjs +247 -0
- package/dist/pr-watch.d.mts +45 -0
- package/dist/pr-watch.mjs +244 -0
- package/dist/pr.d.mts +65 -0
- package/dist/pr.mjs +571 -0
- package/dist/review-cost.d.mts +18 -0
- package/dist/review-cost.mjs +95 -0
- package/dist/review-tools.d.mts +5 -0
- package/dist/review-tools.mjs +46 -0
- package/dist/review-verdict.d.mts +50 -0
- package/dist/review-verdict.mjs +171 -0
- package/dist/reviewers.d.mts +54 -0
- package/dist/reviewers.mjs +312 -0
- package/dist/runner/github-actions.d.mts +12 -0
- package/dist/runner/github-actions.mjs +97 -0
- package/dist/runner/index.d.mts +9 -0
- package/dist/runner/index.mjs +22 -0
- package/dist/runner/types.d.mts +60 -0
- package/dist/runner/types.mjs +7 -0
- package/dist/session-start.d.mts +20 -0
- package/dist/session-start.mjs +115 -0
- package/dist/site/capture.d.mts +44 -0
- package/dist/site/capture.mjs +114 -0
- package/dist/site/measure.d.mts +83 -0
- package/dist/site/measure.mjs +225 -0
- package/dist/site/pages.d.mts +10 -0
- package/dist/site/pages.mjs +71 -0
- package/dist/site/pixels.d.mts +47 -0
- package/dist/site/pixels.mjs +157 -0
- package/dist/site/servers.d.mts +47 -0
- package/dist/site/servers.mjs +141 -0
- package/dist/site.d.mts +60 -0
- package/dist/site.mjs +341 -0
- package/dist/status.d.mts +100 -0
- package/dist/status.mjs +514 -0
- package/dist/sync.d.mts +59 -0
- package/dist/sync.mjs +292 -0
- package/dist/ticket.d.mts +61 -0
- package/dist/ticket.mjs +562 -0
- package/dist/tickets/github.d.mts +128 -0
- package/dist/tickets/github.mjs +491 -0
- package/dist/tickets/index.d.mts +15 -0
- package/dist/tickets/index.mjs +27 -0
- package/dist/tickets/transport.d.mts +42 -0
- package/dist/tickets/transport.mjs +158 -0
- package/dist/tickets/types.d.mts +191 -0
- package/dist/tickets/types.mjs +76 -0
- package/docs/builds.md +227 -0
- package/docs/cli.md +221 -0
- package/docs/configuration.md +809 -0
- package/docs/design.md +106 -0
- package/docs/developing.md +400 -0
- package/docs/existing-repo.md +138 -0
- package/docs/github/reference.md +288 -0
- package/docs/github/setup.md +312 -0
- package/docs/glossary.md +106 -0
- package/docs/limitations.md +115 -0
- package/docs/plugins.md +192 -0
- package/docs/quick-start.md +222 -0
- package/docs/removing.md +76 -0
- package/docs/skills.md +270 -0
- package/docs/updating.md +108 -0
- package/docs/workflow.md +195 -0
- package/package.json +67 -4
- package/poller.Dockerfile +21 -0
- package/review.md +170 -0
- package/rules.md +313 -0
- package/settings.schema.json +411 -0
- package/skills/factory-address-review/SKILL.md +547 -0
- package/skills/factory-address-review/patch-coverage.py +68 -0
- package/skills/factory-build/SKILL.md +412 -0
- package/skills/factory-deps/SKILL.md +185 -0
- package/skills/factory-design/SKILL.md +159 -0
- package/skills/factory-diagnose/SKILL.md +93 -0
- package/skills/factory-mutate/SKILL.md +76 -0
- package/skills/factory-refine/SKILL.md +417 -0
- package/skills/factory-resolve-conflicts/SKILL.md +235 -0
- package/workflows/factory-board-sync.yml +91 -0
- package/workflows/factory-build.yml +72 -0
- package/workflows/factory-check.yml +37 -0
- package/workflows/factory-claude-review.yml +133 -0
- package/workflows/factory-conflicts.yml +69 -0
- package/workflows/factory-deps-auto-merge.yml +43 -0
- package/workflows/factory-deps.yml +59 -0
- package/workflows/factory-merge-policy.yml +121 -0
- package/workflows/factory-review-verdict.yml +54 -0
- package/workflows/factory-review.yml +75 -0
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// Starting a site with the repository's `start` command, waiting for it to
|
|
2
|
+
// answer, and stopping it. A site is a process group, so stopping it ends
|
|
3
|
+
// whatever the command started.
|
|
4
|
+
import { spawn, execFileSync } from "node:child_process";
|
|
5
|
+
import { createServer } from "node:net";
|
|
6
|
+
import { closeSync, existsSync, mkdirSync, mkdtempSync, openSync, readFileSync, rmSync, writeFileSync, } from "node:fs";
|
|
7
|
+
import { tmpdir } from "node:os";
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
export class SiteError extends Error {
|
|
10
|
+
}
|
|
11
|
+
/** A port nothing is listening on. */
|
|
12
|
+
export function freePort() {
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
const s = createServer();
|
|
15
|
+
s.once("error", reject);
|
|
16
|
+
s.listen(0, "127.0.0.1", () => {
|
|
17
|
+
const { port } = s.address();
|
|
18
|
+
s.close(() => resolve(port));
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
/** `url` with `$PORT` filled in. */
|
|
23
|
+
export const withPort = (text, port) => text.replaceAll("$PORT", String(port));
|
|
24
|
+
/** Run `start` from `cwd` with PORT set, and wait up to `minutes` for `url`
|
|
25
|
+
* to answer. Fails with the last lines the command printed. */
|
|
26
|
+
export async function startSite(opts, onSpawn = () => { }) {
|
|
27
|
+
// The command's output goes to a file, not a pipe: a pipe fills when
|
|
28
|
+
// nothing reads it, which blocks the site while a check runs and breaks it
|
|
29
|
+
// once `--keep` has let this process end.
|
|
30
|
+
const log = join(mkdtempSync(join(tmpdir(), "factory-site-log-")), "log");
|
|
31
|
+
const out = openSync(log, "a");
|
|
32
|
+
const child = spawn("sh", ["-c", opts.start], {
|
|
33
|
+
cwd: opts.cwd,
|
|
34
|
+
env: { ...process.env, PORT: String(opts.port) },
|
|
35
|
+
detached: true,
|
|
36
|
+
stdio: ["ignore", out, out],
|
|
37
|
+
});
|
|
38
|
+
closeSync(out);
|
|
39
|
+
// A site outlives the command that started it when `--keep` is given.
|
|
40
|
+
child.unref();
|
|
41
|
+
onSpawn(child.pid);
|
|
42
|
+
let exited = false;
|
|
43
|
+
child.on("exit", () => (exited = true));
|
|
44
|
+
const url = withPort(opts.url, opts.port);
|
|
45
|
+
const running = {
|
|
46
|
+
url,
|
|
47
|
+
pid: child.pid,
|
|
48
|
+
tail: () => readFileSync(log, "utf8").split("\n").filter(Boolean).slice(-15),
|
|
49
|
+
};
|
|
50
|
+
const deadline = Date.now() + opts.minutes * 60_000;
|
|
51
|
+
while (Date.now() < deadline) {
|
|
52
|
+
if (exited)
|
|
53
|
+
break;
|
|
54
|
+
try {
|
|
55
|
+
await fetch(url, { signal: AbortSignal.timeout(2000) });
|
|
56
|
+
return running;
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
await new Promise((r) => setTimeout(r, 500));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
stopProcess(running.pid);
|
|
63
|
+
throw new SiteError(`the site did not answer at ${url}${exited ? " (its command exited)" : ` in ${opts.minutes} minutes`}. Last lines it printed:\n${running.tail().join("\n")}`);
|
|
64
|
+
}
|
|
65
|
+
/** Starts every site at once, and resolves when each answers. Each process
|
|
66
|
+
* is in `pids` from the moment it is spawned: when one site fails to start,
|
|
67
|
+
* or the run is interrupted while they start, the caller's cleanup is all
|
|
68
|
+
* that stops the others. */
|
|
69
|
+
export function startSites(sites, pids) {
|
|
70
|
+
return Promise.all(sites.map((site) => startSite(site, (pid) => pids.push(pid))));
|
|
71
|
+
}
|
|
72
|
+
export function stopProcess(pid) {
|
|
73
|
+
for (const target of [-pid, pid]) {
|
|
74
|
+
try {
|
|
75
|
+
process.kill(target, "SIGTERM");
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
// Already gone.
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/** Whether the repository is a git checkout with this ref. */
|
|
84
|
+
export const defaultBranch = (root) => {
|
|
85
|
+
try {
|
|
86
|
+
return execFileSync("git", ["symbolic-ref", "--short", "refs/remotes/origin/HEAD"], { cwd: root, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim();
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
return "origin/main";
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
/** A detached worktree of `ref` in a temporary folder, with its dependencies
|
|
93
|
+
* installed when it has a lockfile. Returns its path. */
|
|
94
|
+
export function addWorktree(root, ref) {
|
|
95
|
+
const dir = join(mkdtempSync(join(tmpdir(), "factory-site-")), "base");
|
|
96
|
+
execFileSync("git", ["worktree", "add", "--detach", dir, ref], {
|
|
97
|
+
cwd: root,
|
|
98
|
+
stdio: "ignore",
|
|
99
|
+
});
|
|
100
|
+
if (existsSync(join(dir, "package-lock.json")))
|
|
101
|
+
execFileSync("npm", ["ci", "--prefer-offline", "--no-audit", "--no-fund"], {
|
|
102
|
+
cwd: dir,
|
|
103
|
+
stdio: "ignore",
|
|
104
|
+
});
|
|
105
|
+
return dir;
|
|
106
|
+
}
|
|
107
|
+
export function removeWorktree(root, dir) {
|
|
108
|
+
try {
|
|
109
|
+
execFileSync("git", ["worktree", "remove", "--force", dir], {
|
|
110
|
+
cwd: root,
|
|
111
|
+
stdio: "ignore",
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
// Already removed.
|
|
116
|
+
}
|
|
117
|
+
rmSync(join(dir, ".."), { recursive: true, force: true });
|
|
118
|
+
// A worktree whose folder is already gone is only a record until pruned.
|
|
119
|
+
try {
|
|
120
|
+
execFileSync("git", ["worktree", "prune"], { cwd: root, stdio: "ignore" });
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
// Nothing to prune.
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
const stateFile = (root) => join(root, "node_modules/.cache/factory/site/kept.json");
|
|
127
|
+
export function keep(root, kept) {
|
|
128
|
+
mkdirSync(join(stateFile(root), ".."), { recursive: true });
|
|
129
|
+
writeFileSync(stateFile(root), JSON.stringify(kept));
|
|
130
|
+
}
|
|
131
|
+
/** Ends what `--keep` left running and removes its worktree. */
|
|
132
|
+
export function stopKept(root) {
|
|
133
|
+
if (!existsSync(stateFile(root)))
|
|
134
|
+
return false;
|
|
135
|
+
const kept = JSON.parse(readFileSync(stateFile(root), "utf8"));
|
|
136
|
+
stopProcess(kept.before.pid);
|
|
137
|
+
stopProcess(kept.after.pid);
|
|
138
|
+
removeWorktree(root, kept.worktree);
|
|
139
|
+
rmSync(stateFile(root), { force: true });
|
|
140
|
+
return true;
|
|
141
|
+
}
|
package/dist/site.d.mts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { type Site } from "./config.mts";
|
|
3
|
+
import { type Status } from "./site/pixels.mts";
|
|
4
|
+
import { type PwBrowser } from "./site/capture.mts";
|
|
5
|
+
import { type Finding } from "./site/measure.mts";
|
|
6
|
+
export type Row = {
|
|
7
|
+
page: string;
|
|
8
|
+
width: number;
|
|
9
|
+
scheme: "light" | "dark";
|
|
10
|
+
status: Status;
|
|
11
|
+
pixels: number;
|
|
12
|
+
rows: [number, number] | null;
|
|
13
|
+
files: {
|
|
14
|
+
before: string;
|
|
15
|
+
after: string;
|
|
16
|
+
changed: string;
|
|
17
|
+
} | null;
|
|
18
|
+
};
|
|
19
|
+
export type Report = {
|
|
20
|
+
before: string;
|
|
21
|
+
after: string;
|
|
22
|
+
pages: string[];
|
|
23
|
+
widths: number[];
|
|
24
|
+
darkSkipped: boolean;
|
|
25
|
+
rows: Row[];
|
|
26
|
+
/** What got worse on a changed view, by what a standard or the browser
|
|
27
|
+
* defines. */
|
|
28
|
+
measured: Finding[];
|
|
29
|
+
unchanged: number;
|
|
30
|
+
folder: string;
|
|
31
|
+
};
|
|
32
|
+
/** The pages of a running site: its sitemap, or else its links. */
|
|
33
|
+
export declare function findPages(base: string): Promise<string[]>;
|
|
34
|
+
/** How many views to picture at once on a machine with this many cores. A
|
|
35
|
+
* view is already two pages, the base's and the branch's. On two cores, as
|
|
36
|
+
* a GitHub runner has, three at once starve a long page past the
|
|
37
|
+
* screenshot's limit, and one at a time is quicker there. */
|
|
38
|
+
export declare function viewsAtOnce(cores: number): number;
|
|
39
|
+
export declare function compareSites(o: {
|
|
40
|
+
root: string;
|
|
41
|
+
before: string;
|
|
42
|
+
after: string;
|
|
43
|
+
site: Pick<Site, "pages" | "widths" | "hide"> & Partial<Pick<Site, "accessibility" | "newValues">>;
|
|
44
|
+
folder: string;
|
|
45
|
+
browser: PwBrowser;
|
|
46
|
+
}): Promise<Report>;
|
|
47
|
+
export declare function table(r: Report): string[];
|
|
48
|
+
/** Run each of the repository's checks with the report on its stdin. Each
|
|
49
|
+
* non-empty line it prints is a finding; its exit code says nothing. */
|
|
50
|
+
export declare function runChecks(root: string, checks: string[], input: object): {
|
|
51
|
+
command: string;
|
|
52
|
+
findings: string[];
|
|
53
|
+
}[];
|
|
54
|
+
/** The two running sites `--before` and `--after` name, or null when the
|
|
55
|
+
* command is to start them itself. */
|
|
56
|
+
export declare function givenUrls(argv: string[]): {
|
|
57
|
+
before: string;
|
|
58
|
+
after: string;
|
|
59
|
+
} | null;
|
|
60
|
+
export declare function run(argv: string[]): Promise<number>;
|
package/dist/site.mjs
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// `factory site compare` and `factory site stop`: pictures of a website's
|
|
3
|
+
// pages before and after a change, and which pixels differ.
|
|
4
|
+
//
|
|
5
|
+
// factory site compare [--before URL --after URL] [--base REF] [--keep]
|
|
6
|
+
// factory site stop
|
|
7
|
+
//
|
|
8
|
+
// With no addresses it starts the working tree and a worktree of the base with
|
|
9
|
+
// `site.start` and stops both when it ends. It exits 0 whether or not anything
|
|
10
|
+
// changed, and non-zero only when it could not do its job.
|
|
11
|
+
import { spawnSync } from "node:child_process";
|
|
12
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
13
|
+
import { availableParallelism } from "node:os";
|
|
14
|
+
import { join } from "node:path";
|
|
15
|
+
import { loadSettings, repoRoot, SettingsError } from "./config.mjs";
|
|
16
|
+
import { crawl, normalize, sitemapPaths } from "./site/pages.mjs";
|
|
17
|
+
import { changedWithoutNoise, classify, diff, } from "./site/pixels.mjs";
|
|
18
|
+
import { encode, launch, measureView, shoot, } from "./site/capture.mjs";
|
|
19
|
+
import { addToCensus, AXE_TAGS, emptyCensus, formatFinding, newValues, newViolations, sidewaysScroll, } from "./site/measure.mjs";
|
|
20
|
+
import { addWorktree, defaultBranch, freePort, keep, removeWorktree, SiteError, startSites, stopKept, stopProcess, } from "./site/servers.mjs";
|
|
21
|
+
const slug = (p) => p.replace(/^\/|\/$/g, "").replace(/\W+/g, "-") || "home";
|
|
22
|
+
async function text(url) {
|
|
23
|
+
try {
|
|
24
|
+
const res = await fetch(url, { signal: AbortSignal.timeout(10_000) });
|
|
25
|
+
return res.ok ? await res.text() : null;
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/** The pages of a running site: its sitemap, or else its links. */
|
|
32
|
+
export async function findPages(base) {
|
|
33
|
+
const xml = await text(new URL("/sitemap.xml", base).href);
|
|
34
|
+
const fromMap = xml ? sitemapPaths(xml) : [];
|
|
35
|
+
if (fromMap.length)
|
|
36
|
+
return fromMap;
|
|
37
|
+
return crawl((p) => text(new URL(p, base).href));
|
|
38
|
+
}
|
|
39
|
+
/** How many views to picture at once on a machine with this many cores. A
|
|
40
|
+
* view is already two pages, the base's and the branch's. On two cores, as
|
|
41
|
+
* a GitHub runner has, three at once starve a long page past the
|
|
42
|
+
* screenshot's limit, and one at a time is quicker there. */
|
|
43
|
+
export function viewsAtOnce(cores) {
|
|
44
|
+
return cores >= 8 ? 3 : 1;
|
|
45
|
+
}
|
|
46
|
+
async function pool(items, size, f) {
|
|
47
|
+
let next = 0;
|
|
48
|
+
await Promise.all(Array.from({ length: Math.min(size, items.length) }, async () => {
|
|
49
|
+
while (next < items.length)
|
|
50
|
+
await f(items[next++]);
|
|
51
|
+
}));
|
|
52
|
+
}
|
|
53
|
+
/** Outline the changed pixels on the after picture. */
|
|
54
|
+
function outline(after, mask, maskWidth, rows) {
|
|
55
|
+
const data = new Uint8Array(after.data);
|
|
56
|
+
for (let y = rows[0]; y <= rows[1] && y < after.height; y++)
|
|
57
|
+
for (let x = 0; x < after.width && x < maskWidth; x++)
|
|
58
|
+
if (mask[y * maskWidth + x]) {
|
|
59
|
+
const i = (y * after.width + x) * 4;
|
|
60
|
+
data[i] = 255;
|
|
61
|
+
data[i + 1] = 0;
|
|
62
|
+
data[i + 2] = 0;
|
|
63
|
+
data[i + 3] = 255;
|
|
64
|
+
}
|
|
65
|
+
return { width: after.width, height: after.height, data };
|
|
66
|
+
}
|
|
67
|
+
export async function compareSites(o) {
|
|
68
|
+
const { site, browser } = o;
|
|
69
|
+
const pages = (site.pages ?? [
|
|
70
|
+
...new Set([
|
|
71
|
+
...(await findPages(o.before)),
|
|
72
|
+
...(await findPages(o.after)),
|
|
73
|
+
]),
|
|
74
|
+
]).map(normalize);
|
|
75
|
+
mkdirSync(o.folder, { recursive: true });
|
|
76
|
+
const schemes = ["light", "dark"];
|
|
77
|
+
// Dark mode: when the first page's dark picture is its light one, the site
|
|
78
|
+
// has none, and dark is skipped.
|
|
79
|
+
let darkSkipped = false;
|
|
80
|
+
if (pages.length) {
|
|
81
|
+
const w = site.widths[0];
|
|
82
|
+
const light = await shoot(browser, o.after, pages[0], { width: w, scheme: "light" }, site.hide);
|
|
83
|
+
const dark = await shoot(browser, o.after, pages[0], { width: w, scheme: "dark" }, site.hide);
|
|
84
|
+
darkSkipped = !!light && !!dark && diff(light, dark).count === 0;
|
|
85
|
+
}
|
|
86
|
+
const use = darkSkipped ? schemes.slice(0, 1) : schemes;
|
|
87
|
+
const rows = [];
|
|
88
|
+
const census = emptyCensus();
|
|
89
|
+
const jobs = pages.flatMap((page) => site.widths.flatMap((width) => use.map((scheme) => ({ page, width, scheme }))));
|
|
90
|
+
const atOnce = viewsAtOnce(availableParallelism());
|
|
91
|
+
await pool(jobs, atOnce, async ({ page, width, scheme }) => {
|
|
92
|
+
const view = { width, scheme };
|
|
93
|
+
const [b, a] = await Promise.all([
|
|
94
|
+
shoot(browser, o.before, page, view, site.hide, (snap) => addToCensus(census, scheme, snap.entries)),
|
|
95
|
+
shoot(browser, o.after, page, view, site.hide),
|
|
96
|
+
]);
|
|
97
|
+
let pixels = 0;
|
|
98
|
+
let changedRows = null;
|
|
99
|
+
let files = null;
|
|
100
|
+
if (a && b) {
|
|
101
|
+
let d = diff(b, a);
|
|
102
|
+
if (d.count > 0) {
|
|
103
|
+
// Shot again, both sites: what differs between two pictures of one
|
|
104
|
+
// site is noise, and a pixel has to differ in both pairs.
|
|
105
|
+
const [againB, againA] = await Promise.all([
|
|
106
|
+
shoot(browser, o.before, page, view, site.hide),
|
|
107
|
+
shoot(browser, o.after, page, view, site.hide),
|
|
108
|
+
]);
|
|
109
|
+
if (againA && againB)
|
|
110
|
+
d = changedWithoutNoise(b, a, againA, againB);
|
|
111
|
+
}
|
|
112
|
+
pixels = d.count;
|
|
113
|
+
changedRows = d.rows;
|
|
114
|
+
if (d.count > 0 && d.rows) {
|
|
115
|
+
const name = `${slug(page)}-${width}-${scheme}`;
|
|
116
|
+
files = {
|
|
117
|
+
before: `${name}-before.png`,
|
|
118
|
+
after: `${name}-after.png`,
|
|
119
|
+
changed: `${name}-changed.png`,
|
|
120
|
+
};
|
|
121
|
+
writeFileSync(join(o.folder, files.before), encode(b));
|
|
122
|
+
writeFileSync(join(o.folder, files.after), encode(a));
|
|
123
|
+
writeFileSync(join(o.folder, files.changed), encode(outline(a, d.mask, d.width, d.rows)));
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
const status = classify({ before: !!b, after: !!a }, pixels);
|
|
127
|
+
rows.push({
|
|
128
|
+
page,
|
|
129
|
+
width,
|
|
130
|
+
scheme,
|
|
131
|
+
status,
|
|
132
|
+
pixels,
|
|
133
|
+
rows: changedRows,
|
|
134
|
+
files,
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
rows.sort((x, y) => pages.indexOf(x.page) - pages.indexOf(y.page) ||
|
|
138
|
+
y.width - x.width ||
|
|
139
|
+
x.scheme.localeCompare(y.scheme));
|
|
140
|
+
// Only a view that changed is measured, once every base view is in the
|
|
141
|
+
// census: views finish out of order.
|
|
142
|
+
const level = site.accessibility ?? "wcag22aa";
|
|
143
|
+
const tags = level === "none" ? [] : AXE_TAGS[level];
|
|
144
|
+
const measured = [];
|
|
145
|
+
const toMeasure = rows.filter((r) => r.status === "changed" || r.status === "new");
|
|
146
|
+
await pool(toMeasure, atOnce, async (row) => {
|
|
147
|
+
const view = { page: row.page, width: row.width, scheme: row.scheme };
|
|
148
|
+
const [b, a] = await Promise.all([
|
|
149
|
+
row.status === "new"
|
|
150
|
+
? null
|
|
151
|
+
: measureView(browser, o.before, row.page, view, site.hide, tags),
|
|
152
|
+
measureView(browser, o.after, row.page, view, site.hide, tags),
|
|
153
|
+
]);
|
|
154
|
+
if (!a)
|
|
155
|
+
return;
|
|
156
|
+
measured.push(...newViolations(b ? b.violations : null, a.violations, view), ...sidewaysScroll(b ? b.scrollWidth : null, a.scrollWidth, view), ...(site.newValues === false ? [] : newValues(census, a.entries, view)));
|
|
157
|
+
});
|
|
158
|
+
const order = (f) => `${String(pages.indexOf(f.page)).padStart(5, "0")}|${String(100000 - f.width).padStart(6, "0")}|${f.scheme}|${f.kind}|${f.rule}|${f.element}`;
|
|
159
|
+
measured.sort((x, y) => order(x).localeCompare(order(y)));
|
|
160
|
+
const report = {
|
|
161
|
+
before: o.before,
|
|
162
|
+
after: o.after,
|
|
163
|
+
pages,
|
|
164
|
+
widths: site.widths,
|
|
165
|
+
darkSkipped,
|
|
166
|
+
rows,
|
|
167
|
+
measured,
|
|
168
|
+
unchanged: pages.filter((p) => rows.filter((r) => r.page === p).every((r) => r.status === "same")).length,
|
|
169
|
+
folder: o.folder,
|
|
170
|
+
};
|
|
171
|
+
writeFileSync(join(o.folder, "report.json"), JSON.stringify(report, null, 2));
|
|
172
|
+
return report;
|
|
173
|
+
}
|
|
174
|
+
const n = (x) => x.toLocaleString("en-US");
|
|
175
|
+
export function table(r) {
|
|
176
|
+
const lines = [
|
|
177
|
+
`${r.pages.length} pages, ${r.rows.length / Math.max(1, r.pages.length)} views each.`,
|
|
178
|
+
"",
|
|
179
|
+
];
|
|
180
|
+
for (const row of r.rows) {
|
|
181
|
+
if (row.status === "same")
|
|
182
|
+
continue;
|
|
183
|
+
const rows = row.rows ? `rows ${n(row.rows[0])} to ${n(row.rows[1])}` : "";
|
|
184
|
+
lines.push(` ${row.page.padEnd(28)} ${String(row.width).padStart(4)} ${row.scheme.padEnd(5)} ${row.status.padEnd(8)} ${n(row.pixels)} px ${rows}`.trimEnd());
|
|
185
|
+
}
|
|
186
|
+
lines.push(` ${r.unchanged} pages did not change.`);
|
|
187
|
+
if (r.darkSkipped)
|
|
188
|
+
lines.push(" The site has no dark mode, so dark was skipped.");
|
|
189
|
+
return lines;
|
|
190
|
+
}
|
|
191
|
+
/** Run each of the repository's checks with the report on its stdin. Each
|
|
192
|
+
* non-empty line it prints is a finding; its exit code says nothing. */
|
|
193
|
+
export function runChecks(root, checks, input) {
|
|
194
|
+
return checks.map((command) => {
|
|
195
|
+
const res = spawnSync("sh", ["-c", command], {
|
|
196
|
+
cwd: root,
|
|
197
|
+
input: JSON.stringify(input),
|
|
198
|
+
encoding: "utf8",
|
|
199
|
+
});
|
|
200
|
+
const findings = (res.stdout ?? "").split("\n").filter((l) => l.trim());
|
|
201
|
+
return { command, findings };
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
function flag(argv, name) {
|
|
205
|
+
const i = argv.indexOf(name);
|
|
206
|
+
return i >= 0 ? argv[i + 1] : undefined;
|
|
207
|
+
}
|
|
208
|
+
/** The two running sites `--before` and `--after` name, or null when the
|
|
209
|
+
* command is to start them itself. */
|
|
210
|
+
export function givenUrls(argv) {
|
|
211
|
+
const before = flag(argv, "--before");
|
|
212
|
+
const after = flag(argv, "--after");
|
|
213
|
+
if (before && after)
|
|
214
|
+
return { before, after };
|
|
215
|
+
if (before || after)
|
|
216
|
+
throw new SiteError(`--before and --after go together: ${before ? "--after" : "--before"} is missing`);
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
async function compare(argv) {
|
|
220
|
+
const root = repoRoot();
|
|
221
|
+
const settings = await loadSettings();
|
|
222
|
+
const site = settings.site;
|
|
223
|
+
if (!site)
|
|
224
|
+
throw new SiteError(".claude/factory.yaml: site: not written");
|
|
225
|
+
const keepRunning = argv.includes("--keep");
|
|
226
|
+
const given = givenUrls(argv);
|
|
227
|
+
let worktree = null;
|
|
228
|
+
const pids = [];
|
|
229
|
+
let started = null;
|
|
230
|
+
let browser = null;
|
|
231
|
+
// What `--keep` leaves is only what a finished comparison recorded for
|
|
232
|
+
// `factory site stop`; a failed one stops what it started.
|
|
233
|
+
let kept = false;
|
|
234
|
+
const cleanup = () => {
|
|
235
|
+
if (kept)
|
|
236
|
+
return;
|
|
237
|
+
pids.forEach(stopProcess);
|
|
238
|
+
if (worktree)
|
|
239
|
+
removeWorktree(root, worktree);
|
|
240
|
+
worktree = null;
|
|
241
|
+
};
|
|
242
|
+
for (const sig of ["SIGINT", "SIGTERM"])
|
|
243
|
+
process.once(sig, () => {
|
|
244
|
+
cleanup();
|
|
245
|
+
process.exit(130);
|
|
246
|
+
});
|
|
247
|
+
try {
|
|
248
|
+
browser = await launch(root);
|
|
249
|
+
let before;
|
|
250
|
+
let after;
|
|
251
|
+
if (given) {
|
|
252
|
+
for (const url of [given.before, given.after]) {
|
|
253
|
+
try {
|
|
254
|
+
await fetch(url, { signal: AbortSignal.timeout(5000) });
|
|
255
|
+
}
|
|
256
|
+
catch {
|
|
257
|
+
throw new SiteError(`nothing answers at ${url}`);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
before = given.before;
|
|
261
|
+
after = given.after;
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
const base = flag(argv, "--base") ?? defaultBranch(root);
|
|
265
|
+
const [pa, pb] = [await freePort(), await freePort()];
|
|
266
|
+
worktree = addWorktree(root, base);
|
|
267
|
+
const common = { start: site.start, minutes: site.startMinutes };
|
|
268
|
+
const [a, b] = await startSites([
|
|
269
|
+
{ ...common, url: site.url, cwd: root, port: pa },
|
|
270
|
+
{ ...common, url: site.url, cwd: worktree, port: pb },
|
|
271
|
+
], pids);
|
|
272
|
+
started = { after: a.pid, before: b.pid };
|
|
273
|
+
after = a.url;
|
|
274
|
+
before = b.url;
|
|
275
|
+
console.log(`Started this branch at ${after} and ${base} at ${before}.`);
|
|
276
|
+
}
|
|
277
|
+
const folder = join(root, "node_modules/.cache/factory/site", new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19));
|
|
278
|
+
const report = await compareSites({
|
|
279
|
+
root,
|
|
280
|
+
before,
|
|
281
|
+
after,
|
|
282
|
+
site,
|
|
283
|
+
folder,
|
|
284
|
+
browser,
|
|
285
|
+
});
|
|
286
|
+
for (const line of table(report))
|
|
287
|
+
console.log(line);
|
|
288
|
+
for (const f of report.measured)
|
|
289
|
+
console.log(formatFinding(f));
|
|
290
|
+
const changedPages = [
|
|
291
|
+
...new Set(report.rows.filter((r) => r.status !== "same").map((r) => r.page)),
|
|
292
|
+
];
|
|
293
|
+
const checks = runChecks(root, site.checks, {
|
|
294
|
+
before,
|
|
295
|
+
after,
|
|
296
|
+
pages: changedPages,
|
|
297
|
+
report: folder,
|
|
298
|
+
});
|
|
299
|
+
for (const c of checks)
|
|
300
|
+
for (const f of c.findings)
|
|
301
|
+
console.log(`${c.command}: ${f}`);
|
|
302
|
+
writeFileSync(join(folder, "report.json"), JSON.stringify({ ...report, checks }, null, 2));
|
|
303
|
+
console.log(`\nPictures and the full list: ${folder}/`);
|
|
304
|
+
if (keepRunning && worktree && started) {
|
|
305
|
+
kept = true;
|
|
306
|
+
keep(root, {
|
|
307
|
+
before: { url: before, pid: started.before },
|
|
308
|
+
after: { url: after, pid: started.after },
|
|
309
|
+
worktree,
|
|
310
|
+
});
|
|
311
|
+
console.log(`Left running: ${after} (this branch) and ${before} (base). \`factory site stop\` ends them.`);
|
|
312
|
+
}
|
|
313
|
+
return 0;
|
|
314
|
+
}
|
|
315
|
+
finally {
|
|
316
|
+
await browser?.close();
|
|
317
|
+
cleanup();
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
export async function run(argv) {
|
|
321
|
+
const [command, ...rest] = argv;
|
|
322
|
+
try {
|
|
323
|
+
if (command === "compare")
|
|
324
|
+
return await compare(rest);
|
|
325
|
+
if (command === "stop") {
|
|
326
|
+
stopKept(repoRoot());
|
|
327
|
+
return 0;
|
|
328
|
+
}
|
|
329
|
+
console.error("Usage: factory site compare | site stop");
|
|
330
|
+
return 2;
|
|
331
|
+
}
|
|
332
|
+
catch (e) {
|
|
333
|
+
// Anything that stopped the comparison is a job not done, not a finding.
|
|
334
|
+
if (e instanceof SiteError || e instanceof SettingsError) {
|
|
335
|
+
console.error(e.message);
|
|
336
|
+
return 1;
|
|
337
|
+
}
|
|
338
|
+
console.error(e instanceof Error ? e.message : String(e));
|
|
339
|
+
return 1;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { type Settings, type Target } from "./config.mts";
|
|
3
|
+
import { type Columns, type Board, type Derived, type PullRequest, type Issue, type RepoSnapshot, type ReviewerRun } from "./lib/work-status.mts";
|
|
4
|
+
import { type Tickets } from "./tickets/types.mts";
|
|
5
|
+
import { type CodeHost } from "./code-host/index.mts";
|
|
6
|
+
import { type Runner } from "./runner/index.mts";
|
|
7
|
+
export type Row = {
|
|
8
|
+
number: number;
|
|
9
|
+
kind: "issue" | "pull";
|
|
10
|
+
title: string;
|
|
11
|
+
derived: Derived | null;
|
|
12
|
+
pulls: PullRequest[];
|
|
13
|
+
/** Each reviewer's latest run in the card's current status. */
|
|
14
|
+
runs: ReviewerRun[];
|
|
15
|
+
flags: string[];
|
|
16
|
+
/** The card's board Cost, which the sync writes; null with none. */
|
|
17
|
+
cost: number | null;
|
|
18
|
+
};
|
|
19
|
+
/** Each reviewer's latest run on the issue since its current session began:
|
|
20
|
+
* the newest `refining` marker for a card In refinement, the newest claim
|
|
21
|
+
* for one In progress or In review. A run needs a session clock, which only
|
|
22
|
+
* those markers start, so a build's card does not carry its refinement's
|
|
23
|
+
* runs. */
|
|
24
|
+
export declare function cardRuns(issue: Issue, derived: Derived): ReviewerRun[];
|
|
25
|
+
export declare function age(since: string | null, now: Date): string;
|
|
26
|
+
export declare function isWatching(body: string, now: Date): boolean;
|
|
27
|
+
/** One row per open issue, and one per open pull request that closes no
|
|
28
|
+
* issue and builds no part of the follow-ups, such as Dependabot's. */
|
|
29
|
+
export declare function toRows(snapshot: RepoSnapshot, board: Board, blockers: Map<number, number[]>, now: Date,
|
|
30
|
+
/** Whether something starts builds from the board, so that a card left
|
|
31
|
+
* in Ready is worth flagging. False when builds.trigger is manual. */
|
|
32
|
+
watched?: boolean, columns?: Columns): {
|
|
33
|
+
rows: Row[];
|
|
34
|
+
closed: Row[];
|
|
35
|
+
};
|
|
36
|
+
/** Where a ticket's number, a pull request's number, and an Actions run's
|
|
37
|
+
* ID each lead. */
|
|
38
|
+
export type Links = {
|
|
39
|
+
ticket: (n: number) => string;
|
|
40
|
+
pull: (n: number) => string;
|
|
41
|
+
run: (id: string) => string;
|
|
42
|
+
};
|
|
43
|
+
/** Whether and how to dress the output: colors, links, and fitting to the
|
|
44
|
+
* window are for a terminal; anything else gets plain text. Numbers and
|
|
45
|
+
* runs link where `links` says, when it is given. */
|
|
46
|
+
export type Style = {
|
|
47
|
+
color: boolean;
|
|
48
|
+
width: number | null;
|
|
49
|
+
links?: Links;
|
|
50
|
+
/** The board's columns, which head the groups. */
|
|
51
|
+
columns?: Columns;
|
|
52
|
+
};
|
|
53
|
+
export declare const PLAIN: Style;
|
|
54
|
+
/** A terminal unless NO_COLOR is set to anything (no-color.org). */
|
|
55
|
+
export declare function styleFor(isTTY: boolean | undefined, columns: number | undefined, noColor: string | undefined): Style;
|
|
56
|
+
/** One reviewer's row under its card: a mark that carries the result
|
|
57
|
+
* without color, the name, the model, the result, how long it ran, and its
|
|
58
|
+
* cost in subscription dollars. */
|
|
59
|
+
export declare function runLine(run: ReviewerRun, now: Date, style: Style): string;
|
|
60
|
+
/** The board's Runs in, lowercased, before a session ID; a phrase such as
|
|
61
|
+
* "Waiting on you" as it is. A desktop app session's `local_…` ID shows as
|
|
62
|
+
* its UUID's first eight characters. */
|
|
63
|
+
export declare function sessionCell(derived: Derived | null): string;
|
|
64
|
+
export declare function formatRows({ rows, closed }: {
|
|
65
|
+
rows: Row[];
|
|
66
|
+
closed: Row[];
|
|
67
|
+
}, now: Date, withClosed?: boolean, style?: Style): string[];
|
|
68
|
+
/** The one line a failed read prints. */
|
|
69
|
+
export declare function failureLine(error: unknown): string;
|
|
70
|
+
export { targetOf, type Target } from "./config.mts";
|
|
71
|
+
/** Where a reading comes from: the ticket system, the code host's pull
|
|
72
|
+
* requests, and the runner, for the page of a run. */
|
|
73
|
+
export type Source = {
|
|
74
|
+
tickets: Tickets;
|
|
75
|
+
pulls: Pick<CodeHost, "open" | "address">;
|
|
76
|
+
runner: Pick<Runner, "runPage">;
|
|
77
|
+
};
|
|
78
|
+
export type Deps = {
|
|
79
|
+
/** The ticket system and pull requests for the target. */
|
|
80
|
+
source: (settings: Settings, target: Target) => Promise<Source>;
|
|
81
|
+
now: () => Date;
|
|
82
|
+
out: (line: string) => void;
|
|
83
|
+
sleep: (ms: number) => Promise<unknown>;
|
|
84
|
+
/** Writes screen control and whole frames for --watch, with no newline. */
|
|
85
|
+
write: (text: string) => void;
|
|
86
|
+
/** Registers what restores the terminal when --watch is interrupted. */
|
|
87
|
+
onInterrupt: (leave: () => void) => void;
|
|
88
|
+
/** Read on every draw, so --watch follows a resize. The hook is plain. */
|
|
89
|
+
style?: () => Style;
|
|
90
|
+
/** The factory's settings, read once when the command starts. */
|
|
91
|
+
settings: () => Promise<Settings>;
|
|
92
|
+
env: Record<string, string | undefined>;
|
|
93
|
+
};
|
|
94
|
+
/** A frame that overwrites the one on screen: home, each line erased to its
|
|
95
|
+
* end, then everything below. No newline ends it, which would scroll a
|
|
96
|
+
* frame as tall as the window. */
|
|
97
|
+
export declare function frame(lines: string[]): string;
|
|
98
|
+
export declare function main(argv: string[], d: Deps): Promise<number>;
|
|
99
|
+
/** `factory status [--watch]`, and `factory session-start`'s second half. */
|
|
100
|
+
export declare function run(argv: string[]): Promise<number>;
|