@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,97 @@
|
|
|
1
|
+
/** The workflow a build is a run of. */
|
|
2
|
+
export const BUILD_WORKFLOW = "factory-build.yml";
|
|
3
|
+
const REVIEW_WORKFLOW = "factory-claude-review.yml";
|
|
4
|
+
const SITE = "https://github.com";
|
|
5
|
+
/** A terminal's escape sequences, such as the color codes a job's log is
|
|
6
|
+
* full of. */
|
|
7
|
+
const ESCAPES = /\u001b\[[0-9;?]*[ -/]*[@-~]/g;
|
|
8
|
+
/** How a job ended without passing. */
|
|
9
|
+
const FAILED = ["failure", "timed_out", "startup_failure"];
|
|
10
|
+
export function githubActionsRunner(o) {
|
|
11
|
+
const t = o.transport;
|
|
12
|
+
const API = `repos/${o.repository}`;
|
|
13
|
+
const reviewsOf = async (sha) => (await t.rest("GET", `${API}/actions/workflows/${REVIEW_WORKFLOW}/runs?head_sha=${sha}&per_page=5`)).workflow_runs;
|
|
14
|
+
return {
|
|
15
|
+
async startBuild(ticket, branch) {
|
|
16
|
+
await t.rest("POST", `${API}/actions/workflows/${BUILD_WORKFLOW}/dispatches`, { fields: { ref: branch, inputs: { ticket: String(ticket) } } });
|
|
17
|
+
},
|
|
18
|
+
// GitHub answers a dispatch with no run. The build workflow names each
|
|
19
|
+
// run `Build #N`, and GitHub's times are whole seconds, so `since` is
|
|
20
|
+
// rounded down to one.
|
|
21
|
+
async findBuild(ticket, since) {
|
|
22
|
+
const from = Math.floor(since / 1000) * 1000;
|
|
23
|
+
const answer = await t.rest("GET", `${API}/actions/workflows/${BUILD_WORKFLOW}/runs?event=workflow_dispatch&per_page=30`);
|
|
24
|
+
const found = answer.workflow_runs.find((r) => r.display_title === `Build #${ticket}` &&
|
|
25
|
+
Date.parse(r.created_at) >= from);
|
|
26
|
+
return found?.html_url ?? null;
|
|
27
|
+
},
|
|
28
|
+
buildsPage: () => `${SITE}/${o.repository}/actions/workflows/${BUILD_WORKFLOW}`,
|
|
29
|
+
runPage: (id) => `${SITE}/${o.repository}/actions/runs/${id}`,
|
|
30
|
+
thisRun(env) {
|
|
31
|
+
if (env.GITHUB_ACTIONS !== "true")
|
|
32
|
+
return null;
|
|
33
|
+
const id = env.GITHUB_RUN_ID || "unknown";
|
|
34
|
+
const server = env.GITHUB_SERVER_URL || SITE;
|
|
35
|
+
const page = id !== "unknown" && env.GITHUB_REPOSITORY
|
|
36
|
+
? `${server}/${env.GITHUB_REPOSITORY}/actions/runs/${id}`
|
|
37
|
+
: undefined;
|
|
38
|
+
return {
|
|
39
|
+
id,
|
|
40
|
+
...(page ? { page } : {}),
|
|
41
|
+
...(env.GITHUB_ACTOR ? { startedBy: env.GITHUB_ACTOR } : {}),
|
|
42
|
+
};
|
|
43
|
+
},
|
|
44
|
+
async runs(sha) {
|
|
45
|
+
const answer = await t.rest("GET", `${API}/actions/runs?head_sha=${sha}&per_page=100`);
|
|
46
|
+
return answer.workflow_runs.map((r) => ({
|
|
47
|
+
id: r.id,
|
|
48
|
+
jobs: r.workflow_id,
|
|
49
|
+
event: r.event,
|
|
50
|
+
status: r.status,
|
|
51
|
+
conclusion: r.conclusion,
|
|
52
|
+
checksId: r.check_suite_id,
|
|
53
|
+
}));
|
|
54
|
+
},
|
|
55
|
+
async reviewRuns(sha) {
|
|
56
|
+
return (await reviewsOf(sha)).map(({ status, conclusion }) => ({
|
|
57
|
+
status,
|
|
58
|
+
conclusion,
|
|
59
|
+
}));
|
|
60
|
+
},
|
|
61
|
+
async reviewAgain(sha) {
|
|
62
|
+
const [newest] = await reviewsOf(sha);
|
|
63
|
+
if (!newest)
|
|
64
|
+
return false;
|
|
65
|
+
await t.rest("POST", `${API}/actions/runs/${newest.id}/rerun`);
|
|
66
|
+
return true;
|
|
67
|
+
},
|
|
68
|
+
async failedJobs(run) {
|
|
69
|
+
const { jobs } = await t.rest("GET", `${API}/actions/runs/${run}/jobs?per_page=100`);
|
|
70
|
+
const failed = [];
|
|
71
|
+
for (const job of jobs) {
|
|
72
|
+
if (!FAILED.includes(job.conclusion ?? ""))
|
|
73
|
+
continue;
|
|
74
|
+
failed.push({
|
|
75
|
+
name: job.name,
|
|
76
|
+
log: (await t.text(`${API}/actions/jobs/${job.id}/logs`)).replace(ESCAPES, ""),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
return failed;
|
|
80
|
+
},
|
|
81
|
+
async secretNames() {
|
|
82
|
+
// The list is an object, not an array, so it is paged by hand.
|
|
83
|
+
const names = [];
|
|
84
|
+
for (let page = 1;; page++) {
|
|
85
|
+
const listed = await t.rest("GET", `${API}/actions/secrets?per_page=100&page=${page}`);
|
|
86
|
+
names.push(...listed.secrets.map((x) => x.name));
|
|
87
|
+
if (listed.secrets.length < 100)
|
|
88
|
+
return names;
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
async setSecret(name, value) {
|
|
92
|
+
if (!o.cli)
|
|
93
|
+
throw new Error("Storing a secret needs the GitHub CLI.");
|
|
94
|
+
await o.cli(["secret", "set", name, "--repo", o.repository], value);
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type Gh, type GhIn } from "../lib/gh.mts";
|
|
2
|
+
import type { Runner } from "./types.mts";
|
|
3
|
+
export * from "./types.mts";
|
|
4
|
+
export declare const DEFAULT_RUNNER: "github-actions";
|
|
5
|
+
/** The message for a runner that is not built. */
|
|
6
|
+
export declare const unsupportedRunner: (name: string) => string;
|
|
7
|
+
/** The runner of that name for the repository, reached through the GitHub
|
|
8
|
+
* CLI. */
|
|
9
|
+
export declare function openRunner(repository: string, name?: string, run?: Gh, runIn?: GhIn): Runner;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Which runner a name picks, built for a command in a session or CI.
|
|
2
|
+
// `github-actions` is the only one, and the settings refuse any other value
|
|
3
|
+
// of `builds.runner` before this runs (`config.mts`). A command that has not
|
|
4
|
+
// read the settings asks for the default.
|
|
5
|
+
import { gh, ghIn } from "../lib/gh.mjs";
|
|
6
|
+
import { ghTransport } from "../tickets/transport.mjs";
|
|
7
|
+
import { githubActionsRunner } from "./github-actions.mjs";
|
|
8
|
+
export * from "./types.mjs";
|
|
9
|
+
export const DEFAULT_RUNNER = "github-actions";
|
|
10
|
+
/** The message for a runner that is not built. */
|
|
11
|
+
export const unsupportedRunner = (name) => `builds.runner "${name}" is not supported. "github-actions" is the only runner built.`;
|
|
12
|
+
/** The runner of that name for the repository, reached through the GitHub
|
|
13
|
+
* CLI. */
|
|
14
|
+
export function openRunner(repository, name = DEFAULT_RUNNER, run = gh, runIn = ghIn) {
|
|
15
|
+
if (name !== "github-actions")
|
|
16
|
+
throw new Error(unsupportedRunner(name));
|
|
17
|
+
return githubActionsRunner({
|
|
18
|
+
transport: ghTransport(run),
|
|
19
|
+
repository,
|
|
20
|
+
cli: runIn,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/** One run of the repository's jobs for a commit. `status` is `completed`
|
|
2
|
+
* once it has ended, and `conclusion` says how. */
|
|
3
|
+
export type Run = {
|
|
4
|
+
id: number;
|
|
5
|
+
/** The runner's ID for the set of jobs this is a run of, so two runs of
|
|
6
|
+
* the same set can be told from runs of another. */
|
|
7
|
+
jobs: number;
|
|
8
|
+
/** What started it, such as `push` or `pull_request`. */
|
|
9
|
+
event: string;
|
|
10
|
+
status: string;
|
|
11
|
+
conclusion: string | null;
|
|
12
|
+
/** The ID the run shares with the checks it reports on its commit. */
|
|
13
|
+
checksId: number;
|
|
14
|
+
};
|
|
15
|
+
/** The run a session is in. */
|
|
16
|
+
export type ThisRun = {
|
|
17
|
+
/** `unknown` when the runner does not say. */
|
|
18
|
+
id: string;
|
|
19
|
+
page?: string;
|
|
20
|
+
/** The account that started it. */
|
|
21
|
+
startedBy?: string;
|
|
22
|
+
};
|
|
23
|
+
/** A job that failed, and everything it printed. */
|
|
24
|
+
export type FailedJob = {
|
|
25
|
+
name: string;
|
|
26
|
+
log: string;
|
|
27
|
+
};
|
|
28
|
+
export type Runner = {
|
|
29
|
+
/** Starts a build of the ticket on the branch. The runner may answer
|
|
30
|
+
* before the run exists, so `findBuild` finds it. */
|
|
31
|
+
startBuild(ticket: number, branch: string): Promise<void>;
|
|
32
|
+
/** The page of the ticket's build started at or after `since`, in
|
|
33
|
+
* milliseconds, or null when none shows yet. */
|
|
34
|
+
findBuild(ticket: number, since: number): Promise<string | null>;
|
|
35
|
+
/** The page that lists builds. */
|
|
36
|
+
buildsPage(): string;
|
|
37
|
+
/** The page of a run, from its ID. */
|
|
38
|
+
runPage(id: string): string;
|
|
39
|
+
/** The run this session is in, read from its environment, or null
|
|
40
|
+
* outside one. */
|
|
41
|
+
thisRun(env: Record<string, string | undefined>): ThisRun | null;
|
|
42
|
+
/** Every run for a commit. */
|
|
43
|
+
runs(sha: string): Promise<Run[]>;
|
|
44
|
+
/** The newest runs of the review of a commit, newest first. */
|
|
45
|
+
reviewRuns(sha: string): Promise<{
|
|
46
|
+
status: string;
|
|
47
|
+
conclusion: string | null;
|
|
48
|
+
}[]>;
|
|
49
|
+
/** Has the review of a commit run again, and answers false when none
|
|
50
|
+
* has run on it. */
|
|
51
|
+
reviewAgain(sha: string): Promise<boolean>;
|
|
52
|
+
/** The jobs that failed in one run, each with its log. Which runs count
|
|
53
|
+
* is the caller's: `factory pr failed` takes those `wait-checks` waits on. */
|
|
54
|
+
failedJobs(run: number): Promise<FailedJob[]>;
|
|
55
|
+
/** The names of the secrets the runner's jobs can read. */
|
|
56
|
+
secretNames(): Promise<string[]>;
|
|
57
|
+
/** Stores a secret the runner's jobs can read. The value never appears
|
|
58
|
+
* in a command's arguments or its output. */
|
|
59
|
+
setSecret(name: string, value: string): Promise<void>;
|
|
60
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// The one interface every runner's implementation fills: where an
|
|
2
|
+
// unattended build runs. The factory's commands start a build and read its
|
|
3
|
+
// runs only through it. The job files a runner needs, such as GitHub
|
|
4
|
+
// Actions' workflows, are its own and no part of this.
|
|
5
|
+
//
|
|
6
|
+
// Node built-ins only: the poller's image copies this file.
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
type Git = (args: string[]) => {
|
|
2
|
+
ok: boolean;
|
|
3
|
+
out: string;
|
|
4
|
+
};
|
|
5
|
+
export declare const gitIn: (cwd: string) => Git;
|
|
6
|
+
/** Each local branch but main with work origin/main lacks, as
|
|
7
|
+
* "NAME (N commits not on origin/main)". Pull requests are squash-merged, so
|
|
8
|
+
* a merged branch still has commits main lacks: it counts as merged when
|
|
9
|
+
* merging it into main would change nothing. */
|
|
10
|
+
export declare function unmergedBranches(git: Git): string[];
|
|
11
|
+
/** What the hook prints before the tickets' state. */
|
|
12
|
+
export declare function repositoryState(git: Git): string[];
|
|
13
|
+
/** A line telling the person to run `npm ci` when `node_modules` is older
|
|
14
|
+
* than `package-lock.json`, or null when it is current or there is no
|
|
15
|
+
* lockfile. Without it, a command installed since the lockfile last changed,
|
|
16
|
+
* such as `factory` once it became a workspace package, is missing and the
|
|
17
|
+
* shell's "Permission denied" says nothing of why. */
|
|
18
|
+
export declare function installBehind(dir: string): string | null;
|
|
19
|
+
export declare function run(): Promise<number>;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// The SessionStart hook, run as `factory session-start`. Its output opens
|
|
2
|
+
// the session's context, so work left on another branch is visible before
|
|
3
|
+
// anything new starts: the checkout's state, the local branches with work
|
|
4
|
+
// not on origin/main, then each ticket's state.
|
|
5
|
+
import { spawnSync } from "node:child_process";
|
|
6
|
+
import { readFileSync } from "node:fs";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
export const gitIn = (cwd) => (args) => {
|
|
9
|
+
const r = spawnSync("git", args, {
|
|
10
|
+
cwd,
|
|
11
|
+
encoding: "utf8",
|
|
12
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
13
|
+
});
|
|
14
|
+
return { ok: r.status === 0, out: r.stdout ?? "" };
|
|
15
|
+
};
|
|
16
|
+
/** Each local branch but main with work origin/main lacks, as
|
|
17
|
+
* "NAME (N commits not on origin/main)". Pull requests are squash-merged, so
|
|
18
|
+
* a merged branch still has commits main lacks: it counts as merged when
|
|
19
|
+
* merging it into main would change nothing. */
|
|
20
|
+
export function unmergedBranches(git) {
|
|
21
|
+
const mainTree = git(["rev-parse", "origin/main^{tree}"]).out.trim();
|
|
22
|
+
const branches = git([
|
|
23
|
+
"for-each-ref",
|
|
24
|
+
"--format=%(refname:short)",
|
|
25
|
+
"refs/heads",
|
|
26
|
+
])
|
|
27
|
+
.out.split("\n")
|
|
28
|
+
.filter((b) => b && b !== "main");
|
|
29
|
+
const found = [];
|
|
30
|
+
for (const b of branches) {
|
|
31
|
+
const merged = git(["merge-tree", "--write-tree", "origin/main", b]);
|
|
32
|
+
if (merged.ok && merged.out.split("\n")[0] === mainTree)
|
|
33
|
+
continue;
|
|
34
|
+
const count = Number(git(["rev-list", "--count", `origin/main..${b}`]).out.trim() || 0);
|
|
35
|
+
if (count > 0)
|
|
36
|
+
found.push(`${b} (${count} commits not on origin/main)`);
|
|
37
|
+
}
|
|
38
|
+
return found;
|
|
39
|
+
}
|
|
40
|
+
/** What the hook prints before the tickets' state. */
|
|
41
|
+
export function repositoryState(git) {
|
|
42
|
+
git(["fetch", "-q", "origin", "main"]);
|
|
43
|
+
const lines = [
|
|
44
|
+
"## Repository state at session start",
|
|
45
|
+
"",
|
|
46
|
+
"```",
|
|
47
|
+
git(["status", "-sb"]).out.trimEnd(),
|
|
48
|
+
"```",
|
|
49
|
+
"",
|
|
50
|
+
];
|
|
51
|
+
const unmerged = unmergedBranches(git);
|
|
52
|
+
if (unmerged.length) {
|
|
53
|
+
lines.push("Local branches with work not on origin/main:", "", ...unmerged.map((b) => `- ${b}`), "", "Mention any of these to the user before starting work on another branch.");
|
|
54
|
+
}
|
|
55
|
+
return lines;
|
|
56
|
+
}
|
|
57
|
+
const readPackages = (file) => {
|
|
58
|
+
try {
|
|
59
|
+
return JSON.parse(readFileSync(file, "utf8")).packages ?? null;
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
/** A line telling the person to run `npm ci` when `node_modules` is older
|
|
66
|
+
* than `package-lock.json`, or null when it is current or there is no
|
|
67
|
+
* lockfile. Without it, a command installed since the lockfile last changed,
|
|
68
|
+
* such as `factory` once it became a workspace package, is missing and the
|
|
69
|
+
* shell's "Permission denied" says nothing of why. */
|
|
70
|
+
export function installBehind(dir) {
|
|
71
|
+
const wanted = readPackages(join(dir, "package-lock.json"));
|
|
72
|
+
if (!wanted)
|
|
73
|
+
return null;
|
|
74
|
+
const line = "Your installed dependencies are behind package-lock.json: run `npm ci`.";
|
|
75
|
+
const installed = readPackages(join(dir, "node_modules", ".package-lock.json"));
|
|
76
|
+
if (!installed)
|
|
77
|
+
return line;
|
|
78
|
+
for (const [name, entry] of Object.entries(wanted)) {
|
|
79
|
+
if (name === "" || entry.optional)
|
|
80
|
+
continue;
|
|
81
|
+
const have = installed[name];
|
|
82
|
+
if (!have)
|
|
83
|
+
return line;
|
|
84
|
+
if (entry.version !== undefined && have.version !== entry.version)
|
|
85
|
+
return line;
|
|
86
|
+
}
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
export async function run() {
|
|
90
|
+
const dir = process.env.CLAUDE_PROJECT_DIR || process.cwd();
|
|
91
|
+
try {
|
|
92
|
+
process.chdir(dir);
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return 0;
|
|
96
|
+
}
|
|
97
|
+
console.log(repositoryState(gitIn(dir)).join("\n"));
|
|
98
|
+
const behind = installBehind(dir);
|
|
99
|
+
if (behind)
|
|
100
|
+
console.log(`\n${behind}\n`);
|
|
101
|
+
// What each ticket's board card says, and who is working on it. The status
|
|
102
|
+
// command enforces its own time limit, since a hook the harness kills
|
|
103
|
+
// prints nothing.
|
|
104
|
+
const error = console.error;
|
|
105
|
+
console.error = () => { };
|
|
106
|
+
try {
|
|
107
|
+
return await (await import("./status.mjs")).run(["--hook"]);
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
return 0;
|
|
111
|
+
}
|
|
112
|
+
finally {
|
|
113
|
+
console.error = error;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { Picture } from "./pixels.mts";
|
|
2
|
+
import { type AxeViolation, type Snapshot } from "./measure.mts";
|
|
3
|
+
export declare const NO_BROWSER = "site needs a browser: npx playwright install chromium";
|
|
4
|
+
type PwPage = {
|
|
5
|
+
goto: (u: string, o: object) => Promise<{
|
|
6
|
+
status: () => number;
|
|
7
|
+
} | null>;
|
|
8
|
+
addStyleTag: (o: {
|
|
9
|
+
content: string;
|
|
10
|
+
}) => Promise<unknown>;
|
|
11
|
+
evaluate: (f: string) => Promise<unknown>;
|
|
12
|
+
addScriptTag: (o: {
|
|
13
|
+
content: string;
|
|
14
|
+
}) => Promise<unknown>;
|
|
15
|
+
waitForLoadState: (s: string, o: object) => Promise<void>;
|
|
16
|
+
screenshot: (o: object) => Promise<Buffer>;
|
|
17
|
+
};
|
|
18
|
+
type PwContext = {
|
|
19
|
+
addInitScript: (s: string) => Promise<void>;
|
|
20
|
+
newPage: () => Promise<PwPage>;
|
|
21
|
+
close: () => Promise<void>;
|
|
22
|
+
};
|
|
23
|
+
export type PwBrowser = {
|
|
24
|
+
newContext: (o: object) => Promise<PwContext>;
|
|
25
|
+
close: () => Promise<void>;
|
|
26
|
+
};
|
|
27
|
+
export declare function launch(root: string): Promise<PwBrowser>;
|
|
28
|
+
/** One picture of `path` at `base`, or null when the site has no such page.
|
|
29
|
+
* `census` also gets the page's style values, from the same load. */
|
|
30
|
+
export declare function shoot(browser: PwBrowser, base: string, path: string, view: {
|
|
31
|
+
width: number;
|
|
32
|
+
scheme: "light" | "dark";
|
|
33
|
+
}, hide: string[], census?: (s: Snapshot) => void): Promise<Picture | null>;
|
|
34
|
+
/** What measuring a view needs: its style values, how wide it scrolls, and
|
|
35
|
+
* axe-core's failures at the given tags (none when `tags` is empty). */
|
|
36
|
+
export declare function measureView(browser: PwBrowser, base: string, path: string, view: {
|
|
37
|
+
width: number;
|
|
38
|
+
scheme: "light" | "dark";
|
|
39
|
+
}, hide: string[], tags: string[]): Promise<(Snapshot & {
|
|
40
|
+
violations: AxeViolation[];
|
|
41
|
+
}) | null>;
|
|
42
|
+
export declare function decode(png: Buffer): Picture;
|
|
43
|
+
export declare function encode(p: Picture): Buffer;
|
|
44
|
+
export {};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// Taking a picture of a page the same way every time: motion off, media
|
|
2
|
+
// paused at its start, fonts loaded, the network idle. Needs Playwright from
|
|
3
|
+
// the repository, and its browser.
|
|
4
|
+
import { readFileSync } from "node:fs";
|
|
5
|
+
import { createRequire } from "node:module";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { PNG } from "pngjs";
|
|
8
|
+
import { axeRun, COLLECT, tidy, } from "./measure.mjs";
|
|
9
|
+
import { SiteError } from "./servers.mjs";
|
|
10
|
+
export const NO_BROWSER = "site needs a browser: npx playwright install chromium";
|
|
11
|
+
export async function launch(root) {
|
|
12
|
+
const require = createRequire(join(root, "package.json"));
|
|
13
|
+
let chromium;
|
|
14
|
+
try {
|
|
15
|
+
chromium = require("playwright").chromium;
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
throw new SiteError("site needs Playwright: npm install -D playwright");
|
|
19
|
+
}
|
|
20
|
+
try {
|
|
21
|
+
return await chromium.launch();
|
|
22
|
+
}
|
|
23
|
+
catch (e) {
|
|
24
|
+
if (/Executable doesn't exist|browserType\.launch/.test(String(e)))
|
|
25
|
+
throw new SiteError(NO_BROWSER);
|
|
26
|
+
throw e;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const STILL = `*, *::before, *::after {
|
|
30
|
+
animation: none !important; transition: none !important;
|
|
31
|
+
caret-color: transparent !important; scroll-behavior: auto !important;
|
|
32
|
+
}`;
|
|
33
|
+
// A page that calls play() itself, when its video can play, must not start it
|
|
34
|
+
// again after the picture's pause.
|
|
35
|
+
const NO_PLAY = `HTMLMediaElement.prototype.play = function () {
|
|
36
|
+
this.pause();
|
|
37
|
+
return Promise.resolve();
|
|
38
|
+
};`;
|
|
39
|
+
const PAUSE = `(async () => {
|
|
40
|
+
for (const m of document.querySelectorAll('video, audio')) {
|
|
41
|
+
m.pause();
|
|
42
|
+
try { m.currentTime = 0; } catch {}
|
|
43
|
+
}
|
|
44
|
+
await new Promise((r) => setTimeout(r, 300));
|
|
45
|
+
if (document.fonts) await document.fonts.ready;
|
|
46
|
+
})()`;
|
|
47
|
+
/** Open `path` at `base` the same way every time and call `run` on the page;
|
|
48
|
+
* null when the site has no such page. */
|
|
49
|
+
async function inView(browser, base, path, view, hide, run) {
|
|
50
|
+
const ctx = await browser.newContext({
|
|
51
|
+
viewport: { width: view.width, height: 800 },
|
|
52
|
+
colorScheme: view.scheme,
|
|
53
|
+
reducedMotion: "reduce",
|
|
54
|
+
});
|
|
55
|
+
try {
|
|
56
|
+
await ctx.addInitScript(NO_PLAY);
|
|
57
|
+
const page = await ctx.newPage();
|
|
58
|
+
const res = await page.goto(new URL(path, base).href, {
|
|
59
|
+
waitUntil: "load",
|
|
60
|
+
});
|
|
61
|
+
if (!res || res.status() >= 400)
|
|
62
|
+
return null;
|
|
63
|
+
await page
|
|
64
|
+
.waitForLoadState("networkidle", { timeout: 5000 })
|
|
65
|
+
.catch(() => { });
|
|
66
|
+
const hidden = hide.length
|
|
67
|
+
? `${hide.join(", ")} { visibility: hidden !important; }`
|
|
68
|
+
: "";
|
|
69
|
+
await page.addStyleTag({ content: STILL + hidden });
|
|
70
|
+
await page.evaluate(PAUSE);
|
|
71
|
+
return await run(page);
|
|
72
|
+
}
|
|
73
|
+
finally {
|
|
74
|
+
await ctx.close();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/** One picture of `path` at `base`, or null when the site has no such page.
|
|
78
|
+
* `census` also gets the page's style values, from the same load. */
|
|
79
|
+
export async function shoot(browser, base, path, view, hide, census) {
|
|
80
|
+
return inView(browser, base, path, view, hide, async (page) => {
|
|
81
|
+
if (census)
|
|
82
|
+
census(tidied((await page.evaluate(COLLECT))));
|
|
83
|
+
const png = await page.screenshot({
|
|
84
|
+
fullPage: true,
|
|
85
|
+
caret: "hide",
|
|
86
|
+
timeout: 180_000,
|
|
87
|
+
});
|
|
88
|
+
return decode(png);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
const tidied = (s) => ({ ...s, entries: tidy(s.entries) });
|
|
92
|
+
/** What measuring a view needs: its style values, how wide it scrolls, and
|
|
93
|
+
* axe-core's failures at the given tags (none when `tags` is empty). */
|
|
94
|
+
export async function measureView(browser, base, path, view, hide, tags) {
|
|
95
|
+
return inView(browser, base, path, view, hide, async (page) => {
|
|
96
|
+
const snap = tidied((await page.evaluate(COLLECT)));
|
|
97
|
+
let violations = [];
|
|
98
|
+
if (tags.length) {
|
|
99
|
+
const src = readFileSync(createRequire(import.meta.url).resolve("axe-core/axe.min.js"), "utf8");
|
|
100
|
+
await page.addScriptTag({ content: src });
|
|
101
|
+
violations = (await page.evaluate(axeRun(tags)));
|
|
102
|
+
}
|
|
103
|
+
return { ...snap, violations };
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
export function decode(png) {
|
|
107
|
+
const p = PNG.sync.read(png);
|
|
108
|
+
return { width: p.width, height: p.height, data: p.data };
|
|
109
|
+
}
|
|
110
|
+
export function encode(p) {
|
|
111
|
+
const png = new PNG({ width: p.width, height: p.height });
|
|
112
|
+
png.data = Buffer.from(p.data);
|
|
113
|
+
return PNG.sync.write(png);
|
|
114
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { Accessibility } from "../config.mts";
|
|
2
|
+
export type Scheme = "light" | "dark";
|
|
3
|
+
/** The axe-core tags that make up each level. */
|
|
4
|
+
export declare const AXE_TAGS: Record<Exclude<Accessibility, "none">, string[]>;
|
|
5
|
+
/** One failing element, as axe-core reports it, cut to what is used. */
|
|
6
|
+
export type AxeNode = {
|
|
7
|
+
selector: string;
|
|
8
|
+
html: string;
|
|
9
|
+
words: string;
|
|
10
|
+
};
|
|
11
|
+
export type AxeViolation = {
|
|
12
|
+
id: string;
|
|
13
|
+
nodes: AxeNode[];
|
|
14
|
+
};
|
|
15
|
+
/** One value of one style property in a view, with the first element that
|
|
16
|
+
* has it and how many have it. */
|
|
17
|
+
export type Entry = {
|
|
18
|
+
prop: Prop;
|
|
19
|
+
value: string;
|
|
20
|
+
tag: string;
|
|
21
|
+
text: string;
|
|
22
|
+
count: number;
|
|
23
|
+
};
|
|
24
|
+
export type Snapshot = {
|
|
25
|
+
/** The widest the page scrolls, in px. */
|
|
26
|
+
scrollWidth: number;
|
|
27
|
+
entries: Entry[];
|
|
28
|
+
};
|
|
29
|
+
export declare const SIZE_PROPS: readonly ["font-size", "padding", "border-radius", "gap"];
|
|
30
|
+
export declare const COLOR_PROPS: readonly ["color", "background-color", "border-color"];
|
|
31
|
+
export type Prop = (typeof SIZE_PROPS)[number] | (typeof COLOR_PROPS)[number];
|
|
32
|
+
export type Finding = {
|
|
33
|
+
page: string;
|
|
34
|
+
width: number;
|
|
35
|
+
scheme: Scheme;
|
|
36
|
+
kind: "accessibility" | "sideways scroll" | "new value";
|
|
37
|
+
rule: string | null;
|
|
38
|
+
element: string;
|
|
39
|
+
words: string;
|
|
40
|
+
};
|
|
41
|
+
export type View = {
|
|
42
|
+
page: string;
|
|
43
|
+
width: number;
|
|
44
|
+
scheme: Scheme;
|
|
45
|
+
};
|
|
46
|
+
/** The line a finding prints, as `factory site compare` gives it. */
|
|
47
|
+
export declare function formatFinding(f: Finding): string;
|
|
48
|
+
/** The failures `after` has that `before` lacks: a failure is the base's when
|
|
49
|
+
* it is the same rule on an element with the same selector or the same
|
|
50
|
+
* markup. `before` is null for a page the base does not have. */
|
|
51
|
+
export declare function newViolations(before: AxeViolation[] | null, after: AxeViolation[], view: View): Finding[];
|
|
52
|
+
/** One finding when the branch's page is wider than its window and the base's
|
|
53
|
+
* is not. `before` is the base's scroll width, or null for a missing page. */
|
|
54
|
+
export declare function sidewaysScroll(before: number | null, after: number, view: View): Finding[];
|
|
55
|
+
/** What the base site uses: each scheme's values of each property. */
|
|
56
|
+
export type Census = Record<Scheme, Record<string, Set<string>>>;
|
|
57
|
+
export declare const emptyCensus: () => Census;
|
|
58
|
+
export declare function addToCensus(c: Census, scheme: Scheme, entries: Entry[]): void;
|
|
59
|
+
/** The sizes the site uses for a property: all of them, or the 12 nearest
|
|
60
|
+
* to `near` when there are more, in ascending order. */
|
|
61
|
+
export declare function nearestSizes(used: Set<string>, near: number): string[];
|
|
62
|
+
/** One finding for each value of a property on a changed view of the branch
|
|
63
|
+
* that the base uses in no view of that scheme. */
|
|
64
|
+
export declare function newValues(base: Census, after: Entry[], view: View): Finding[];
|
|
65
|
+
/** A size as the census keeps it, or null to leave it out: pixels rounded to
|
|
66
|
+
* a tenth, and a percentage, which a radius can compute to, as written. */
|
|
67
|
+
export declare function sizeValue(raw: string): string | null;
|
|
68
|
+
/** Whether a computed color is fully transparent. Chrome keeps a color's
|
|
69
|
+
* own notation, so the alpha is read after a comma, as in `rgba(0, 0, 0, 0)`,
|
|
70
|
+
* and after a slash, as in `oklab(0 0 0 / 0)`. */
|
|
71
|
+
export declare function isClear(color: string): boolean;
|
|
72
|
+
/** The entries as the census keeps them, from what the page reported: a size
|
|
73
|
+
* rounded, a fully transparent color left out, and entries that come to the
|
|
74
|
+
* same value joined, the first naming the element. */
|
|
75
|
+
export declare function tidy(raw: Entry[]): Entry[];
|
|
76
|
+
/** Run in the page: every visible element's values as the browser computed
|
|
77
|
+
* them, which `tidy` then rounds and filters. A border's color is reported
|
|
78
|
+
* only where the border has a width. A padding is its four sides, a radius
|
|
79
|
+
* its four corners, and a gap its row and column. */
|
|
80
|
+
export declare const COLLECT = "(() => {\n const found = new Map();\n const add = (prop, value, el) => {\n if (!value) return;\n const key = prop + '|' + value;\n const hit = found.get(key);\n if (hit) { hit.count++; return; }\n const own = [...el.childNodes].filter((n) => n.nodeType === 3)\n .map((n) => n.textContent).join(' ').replace(/\\s+/g, ' ').trim();\n const text = (own || (el.textContent || '').replace(/\\s+/g, ' ').trim()).slice(0, 40);\n found.set(key, { prop, value, tag: el.tagName.toLowerCase(), text, count: 1 });\n };\n for (const el of document.body.querySelectorAll('*')) {\n if (['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEMPLATE', 'HEAD'].includes(el.tagName)) continue;\n const r = el.getBoundingClientRect();\n const s = getComputedStyle(el);\n if (!r.width || !r.height || s.display === 'none' || s.visibility === 'hidden') continue;\n const hasText = [...el.childNodes].some((n) => n.nodeType === 3 && n.textContent.trim());\n if (hasText) {\n add('font-size', s.fontSize, el);\n add('color', s.color, el);\n }\n add('background-color', s.backgroundColor, el);\n for (const side of ['Top', 'Right', 'Bottom', 'Left']) {\n if (parseFloat(s['border' + side + 'Width']) > 0)\n add('border-color', s['border' + side + 'Color'], el);\n add('padding', s['padding' + side], el);\n }\n for (const c of ['TopLeft', 'TopRight', 'BottomRight', 'BottomLeft'])\n add('border-radius', s['border' + c + 'Radius'].split(' ')[0], el);\n add('gap', s.rowGap, el);\n add('gap', s.columnGap, el);\n }\n return { scrollWidth: document.documentElement.scrollWidth, entries: [...found.values()] };\n})()";
|
|
81
|
+
/** Run in the page after axe-core's source: the failures of the given tags,
|
|
82
|
+
* outside frames. */
|
|
83
|
+
export declare const axeRun: (tags: string[]) => string;
|