@zgeoff/chex 0.8.10 → 0.8.12
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 +45 -0
- package/src/ci-only/pick-extra-args.ts +18 -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 +79 -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/docs/check-prose.sh +41 -10
- 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,45 @@
|
|
|
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
|
+
|
|
22
|
+
export type CIOnlyVerdict =
|
|
23
|
+
| { readonly kind: 'allow' }
|
|
24
|
+
| { readonly kind: 'refuse'; readonly message: string };
|
|
25
|
+
|
|
26
|
+
// The verdict for a run that CI and the override did not already let through. It allows a run from
|
|
27
|
+
// inside one package or with a scope, and refuses every other run.
|
|
28
|
+
export function pickCIOnlyVerdict(input: Readonly<CIOnlyVerdictInput>): CIOnlyVerdict {
|
|
29
|
+
if (input.isInPackage || input.isScoped) {
|
|
30
|
+
return { kind: 'allow' };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return { kind: 'refuse', message: buildRefusal(input.check) };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function buildRefusal(check: CIOnlyCheck): string {
|
|
37
|
+
const checked = check === 'lint' ? 'the files you changed' : 'the packages you changed';
|
|
38
|
+
|
|
39
|
+
const instead =
|
|
40
|
+
check === 'lint'
|
|
41
|
+
? 'lint named files with `bun run lint <file>...`'
|
|
42
|
+
: 'check one package with `bun run typecheck --filter <package>`';
|
|
43
|
+
|
|
44
|
+
return `${check} runs on the whole repo only in CI and the push hook, which checks ${checked}. Push, or ${instead}. To run it here anyway, set ${overrideVariable}=1.`;
|
|
45
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
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 baseText = /^[\s\S]*\bci-only \S+ -- (?<base>[\s\S]*)$/u.exec(scriptText)?.groups?.['base'];
|
|
15
|
+
const baseWords = baseText === undefined ? null : parseShellWords(baseText);
|
|
16
|
+
|
|
17
|
+
return baseWords === null ? [] : command.slice(baseWords.length);
|
|
18
|
+
}
|
|
@@ -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,79 @@
|
|
|
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 { 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
|
+
check: options.check,
|
|
39
|
+
isInPackage: scope.isInPackage,
|
|
40
|
+
isScoped: scope.isScoped,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
if (verdict.kind === 'refuse') {
|
|
44
|
+
options.writeError(`${verdict.message}\n`);
|
|
45
|
+
|
|
46
|
+
return 1;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
for (const run of pickScriptRuns({
|
|
50
|
+
command: options.command,
|
|
51
|
+
extraArgs,
|
|
52
|
+
filterFolders: scope.filterFolders,
|
|
53
|
+
})) {
|
|
54
|
+
const exitCode = await options.runScript(run.argv, run.cwd);
|
|
55
|
+
|
|
56
|
+
if (exitCode !== 0) {
|
|
57
|
+
return exitCode;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return 0;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function runCIOnlyCommand(check: CIOnlyCheck, command: readonly string[]): Promise<number> {
|
|
65
|
+
return runCIOnlyCheck({
|
|
66
|
+
check,
|
|
67
|
+
command,
|
|
68
|
+
cwd: process.cwd(),
|
|
69
|
+
env: process.env,
|
|
70
|
+
runScript: (argv, cwd) =>
|
|
71
|
+
Bun.spawn([...argv], {
|
|
72
|
+
...(cwd === undefined ? {} : { cwd }),
|
|
73
|
+
stdio: ['inherit', 'inherit', 'inherit'],
|
|
74
|
+
}).exited,
|
|
75
|
+
writeError: (text) => {
|
|
76
|
+
process.stderr.write(text);
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
}
|
|
@@ -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(),
|
package/src/docs/check-prose.sh
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
# Greps committed docs for prose violations; exits non-zero when any are found.
|
|
3
3
|
# Violation greps fail the run; candidate greps print for judgment and never fail it.
|
|
4
4
|
# Usage (from the repo root): check-prose.sh [<path>...]
|
|
5
|
+
# Outside a git work tree, it checks only the paths it is given, each under the current directory,
|
|
6
|
+
# and reads each one as changed.
|
|
5
7
|
# With no path, it reads every README.md outside .claude/ and .agents/, every Markdown file under
|
|
6
8
|
# docs/, every spike's results.md, and every skill's Markdown under .agents/skills/ and
|
|
7
9
|
# .claude/skills/, where a consumer repo installs skills, under skills/, where zgeoff/skills keeps
|
|
@@ -10,11 +12,27 @@
|
|
|
10
12
|
# shellcheck disable=SC2016
|
|
11
13
|
set -uo pipefail
|
|
12
14
|
|
|
15
|
+
in_work_tree=1
|
|
13
16
|
if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
|
|
14
|
-
|
|
15
|
-
|
|
17
|
+
in_work_tree=0
|
|
18
|
+
# pr-body reads the pathspecs from the first `if [ "$#" -eq 0 ]` block, so this test is written apart.
|
|
19
|
+
if (($# == 0)); then
|
|
20
|
+
echo "check-prose: run it inside a git work tree, or name the files to check, because with no path it lists the files with git" >&2
|
|
21
|
+
exit 2
|
|
22
|
+
fi
|
|
16
23
|
fi
|
|
17
24
|
|
|
25
|
+
# Prints the files the pathspecs name, each ending in a NUL. In a work tree it lists the tracked
|
|
26
|
+
# and untracked files git does not ignore; outside one, git grep --no-index lists each file with a
|
|
27
|
+
# line, so an empty file, which holds nothing to check, drops out.
|
|
28
|
+
list_files() {
|
|
29
|
+
if [ "$in_work_tree" -eq 1 ]; then
|
|
30
|
+
git ls-files -z --cached --others --exclude-standard -- "$@"
|
|
31
|
+
else
|
|
32
|
+
git grep --no-index -z -l -e '' -- "$@"
|
|
33
|
+
fi
|
|
34
|
+
}
|
|
35
|
+
|
|
18
36
|
# A skill folder's README is a skill file, so only the READMEs outside the skill folders are listed
|
|
19
37
|
# one by one.
|
|
20
38
|
if [ "$#" -eq 0 ]; then
|
|
@@ -54,19 +72,29 @@ failure_marker=$(mktemp) || exit 2
|
|
|
54
72
|
rm -f "$failure_marker"
|
|
55
73
|
trap 'rm -f "$failure_marker"' EXIT
|
|
56
74
|
|
|
75
|
+
# Outside a work tree, git grep reads the named files with --no-index in place of --untracked.
|
|
76
|
+
grep_scope=--untracked
|
|
77
|
+
[ "$in_work_tree" -eq 1 ] || grep_scope=--no-index
|
|
78
|
+
|
|
57
79
|
probe_status=0
|
|
58
|
-
git grep -P -e x -- ':(exclude)*' > /dev/null 2>&1 || probe_status=$?
|
|
80
|
+
git grep "$grep_scope" -P -e x -- ':(exclude)*' > /dev/null 2>&1 || probe_status=$?
|
|
59
81
|
if [ "$probe_status" -gt 1 ]; then
|
|
60
82
|
echo "check-prose: git was built without PCRE support, so no doc was checked" >&2
|
|
61
83
|
exit 2
|
|
62
84
|
fi
|
|
63
85
|
|
|
64
86
|
# The greps below read git's file list in pipelines, which drop git's exit status, so a git that
|
|
65
|
-
# cannot list the files fails here once.
|
|
66
|
-
|
|
87
|
+
# cannot list the files fails here once. git grep exits 1 when it finds no file.
|
|
88
|
+
list_status=0
|
|
89
|
+
list_files "$@" > /dev/null || list_status=$?
|
|
90
|
+
if [ "$in_work_tree" -eq 1 ] && [ "$list_status" -ne 0 ]; then
|
|
67
91
|
echo "check-prose: git ls-files failed, so no doc was checked" >&2
|
|
68
92
|
exit 2
|
|
69
93
|
fi
|
|
94
|
+
if [ "$in_work_tree" -eq 0 ] && [ "$list_status" -gt 1 ]; then
|
|
95
|
+
echo "check-prose: git could not read the paths; outside a git work tree, name files under the current directory, so no doc was checked" >&2
|
|
96
|
+
exit 2
|
|
97
|
+
fi
|
|
70
98
|
|
|
71
99
|
# Every grep reads untracked files too, so a new doc is checked before it is staged, and skips
|
|
72
100
|
# binary files.
|
|
@@ -75,7 +103,7 @@ fi
|
|
|
75
103
|
# printing "clean".
|
|
76
104
|
grep_docs() {
|
|
77
105
|
local status=0
|
|
78
|
-
git grep
|
|
106
|
+
git grep "$grep_scope" -I "$@" || status=$?
|
|
79
107
|
if [ "$status" -gt 1 ]; then
|
|
80
108
|
echo "check-prose: git grep failed with status $status, so a check did not run" >&2
|
|
81
109
|
: > "$failure_marker"
|
|
@@ -126,7 +154,7 @@ list_markdown() {
|
|
|
126
154
|
if [[ $file == *.md && $file == "${markdown_dir:-}"* && -f $file ]]; then
|
|
127
155
|
printf './%s\0' "$file"
|
|
128
156
|
fi
|
|
129
|
-
done < <(
|
|
157
|
+
done < <(list_files "$@")
|
|
130
158
|
}
|
|
131
159
|
|
|
132
160
|
# Tracks fenced code blocks (backtick or tilde, any indent, closed by the same marker at least as
|
|
@@ -670,7 +698,7 @@ END { flush() }
|
|
|
670
698
|
changed=$'\n'
|
|
671
699
|
while IFS= read -r -d '' file; do
|
|
672
700
|
changed+=$file$'\n'
|
|
673
|
-
done < <(list_changed)
|
|
701
|
+
done < <(if [ "$in_work_tree" -eq 1 ]; then list_changed; else list_files "$@"; fi)
|
|
674
702
|
|
|
675
703
|
# The Markdown files the paths name that the branch changes, each as ./<path> from list_markdown.
|
|
676
704
|
paragraph_files=()
|
|
@@ -694,8 +722,11 @@ candidate "Paragraphs over $paragraph_warn words in a changed file (warning; rer
|
|
|
694
722
|
candidate 'Candidates — filler words with term-of-art uses; cull the filler, keep the terms:' \
|
|
695
723
|
"$(grep_docs -nPi '\b(naturally|cleanly|trivially|easy|simple|quick)\b' -- "$@")"
|
|
696
724
|
|
|
697
|
-
|
|
698
|
-
|
|
725
|
+
# A thing that names is often the noun after another noun, as in "file names". "As the guide says"
|
|
726
|
+
# points the reader at another doc, so (*SKIP)(*F) drops it and the scan goes on after it. The
|
|
727
|
+
# pointer may wrap onto the next line, but never crosses a blank line or the end of a sentence.
|
|
728
|
+
candidate 'Candidates — speech verbs on data artifacts; make the thing the reader wants the subject, as in "The config is in the file":' \
|
|
729
|
+
"$(grep_docs -nPi '\bas(?:[ \t]|\n(?![ \t]*\n))+(?:(?:[^\s.]|\.(?=\S))+(?:[ \t]|\n(?![ \t]*\n))+){1,4}?says\b(*SKIP)(*F)|(?:(?<!file |table |column |export |bucket |method |variable |test )\bnames|\b(?:says|tells|answers|knows|promises))\b(?! (rot|and locations|only))' -- "$@")"
|
|
699
730
|
|
|
700
731
|
candidate 'Candidates — delta-framing words; legal only when the baseline sits in the same doc:' \
|
|
701
732
|
"$(grep_docs -nPi '(^|\. )(Also|Additionally|In addition),? |\b(already|today|before this work|as it does today)\b' -- "$@")"
|
|
@@ -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
|
})
|