@zgeoff/chex 0.8.11 → 0.8.13
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 +6 -6
- package/lefthook.yml +13 -4
- package/package.json +1 -1
- package/src/ci-only/collect-lint-paths.ts +25 -0
- package/src/ci-only/is-ci-only-allowed.ts +11 -0
- package/src/ci-only/parse-filter-args.ts +37 -0
- package/src/ci-only/parse-shell-words.ts +48 -0
- package/src/ci-only/pick-ci-only-verdict.ts +56 -0
- package/src/ci-only/pick-extra-args.ts +36 -0
- package/src/ci-only/pick-script-runs.ts +38 -0
- package/src/ci-only/probe-workspace-member.ts +33 -0
- package/src/ci-only/read-scope.ts +69 -0
- package/src/ci-only/read-workspace-members.ts +58 -0
- package/src/ci-only/run-ci-only-check.ts +80 -0
- package/src/ci-plan/parse-docs-checks.ts +1 -0
- package/src/cli/parse-chex-args.ts +24 -0
- package/src/cli/run-chex.ts +3 -0
- package/src/cli.ts +2 -0
- package/src/init/build-init-package.ts +34 -4
- package/src/init/build-init-plan.ts +10 -2
- package/src/typecheck/run-typecheck-check.ts +3 -0
package/README.md
CHANGED
|
@@ -15,8 +15,7 @@
|
|
|
15
15
|
Run each shared check of a zgeoff repo as one subcommand, and take the lefthook pre-push baseline
|
|
16
16
|
that calls them with one `extends` line.
|
|
17
17
|
|
|
18
|
-
Each check prints its
|
|
19
|
-
when it fails, so a run of several checks shows each one that failed.
|
|
18
|
+
Each check prints its findings, then `cleared: <check>` or `bounced: <check>`.
|
|
20
19
|
|
|
21
20
|
## Install
|
|
22
21
|
|
|
@@ -27,9 +26,9 @@ bun x --no-install chex init
|
|
|
27
26
|
|
|
28
27
|
`chex` runs under Bun. The pre-push baseline runs the related tests through
|
|
29
28
|
[`@zgeoff/rig`](../rig/README.md) and each job under the memory cap of
|
|
30
|
-
[`@zgeoff/diet`](../diet/README.md), so a repo
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
[`@zgeoff/diet`](../diet/README.md), so a repo installs both, and `chex init` fails until it does.
|
|
30
|
+
In a repo that lacks `@commitlint/cli` or `@zgeoff/commitlint-config`, `chex commits` prints a
|
|
31
|
+
notice and passes.
|
|
33
32
|
|
|
34
33
|
## Commands
|
|
35
34
|
|
|
@@ -58,8 +57,9 @@ Run these repo checks on the branch, the pushed commits and the repo's config:
|
|
|
58
57
|
| `test-timeouts` | a `bun test` timeout above its lane's limit, in a script or a `bunfig.toml` | none |
|
|
59
58
|
| `lint-overrides` | an oxlint config that turns a `zgeoff/*` rule, `consistent-type-assertions` or `ban-ts-comment` off or down to a warning, a config in code (`oxlint.config.ts` or `.mts`), or a config that the last commit does not hold as a plain file or that does not parse; it reads each file or symlink named `.oxlintrc*.json` or `.oxlintrc*.jsonc`, each path a `package.json` script gives `oxlint` with `-c` or `--config`, the file each symlink reaches, and each file a config extends by a relative path | none |
|
|
60
59
|
| `test-layout` | a test folder, file name or import outside [the test layout](../../docs/guides/test-layout.md), where test code covers a relative `[test] preload` file, a `.nix`-only `tests/` folder and a private workspace that only test code imports | none |
|
|
61
|
-
| `workflows` | nothing: it prints each fault of the shared workflow lint, and in a repo that calls the shared `checks.yml` each break of the [ci-design](https://github.com/zgeoff/skills/blob/main/skills/ci-design/SKILL.md) rules, and fails when there is one |
|
|
60
|
+
| `workflows` | nothing: it prints each fault of the shared workflow lint, and in a repo that calls the shared `checks.yml` each break of the [ci-design](https://github.com/zgeoff/skills/blob/main/skills/ci-design/SKILL.md) rules, and fails when there is one | none |
|
|
62
61
|
| `dependency-gates` | a stale age-gate exclude, an override or an audit ignore that nothing needs, a first-party package with no age-gate exclude, or a pinned release younger than the age gate ([details](../../docs/architecture/dependency-updates.md#the-dependency-gates-check)) | none |
|
|
62
|
+
| `ci-only` | a whole-repo `lint` or `typecheck` outside CI and the push hook; a repo puts it before the real command of its script ([details](../../docs/architecture/pre-push-baseline.md#keeping-whole-repo-runs-in-ci)) | `lint` or `typecheck`, then `--` and the command |
|
|
63
63
|
| `uncommitted` | a file the branch changes that has an uncommitted edit, which the push does not send | none |
|
|
64
64
|
| `typecheck` | the repo's `typecheck` script, which it runs only over the packages the branch changes and the packages that depend on them, or over the whole repo when it cannot narrow the run, and skips when the branch changes no file a typecheck reads ([details](../../docs/architecture/pre-push-baseline.md#the-type-check)) | none |
|
|
65
65
|
| `pr-body` | a PR description with a heading, a label or a code block that the [PR template](../../.github/PULL_REQUEST_TEMPLATE.md) doesn't allow, or a Deslop or Cold read line with a commit hash the branch lacks or a later doc change outdates ([details](../../docs/architecture/pr-body-check.md)) | `--body-file`, then `--base` and `--head` for the commit checks |
|
package/lefthook.yml
CHANGED
|
@@ -48,11 +48,18 @@ pre-push:
|
|
|
48
48
|
run:
|
|
49
49
|
'{clear-git-env} bun x --no-install diet -- bun x --no-install oxlint --type-aware
|
|
50
50
|
--type-check --threads=2 --no-error-on-unmatched-pattern {files}'
|
|
51
|
-
#
|
|
51
|
+
# The jobs that can run a repo's lint or typecheck script set
|
|
52
|
+
# CHEX_ALLOW_CI_ONLY=1, which lets a script that starts with chex ci-only
|
|
53
|
+
# through: this hook and CI are the two places such a script runs the whole
|
|
54
|
+
# repo. A repo without the script passes: bun run --if-present exits 0.
|
|
52
55
|
- name: chex-deadcode
|
|
53
|
-
run:
|
|
56
|
+
run:
|
|
57
|
+
'{clear-git-env} CHEX_ALLOW_CI_ONLY=1 bun x --no-install diet -- bun run --if-present
|
|
58
|
+
deadcode'
|
|
54
59
|
- name: chex-verify-lint
|
|
55
|
-
run:
|
|
60
|
+
run:
|
|
61
|
+
'{clear-git-env} CHEX_ALLOW_CI_ONLY=1 bun x --no-install diet -- bun run --if-present verify
|
|
62
|
+
lint'
|
|
56
63
|
# Each file check reads only the files the branch changes that match its
|
|
57
64
|
# glob, so a file the branch leaves unchanged never fails a push. A check
|
|
58
65
|
# whose program the machine lacks prints a notice and passes: CI installs
|
|
@@ -152,7 +159,9 @@ pre-push:
|
|
|
152
159
|
# dependents. A change that every package reads, and a repo it cannot
|
|
153
160
|
# narrow, run the whole typecheck script.
|
|
154
161
|
- name: chex-typecheck
|
|
155
|
-
run:
|
|
162
|
+
run:
|
|
163
|
+
'{clear-git-env} CHEX_ALLOW_CI_ONLY=1 bun x --no-install diet -- bun x --no-install chex
|
|
164
|
+
typecheck'
|
|
156
165
|
# A bunfig.toml reaches the job too, because rig related runs a spread
|
|
157
166
|
# of the tests under a changed bunfig.
|
|
158
167
|
- name: chex-test
|
package/package.json
CHANGED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// The flags of oxlint that take no value.
|
|
2
|
+
const flagsWithoutValue =
|
|
3
|
+
/^--(?:fix|fix-suggestions|fix-dangerously|quiet|silent|deny-warnings|no-ignore|type-aware|type-check|init|print-config|rules|version|help|disable-[a-z-]+-plugin|[a-z-]+-plugin)$/u;
|
|
4
|
+
|
|
5
|
+
// Returns the words of a lint's arguments that name a file or folder. A flag chex does not know
|
|
6
|
+
// may take the next word as its value, so that word is not a path.
|
|
7
|
+
export function collectLintPaths(args: readonly string[]): string[] {
|
|
8
|
+
const paths: string[] = [];
|
|
9
|
+
let afterTerminator = false;
|
|
10
|
+
let skipNext = false;
|
|
11
|
+
|
|
12
|
+
for (const arg of args) {
|
|
13
|
+
if (skipNext) {
|
|
14
|
+
skipNext = false;
|
|
15
|
+
} else if (afterTerminator || !arg.startsWith('-')) {
|
|
16
|
+
paths.push(arg);
|
|
17
|
+
} else if (arg === '--') {
|
|
18
|
+
afterTerminator = true;
|
|
19
|
+
} else if (!arg.includes('=') && !flagsWithoutValue.test(arg)) {
|
|
20
|
+
skipNext = true;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return paths;
|
|
25
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export const overrideVariable = 'CHEX_ALLOW_CI_ONLY';
|
|
2
|
+
|
|
3
|
+
// True in CI, and when a person or a hook job sets the override. It reads only the environment,
|
|
4
|
+
// so a caller checks it before it reads anything from disk.
|
|
5
|
+
export function isCIOnlyAllowed(env: Readonly<Record<string, string | undefined>>): boolean {
|
|
6
|
+
const ci = env['CI'];
|
|
7
|
+
|
|
8
|
+
return (
|
|
9
|
+
(ci !== undefined && ci !== '' && ci !== '0' && ci !== 'false') || env[overrideVariable] === '1'
|
|
10
|
+
);
|
|
11
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export interface FilterArgs {
|
|
2
|
+
// The value of each --filter or -F, in order.
|
|
3
|
+
readonly filters: readonly string[];
|
|
4
|
+
|
|
5
|
+
// The other arguments.
|
|
6
|
+
readonly rest: readonly string[];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// Splits the package filters out of a typecheck's arguments. Returns null when a filter has no
|
|
10
|
+
// value.
|
|
11
|
+
export function parseFilterArgs(args: readonly string[]): FilterArgs | null {
|
|
12
|
+
const filters: string[] = [];
|
|
13
|
+
const rest: string[] = [];
|
|
14
|
+
|
|
15
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
16
|
+
const arg = args[index] ?? '';
|
|
17
|
+
const inline = /^(?:--filter|-F)=(?<value>.*)$/u.exec(arg)?.groups?.['value'];
|
|
18
|
+
|
|
19
|
+
if (inline !== undefined) {
|
|
20
|
+
filters.push(inline);
|
|
21
|
+
} else if (arg === '--filter' || arg === '-F') {
|
|
22
|
+
const value = args[index + 1];
|
|
23
|
+
|
|
24
|
+
if (value === undefined) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
filters.push(value);
|
|
29
|
+
|
|
30
|
+
index += 1;
|
|
31
|
+
} else {
|
|
32
|
+
rest.push(arg);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return { filters, rest };
|
|
37
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// Characters a shell expands, chains or redirects outside quotes.
|
|
2
|
+
const unquotedSyntax = /[$`&|;()<>*?[\]{}~!#\\]/u;
|
|
3
|
+
|
|
4
|
+
// Inside double quotes the shell still expands these.
|
|
5
|
+
const doubleQuotedSyntax = /[$`\\]/u;
|
|
6
|
+
|
|
7
|
+
// Splits the text of a script command into the words a shell passes on, understanding quotes.
|
|
8
|
+
// Returns null when the text holds anything a shell expands or chains, since the words are then
|
|
9
|
+
// not the text's own.
|
|
10
|
+
export function parseShellWords(text: string): string[] | null {
|
|
11
|
+
const words: string[] = [];
|
|
12
|
+
let current = '';
|
|
13
|
+
let inWord = false;
|
|
14
|
+
let quote: '"' | "'" | null = null;
|
|
15
|
+
|
|
16
|
+
for (const char of text) {
|
|
17
|
+
if (quote === null) {
|
|
18
|
+
if (char === '"' || char === "'") {
|
|
19
|
+
quote = char;
|
|
20
|
+
inWord = true;
|
|
21
|
+
} else if (/\s/u.test(char)) {
|
|
22
|
+
if (inWord) {
|
|
23
|
+
words.push(current);
|
|
24
|
+
|
|
25
|
+
current = '';
|
|
26
|
+
inWord = false;
|
|
27
|
+
}
|
|
28
|
+
} else if (unquotedSyntax.test(char)) {
|
|
29
|
+
return null;
|
|
30
|
+
} else {
|
|
31
|
+
current += char;
|
|
32
|
+
inWord = true;
|
|
33
|
+
}
|
|
34
|
+
} else if (char === quote) {
|
|
35
|
+
quote = null;
|
|
36
|
+
} else if (quote === '"' && doubleQuotedSyntax.test(char)) {
|
|
37
|
+
return null;
|
|
38
|
+
} else {
|
|
39
|
+
current += char;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (quote !== null) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return inWord ? [...words, current] : words;
|
|
48
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { overrideVariable } from './is-ci-only-allowed.ts';
|
|
2
|
+
|
|
3
|
+
export const ciOnlyChecks = ['lint', 'typecheck'] as const;
|
|
4
|
+
|
|
5
|
+
export type CIOnlyCheck = (typeof ciOnlyChecks)[number];
|
|
6
|
+
|
|
7
|
+
export function isCIOnlyCheck(value: string): value is CIOnlyCheck {
|
|
8
|
+
return ciOnlyChecks.some((check) => check === value);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface CIOnlyVerdictInput {
|
|
12
|
+
readonly check: CIOnlyCheck;
|
|
13
|
+
|
|
14
|
+
// True when the script runs from a workspace package rather than the workspace root.
|
|
15
|
+
readonly isInPackage: boolean;
|
|
16
|
+
|
|
17
|
+
// True when the caller's arguments name files or folders inside the script's folder for lint,
|
|
18
|
+
// or workspace packages for typecheck.
|
|
19
|
+
readonly isScoped: boolean;
|
|
20
|
+
|
|
21
|
+
// True when the caller's arguments reach chex, so a scope the caller types can be seen. False for
|
|
22
|
+
// a chained script, where bun appends them to a later command.
|
|
23
|
+
readonly canSeeArgs: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type CIOnlyVerdict =
|
|
27
|
+
| { readonly kind: 'allow' }
|
|
28
|
+
| { readonly kind: 'refuse'; readonly message: string };
|
|
29
|
+
|
|
30
|
+
// The verdict for a run that CI and the override did not already let through. It allows a run from
|
|
31
|
+
// inside one package or with a scope, and refuses every other run.
|
|
32
|
+
export function pickCIOnlyVerdict(input: Readonly<CIOnlyVerdictInput>): CIOnlyVerdict {
|
|
33
|
+
if (input.isInPackage || input.isScoped) {
|
|
34
|
+
return { kind: 'allow' };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return { kind: 'refuse', message: buildRefusal(input) };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function buildRefusal(input: Readonly<CIOnlyVerdictInput>): string {
|
|
41
|
+
const checked = input.check === 'lint' ? 'the files you changed' : 'the packages you changed';
|
|
42
|
+
|
|
43
|
+
return `${input.check} runs on the whole repo only in CI and the push hook, which checks ${checked}. Push, or ${pickInstead(input)}. To run it here anyway, set ${overrideVariable}=1.`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function pickInstead(input: Readonly<CIOnlyVerdictInput>): string {
|
|
47
|
+
if (input.check === 'lint') {
|
|
48
|
+
return input.canSeeArgs
|
|
49
|
+
? 'lint named files with `bun run lint <file>...`'
|
|
50
|
+
: 'lint named files with `bun x oxlint <file>...`';
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return input.canSeeArgs
|
|
54
|
+
? 'check one package with `bun run typecheck --filter <package>`'
|
|
55
|
+
: "check one package by running `bun run typecheck` in that package's folder";
|
|
56
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { parseShellWords } from './parse-shell-words.ts';
|
|
2
|
+
|
|
3
|
+
// bun run appends the caller's arguments to the script's command. The script text says what the
|
|
4
|
+
// command was, so the words after its own are the caller's. When the text holds shell syntax that
|
|
5
|
+
// chex cannot count, no word is known to be the caller's, so the result is empty.
|
|
6
|
+
export function pickExtraArgs(
|
|
7
|
+
command: readonly string[],
|
|
8
|
+
scriptText: string | undefined,
|
|
9
|
+
): readonly string[] {
|
|
10
|
+
if (scriptText === undefined) {
|
|
11
|
+
return command.slice(1);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const baseWords = splitBaseWords(scriptText);
|
|
15
|
+
|
|
16
|
+
return baseWords === null ? [] : command.slice(baseWords.length);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// True when chex can tell the caller's arguments from the script's own: no script text to read, or a
|
|
20
|
+
// command after `--` that is plain words. A chained or expanded command hides them, and so does a
|
|
21
|
+
// later guard for another check, which is the one that gets bun's appended arguments.
|
|
22
|
+
export function canSeeCallerArgs(check: string, scriptText: string | undefined): boolean {
|
|
23
|
+
if (scriptText === undefined) {
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const lastCheck = /^[\s\S]*\bci-only (?<check>\S+) -- /u.exec(scriptText)?.groups?.['check'];
|
|
28
|
+
|
|
29
|
+
return lastCheck === check && splitBaseWords(scriptText) !== null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function splitBaseWords(scriptText: string): string[] | null {
|
|
33
|
+
const baseText = /^[\s\S]*\bci-only \S+ -- (?<base>[\s\S]*)$/u.exec(scriptText)?.groups?.['base'];
|
|
34
|
+
|
|
35
|
+
return baseText === undefined ? null : parseShellWords(baseText);
|
|
36
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { basename } from 'node:path';
|
|
2
|
+
import { parseFilterArgs } from './parse-filter-args.ts';
|
|
3
|
+
|
|
4
|
+
export interface ScriptRun {
|
|
5
|
+
readonly argv: readonly string[];
|
|
6
|
+
|
|
7
|
+
// The folder to run in, or undefined for the folder chex started in.
|
|
8
|
+
readonly cwd: string | undefined;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ScriptRunsInput {
|
|
12
|
+
readonly command: readonly string[];
|
|
13
|
+
readonly extraArgs: readonly string[];
|
|
14
|
+
readonly filterFolders: readonly string[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// These programs check the folder they run in and know no package filter.
|
|
18
|
+
const folderPrograms = new Set(['tsgo', 'tsc']);
|
|
19
|
+
|
|
20
|
+
// A program that checks its own folder and knows no filter never gets the filter. It runs in the
|
|
21
|
+
// folder of each filtered package, or once where chex started when no package matches.
|
|
22
|
+
export function pickScriptRuns(input: Readonly<ScriptRunsInput>): ScriptRun[] {
|
|
23
|
+
const program = basename(input.command[0] ?? '');
|
|
24
|
+
const parsed = parseFilterArgs(input.extraArgs);
|
|
25
|
+
|
|
26
|
+
if (parsed === null || !folderPrograms.has(program)) {
|
|
27
|
+
return [{ argv: input.command, cwd: undefined }];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const own = input.command.slice(0, input.command.length - input.extraArgs.length);
|
|
31
|
+
const argv = [...own, ...parsed.rest];
|
|
32
|
+
|
|
33
|
+
if (input.filterFolders.length === 0) {
|
|
34
|
+
return [{ argv, cwd: undefined }];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return input.filterFolders.map((folder) => ({ argv, cwd: folder }));
|
|
38
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { realpathSync } from 'node:fs';
|
|
2
|
+
import { dirname } from 'node:path';
|
|
3
|
+
import { readWorkspaceMembers } from './read-workspace-members.ts';
|
|
4
|
+
|
|
5
|
+
// True when a package.json in a folder above this one lists this folder as a workspace package.
|
|
6
|
+
// The workspace root itself is not a member, and neither is a folder in no workspace.
|
|
7
|
+
export function probeWorkspaceMember(folder: string): boolean {
|
|
8
|
+
let real: string;
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
real = realpathSync(folder);
|
|
12
|
+
} catch {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
for (let above = dirname(real); above !== dirname(above); above = dirname(above)) {
|
|
17
|
+
const members = readWorkspaceMembers(above);
|
|
18
|
+
|
|
19
|
+
if (members !== null) {
|
|
20
|
+
return members.some((member) => safeRealpath(member.folder) === real);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function safeRealpath(path: string): string | null {
|
|
28
|
+
try {
|
|
29
|
+
return realpathSync(path);
|
|
30
|
+
} catch {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { realpathSync } from 'node:fs';
|
|
2
|
+
import { isAbsolute, relative, resolve } from 'node:path';
|
|
3
|
+
import { collectLintPaths } from './collect-lint-paths.ts';
|
|
4
|
+
import { parseFilterArgs } from './parse-filter-args.ts';
|
|
5
|
+
import type { CIOnlyCheck } from './pick-ci-only-verdict.ts';
|
|
6
|
+
import { probeWorkspaceMember } from './probe-workspace-member.ts';
|
|
7
|
+
import { readWorkspaceMembers } from './read-workspace-members.ts';
|
|
8
|
+
|
|
9
|
+
export interface ScopeFacts {
|
|
10
|
+
readonly isInPackage: boolean;
|
|
11
|
+
readonly isScoped: boolean;
|
|
12
|
+
|
|
13
|
+
// The folder of each package a typecheck filter names, in the order of the filters.
|
|
14
|
+
readonly filterFolders: readonly string[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface ScopeRequest {
|
|
18
|
+
readonly check: CIOnlyCheck;
|
|
19
|
+
readonly extraArgs: readonly string[];
|
|
20
|
+
|
|
21
|
+
// The folder of the package.json that holds the script, where bun run starts it.
|
|
22
|
+
readonly scriptFolder: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Reads what the script's folder and the caller's arguments say about the run's scope. A scope
|
|
26
|
+
// counts only when chex can prove it: a path that exists inside the script's folder, and a filter
|
|
27
|
+
// that is the exact name of a workspace package. Anything else is no scope.
|
|
28
|
+
export function readScope(request: Readonly<ScopeRequest>): ScopeFacts {
|
|
29
|
+
const none = { isInPackage: probeWorkspaceMember(request.scriptFolder), isScoped: false };
|
|
30
|
+
|
|
31
|
+
if (none.isInPackage) {
|
|
32
|
+
return { ...none, filterFolders: [] };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (request.check === 'lint') {
|
|
36
|
+
const paths = collectLintPaths(request.extraArgs);
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
...none,
|
|
40
|
+
isScoped: paths.length > 0 && paths.every((path) => probeInside(request.scriptFolder, path)),
|
|
41
|
+
filterFolders: [],
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const parsed = parseFilterArgs(request.extraArgs);
|
|
46
|
+
const members = readWorkspaceMembers(request.scriptFolder);
|
|
47
|
+
|
|
48
|
+
if (parsed === null || parsed.filters.length === 0 || members === null) {
|
|
49
|
+
return { ...none, filterFolders: [] };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const matched = parsed.filters.map((filter) => members.find((member) => member.name === filter));
|
|
53
|
+
const folders = matched.flatMap((member) => (member === undefined ? [] : [member.folder]));
|
|
54
|
+
|
|
55
|
+
return folders.length === matched.length
|
|
56
|
+
? { ...none, isScoped: true, filterFolders: folders }
|
|
57
|
+
: { ...none, filterFolders: [] };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// True when the path exists and sits strictly inside the folder.
|
|
61
|
+
function probeInside(folder: string, path: string): boolean {
|
|
62
|
+
try {
|
|
63
|
+
const inside = relative(realpathSync(folder), realpathSync(resolve(folder, path)));
|
|
64
|
+
|
|
65
|
+
return inside !== '' && !inside.startsWith('..') && !isAbsolute(inside);
|
|
66
|
+
} catch {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join, resolve } from 'node:path';
|
|
3
|
+
import { isRecord } from '@zgeoff/std';
|
|
4
|
+
|
|
5
|
+
export interface WorkspaceMember {
|
|
6
|
+
readonly name: string;
|
|
7
|
+
readonly folder: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// Returns the packages that the package.json in a folder lists under workspaces, or null when it
|
|
11
|
+
// has none or cannot be read.
|
|
12
|
+
export function readWorkspaceMembers(folder: string): WorkspaceMember[] | null {
|
|
13
|
+
const manifest = readManifest(join(folder, 'package.json'));
|
|
14
|
+
const workspaces = manifest === null ? undefined : manifest['workspaces'];
|
|
15
|
+
const patterns = isRecord(workspaces) ? workspaces['packages'] : workspaces;
|
|
16
|
+
|
|
17
|
+
if (!Array.isArray(patterns) || !patterns.every((pattern) => typeof pattern === 'string')) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const excluded = patterns
|
|
22
|
+
.filter((pattern) => pattern.startsWith('!'))
|
|
23
|
+
.map((pattern) => new Bun.Glob(trimPattern(pattern.slice(1))));
|
|
24
|
+
|
|
25
|
+
const members: WorkspaceMember[] = [];
|
|
26
|
+
|
|
27
|
+
for (const pattern of patterns.filter((entry) => !entry.startsWith('!'))) {
|
|
28
|
+
const glob = new Bun.Glob(`${trimPattern(pattern)}/package.json`);
|
|
29
|
+
|
|
30
|
+
for (const file of glob.scanSync({ cwd: folder, onlyFiles: true })) {
|
|
31
|
+
const memberPath = dirname(file);
|
|
32
|
+
const name = readManifest(join(folder, file))?.['name'];
|
|
33
|
+
|
|
34
|
+
if (
|
|
35
|
+
typeof name === 'string' &&
|
|
36
|
+
!excluded.some((excludedGlob) => excludedGlob.match(memberPath))
|
|
37
|
+
) {
|
|
38
|
+
members.push({ name, folder: resolve(folder, memberPath) });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return members;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function readManifest(path: string): Record<string, unknown> | null {
|
|
47
|
+
try {
|
|
48
|
+
const parsed: unknown = JSON.parse(readFileSync(path, 'utf8'));
|
|
49
|
+
|
|
50
|
+
return isRecord(parsed) ? parsed : null;
|
|
51
|
+
} catch {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function trimPattern(pattern: string): string {
|
|
57
|
+
return pattern.replace(/^\.\//u, '').replace(/\/+$/u, '');
|
|
58
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { isCIOnlyAllowed } from './is-ci-only-allowed.ts';
|
|
2
|
+
import type { CIOnlyCheck } from './pick-ci-only-verdict.ts';
|
|
3
|
+
import { pickCIOnlyVerdict } from './pick-ci-only-verdict.ts';
|
|
4
|
+
import { canSeeCallerArgs, pickExtraArgs } from './pick-extra-args.ts';
|
|
5
|
+
import { pickScriptRuns } from './pick-script-runs.ts';
|
|
6
|
+
import { readScope } from './read-scope.ts';
|
|
7
|
+
|
|
8
|
+
export interface CIOnlyCheckOptions {
|
|
9
|
+
readonly check: CIOnlyCheck;
|
|
10
|
+
|
|
11
|
+
// The real command, which runs when the verdict allows it.
|
|
12
|
+
readonly command: readonly string[];
|
|
13
|
+
|
|
14
|
+
readonly cwd: string;
|
|
15
|
+
readonly env: Readonly<Record<string, string | undefined>>;
|
|
16
|
+
|
|
17
|
+
// Runs a command with this process's output streams, and returns its exit code.
|
|
18
|
+
readonly runScript: (argv: readonly string[], cwd: string | undefined) => Promise<number>;
|
|
19
|
+
|
|
20
|
+
readonly writeError: (text: string) => void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Returns 1 for a refusal, and otherwise the exit code of the command.
|
|
24
|
+
export async function runCIOnlyCheck(options: Readonly<CIOnlyCheckOptions>): Promise<number> {
|
|
25
|
+
const extraArgs = pickExtraArgs(options.command, options.env['npm_lifecycle_script']);
|
|
26
|
+
|
|
27
|
+
// bun run starts a script in the folder of its package.json. npm_package_json can name an outer
|
|
28
|
+
// run's file, so the working folder is the one to trust.
|
|
29
|
+
const scope = readScope({
|
|
30
|
+
check: options.check,
|
|
31
|
+
extraArgs,
|
|
32
|
+
scriptFolder: options.cwd,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const verdict = isCIOnlyAllowed(options.env)
|
|
36
|
+
? { kind: 'allow' as const }
|
|
37
|
+
: pickCIOnlyVerdict({
|
|
38
|
+
canSeeArgs: canSeeCallerArgs(options.check, options.env['npm_lifecycle_script']),
|
|
39
|
+
check: options.check,
|
|
40
|
+
isInPackage: scope.isInPackage,
|
|
41
|
+
isScoped: scope.isScoped,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
if (verdict.kind === 'refuse') {
|
|
45
|
+
options.writeError(`${verdict.message}\n`);
|
|
46
|
+
|
|
47
|
+
return 1;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
for (const run of pickScriptRuns({
|
|
51
|
+
command: options.command,
|
|
52
|
+
extraArgs,
|
|
53
|
+
filterFolders: scope.filterFolders,
|
|
54
|
+
})) {
|
|
55
|
+
const exitCode = await options.runScript(run.argv, run.cwd);
|
|
56
|
+
|
|
57
|
+
if (exitCode !== 0) {
|
|
58
|
+
return exitCode;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return 0;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function runCIOnlyCommand(check: CIOnlyCheck, command: readonly string[]): Promise<number> {
|
|
66
|
+
return runCIOnlyCheck({
|
|
67
|
+
check,
|
|
68
|
+
command,
|
|
69
|
+
cwd: process.cwd(),
|
|
70
|
+
env: process.env,
|
|
71
|
+
runScript: (argv, cwd) =>
|
|
72
|
+
Bun.spawn([...argv], {
|
|
73
|
+
...(cwd === undefined ? {} : { cwd }),
|
|
74
|
+
stdio: ['inherit', 'inherit', 'inherit'],
|
|
75
|
+
}).exited,
|
|
76
|
+
writeError: (text) => {
|
|
77
|
+
process.stderr.write(text);
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
}
|
|
@@ -33,6 +33,7 @@ export function parseDocsChecks(script: string | undefined): DocsChecks {
|
|
|
33
33
|
.with({ kind: 'init' }, () => refusal)
|
|
34
34
|
.with({ kind: 'ci-plan' }, () => refusal)
|
|
35
35
|
.with({ kind: 'ci-plan-json' }, () => refusal)
|
|
36
|
+
.with({ kind: 'ci-only' }, () => refusal)
|
|
36
37
|
.with({ kind: 'refused' }, () => refusal)
|
|
37
38
|
.exhaustive();
|
|
38
39
|
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { parseArgs } from 'node:util';
|
|
2
|
+
import type { CIOnlyCheck } from '../ci-only/pick-ci-only-verdict.ts';
|
|
3
|
+
import { ciOnlyChecks, isCIOnlyCheck } from '../ci-only/pick-ci-only-verdict.ts';
|
|
2
4
|
import type { CheckName } from './check-names.ts';
|
|
3
5
|
import { checkNames, isCheckName } from './check-names.ts';
|
|
4
6
|
|
|
@@ -6,6 +8,11 @@ export type ChexRequest =
|
|
|
6
8
|
| { readonly kind: 'init' }
|
|
7
9
|
| { readonly kind: 'ci-plan' }
|
|
8
10
|
| { readonly kind: 'ci-plan-json' }
|
|
11
|
+
| {
|
|
12
|
+
readonly kind: 'ci-only';
|
|
13
|
+
readonly check: CIOnlyCheck;
|
|
14
|
+
readonly command: readonly string[];
|
|
15
|
+
}
|
|
9
16
|
| {
|
|
10
17
|
readonly kind: 'checks';
|
|
11
18
|
readonly names: readonly CheckName[];
|
|
@@ -24,6 +31,7 @@ const checksWithArgs: ReadonlySet<CheckName> = new Set([
|
|
|
24
31
|
]);
|
|
25
32
|
|
|
26
33
|
export const chexUsage = `usage: chex <check>... | chex <check> [args...] | chex init | chex ci-plan [--json]
|
|
34
|
+
| chex ci-only <${ciOnlyChecks.join('|')}> -- command...
|
|
27
35
|
checks: ${checkNames.join(', ')}`;
|
|
28
36
|
|
|
29
37
|
export function parseChexArgs(argv: readonly string[]): ChexRequest {
|
|
@@ -49,6 +57,10 @@ export function parseChexArgs(argv: readonly string[]): ChexRequest {
|
|
|
49
57
|
: { kind: 'refused', message: 'ci-plan takes no argument but --json' };
|
|
50
58
|
}
|
|
51
59
|
|
|
60
|
+
if (first === 'ci-only') {
|
|
61
|
+
return parseCIOnlyArgs(rest);
|
|
62
|
+
}
|
|
63
|
+
|
|
52
64
|
// Each check parses its own options, so the tokens only mark where the check names end.
|
|
53
65
|
const tokens = parseArgs({
|
|
54
66
|
args: [...argv],
|
|
@@ -87,3 +99,15 @@ export function parseChexArgs(argv: readonly string[]): ChexRequest {
|
|
|
87
99
|
|
|
88
100
|
return { kind: 'checks', names, args };
|
|
89
101
|
}
|
|
102
|
+
|
|
103
|
+
function parseCIOnlyArgs(rest: readonly string[]): ChexRequest {
|
|
104
|
+
const [check, separator, ...command] = rest;
|
|
105
|
+
|
|
106
|
+
if (check === undefined || !isCIOnlyCheck(check)) {
|
|
107
|
+
return { kind: 'refused', message: `ci-only takes ${ciOnlyChecks.join(' or ')}` };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return separator === '--' && command.length > 0
|
|
111
|
+
? { kind: 'ci-only', check, command }
|
|
112
|
+
: { kind: 'refused', message: 'ci-only takes the command to guard after --' };
|
|
113
|
+
}
|
package/src/cli/run-chex.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { match } from 'ts-pattern';
|
|
2
|
+
import type { CIOnlyCheck } from '../ci-only/pick-ci-only-verdict.ts';
|
|
2
3
|
import type { CheckName } from './check-names.ts';
|
|
3
4
|
import { chexUsage, parseChexArgs } from './parse-chex-args.ts';
|
|
4
5
|
|
|
@@ -7,6 +8,7 @@ export interface ChexDeps {
|
|
|
7
8
|
readonly runInit: () => Promise<number>;
|
|
8
9
|
readonly runCIPlan: () => Promise<number>;
|
|
9
10
|
readonly runCIPlanJSON: () => Promise<number>;
|
|
11
|
+
readonly runCIOnly: (check: CIOnlyCheck, command: readonly string[]) => Promise<number>;
|
|
10
12
|
readonly writeOutput: (text: string) => void;
|
|
11
13
|
readonly writeError: (text: string) => void;
|
|
12
14
|
}
|
|
@@ -23,6 +25,7 @@ export function runChex(argv: readonly string[], deps: ChexDeps): Promise<number
|
|
|
23
25
|
.with({ kind: 'init' }, () => deps.runInit())
|
|
24
26
|
.with({ kind: 'ci-plan' }, () => deps.runCIPlan())
|
|
25
27
|
.with({ kind: 'ci-plan-json' }, () => deps.runCIPlanJSON())
|
|
28
|
+
.with({ kind: 'ci-only' }, (request) => deps.runCIOnly(request.check, request.command))
|
|
26
29
|
.with({ kind: 'checks' }, (request) => runChecks(request.names, request.args, deps))
|
|
27
30
|
.exhaustive();
|
|
28
31
|
}
|
package/src/cli.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { rename, rm } from 'node:fs/promises';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
4
|
import { readOptionalText } from '@zgeoff/std';
|
|
5
|
+
import { runCIOnlyCommand } from './ci-only/run-ci-only-check.ts';
|
|
5
6
|
import { runCIPlanCommand } from './ci-plan/write-check-plan.ts';
|
|
6
7
|
import { writeCIPlanJSON } from './ci-plan/write-ci-plan-json.ts';
|
|
7
8
|
import { makeCheckRunner } from './cli/make-check-runner.ts';
|
|
@@ -51,6 +52,7 @@ process.exitCode = await runChex(process.argv.slice(2), {
|
|
|
51
52
|
},
|
|
52
53
|
}),
|
|
53
54
|
runCIPlan: runCIPlanCommand,
|
|
55
|
+
runCIOnly: runCIOnlyCommand,
|
|
54
56
|
runCIPlanJSON: () =>
|
|
55
57
|
writeCIPlanJSON({
|
|
56
58
|
cwd: process.cwd(),
|
|
@@ -4,10 +4,27 @@ import { isRecord } from '@zgeoff/std';
|
|
|
4
4
|
export const baselinePackages = ['@zgeoff/chex', '@zgeoff/rig', '@zgeoff/diet'] as const;
|
|
5
5
|
export const docsScript = 'chex prose length links readmes skill';
|
|
6
6
|
|
|
7
|
+
// A script that init writes for a tool the repo installs and has no script for. It starts with
|
|
8
|
+
// chex ci-only, so a whole-repo run happens only in CI and the push hook.
|
|
9
|
+
export const toolScripts = [
|
|
10
|
+
{ name: 'lint', tool: 'oxlint', body: 'chex ci-only lint -- oxlint' },
|
|
11
|
+
{
|
|
12
|
+
name: 'typecheck',
|
|
13
|
+
tool: '@typescript/native-preview',
|
|
14
|
+
body: 'chex ci-only typecheck -- tsgo --noEmit',
|
|
15
|
+
},
|
|
16
|
+
] as const;
|
|
17
|
+
|
|
7
18
|
export type PackageOutcome =
|
|
8
19
|
| { readonly kind: 'missing'; readonly packages: readonly string[] }
|
|
9
20
|
| { readonly kind: 'unchanged' }
|
|
10
|
-
| {
|
|
21
|
+
| {
|
|
22
|
+
readonly kind: 'changed';
|
|
23
|
+
readonly text: string;
|
|
24
|
+
|
|
25
|
+
// The names of the scripts it adds.
|
|
26
|
+
readonly added: readonly string[];
|
|
27
|
+
}
|
|
11
28
|
| { readonly kind: 'refused'; readonly reason: string };
|
|
12
29
|
|
|
13
30
|
export function buildInitPackage(text: string): PackageOutcome {
|
|
@@ -43,13 +60,26 @@ export function buildInitPackage(text: string): PackageOutcome {
|
|
|
43
60
|
return { kind: 'refused', reason: 'the scripts of package.json are not a JSON object' };
|
|
44
61
|
}
|
|
45
62
|
|
|
46
|
-
|
|
63
|
+
const added = {
|
|
64
|
+
...(scripts['check:docs'] === undefined ? { 'check:docs': docsScript } : {}),
|
|
65
|
+
...Object.fromEntries(
|
|
66
|
+
toolScripts
|
|
67
|
+
.filter((entry) => installed.has(entry.tool) && scripts[entry.name] === undefined)
|
|
68
|
+
.map((entry) => [entry.name, entry.body]),
|
|
69
|
+
),
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
if (Object.keys(added).length === 0) {
|
|
47
73
|
return { kind: 'unchanged' };
|
|
48
74
|
}
|
|
49
75
|
|
|
50
|
-
const edited = { ...parsed, scripts: { ...scripts,
|
|
76
|
+
const edited = { ...parsed, scripts: { ...scripts, ...added } };
|
|
51
77
|
|
|
52
|
-
return {
|
|
78
|
+
return {
|
|
79
|
+
kind: 'changed',
|
|
80
|
+
text: `${JSON.stringify(edited, null, 2)}\n`,
|
|
81
|
+
added: Object.keys(added),
|
|
82
|
+
};
|
|
53
83
|
}
|
|
54
84
|
|
|
55
85
|
function listKeys(value: unknown): string[] {
|
|
@@ -3,7 +3,7 @@ import type { LefthookOutcome } from '../lefthook/build-lefthook-extends.ts';
|
|
|
3
3
|
import { baselinePath, buildLefthookExtends } from '../lefthook/build-lefthook-extends.ts';
|
|
4
4
|
import { buildLefthookGuard, guardName, guardedHooks } from '../lefthook/build-lefthook-guard.ts';
|
|
5
5
|
import type { PackageOutcome } from './build-init-package.ts';
|
|
6
|
-
import { buildInitPackage, docsScript } from './build-init-package.ts';
|
|
6
|
+
import { buildInitPackage, docsScript, toolScripts } from './build-init-package.ts';
|
|
7
7
|
|
|
8
8
|
export interface InitWrite {
|
|
9
9
|
readonly file: 'lefthook.yml' | 'package.json';
|
|
@@ -44,7 +44,7 @@ export function buildInitPlan(
|
|
|
44
44
|
{
|
|
45
45
|
reasons: [],
|
|
46
46
|
writes: [{ file: 'package.json', text: outcome.text }],
|
|
47
|
-
notes:
|
|
47
|
+
notes: outcome.added.map((name) => describeAddedScript(name)),
|
|
48
48
|
},
|
|
49
49
|
]),
|
|
50
50
|
)
|
|
@@ -112,3 +112,11 @@ function buildGuardStep(text: string, notes: readonly string[]): InitStep {
|
|
|
112
112
|
.with({ kind: 'refused' }, (outcome) => ({ reasons: [outcome.reason], writes: [], notes: [] }))
|
|
113
113
|
.exhaustive();
|
|
114
114
|
}
|
|
115
|
+
|
|
116
|
+
function describeAddedScript(name: string): string {
|
|
117
|
+
const tool = toolScripts.find((entry) => entry.name === name);
|
|
118
|
+
|
|
119
|
+
return tool === undefined
|
|
120
|
+
? `package.json runs "${docsScript}" as ${name}`
|
|
121
|
+
: `package.json runs "${tool.body}" as ${name}`;
|
|
122
|
+
}
|
|
@@ -51,6 +51,8 @@ export async function runTypecheckCheck(options: Readonly<TypecheckCheckOptions>
|
|
|
51
51
|
.with({ kind: 'whole' }, (whole) => {
|
|
52
52
|
options.writeOutput(`typecheck: the whole repo, because ${whole.reason}\n`);
|
|
53
53
|
|
|
54
|
+
// A hook job sets CHEX_ALLOW_CI_ONLY itself, so a person who runs chex typecheck by hand
|
|
55
|
+
// gets the script's own refusal.
|
|
54
56
|
return options.runScript(['bun', 'run', 'typecheck'], options.env);
|
|
55
57
|
})
|
|
56
58
|
.with({ kind: 'scoped' }, (scoped) => {
|
|
@@ -65,6 +67,7 @@ export async function runTypecheckCheck(options: Readonly<TypecheckCheckOptions>
|
|
|
65
67
|
|
|
66
68
|
return options.runScript(['sh', '-c', scoped.command], {
|
|
67
69
|
...options.env,
|
|
70
|
+
CHEX_ALLOW_CI_ONLY: '1',
|
|
68
71
|
PATH: searchPath,
|
|
69
72
|
});
|
|
70
73
|
})
|