@zgeoff/chex 0.8.7 → 0.8.9

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.
Files changed (54) hide show
  1. package/README.md +4 -4
  2. package/lefthook.yml +22 -5
  3. package/package.json +1 -1
  4. package/src/ci-plan/build-ci-plan-output.ts +1 -37
  5. package/src/ci-plan/collect-shared-checks.ts +1 -41
  6. package/src/ci-plan/find-chex-args.ts +40 -0
  7. package/src/ci-plan/parse-package-scripts.ts +36 -0
  8. package/src/cli/check-names.ts +1 -0
  9. package/src/cli.ts +2 -0
  10. package/src/typecheck/pick-typecheck-plan.ts +172 -0
  11. package/src/typecheck/run-typecheck-check.ts +245 -0
  12. package/src/workflows/collect-allowances.ts +101 -0
  13. package/src/workflows/collect-concurrency-findings.ts +1 -1
  14. package/src/workflows/collect-hook-findings.ts +13 -0
  15. package/src/workflows/collect-moved-repo-findings.ts +34 -0
  16. package/src/workflows/collect-run-commands.ts +18 -0
  17. package/src/workflows/collect-runner-labels.ts +8 -0
  18. package/src/workflows/collect-steps.ts +9 -0
  19. package/src/workflows/constants.ts +4 -0
  20. package/src/workflows/find-heavy-runner.ts +20 -0
  21. package/src/workflows/is-list.ts +4 -0
  22. package/src/workflows/parse-repo-slug.ts +7 -0
  23. package/src/workflows/pick-checked-paths.ts +33 -0
  24. package/src/workflows/pick-repo-move.ts +28 -0
  25. package/src/workflows/read-repo-slug.ts +12 -0
  26. package/src/workflows/read-repo-state.ts +57 -0
  27. package/src/workflows/rules/build-rule-finding.ts +10 -0
  28. package/src/workflows/rules/collect-condition-terms.ts +116 -0
  29. package/src/workflows/rules/collect-hook-check-findings.ts +84 -0
  30. package/src/workflows/rules/collect-main-only-job-findings.ts +123 -0
  31. package/src/workflows/rules/collect-main-run-concurrency-findings.ts +77 -0
  32. package/src/workflows/rules/collect-main-run-shape-findings.ts +161 -0
  33. package/src/workflows/rules/collect-needs.ts +13 -0
  34. package/src/workflows/rules/collect-path-filter-findings.ts +56 -0
  35. package/src/workflows/rules/collect-pr-gate-findings.ts +72 -0
  36. package/src/workflows/rules/collect-publish-findings.ts +83 -0
  37. package/src/workflows/rules/collect-release-jobs.ts +55 -0
  38. package/src/workflows/rules/collect-release-shape-findings.ts +67 -0
  39. package/src/workflows/rules/find-file-filter.ts +91 -0
  40. package/src/workflows/rules/find-image-build.ts +59 -0
  41. package/src/workflows/rules/find-job-environment.ts +9 -0
  42. package/src/workflows/rules/find-matrix-runner.ts +38 -0
  43. package/src/workflows/rules/find-shipped-build.ts +36 -0
  44. package/src/workflows/rules/is-arm-label.ts +5 -0
  45. package/src/workflows/rules/is-release-condition.ts +19 -0
  46. package/src/workflows/rules/is-shared-check-script.ts +22 -0
  47. package/src/workflows/rules/is-shared-release-workflow.ts +14 -0
  48. package/src/workflows/rules/is-workflow-call.ts +12 -0
  49. package/src/workflows/rules/types.ts +31 -0
  50. package/src/workflows/run-workflows-check.ts +60 -55
  51. package/src/workflows/to-unwrapped-command.ts +28 -0
  52. package/src/workflows/to-workflow-findings.ts +24 -0
  53. package/src/workflows/types.ts +68 -0
  54. package/src/workflows/workflow-context.ts +0 -24
package/README.md CHANGED
@@ -58,9 +58,10 @@ Run these repo checks on the branch, the pushed commits and the repo's config:
58
58
  | `test-timeouts` | a `bun test` timeout above its lane's limit, in a script or a `bunfig.toml` | none |
59
59
  | `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
60
  | `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 fails when there is one | the paths |
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 |
62
62
  | `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 |
63
63
  | `uncommitted` | a file the branch changes that has an uncommitted edit, which the push does not send | none |
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 |
64
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 |
65
66
 
66
67
  With no path, `prose` and `links` read every README outside `.claude/` and `.agents/`, every file
@@ -95,6 +96,5 @@ extends:
95
96
  - node_modules/@zgeoff/chex/lefthook.yml
96
97
  ```
97
98
 
98
- The baseline's jobs, which [its doc](../../docs/architecture/pre-push-baseline.md) describes, call
99
- `chex <check>`, `bun run check:docs`, `rig related` and the repo's own `format:check` and
100
- `typecheck` scripts.
99
+ The baseline's jobs, which [its doc](../../docs/architecture/pre-push-baseline.md) lists, call chex,
100
+ rig, the linters and the repo's `format:check`, `typecheck` and `check:docs` scripts.
package/lefthook.yml CHANGED
@@ -3,8 +3,9 @@
3
3
  # extends:
4
4
  # - node_modules/@zgeoff/chex/lefthook.yml
5
5
  #
6
- # It checks what the branch changes, plus the whole-tree gates that fit a
7
- # small host: format:check and typecheck. CI runs every whole-tree check. It
6
+ # It checks what the branch changes, plus format:check over the whole tree,
7
+ # which fits a small host. CI runs every whole-tree check, the typecheck and
8
+ # the dependency gates among them. It
8
9
  # adds no job before a commit, so a commit costs only the repo's own jobs, such
9
10
  # as its staged-file fixers. A repo adds its own jobs under pre-commit.jobs and pre-push.jobs in
10
11
  # lefthook.yml; the pre-push ones run before these.
@@ -107,6 +108,19 @@ pre-push:
107
108
  fi
108
109
  bun x --no-install diet -- bun x --no-install chex workflows {files} || status=1
109
110
  exit "$status"
111
+ # In a moved repo the workflow rules also read .github/ci.yml, package.json
112
+ # and the lefthook files, so a change to one of them runs the lint over
113
+ # every workflow and hook. actionlint reads workflows only, so these files
114
+ # get a job of their own.
115
+ - name: chex-workflow-config
116
+ files:
117
+ unset $(git rev-parse --local-env-vars); bun
118
+ node_modules/@zgeoff/chex/src/branch/read-branch-files.ts
119
+ glob:
120
+ - '.github/ci.yml'
121
+ - 'package.json'
122
+ - 'lefthook.{yml,yaml}'
123
+ run: '{clear-git-env} bun x --no-install diet -- bun x --no-install chex workflows {files}'
110
124
  # git lists the refs a push sends on the hook's stdin, and lefthook hands
111
125
  # stdin only to a job that asks for it. lefthook 2.2.2 skips a pre-push
112
126
  # job whose run substitutes no file when the checked-out branch has
@@ -132,10 +146,13 @@ pre-push:
132
146
  run: '{clear-git-env} bun x --no-install diet -- bun x --no-install chex test-timeouts'
133
147
  - name: chex-lint-overrides
134
148
  run: '{clear-git-env} bun x --no-install diet -- bun x --no-install chex lint-overrides'
135
- - name: chex-dependency-gates
136
- run: '{clear-git-env} bun x --no-install diet -- bun x --no-install chex dependency-gates'
149
+ # chex typecheck reads the files the branch changes or deletes. It skips
150
+ # when none is a file a typecheck reads, and in a repo whose typecheck
151
+ # script runs turbo it narrows turbo to the changed packages and their
152
+ # dependents. A change that every package reads, and a repo it cannot
153
+ # narrow, run the whole typecheck script.
137
154
  - name: chex-typecheck
138
- run: '{clear-git-env} bun x --no-install diet -- bun run typecheck'
155
+ run: '{clear-git-env} bun x --no-install diet -- bun x --no-install chex typecheck'
139
156
  # A bunfig.toml reaches the job too, because rig related runs a spread
140
157
  # of the tests under a changed bunfig.
141
158
  - name: chex-test
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zgeoff/chex",
3
- "version": "0.8.7",
3
+ "version": "0.8.9",
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",
@@ -1,4 +1,5 @@
1
1
  import { parseCIPlan } from './parse-ci-plan.ts';
2
+ import { parsePackageScripts } from './parse-package-scripts.ts';
2
3
 
3
4
  export interface CIPlanOutput {
4
5
  readonly exitCode: number;
@@ -24,43 +25,6 @@ export function buildCIPlanOutput(
24
25
  return { exitCode: 0, lines: [JSON.stringify(outcome.plan)] };
25
26
  }
26
27
 
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
28
  function buildErrorOutput(messages: readonly string[]): CIPlanOutput {
65
29
  return { exitCode: 1, lines: messages.map((message) => `::error::${escapeData(message)}`) };
66
30
  }
@@ -1,4 +1,5 @@
1
1
  import { parseChexArgs } from '../cli/parse-chex-args.ts';
2
+ import { findChexArgs } from './find-chex-args.ts';
2
3
 
3
4
  export function collectSharedChecks(command: string | undefined): readonly string[] {
4
5
  if (command === undefined) {
@@ -59,44 +60,3 @@ function splitSimpleCommands(command: string): string[][] {
59
60
 
60
61
  return commands.filter((words) => words.length > 0);
61
62
  }
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,40 @@
1
+ const chexWord = /(?:^|\/)chex$|^@zgeoff\/chex(?:@\S*)?$/u;
2
+ const assignment = /^\w+=/u;
3
+
4
+ // The arguments after chex when the command runs chex itself, as a path, or through bunx, npx or
5
+ // bun x. A command that only mentions chex as an argument, such as echo chex, runs no check.
6
+ export function findChexArgs(words: readonly string[]): readonly string[] | undefined {
7
+ const start = words.findIndex((word) => !assignment.test(word));
8
+ const [first, second] = words.slice(start);
9
+
10
+ if (start === -1 || first === undefined) {
11
+ return undefined;
12
+ }
13
+
14
+ if (chexWord.test(first)) {
15
+ return words.slice(start + 1);
16
+ }
17
+
18
+ const runnerLength = runnerWordCount(first, second);
19
+
20
+ if (runnerLength === 0) {
21
+ return undefined;
22
+ }
23
+
24
+ const rest = words.slice(start + runnerLength);
25
+ const toolIndex = rest.findIndex((word) => !word.startsWith('-'));
26
+ const tool = rest[toolIndex];
27
+ const args = rest.slice(toolIndex + 1);
28
+
29
+ return tool !== undefined && chexWord.test(tool) ? args : undefined;
30
+ }
31
+
32
+ const runners = new Set(['bunx', 'npx']);
33
+
34
+ function runnerWordCount(first: string, second: string | undefined): number {
35
+ if (runners.has(first)) {
36
+ return 1;
37
+ }
38
+
39
+ return first === 'bun' && second === 'x' ? 2 : 0;
40
+ }
@@ -0,0 +1,36 @@
1
+ type PackageScripts =
2
+ | { readonly kind: 'read'; readonly scripts: Readonly<Record<string, string>> | undefined }
3
+ | { readonly kind: 'invalid'; readonly message: string };
4
+
5
+ export function parsePackageScripts(packageText: string | undefined): PackageScripts {
6
+ if (packageText === undefined) {
7
+ return { kind: 'read', scripts: undefined };
8
+ }
9
+
10
+ let packageJSON: unknown;
11
+
12
+ try {
13
+ packageJSON = JSON.parse(packageText);
14
+ } catch {
15
+ return { kind: 'invalid', message: 'package.json is not valid JSON; fix the syntax.' };
16
+ }
17
+
18
+ if (typeof packageJSON !== 'object' || packageJSON === null || !('scripts' in packageJSON)) {
19
+ return { kind: 'read', scripts: {} };
20
+ }
21
+
22
+ const scripts = packageJSON.scripts;
23
+
24
+ if (typeof scripts !== 'object' || scripts === null) {
25
+ return { kind: 'read', scripts: {} };
26
+ }
27
+
28
+ return {
29
+ kind: 'read',
30
+ scripts: Object.fromEntries(
31
+ Object.entries(scripts).filter(
32
+ (entry): entry is [string, string] => typeof entry[1] === 'string',
33
+ ),
34
+ ),
35
+ };
36
+ }
@@ -11,6 +11,7 @@ export const checkNames = [
11
11
  'workflows',
12
12
  'dependency-gates',
13
13
  'uncommitted',
14
+ 'typecheck',
14
15
  'pr-body',
15
16
  ] as const;
16
17
 
package/src/cli.ts CHANGED
@@ -15,6 +15,7 @@ import { writeTextFile } from './output/write-text-file.ts';
15
15
  import { runPrBodyCheck } from './pr-body/run-pr-body-check.ts';
16
16
  import { runTestLayoutCommand } from './test-layout/run-test-layout-check.ts';
17
17
  import { runTestTimeoutsCommand } from './test-timeouts/run-test-timeouts-check.ts';
18
+ import { runTypecheckCommand } from './typecheck/run-typecheck-check.ts';
18
19
  import { runUncommittedCommand } from './uncommitted/run-uncommitted-check.ts';
19
20
  import { runWorkflowsCommand } from './workflows/run-workflows-check.ts';
20
21
 
@@ -31,6 +32,7 @@ process.exitCode = await runChex(process.argv.slice(2), {
31
32
  workflows: runWorkflowsCommand,
32
33
  'dependency-gates': runDependencyGatesCommand,
33
34
  uncommitted: runUncommittedCommand,
35
+ typecheck: runTypecheckCommand,
34
36
  'pr-body': runPrBodyCheck,
35
37
  },
36
38
  }),
@@ -0,0 +1,172 @@
1
+ import { basename } from 'node:path';
2
+ import { match } from 'ts-pattern';
3
+
4
+ export interface WorkspacePackage {
5
+ readonly name: string;
6
+
7
+ // Relative to the repo root, with no trailing slash.
8
+ readonly path: string;
9
+ }
10
+
11
+ // How the repo could narrow its typecheck: through turbo, or not at all.
12
+ export type TypecheckScope =
13
+ | { readonly kind: 'none'; readonly reason: string }
14
+ | { readonly kind: 'turbo'; readonly packages: readonly WorkspacePackage[] };
15
+
16
+ // The files the branch changes or deletes, or why chex cannot read them.
17
+ export type BranchFiles =
18
+ | { readonly kind: 'read'; readonly files: readonly string[] }
19
+ | { readonly kind: 'unread'; readonly reason: string };
20
+
21
+ export interface TypecheckPlanInput {
22
+ readonly branchFiles: BranchFiles;
23
+
24
+ // The repo's typecheck script, or null when package.json has none.
25
+ readonly script: string | null;
26
+
27
+ readonly scope: TypecheckScope;
28
+
29
+ // The globalDependencies globs of the turbo config, relative to the repo root; empty without a
30
+ // config, and null for a config chex cannot read, where any changed file could be one.
31
+ readonly globalDependencies: readonly string[] | null;
32
+ }
33
+
34
+ export type TypecheckPlan =
35
+ | { readonly kind: 'skip'; readonly reason: string }
36
+ | { readonly kind: 'whole'; readonly reason: string }
37
+ | {
38
+ readonly kind: 'scoped';
39
+ readonly packageNames: readonly string[];
40
+
41
+ // The repo's typecheck script with a turbo filter for each package and its dependents.
42
+ readonly command: string;
43
+ };
44
+
45
+ // Never skips when it cannot tell: an unread branch, a repo it cannot narrow or a change that
46
+ // every package reads all run the whole typecheck.
47
+ export function pickTypecheckPlan(input: Readonly<TypecheckPlanInput>): TypecheckPlan {
48
+ return match(input.branchFiles)
49
+ .with({ kind: 'unread' }, (unread): TypecheckPlan => ({ kind: 'whole', reason: unread.reason }))
50
+ .with({ kind: 'read' }, (read) => pickBranchPlan(input, read.files))
51
+ .exhaustive();
52
+ }
53
+
54
+ function pickBranchPlan(
55
+ input: Readonly<TypecheckPlanInput>,
56
+ files: readonly string[],
57
+ ): TypecheckPlan {
58
+ if (input.globalDependencies === null) {
59
+ return files.length === 0
60
+ ? { kind: 'skip', reason: 'the branch changes no file' }
61
+ : { kind: 'whole', reason: "chex cannot read the turbo config's globalDependencies" };
62
+ }
63
+
64
+ const globalDependencies = input.globalDependencies;
65
+ const globalFiles = files.filter((file) => isGlobalDependency(file, globalDependencies));
66
+ const typeFiles = files.filter((file) => isTypeFile(file));
67
+
68
+ if (globalFiles.length === 0 && typeFiles.length === 0) {
69
+ return { kind: 'skip', reason: 'the branch changes no file that a typecheck reads' };
70
+ }
71
+
72
+ return match(input.scope)
73
+ .with({ kind: 'none' }, (scope): TypecheckPlan => ({ kind: 'whole', reason: scope.reason }))
74
+ .with({ kind: 'turbo' }, (scope) => pickTurboPlan(input.script, scope, globalFiles, typeFiles))
75
+ .exhaustive();
76
+ }
77
+
78
+ // turbo reads a leading ./ as the repo root, and a ! entry takes files back out, as in its docs.
79
+ function isGlobalDependency(file: string, globalDependencies: readonly string[]): boolean {
80
+ const included = globalDependencies.filter((pattern) => !pattern.startsWith('!'));
81
+ const excluded = globalDependencies.filter((pattern) => pattern.startsWith('!'));
82
+
83
+ return (
84
+ included.some((pattern) => toTurboGlob(pattern).match(file)) &&
85
+ !excluded.some((pattern) => toTurboGlob(pattern.slice(1)).match(file))
86
+ );
87
+ }
88
+
89
+ function toTurboGlob(pattern: string): InstanceType<typeof Bun.Glob> {
90
+ return new Bun.Glob(pattern.replace(/^\.\//u, ''));
91
+ }
92
+
93
+ // A file that a typecheck can read: code, JSON such as a tsconfig or an imported file, or a
94
+ // lockfile, which decides the version of each package's types.
95
+ const typeFilePattern = /\.(?:[cm]?[jt]sx?|json[c5]?)$/u;
96
+
97
+ const lockfileNames = new Set([
98
+ 'bun.lock',
99
+ 'bun.lockb',
100
+ 'package-lock.json',
101
+ 'pnpm-lock.yaml',
102
+ 'pnpm-workspace.yaml',
103
+ 'yarn.lock',
104
+ ]);
105
+
106
+ function isTypeFile(file: string): boolean {
107
+ return typeFilePattern.test(file) || lockfileNames.has(basename(file));
108
+ }
109
+
110
+ // sh splits words only on ASCII blanks, so a script with a character outside printable ASCII,
111
+ // such as a non-breaking space, runs whole rather than risk a filter in the wrong word.
112
+ const turboTypecheckPattern = /\bturbo run typecheck(?=$|[ \t\n;&|)])/gu;
113
+ const nonASCIIPattern = /[^\t\n\r -~]/u;
114
+
115
+ function pickTurboPlan(
116
+ script: string | null,
117
+ scope: Extract<TypecheckScope, { kind: 'turbo' }>,
118
+ globalFiles: readonly string[],
119
+ typeFiles: readonly string[],
120
+ ): TypecheckPlan {
121
+ const [globalFile] = globalFiles;
122
+
123
+ if (globalFile !== undefined) {
124
+ return {
125
+ kind: 'whole',
126
+ reason: `the branch changes ${globalFile}, which the turbo config lists in globalDependencies`,
127
+ };
128
+ }
129
+
130
+ const owners = typeFiles.map((file) => ({ file, owner: findOwner(file, scope.packages) }));
131
+ const rootFile = owners.find((entry) => entry.owner === null);
132
+
133
+ if (rootFile !== undefined) {
134
+ return {
135
+ kind: 'whole',
136
+ reason: `the branch changes ${rootFile.file}, which no workspace package holds`,
137
+ };
138
+ }
139
+
140
+ const turboCalls = script?.match(turboTypecheckPattern)?.length ?? 0;
141
+
142
+ if (script === null || turboCalls !== 1 || nonASCIIPattern.test(script)) {
143
+ return {
144
+ kind: 'whole',
145
+ reason: 'the typecheck script holds no single `turbo run typecheck` to narrow',
146
+ };
147
+ }
148
+
149
+ const packageNames = [
150
+ ...new Set(owners.flatMap((entry) => (entry.owner === null ? [] : [entry.owner.name]))),
151
+ ].toSorted();
152
+
153
+ const filters = packageNames.map((name) => `'--filter=...${name}'`).join(' ');
154
+
155
+ return {
156
+ kind: 'scoped',
157
+ packageNames,
158
+ command: script.replaceAll(turboTypecheckPattern, `turbo run typecheck ${filters}`),
159
+ };
160
+ }
161
+
162
+ // The package with the longest path that holds the file, so a package nested in another owns its
163
+ // own files.
164
+ function findOwner(file: string, packages: readonly WorkspacePackage[]): WorkspacePackage | null {
165
+ return packages
166
+ .filter((candidate) => file.startsWith(`${candidate.path}/`))
167
+ .reduce<WorkspacePackage | null>(
168
+ (longest, candidate) =>
169
+ longest === null || candidate.path.length > longest.path.length ? candidate : longest,
170
+ null,
171
+ );
172
+ }