@williacj/rigger 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.
- package/LICENSE +21 -0
- package/README.md +370 -2
- package/package.json +56 -4
- package/scripts/doc-reference-check.mjs +59 -0
- package/scripts/gate-check.mjs +249 -0
- package/scripts/lib/pack-result.mjs +9 -0
- package/scripts/record-commit.mjs +67 -0
- package/src/cli/doctor.mjs +1021 -0
- package/src/cli/init.mjs +384 -0
- package/src/cli/once.mjs +489 -0
- package/src/cli/pause.mjs +56 -0
- package/src/cli/plan.mjs +106 -0
- package/src/cli/recording.mjs +52 -0
- package/src/cli/report.mjs +242 -0
- package/src/cli/resume.mjs +96 -0
- package/src/cli/rigger.mjs +12 -0
- package/src/cli/run.mjs +10 -0
- package/src/cli/setup-board.mjs +108 -0
- package/src/cli/verbs.mjs +82 -0
- package/src/cli/verdict.mjs +180 -0
- package/src/config/validate.mjs +960 -0
- package/src/execution/groups.mjs +185 -0
- package/src/execution/kept-output.mjs +21 -0
- package/src/execution/role.mjs +171 -0
- package/src/execution/run.mjs +490 -0
- package/src/execution/step.mjs +26 -0
- package/src/execution/workspace.mjs +866 -0
- package/src/observation/push.mjs +103 -0
- package/src/observation/report.mjs +415 -0
- package/src/observation/sink.mjs +271 -0
- package/src/package-commit.json +1 -0
- package/src/scheduling/admission.mjs +390 -0
- package/src/scheduling/clock.mjs +98 -0
- package/src/scheduling/lane.mjs +160 -0
- package/src/scheduling/loop.mjs +1738 -0
- package/src/scheduling/pull-order.mjs +46 -0
- package/src/substrate/data-ref.mjs +147 -0
- package/src/substrate/forge/assets.mjs +5 -0
- package/src/substrate/forge/graphql.mjs +258 -0
- package/src/substrate/forge/item-write.mjs +74 -0
- package/src/substrate/forge/read.mjs +998 -0
- package/src/substrate/forge/repository-write.mjs +152 -0
- package/src/substrate/forge/runners.mjs +510 -0
- package/src/substrate/forge/schema-write.mjs +70 -0
- package/src/substrate/forge/write-made.mjs +40 -0
- package/src/substrate/git-environment.mjs +54 -0
- package/src/substrate/host.mjs +23 -0
- package/src/substrate/launchd.mjs +23 -0
- package/src/substrate/process-table.sh +48 -0
- package/src/substrate/process.mjs +2474 -0
- package/src/substrate/providers/adapters.mjs +8 -0
- package/src/substrate/providers/claude.mjs +427 -0
- package/src/substrate/providers/codex.mjs +349 -0
- package/src/substrate/standard-error.mjs +134 -0
- package/src/substrate/worktrees.mjs +517 -0
- package/src/workflow/clock-mark.mjs +40 -0
- package/src/workflow/escalation.mjs +110 -0
- package/src/workflow/facts.mjs +92 -0
- package/src/workflow/first-above.mjs +17 -0
- package/src/workflow/form-check.mjs +88 -0
- package/src/workflow/gate.mjs +62 -0
- package/src/workflow/judges.mjs +695 -0
- package/src/workflow/marker.mjs +265 -0
- package/src/workflow/next-action.mjs +456 -0
- package/src/workflow/status-issue.mjs +268 -0
- package/src/workflow/transitions.mjs +588 -0
- package/templates/claude/agents/architect.md +116 -0
- package/templates/claude/agents/engineer.md +57 -0
- package/templates/claude/agents/pm.md +114 -0
- package/templates/claude/agents/reviewer.md +37 -0
- package/templates/claude/agents/spike-engineer.md +80 -0
- package/templates/claude/hooks/refuse-reserved-git-commands.mjs +909 -0
- package/templates/claude/settings.json +50 -0
- package/templates/claude/skills/acceptance/SKILL.md +275 -0
- package/templates/claude/skills/agent-style/SKILL.md +78 -0
- package/templates/claude/skills/code-review/SKILL.md +242 -0
- package/templates/claude/skills/proposal/SKILL.md +175 -0
- package/templates/claude/skills/spec-style/SKILL.md +112 -0
- package/templates/claude/skills/tdd/SKILL.md +230 -0
- package/templates/codex/config.toml +19 -0
- package/templates/doc-reference-check.md +25 -0
- package/templates/githooks/pre-push +24 -0
- package/templates/github/workflows/rigger-gate.yml +209 -0
- package/templates/launchd/com.williacj.rigger.plist +31 -0
- package/templates/rigger.config.mjs +78 -0
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
// ABOUTME: The gate check the forge's required check runs: it reads through L0's read side alone,
|
|
2
|
+
// with the job's read-only token, and hands L2's gate rule what it read. It never posts; the
|
|
3
|
+
// workflow posts on the head SHA it prints, with a state taken from its exit status alone.
|
|
4
|
+
|
|
5
|
+
// It is run as one of three modes, each named by its first argument:
|
|
6
|
+
// - `card`: the card the event concerns, printed as `card=<number>` for the first job's output;
|
|
7
|
+
// - `map`: the open pull requests the event concerns, one number per line;
|
|
8
|
+
// - `rule <number>`: the gate's answer for that pull request, exiting 0 where the rule admits.
|
|
9
|
+
// The event reaches it through the environment alone: `GITHUB_REPOSITORY`, `NUMBER`, the issue or
|
|
10
|
+
// pull request number the event names, `IS_PULL`, `true` where that number is a pull request's,
|
|
11
|
+
// and `CARD`, the first job's output.
|
|
12
|
+
|
|
13
|
+
import { mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
|
|
14
|
+
import { tmpdir } from 'node:os';
|
|
15
|
+
import { join } from 'node:path';
|
|
16
|
+
import { fileURLToPath } from 'node:url';
|
|
17
|
+
|
|
18
|
+
import { consumerConfig } from '../src/cli/doctor.mjs';
|
|
19
|
+
import { CONFIG } from '../src/cli/init.mjs';
|
|
20
|
+
import { recording } from '../src/cli/recording.mjs';
|
|
21
|
+
import { sameLabel, validate, worktreeTopic } from '../src/config/validate.mjs';
|
|
22
|
+
import { cardOfBranch, topicFor } from '../src/execution/workspace.mjs';
|
|
23
|
+
import { repositoryReads } from '../src/substrate/forge/read.mjs';
|
|
24
|
+
import { acceptanceItems, checkAcceptanceForm } from '../src/workflow/form-check.mjs';
|
|
25
|
+
import { GATE_CONTEXT, gateAnswer } from '../src/workflow/gate.mjs';
|
|
26
|
+
import { acceptanceDigest, undispatchedInstruction } from '../src/workflow/marker.mjs';
|
|
27
|
+
import { kindOf, panelOf } from '../src/workflow/next-action.mjs';
|
|
28
|
+
|
|
29
|
+
/** The exit status of a run that read no pull request to rule on, so the workflow posts nothing. */
|
|
30
|
+
const UNREAD = 2;
|
|
31
|
+
|
|
32
|
+
/** What a failed read says, prefixed with what was being read. */
|
|
33
|
+
const failedRead = (what, error) => `the read of ${what} failed: ${error.message}`;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The default branch's config, as `{ config, at, commit }`, or `{ problem }` saying why there is none:
|
|
37
|
+
* read from the default branch at the commit read at the run's start, whatever the pull request's
|
|
38
|
+
* base (`ARCHITECTURE.md`, "The required check on the forge"), through `consumerConfig`, from the
|
|
39
|
+
* one file read at that commit, validated, and naming the repository the event names. `commit` is
|
|
40
|
+
* that commit, at which this repository's self-hosting check reads its lists.
|
|
41
|
+
*/
|
|
42
|
+
export async function defaultConfig(reads, repository) {
|
|
43
|
+
let branch;
|
|
44
|
+
let text;
|
|
45
|
+
try {
|
|
46
|
+
branch = await reads.readDefaultBranch();
|
|
47
|
+
text = await reads.readFileAt(branch.head, CONFIG);
|
|
48
|
+
} catch (error) {
|
|
49
|
+
return { problem: failedRead(`the default branch's \`${CONFIG}\``, error) };
|
|
50
|
+
}
|
|
51
|
+
const at = `${branch.name} at ${branch.head}`;
|
|
52
|
+
if (text === null) return { problem: `\`${CONFIG}\` is absent from the default branch, ${at}` };
|
|
53
|
+
const directory = mkdtempSync(join(tmpdir(), 'rigger-gate-config-'));
|
|
54
|
+
let loaded;
|
|
55
|
+
try {
|
|
56
|
+
writeFileSync(join(directory, CONFIG), text);
|
|
57
|
+
loaded = await consumerConfig(directory);
|
|
58
|
+
} finally {
|
|
59
|
+
rmSync(directory, { recursive: true, force: true });
|
|
60
|
+
}
|
|
61
|
+
if (loaded.problem) return { problem: `${loaded.problem}, on the default branch, ${at}` };
|
|
62
|
+
const refusals = validate(loaded.config);
|
|
63
|
+
if (refusals.length > 0) return { problem: `\`${CONFIG}\` on the default branch, ${at}, is refused: ${refusals.join('; ')}` };
|
|
64
|
+
const named = loaded.config.repo;
|
|
65
|
+
if (typeof named !== 'string' || named.toLowerCase() !== repository.toLowerCase()) {
|
|
66
|
+
return { problem: `\`${CONFIG}\` on the default branch, ${at}, names the repository ${named}, and the event names ${repository}` };
|
|
67
|
+
}
|
|
68
|
+
return { config: loaded.config, at, commit: branch.head };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The event's number from `NUMBER`, or a throw naming it. */
|
|
72
|
+
function eventNumber(env) {
|
|
73
|
+
const number = Number(env.NUMBER);
|
|
74
|
+
if (!Number.isInteger(number) || number < 1) throw new Error(`NUMBER is ${JSON.stringify(env.NUMBER)}, which is no issue or pull request number`);
|
|
75
|
+
return number;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The `card` mode: the card the event concerns. An issue's is its own number. A pull request's is
|
|
80
|
+
* the number the default branch's topic rule names for its head branch, and where none is named,
|
|
81
|
+
* or the config cannot be read, the pull request's own number, so its runs still share one group.
|
|
82
|
+
*/
|
|
83
|
+
export async function card({ env, reads, out, log }) {
|
|
84
|
+
const number = eventNumber(env);
|
|
85
|
+
if (env.IS_PULL !== 'true') {
|
|
86
|
+
out(`card=${number}`);
|
|
87
|
+
return 0;
|
|
88
|
+
}
|
|
89
|
+
let pull;
|
|
90
|
+
try {
|
|
91
|
+
pull = await reads.readPullRequestByNumber(number);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
log(failedRead(`pull request #${number}`, error));
|
|
94
|
+
return UNREAD;
|
|
95
|
+
}
|
|
96
|
+
const { config, problem } = await defaultConfig(reads, env.GITHUB_REPOSITORY);
|
|
97
|
+
if (problem) log(problem);
|
|
98
|
+
const resolved = config === undefined ? null : cardOfBranch(worktreeTopic(config), pull.branch);
|
|
99
|
+
if (resolved === null) log(`pull request #${number}'s branch ${pull.branch} names no card, so its own number keys its runs`);
|
|
100
|
+
out(`card=${resolved ?? number}`);
|
|
101
|
+
return 0;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The `map` mode: the open pull requests the event concerns, one per line. A pull request's event
|
|
106
|
+
* concerns that pull request. An issue's concerns the open pull requests from the branch the
|
|
107
|
+
* default branch's topic rule names for it, which may be none.
|
|
108
|
+
*/
|
|
109
|
+
export async function map({ env, reads, out, log }) {
|
|
110
|
+
const number = eventNumber(env);
|
|
111
|
+
if (env.IS_PULL === 'true') {
|
|
112
|
+
out(String(number));
|
|
113
|
+
return 0;
|
|
114
|
+
}
|
|
115
|
+
const { config, problem } = await defaultConfig(reads, env.GITHUB_REPOSITORY);
|
|
116
|
+
if (problem) {
|
|
117
|
+
log(problem);
|
|
118
|
+
return 1;
|
|
119
|
+
}
|
|
120
|
+
const branch = topicFor(worktreeTopic(config), number);
|
|
121
|
+
let open;
|
|
122
|
+
try {
|
|
123
|
+
({ open } = await reads.readPullRequests(branch));
|
|
124
|
+
} catch (error) {
|
|
125
|
+
log(failedRead(`the pull requests from ${branch}`, error));
|
|
126
|
+
return 1;
|
|
127
|
+
}
|
|
128
|
+
if (open.length === 0) log(`issue #${number} has no open pull request from ${branch}, so nothing is posted`);
|
|
129
|
+
for (const pull of open) out(String(pull.number));
|
|
130
|
+
return 0;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* The `rule` mode: reads pull request `number` and everything the rule rules on, after the run
|
|
135
|
+
* starts, and prints its head SHA, the facts it read and every refusal. A judge the rule still
|
|
136
|
+
* needs has the marker block it must post printed beside its required provider.
|
|
137
|
+
*/
|
|
138
|
+
async function rule({ env, reads, number, out, log }) {
|
|
139
|
+
out(`# ${GATE_CONTEXT} on pull request #${number}`);
|
|
140
|
+
out('');
|
|
141
|
+
let pull;
|
|
142
|
+
try {
|
|
143
|
+
pull = await reads.readPullRequestByNumber(number);
|
|
144
|
+
} catch (error) {
|
|
145
|
+
log(failedRead(`pull request #${number}`, error));
|
|
146
|
+
return UNREAD;
|
|
147
|
+
}
|
|
148
|
+
out(`head: ${pull.head}`);
|
|
149
|
+
out(`base: ${pull.base}`);
|
|
150
|
+
const refuse = (...reasons) => {
|
|
151
|
+
for (const reason of reasons) out(`refused: ${reason}`);
|
|
152
|
+
return 1;
|
|
153
|
+
};
|
|
154
|
+
if (!pull.fromRepository) {
|
|
155
|
+
return refuse(pull.headRepository === null
|
|
156
|
+
? `pull request #${number}'s head repository was deleted, so its head is not ${env.GITHUB_REPOSITORY}'s`
|
|
157
|
+
: `pull request #${number} is from ${pull.headRepository}, not ${env.GITHUB_REPOSITORY}`);
|
|
158
|
+
}
|
|
159
|
+
if (pull.state !== 'open') return refuse(`pull request #${number} is ${pull.state}, not open`);
|
|
160
|
+
const { config, at, problem } = await defaultConfig(reads, env.GITHUB_REPOSITORY);
|
|
161
|
+
if (problem) return refuse(problem);
|
|
162
|
+
out(`config: \`${CONFIG}\` on the default branch, ${at}`);
|
|
163
|
+
const topic = worktreeTopic(config);
|
|
164
|
+
const cardNumber = cardOfBranch(topic, pull.branch);
|
|
165
|
+
if (cardNumber === null) return refuse(`pull request #${number} matches no card: the topic rule \`${topic}\` names no card's branch ${pull.branch}`);
|
|
166
|
+
out(`card: #${cardNumber}`);
|
|
167
|
+
let issue;
|
|
168
|
+
try {
|
|
169
|
+
issue = await reads.readIssue(cardNumber);
|
|
170
|
+
} catch (error) {
|
|
171
|
+
return refuse(failedRead(`issue #${cardNumber}`, error));
|
|
172
|
+
}
|
|
173
|
+
if (issue === null) return refuse(`pull request #${number}'s branch ${pull.branch} names #${cardNumber}, which is no issue in ${env.GITHUB_REPOSITORY}`);
|
|
174
|
+
const held = { number: issue.number, title: issue.title, body: issue.body, labels: issue.labels };
|
|
175
|
+
const { kind, answer } = kindOf(held, config.kinds, config.epicLabel);
|
|
176
|
+
if (answer?.action === 'ignore') {
|
|
177
|
+
return refuse(issue.labels.some((label) => sameLabel(label, config.epicLabel))
|
|
178
|
+
? `card #${cardNumber} carries the epic label ${config.epicLabel}, so no kind selects it`
|
|
179
|
+
: `no kind selects card #${cardNumber}`);
|
|
180
|
+
}
|
|
181
|
+
if (answer !== undefined) return refuse(`card #${cardNumber} is ${answer.reason}`);
|
|
182
|
+
const form = checkAcceptanceForm(held);
|
|
183
|
+
if (!form.admitted) return refuse(`card #${cardNumber}'s acceptance fails the form check: ${form.reason}`);
|
|
184
|
+
const judges = panelOf(held, config.kinds[kind], config.roles);
|
|
185
|
+
out(`judges: ${judges.map((judge) => (judge.conflict !== undefined ? `(conflict: ${judge.conflict})` : judge.provider === undefined ? judge.role : `${judge.role} on ${judge.provider}`)).join(', ')}`);
|
|
186
|
+
let comments;
|
|
187
|
+
try {
|
|
188
|
+
comments = await reads.readComments(number);
|
|
189
|
+
} catch (error) {
|
|
190
|
+
return refuse(failedRead(`pull request #${number}'s comments`, error));
|
|
191
|
+
}
|
|
192
|
+
const authors = new Map(comments.map((comment) => [comment.author, comment.permission]));
|
|
193
|
+
for (const [author, permission] of authors) out(`permission: ${author ?? '(a deleted account)'} ${permission}`);
|
|
194
|
+
const items = acceptanceItems(issue.body);
|
|
195
|
+
const round = { card: cardNumber, pull: number, head: pull.head, digest: acceptanceDigest(issue.body), count: items.length };
|
|
196
|
+
// The forge enforces the required checks itself, so the rule is handed none.
|
|
197
|
+
const ruled = gateAnswer({ comments, ...round, judges, checks: {} });
|
|
198
|
+
if (ruled.admit) {
|
|
199
|
+
out('admitted');
|
|
200
|
+
return 0;
|
|
201
|
+
}
|
|
202
|
+
refuse(...ruled.refusals.map((refusal) => `${refusal.judge ?? refusal.check}: ${refusal.reason}`));
|
|
203
|
+
const needed = new Set(ruled.refusals.map((refusal) => refusal.judge));
|
|
204
|
+
for (const judge of judges.filter((entry) => entry.conflict === undefined && needed.has(entry.role))) {
|
|
205
|
+
out('');
|
|
206
|
+
out(judge.provider === undefined ? `## The marker ${judge.role} must post` : `## The marker ${judge.role} must post, on provider ${judge.provider}`);
|
|
207
|
+
out('');
|
|
208
|
+
out(undispatchedInstruction({ ...round, role: judge.role, provider: judge.provider, items }));
|
|
209
|
+
}
|
|
210
|
+
return 1;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const MODES = { card, map, rule };
|
|
214
|
+
|
|
215
|
+
/** Runs the mode `argv` names, against the repository the environment names, and answers its exit status. */
|
|
216
|
+
async function main(argv, env) {
|
|
217
|
+
const [mode, given] = argv;
|
|
218
|
+
const log = (line) => process.stderr.write(`${line}\n`);
|
|
219
|
+
const out = (line) => process.stdout.write(`${line}\n`);
|
|
220
|
+
if (!Object.hasOwn(MODES, mode)) {
|
|
221
|
+
log(`gate-check: the mode is ${JSON.stringify(mode)}, and it is one of ${Object.keys(MODES).join(', ')}`);
|
|
222
|
+
return UNREAD;
|
|
223
|
+
}
|
|
224
|
+
if (!/^[^/\s]+\/[^/\s]+$/.test(env.GITHUB_REPOSITORY ?? '')) {
|
|
225
|
+
log(`gate-check: GITHUB_REPOSITORY is ${JSON.stringify(env.GITHUB_REPOSITORY)}, which names no repository as owner/name`);
|
|
226
|
+
return UNREAD;
|
|
227
|
+
}
|
|
228
|
+
const number = Number(given);
|
|
229
|
+
if (mode === 'rule' && (!Number.isInteger(number) || number < 1)) {
|
|
230
|
+
log(`gate-check: rule was given ${JSON.stringify(given)}, which is no pull request number`);
|
|
231
|
+
return UNREAD;
|
|
232
|
+
}
|
|
233
|
+
return recording(async ({ sink }) => {
|
|
234
|
+
// The gate reads the repository the event names and no board, so its reads name none.
|
|
235
|
+
const reads = repositoryReads({ repo: env.GITHUB_REPOSITORY, project: 'none' }, { emitter: sink.emitter({ layer: 'L0' }) });
|
|
236
|
+
try {
|
|
237
|
+
return await MODES[mode]({ env, reads, number, out, log });
|
|
238
|
+
} catch (error) {
|
|
239
|
+
log(`gate-check: ${error.message}`);
|
|
240
|
+
return UNREAD;
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// Run as a script, it runs the mode its arguments name. Imported, as this repository's
|
|
246
|
+
// self-hosting check imports it, it runs none. The path it was run by is resolved first, because
|
|
247
|
+
// the module's own is.
|
|
248
|
+
const ranDirectly = process.argv[1] !== undefined && realpathSync(process.argv[1]) === fileURLToPath(import.meta.url);
|
|
249
|
+
if (ranDirectly) process.exitCode = await main(process.argv.slice(2), process.env);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// ABOUTME: Selects npm's package metadata from its observed single-package JSON forms.
|
|
2
|
+
|
|
3
|
+
// #1213's controlled comparison measured an array from npm 11.20.0 and a package-keyed
|
|
4
|
+
// object from npm 12.2.2. npm owns the metadata; selection leaves its fields unchanged (D16).
|
|
5
|
+
export function packResult(stdout) {
|
|
6
|
+
const packages = Object.values(JSON.parse(stdout));
|
|
7
|
+
if (packages.length !== 1) throw new Error('npm pack must return exactly one package');
|
|
8
|
+
return packages[0];
|
|
9
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// ABOUTME: The prepack script: records the commit the package is packed from in
|
|
2
|
+
// src/package-commit.json, which `init` pins the gate workflow to, and only where the pack holds
|
|
3
|
+
// exactly that commit's files; otherwise it removes any record, so nothing pins code not packed.
|
|
4
|
+
|
|
5
|
+
import { execFileSync } from 'node:child_process';
|
|
6
|
+
import { readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
7
|
+
import { dirname, join } from 'node:path';
|
|
8
|
+
import { fileURLToPath } from 'node:url';
|
|
9
|
+
|
|
10
|
+
import { gitEnvironment } from '../src/substrate/git-environment.mjs';
|
|
11
|
+
import { packResult } from './lib/pack-result.mjs';
|
|
12
|
+
|
|
13
|
+
/** The package this script ships in: the directory above `scripts/`. */
|
|
14
|
+
const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
|
|
15
|
+
|
|
16
|
+
/** The record, as a path relative to the package. `.gitignore` names it, and `files` carries it. */
|
|
17
|
+
const RECORD = 'src/package-commit.json';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* What `command` printed on stdout run in the package under `env`, or null where it did not exit
|
|
21
|
+
* 0. git is handed the environment `gitEnvironment` gives, so a pack run under a git hook asks
|
|
22
|
+
* about this package and not the repository the hook names.
|
|
23
|
+
*/
|
|
24
|
+
function answer(command, args, env = command === 'git' ? gitEnvironment() : process.env) {
|
|
25
|
+
try {
|
|
26
|
+
return execFileSync(command, args, { cwd: ROOT, env, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] });
|
|
27
|
+
} catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The commit to record: `HEAD`'s full SHA, or null where the package is outside a git repository,
|
|
34
|
+
* has no commit, or the pack would not hold exactly `HEAD`'s files. npm is asked what the pack
|
|
35
|
+
* carries (`D16`), with scripts off so this script does not run again; git is asked what `HEAD`
|
|
36
|
+
* holds. The pack holds `HEAD`'s files where:
|
|
37
|
+
* 1. every path the pack carries is a file `HEAD` holds, whatever git's ignore rules say of it;
|
|
38
|
+
* 2. every file `HEAD` holds that `files` names, or holds under a directory `files` names, is one the
|
|
39
|
+
* pack carries;
|
|
40
|
+
* 3. no path the pack carries differs from `HEAD` in the tree.
|
|
41
|
+
*/
|
|
42
|
+
function commitToRecord() {
|
|
43
|
+
const head = answer('git', ['rev-parse', '--verify', '--quiet', 'HEAD^{commit}'])?.trim();
|
|
44
|
+
if (!head) return null;
|
|
45
|
+
const listed = answer('npm', ['pack', '--dry-run', '--json', '--ignore-scripts', '--loglevel=error']);
|
|
46
|
+
if (listed === null) return null;
|
|
47
|
+
const packed = packResult(listed).files.map((file) => file.path).filter((path) => path !== RECORD);
|
|
48
|
+
const held = answer('git', ['ls-tree', '-r', '-z', '--name-only', head]);
|
|
49
|
+
if (held === null) return null;
|
|
50
|
+
const atHead = new Set(held.split('\0').filter(Boolean));
|
|
51
|
+
if (!packed.every((path) => atHead.has(path))) return null;
|
|
52
|
+
const declared = JSON.parse(readFileSync(join(ROOT, 'package.json'), 'utf8')).files ?? [];
|
|
53
|
+
const under = [...atHead].filter((path) => declared.some((entry) => path === entry || path.startsWith(entry.endsWith('/') ? entry : `${entry}/`)));
|
|
54
|
+
if (!under.every((path) => packed.includes(path))) return null;
|
|
55
|
+
return answer('git', ['diff', '--quiet', head, '--', ...packed]) === null ? null : head;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const commit = commitToRecord();
|
|
59
|
+
const record = join(ROOT, RECORD);
|
|
60
|
+
if (commit === null) {
|
|
61
|
+
rmSync(record, { force: true });
|
|
62
|
+
} else {
|
|
63
|
+
// Written whole and then moved into place, so a pack running beside this one never reads half.
|
|
64
|
+
const written = `${record}.${process.pid}`;
|
|
65
|
+
writeFileSync(written, `${JSON.stringify({ commit })}\n`);
|
|
66
|
+
renameSync(written, record);
|
|
67
|
+
}
|