@zgeoff/chex 0.8.6 → 0.8.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/package.json +5 -2
- package/src/ci-plan/build-ci-plan-output.ts +72 -0
- package/src/ci-plan/collect-shared-checks.ts +102 -0
- package/src/ci-plan/parse-ci-plan.ts +299 -0
- package/src/ci-plan/parse-docs-checks.ts +1 -0
- package/src/ci-plan/write-ci-plan-json.ts +21 -0
- package/src/cli/parse-chex-args.ts +7 -2
- package/src/cli/run-chex.ts +2 -0
- package/src/cli.ts +9 -0
package/README.md
CHANGED
|
@@ -83,7 +83,8 @@ Some docs checks skip files even when you name them:
|
|
|
83
83
|
`chex init` writes the `extends` line and a
|
|
84
84
|
[`chex-baseline` job](../../docs/architecture/pre-push-baseline.md#a-checkout-without-the-baseline)
|
|
85
85
|
into `lefthook.yml`, adds a `check:docs` script of `chex prose length links readmes skill` when the
|
|
86
|
-
repo has none, and changes nothing on a second run. `chex ci-plan` plans a `bun-checks` run
|
|
86
|
+
repo has none, and changes nothing on a second run. `chex ci-plan` plans a `bun-checks` run, and
|
|
87
|
+
`--json` prints the [`.github/ci.yml`](../../docs/guides/ci-settings.md) plan.
|
|
87
88
|
|
|
88
89
|
## The pre-push baseline
|
|
89
90
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zgeoff/chex",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.7",
|
|
4
4
|
"description": "Run each shared docs and repo check as one subcommand, with the lefthook pre-push baseline that calls them",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"checks",
|
|
@@ -42,6 +42,7 @@
|
|
|
42
42
|
"./src/make-file-probe.ts": "./src/uncommitted/make-file-probe.ts",
|
|
43
43
|
"./src/parse-added-lines.ts": "./src/local-paths/parse-added-lines.ts",
|
|
44
44
|
"./src/parse-chex-args.ts": "./src/cli/parse-chex-args.ts",
|
|
45
|
+
"./src/parse-ci-plan.ts": "./src/ci-plan/parse-ci-plan.ts",
|
|
45
46
|
"./src/parse-lane-limits.ts": "./src/test-timeouts/parse-lane-limits.ts",
|
|
46
47
|
"./src/parse-lefthook-config.ts": "./src/lefthook/parse-lefthook-config.ts",
|
|
47
48
|
"./src/parse-pushed-refs.ts": "./src/push/parse-pushed-refs.ts",
|
|
@@ -66,6 +67,7 @@
|
|
|
66
67
|
"./src/run-workflows-check.ts": "./src/workflows/run-workflows-check.ts",
|
|
67
68
|
"./src/shell-escape-parsers.ts": "./src/shell/shell-escape-parsers.ts",
|
|
68
69
|
"./src/workflow-context.ts": "./src/workflows/workflow-context.ts",
|
|
70
|
+
"./src/write-ci-plan-json.ts": "./src/ci-plan/write-ci-plan-json.ts",
|
|
69
71
|
"./src/write-step-outputs.ts": "./src/output/write-step-outputs.ts",
|
|
70
72
|
"./src/write-text-file.ts": "./src/output/write-text-file.ts",
|
|
71
73
|
"./*": "./*"
|
|
@@ -79,7 +81,8 @@
|
|
|
79
81
|
"dependencies": {
|
|
80
82
|
"@zgeoff/std": "0.8.1",
|
|
81
83
|
"oxc-parser": "0.160.0",
|
|
82
|
-
"ts-pattern": "5.9.2"
|
|
84
|
+
"ts-pattern": "5.9.2",
|
|
85
|
+
"yaml": "2.9.1"
|
|
83
86
|
},
|
|
84
87
|
"devDependencies": {
|
|
85
88
|
"@types/bun": "1.4.4",
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { parseCIPlan } from './parse-ci-plan.ts';
|
|
2
|
+
|
|
3
|
+
export interface CIPlanOutput {
|
|
4
|
+
readonly exitCode: number;
|
|
5
|
+
readonly lines: readonly string[];
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function buildCIPlanOutput(
|
|
9
|
+
settingsText: string | undefined,
|
|
10
|
+
packageText: string | undefined,
|
|
11
|
+
): CIPlanOutput {
|
|
12
|
+
const packageScripts = parsePackageScripts(packageText);
|
|
13
|
+
|
|
14
|
+
if (packageScripts.kind === 'invalid') {
|
|
15
|
+
return buildErrorOutput([packageScripts.message]);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const outcome = parseCIPlan({ settingsText, packageScripts: packageScripts.scripts });
|
|
19
|
+
|
|
20
|
+
if (outcome.kind === 'invalid') {
|
|
21
|
+
return buildErrorOutput(outcome.errors);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return { exitCode: 0, lines: [JSON.stringify(outcome.plan)] };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
type PackageScripts =
|
|
28
|
+
| { readonly kind: 'read'; readonly scripts: Readonly<Record<string, string>> | undefined }
|
|
29
|
+
| { readonly kind: 'invalid'; readonly message: string };
|
|
30
|
+
|
|
31
|
+
function parsePackageScripts(packageText: string | undefined): PackageScripts {
|
|
32
|
+
if (packageText === undefined) {
|
|
33
|
+
return { kind: 'read', scripts: undefined };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let packageJSON: unknown;
|
|
37
|
+
|
|
38
|
+
try {
|
|
39
|
+
packageJSON = JSON.parse(packageText);
|
|
40
|
+
} catch {
|
|
41
|
+
return { kind: 'invalid', message: 'package.json is not valid JSON; fix the syntax.' };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (typeof packageJSON !== 'object' || packageJSON === null || !('scripts' in packageJSON)) {
|
|
45
|
+
return { kind: 'read', scripts: {} };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const scripts = packageJSON.scripts;
|
|
49
|
+
|
|
50
|
+
if (typeof scripts !== 'object' || scripts === null) {
|
|
51
|
+
return { kind: 'read', scripts: {} };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
kind: 'read',
|
|
56
|
+
scripts: Object.fromEntries(
|
|
57
|
+
Object.entries(scripts).filter(
|
|
58
|
+
(entry): entry is [string, string] => typeof entry[1] === 'string',
|
|
59
|
+
),
|
|
60
|
+
),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function buildErrorOutput(messages: readonly string[]): CIPlanOutput {
|
|
65
|
+
return { exitCode: 1, lines: messages.map((message) => `::error::${escapeData(message)}`) };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// A runner reads each line of stdout as a workflow command, so a newline in a message would start
|
|
69
|
+
// a second command.
|
|
70
|
+
function escapeData(message: string): string {
|
|
71
|
+
return message.replaceAll('%', '%25').replaceAll('\r', '%0D').replaceAll('\n', '%0A');
|
|
72
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { parseChexArgs } from '../cli/parse-chex-args.ts';
|
|
2
|
+
|
|
3
|
+
export function collectSharedChecks(command: string | undefined): readonly string[] {
|
|
4
|
+
if (command === undefined) {
|
|
5
|
+
return [];
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
return splitSimpleCommands(command).flatMap((words) => {
|
|
9
|
+
const chexArgs = findChexArgs(words);
|
|
10
|
+
|
|
11
|
+
if (chexArgs === undefined) {
|
|
12
|
+
return [];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const request = parseChexArgs(chexArgs);
|
|
16
|
+
|
|
17
|
+
return request.kind === 'checks' ? request.names : [];
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Splits a command line into simple commands at &&, ||, ; and |, and into words with the quotes
|
|
22
|
+
// removed, so that "chex" reads as chex and a quoted operator stays in its word.
|
|
23
|
+
function splitSimpleCommands(command: string): string[][] {
|
|
24
|
+
const commands: string[][] = [[]];
|
|
25
|
+
let word: string | undefined;
|
|
26
|
+
let quote: string | undefined;
|
|
27
|
+
|
|
28
|
+
const endWord = (): void => {
|
|
29
|
+
if (word !== undefined) {
|
|
30
|
+
commands.at(-1)?.push(word);
|
|
31
|
+
word = undefined;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
for (const char of command) {
|
|
36
|
+
if (quote !== undefined) {
|
|
37
|
+
if (char === quote) {
|
|
38
|
+
quote = undefined;
|
|
39
|
+
} else {
|
|
40
|
+
word = (word ?? '') + char;
|
|
41
|
+
}
|
|
42
|
+
} else if (char === '"' || char === "'") {
|
|
43
|
+
quote = char;
|
|
44
|
+
word ??= '';
|
|
45
|
+
} else if (/\s/u.test(char)) {
|
|
46
|
+
endWord();
|
|
47
|
+
} else if (char === '&' || char === '|' || char === ';') {
|
|
48
|
+
endWord();
|
|
49
|
+
|
|
50
|
+
if (commands.at(-1)?.length !== 0) {
|
|
51
|
+
commands.push([]);
|
|
52
|
+
}
|
|
53
|
+
} else {
|
|
54
|
+
word = (word ?? '') + char;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
endWord();
|
|
59
|
+
|
|
60
|
+
return commands.filter((words) => words.length > 0);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const chexWord = /(?:^|\/)chex$|^@zgeoff\/chex(?:@\S*)?$/u;
|
|
64
|
+
const assignment = /^\w+=/u;
|
|
65
|
+
|
|
66
|
+
// The arguments after chex when the command runs chex itself, as a path, or through bunx, npx or
|
|
67
|
+
// bun x. A command that only mentions chex as an argument, such as echo chex, runs no check.
|
|
68
|
+
function findChexArgs(words: readonly string[]): readonly string[] | undefined {
|
|
69
|
+
const start = words.findIndex((word) => !assignment.test(word));
|
|
70
|
+
const [first, second] = words.slice(start);
|
|
71
|
+
|
|
72
|
+
if (start === -1 || first === undefined) {
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (chexWord.test(first)) {
|
|
77
|
+
return words.slice(start + 1);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const runnerLength = runnerWordCount(first, second);
|
|
81
|
+
|
|
82
|
+
if (runnerLength === 0) {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const rest = words.slice(start + runnerLength);
|
|
87
|
+
const toolIndex = rest.findIndex((word) => !word.startsWith('-'));
|
|
88
|
+
const tool = rest[toolIndex];
|
|
89
|
+
const args = rest.slice(toolIndex + 1);
|
|
90
|
+
|
|
91
|
+
return tool !== undefined && chexWord.test(tool) ? args : undefined;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const runners = new Set(['bunx', 'npx']);
|
|
95
|
+
|
|
96
|
+
function runnerWordCount(first: string, second: string | undefined): number {
|
|
97
|
+
if (runners.has(first)) {
|
|
98
|
+
return 1;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return first === 'bun' && second === 'x' ? 2 : 0;
|
|
102
|
+
}
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { LineCounter, parseDocument } from 'yaml';
|
|
2
|
+
import { collectSharedChecks } from './collect-shared-checks.ts';
|
|
3
|
+
import { laneNames } from './plan-check-scripts.ts';
|
|
4
|
+
|
|
5
|
+
export type SlowTestRule = { readonly runWhenChanged: readonly string[] } | 'always' | 'daily';
|
|
6
|
+
|
|
7
|
+
export interface CIPlan {
|
|
8
|
+
readonly scripts: readonly string[];
|
|
9
|
+
readonly nodeVersion: string | null;
|
|
10
|
+
readonly timeoutMinutes: number;
|
|
11
|
+
readonly slowTests: Readonly<Record<string, SlowTestRule>>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type CIPlanOutcome =
|
|
15
|
+
| { readonly kind: 'planned'; readonly plan: CIPlan }
|
|
16
|
+
| { readonly kind: 'invalid'; readonly errors: readonly string[] };
|
|
17
|
+
|
|
18
|
+
export interface CIPlanSource {
|
|
19
|
+
// The text of .github/ci.yml, or undefined when the repo has no such file.
|
|
20
|
+
readonly settingsText: string | undefined;
|
|
21
|
+
|
|
22
|
+
// The scripts package.json defines, by name, or undefined when the repo has no package.json.
|
|
23
|
+
readonly packageScripts: Readonly<Record<string, string>> | undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const standardScripts = ['format:check', 'lint', 'typecheck', 'deadcode', 'test'] as const;
|
|
27
|
+
const defaultTimeoutMinutes = 20;
|
|
28
|
+
const knownKeys = ['scripts', 'node-version', 'timeout-minutes', 'slow-tests'];
|
|
29
|
+
const settingsFile = '.github/ci.yml';
|
|
30
|
+
|
|
31
|
+
export function parseCIPlan(source: CIPlanSource): CIPlanOutcome {
|
|
32
|
+
const definedScripts = Object.keys(source.packageScripts ?? {});
|
|
33
|
+
const standard = standardScripts.filter((name) => definedScripts.includes(name));
|
|
34
|
+
|
|
35
|
+
if (source.settingsText === undefined || source.settingsText.trim() === '') {
|
|
36
|
+
return { kind: 'planned', plan: buildPlan(standard, null, defaultTimeoutMinutes, {}) };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const parsed = parseYAMLText(source.settingsText);
|
|
40
|
+
|
|
41
|
+
if (parsed.kind === 'invalid') {
|
|
42
|
+
return parsed;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const document = parsed.value;
|
|
46
|
+
|
|
47
|
+
if (document === null) {
|
|
48
|
+
return { kind: 'planned', plan: buildPlan(standard, null, defaultTimeoutMinutes, {}) };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (!isRecord(document)) {
|
|
52
|
+
return invalid([`${settingsFile} must be a map of settings; write keys such as scripts.`]);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const unknownKeys = Object.keys(document)
|
|
56
|
+
.filter((key) => !knownKeys.includes(key))
|
|
57
|
+
.map(
|
|
58
|
+
(key) =>
|
|
59
|
+
`${settingsFile} has the unknown key "${key}"; remove it, or use one of ${knownKeys.join(', ')}.`,
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
const scripts = parseScripts(document['scripts'], source.packageScripts, standard);
|
|
63
|
+
const nodeVersion = parseNodeVersion(document['node-version']);
|
|
64
|
+
const timeoutMinutes = parseTimeoutMinutes(document['timeout-minutes']);
|
|
65
|
+
const slowTests = parseSlowTests(document['slow-tests']);
|
|
66
|
+
|
|
67
|
+
const errors = [
|
|
68
|
+
...unknownKeys,
|
|
69
|
+
...scripts.errors,
|
|
70
|
+
...nodeVersion.errors,
|
|
71
|
+
...timeoutMinutes.errors,
|
|
72
|
+
...slowTests.errors,
|
|
73
|
+
];
|
|
74
|
+
|
|
75
|
+
if (errors.length > 0) {
|
|
76
|
+
return invalid(errors);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
kind: 'planned',
|
|
81
|
+
plan: buildPlan(scripts.value, nodeVersion.value, timeoutMinutes.value, slowTests.value),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function buildPlan(
|
|
86
|
+
scripts: readonly string[],
|
|
87
|
+
nodeVersion: string | null,
|
|
88
|
+
timeoutMinutes: number,
|
|
89
|
+
slowTests: Readonly<Record<string, SlowTestRule>>,
|
|
90
|
+
): CIPlan {
|
|
91
|
+
return { scripts, nodeVersion, timeoutMinutes, slowTests };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function parseYAMLText(
|
|
95
|
+
text: string,
|
|
96
|
+
):
|
|
97
|
+
| { readonly kind: 'parsed'; readonly value: unknown }
|
|
98
|
+
| { readonly kind: 'invalid'; readonly errors: readonly string[] } {
|
|
99
|
+
const lineCounter = new LineCounter();
|
|
100
|
+
|
|
101
|
+
const document = parseDocument(text, { lineCounter, uniqueKeys: true });
|
|
102
|
+
|
|
103
|
+
if (document.errors.length > 0) {
|
|
104
|
+
return {
|
|
105
|
+
kind: 'invalid',
|
|
106
|
+
errors: document.errors.map((error) => {
|
|
107
|
+
const line = error.linePos?.[0].line;
|
|
108
|
+
const place = line === undefined ? '' : ` at line ${line}`;
|
|
109
|
+
|
|
110
|
+
return error.code === 'DUPLICATE_KEY'
|
|
111
|
+
? `${settingsFile} repeats a key${place}; keep one.`
|
|
112
|
+
: `${settingsFile} is not valid YAML (${error.message.split('\n')[0]}); fix the syntax.`;
|
|
113
|
+
}),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return { kind: 'parsed', value: document.toJS() };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
121
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function invalid(errors: readonly string[]): CIPlanOutcome {
|
|
125
|
+
return { kind: 'invalid', errors };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
interface Read<Value> {
|
|
129
|
+
readonly value: Value;
|
|
130
|
+
readonly errors: readonly string[];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function parseScripts(
|
|
134
|
+
value: unknown,
|
|
135
|
+
packageScripts: Readonly<Record<string, string>> | undefined,
|
|
136
|
+
standard: readonly string[],
|
|
137
|
+
): Read<readonly string[]> {
|
|
138
|
+
if (value === undefined) {
|
|
139
|
+
return { value: standard, errors: [] };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (!Array.isArray(value) || !value.every((name) => typeof name === 'string')) {
|
|
143
|
+
return {
|
|
144
|
+
value: [],
|
|
145
|
+
errors: [`${settingsFile} scripts must be a list of package.json script names.`],
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const names: string[] = value;
|
|
150
|
+
const errors: string[] = [];
|
|
151
|
+
|
|
152
|
+
for (const [index, name] of names.entries()) {
|
|
153
|
+
if (names.indexOf(name) !== index) {
|
|
154
|
+
errors.push(`${settingsFile} names the script "${name}" twice; list it once.`);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
errors.push(...collectScriptErrors(name, packageScripts));
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return { value: packageScripts === undefined ? [] : names, errors };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function collectScriptErrors(
|
|
164
|
+
name: string,
|
|
165
|
+
packageScripts: Readonly<Record<string, string>> | undefined,
|
|
166
|
+
): string[] {
|
|
167
|
+
if (name === 'audit') {
|
|
168
|
+
return [
|
|
169
|
+
`${settingsFile} lists "audit", which the shared checks already run; remove it from scripts.`,
|
|
170
|
+
];
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (laneNames.some((lane) => name === `test:${lane}`)) {
|
|
174
|
+
return [
|
|
175
|
+
`${settingsFile} lists "${name}", a test lane; remove it from scripts and give it a slow-tests entry.`,
|
|
176
|
+
];
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// The shared checks run in every plan, so a listed script that repeats one runs it twice.
|
|
180
|
+
const sharedChecks =
|
|
181
|
+
name === 'check:docs' ? ['check:docs'] : collectSharedChecks(packageScripts?.[name]);
|
|
182
|
+
|
|
183
|
+
if (sharedChecks.length > 0) {
|
|
184
|
+
return [
|
|
185
|
+
`${settingsFile} lists "${name}", which runs ${sharedChecks.join(', ')}, a shared check that every run already makes; remove it from scripts.`,
|
|
186
|
+
];
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (packageScripts !== undefined && !(name in packageScripts)) {
|
|
190
|
+
return [
|
|
191
|
+
`${settingsFile} lists "${name}", but package.json defines no script of that name; add the script or remove it from scripts.`,
|
|
192
|
+
];
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return [];
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function parseNodeVersion(value: unknown): Read<string | null> {
|
|
199
|
+
if (value === undefined || value === null) {
|
|
200
|
+
return { value: null, errors: [] };
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (typeof value === 'string' || (typeof value === 'number' && Number.isFinite(value))) {
|
|
204
|
+
return { value: String(value), errors: [] };
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return {
|
|
208
|
+
value: null,
|
|
209
|
+
errors: [`${settingsFile} node-version must be a version such as 24.`],
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function parseTimeoutMinutes(value: unknown): Read<number> {
|
|
214
|
+
if (value === undefined || value === null) {
|
|
215
|
+
return { value: defaultTimeoutMinutes, errors: [] };
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (typeof value === 'number' && Number.isInteger(value) && value > 0) {
|
|
219
|
+
return { value, errors: [] };
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return {
|
|
223
|
+
value: defaultTimeoutMinutes,
|
|
224
|
+
errors: [`${settingsFile} timeout-minutes must be a whole number of minutes above 0.`],
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function parseSlowTests(value: unknown): Read<Record<string, SlowTestRule>> {
|
|
229
|
+
if (value === undefined || value === null) {
|
|
230
|
+
return { value: {}, errors: [] };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (!isRecord(value)) {
|
|
234
|
+
return {
|
|
235
|
+
value: {},
|
|
236
|
+
errors: [`${settingsFile} slow-tests must be a map with one entry per slow-test job.`],
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const rules: [string, SlowTestRule][] = [];
|
|
241
|
+
const errors: string[] = [];
|
|
242
|
+
|
|
243
|
+
for (const [name, entry] of Object.entries(value)) {
|
|
244
|
+
const read = parseSlowTestRule(name, entry);
|
|
245
|
+
|
|
246
|
+
if (read.kind === 'rule') {
|
|
247
|
+
rules.push([name, read.rule]);
|
|
248
|
+
} else {
|
|
249
|
+
errors.push(read.message);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return { value: Object.fromEntries(rules), errors };
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
type SlowTestParse =
|
|
257
|
+
| { readonly kind: 'rule'; readonly rule: SlowTestRule }
|
|
258
|
+
| { readonly kind: 'invalid'; readonly message: string };
|
|
259
|
+
|
|
260
|
+
const slowTestForms =
|
|
261
|
+
'write it as always, daily, or { run-when-changed: [globs] } with at least one glob';
|
|
262
|
+
|
|
263
|
+
function parseSlowTestRule(name: string, entry: unknown): SlowTestParse {
|
|
264
|
+
if (entry === 'always' || entry === 'daily') {
|
|
265
|
+
return { kind: 'rule', rule: entry };
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const notValid: SlowTestParse = {
|
|
269
|
+
kind: 'invalid',
|
|
270
|
+
message: `${settingsFile} slow-tests entry "${name}" is not valid; ${slowTestForms}.`,
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
if (!isRecord(entry)) {
|
|
274
|
+
return notValid;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const unknownKey = Object.keys(entry).find((key) => key !== 'run-when-changed');
|
|
278
|
+
|
|
279
|
+
if (unknownKey !== undefined) {
|
|
280
|
+
return {
|
|
281
|
+
kind: 'invalid',
|
|
282
|
+
message: `${settingsFile} slow-tests entry "${name}" has the unknown key "${unknownKey}"; ${slowTestForms}.`,
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const globs = entry['run-when-changed'];
|
|
287
|
+
|
|
288
|
+
if (
|
|
289
|
+
!Array.isArray(globs) ||
|
|
290
|
+
globs.length === 0 ||
|
|
291
|
+
!globs.every((glob) => typeof glob === 'string' && glob !== '')
|
|
292
|
+
) {
|
|
293
|
+
return notValid;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const runWhenChanged = globs.filter((glob): glob is string => typeof glob === 'string');
|
|
297
|
+
|
|
298
|
+
return { kind: 'rule', rule: { runWhenChanged } };
|
|
299
|
+
}
|
|
@@ -32,6 +32,7 @@ export function parseDocsChecks(script: string | undefined): DocsChecks {
|
|
|
32
32
|
)
|
|
33
33
|
.with({ kind: 'init' }, () => refusal)
|
|
34
34
|
.with({ kind: 'ci-plan' }, () => refusal)
|
|
35
|
+
.with({ kind: 'ci-plan-json' }, () => refusal)
|
|
35
36
|
.with({ kind: 'refused' }, () => refusal)
|
|
36
37
|
.exhaustive();
|
|
37
38
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { join } from 'node:path';
|
|
2
|
+
import { buildCIPlanOutput } from './build-ci-plan-output.ts';
|
|
3
|
+
|
|
4
|
+
export interface CIPlanJSONStep {
|
|
5
|
+
readonly cwd: string;
|
|
6
|
+
readonly readText: (path: string) => Promise<string | undefined>;
|
|
7
|
+
readonly writeOutput: (text: string) => void;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export async function writeCIPlanJSON(step: Readonly<CIPlanJSONStep>): Promise<number> {
|
|
11
|
+
const settingsText = await step.readText(join(step.cwd, '.github', 'ci.yml'));
|
|
12
|
+
const packageText = await step.readText(join(step.cwd, 'package.json'));
|
|
13
|
+
|
|
14
|
+
const output = buildCIPlanOutput(settingsText, packageText);
|
|
15
|
+
|
|
16
|
+
for (const line of output.lines) {
|
|
17
|
+
step.writeOutput(`${line}\n`);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return output.exitCode;
|
|
21
|
+
}
|
|
@@ -5,6 +5,7 @@ import { checkNames, isCheckName } from './check-names.ts';
|
|
|
5
5
|
export type ChexRequest =
|
|
6
6
|
| { readonly kind: 'init' }
|
|
7
7
|
| { readonly kind: 'ci-plan' }
|
|
8
|
+
| { readonly kind: 'ci-plan-json' }
|
|
8
9
|
| {
|
|
9
10
|
readonly kind: 'checks';
|
|
10
11
|
readonly names: readonly CheckName[];
|
|
@@ -22,7 +23,7 @@ const checksWithArgs: ReadonlySet<CheckName> = new Set([
|
|
|
22
23
|
'pr-body',
|
|
23
24
|
]);
|
|
24
25
|
|
|
25
|
-
export const chexUsage = `usage: chex <check>... | chex <check> [args...] | chex init | chex ci-plan
|
|
26
|
+
export const chexUsage = `usage: chex <check>... | chex <check> [args...] | chex init | chex ci-plan [--json]
|
|
26
27
|
checks: ${checkNames.join(', ')}`;
|
|
27
28
|
|
|
28
29
|
export function parseChexArgs(argv: readonly string[]): ChexRequest {
|
|
@@ -39,9 +40,13 @@ export function parseChexArgs(argv: readonly string[]): ChexRequest {
|
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
if (first === 'ci-plan') {
|
|
43
|
+
if (rest.length === 1 && rest[0] === '--json') {
|
|
44
|
+
return { kind: 'ci-plan-json' };
|
|
45
|
+
}
|
|
46
|
+
|
|
42
47
|
return rest.length === 0
|
|
43
48
|
? { kind: 'ci-plan' }
|
|
44
|
-
: { kind: 'refused', message: 'ci-plan takes no
|
|
49
|
+
: { kind: 'refused', message: 'ci-plan takes no argument but --json' };
|
|
45
50
|
}
|
|
46
51
|
|
|
47
52
|
// Each check parses its own options, so the tokens only mark where the check names end.
|
package/src/cli/run-chex.ts
CHANGED
|
@@ -6,6 +6,7 @@ export interface ChexDeps {
|
|
|
6
6
|
readonly runCheck: (name: CheckName, args: readonly string[]) => Promise<number>;
|
|
7
7
|
readonly runInit: () => Promise<number>;
|
|
8
8
|
readonly runCIPlan: () => Promise<number>;
|
|
9
|
+
readonly runCIPlanJSON: () => Promise<number>;
|
|
9
10
|
readonly writeOutput: (text: string) => void;
|
|
10
11
|
readonly writeError: (text: string) => void;
|
|
11
12
|
}
|
|
@@ -21,6 +22,7 @@ export function runChex(argv: readonly string[], deps: ChexDeps): Promise<number
|
|
|
21
22
|
})
|
|
22
23
|
.with({ kind: 'init' }, () => deps.runInit())
|
|
23
24
|
.with({ kind: 'ci-plan' }, () => deps.runCIPlan())
|
|
25
|
+
.with({ kind: 'ci-plan-json' }, () => deps.runCIPlanJSON())
|
|
24
26
|
.with({ kind: 'checks' }, (request) => runChecks(request.names, request.args, deps))
|
|
25
27
|
.exhaustive();
|
|
26
28
|
}
|
package/src/cli.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { rename, rm } from 'node:fs/promises';
|
|
|
3
3
|
import { join } from 'node:path';
|
|
4
4
|
import { readOptionalText } from '@zgeoff/std';
|
|
5
5
|
import { runCIPlanCommand } from './ci-plan/write-check-plan.ts';
|
|
6
|
+
import { writeCIPlanJSON } from './ci-plan/write-ci-plan-json.ts';
|
|
6
7
|
import { makeCheckRunner } from './cli/make-check-runner.ts';
|
|
7
8
|
import { runChex } from './cli/run-chex.ts';
|
|
8
9
|
import { runCommitsCommand } from './commits/run-commits-check.ts';
|
|
@@ -48,6 +49,14 @@ process.exitCode = await runChex(process.argv.slice(2), {
|
|
|
48
49
|
},
|
|
49
50
|
}),
|
|
50
51
|
runCIPlan: runCIPlanCommand,
|
|
52
|
+
runCIPlanJSON: () =>
|
|
53
|
+
writeCIPlanJSON({
|
|
54
|
+
cwd: process.cwd(),
|
|
55
|
+
readText: readOptionalText,
|
|
56
|
+
writeOutput: (text) => {
|
|
57
|
+
process.stdout.write(text);
|
|
58
|
+
},
|
|
59
|
+
}),
|
|
51
60
|
writeOutput: (text) => {
|
|
52
61
|
process.stdout.write(text);
|
|
53
62
|
},
|