checkchange 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +150 -0
- package/dist/attribution.d.ts +19 -0
- package/dist/attribution.js +175 -0
- package/dist/auto-coverage.d.ts +8 -0
- package/dist/auto-coverage.js +93 -0
- package/dist/cache.d.ts +70 -0
- package/dist/cache.js +458 -0
- package/dist/cli.d.ts +51 -0
- package/dist/cli.js +810 -0
- package/dist/complexity-providers/pythonASTComplexityProvider.d.ts +9 -0
- package/dist/complexity-providers/pythonASTComplexityProvider.js +148 -0
- package/dist/complexity-providers/pythonDescriptorProvider.d.ts +17 -0
- package/dist/complexity-providers/pythonDescriptorProvider.js +119 -0
- package/dist/complexity-providers.d.ts +6 -0
- package/dist/complexity-providers.js +1 -0
- package/dist/complexity.d.ts +14 -0
- package/dist/complexity.js +75 -0
- package/dist/coverage-providers/lcovProvider.d.ts +12 -0
- package/dist/coverage-providers/lcovProvider.js +126 -0
- package/dist/coverage.d.ts +28 -0
- package/dist/coverage.js +451 -0
- package/dist/crap.d.ts +31 -0
- package/dist/crap.js +84 -0
- package/dist/crapCalc.d.ts +5 -0
- package/dist/crapCalc.js +11 -0
- package/dist/delta.d.ts +100 -0
- package/dist/delta.js +214 -0
- package/dist/evidence.d.ts +201 -0
- package/dist/evidence.js +906 -0
- package/dist/execute.d.ts +59 -0
- package/dist/execute.js +92 -0
- package/dist/formatters/github.d.ts +5 -0
- package/dist/formatters/github.js +27 -0
- package/dist/formatters/index.d.ts +47 -0
- package/dist/formatters/index.js +14 -0
- package/dist/formatters/junit.d.ts +5 -0
- package/dist/formatters/junit.js +32 -0
- package/dist/formatters/sarif.d.ts +5 -0
- package/dist/formatters/sarif.js +35 -0
- package/dist/fs-safety.d.ts +10 -0
- package/dist/fs-safety.js +47 -0
- package/dist/git.d.ts +50 -0
- package/dist/git.js +137 -0
- package/dist/help.d.ts +40 -0
- package/dist/help.js +274 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +4 -0
- package/dist/providers/config.d.ts +41 -0
- package/dist/providers/config.js +172 -0
- package/dist/providers/genericCommand.d.ts +3 -0
- package/dist/providers/genericCommand.js +89 -0
- package/dist/providers/index.d.ts +7 -0
- package/dist/providers/index.js +4 -0
- package/dist/providers/prepare.d.ts +124 -0
- package/dist/providers/prepare.js +459 -0
- package/dist/providers/runner-detection.d.ts +30 -0
- package/dist/providers/runner-detection.js +207 -0
- package/dist/rules.d.ts +27 -0
- package/dist/rules.js +54 -0
- package/package.json +52 -0
package/dist/help.js
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
// Help registry + renderers. Owns every user-facing help string so the six
|
|
2
|
+
// subcommands share one formatter instead of six inline console.log blocks
|
|
3
|
+
// (spec §C). Pure data + pure functions: no process/exit, no side effects.
|
|
4
|
+
// Composition (interception, exit codes) lives in src/cli.ts main().
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
8
|
+
const CHECK_USAGE = 'checkchange check [--base <ref>] [--json] [--cache] [--auto-coverage] [--crap-threshold <number>] [--coverage-file <path>] [--format github|junit|sarif] [--provider-config <path>] [--allow-external-config] [--verbose]';
|
|
9
|
+
// ponytail: head is literal, not generated from flags — the hand-aligned option
|
|
10
|
+
// columns and the long "(default: ...)" suffixes are not recoverable from the
|
|
11
|
+
// short JSON descriptions in spec §J.
|
|
12
|
+
const CHECK_HEAD = `Usage: ${CHECK_USAGE}
|
|
13
|
+
Options:
|
|
14
|
+
--base <ref> Git base reference to compare against (optional, default: auto-detect)
|
|
15
|
+
--json Output JSON (default: false)
|
|
16
|
+
--cache Enable incremental caching (default: off; also CHECKCHANGE_CACHE=1 env)
|
|
17
|
+
--crap-threshold <number> CRAP threshold for WARN (default: 30)
|
|
18
|
+
--coverage-file <path> Istanbul coverage JSON file path
|
|
19
|
+
--auto-coverage Detect test runner, generate coverage artifact, retry [experimental]
|
|
20
|
+
--format <name> Output format: github, junit, sarif (default: none)
|
|
21
|
+
--provider-config <path> Path to checkchange.providers.json (overrides builtin defaults)
|
|
22
|
+
--allow-external-config Authorize an external provider config path (default: false)
|
|
23
|
+
--verbose Print diagnostic info to stderr`;
|
|
24
|
+
const CHECK = {
|
|
25
|
+
command: 'check',
|
|
26
|
+
description: 'Analyze changed functions for complexity, coverage, and CRAP score',
|
|
27
|
+
usage: CHECK_USAGE,
|
|
28
|
+
head: CHECK_HEAD,
|
|
29
|
+
flags: [
|
|
30
|
+
{ name: '--base', type: 'string', default: 'auto-detect', description: 'Git base reference to compare against' },
|
|
31
|
+
{ name: '--json', type: 'boolean', default: false, description: 'Output JSON' },
|
|
32
|
+
{ name: '--cache', type: 'boolean', default: false, description: 'Enable incremental caching (CHECKCHANGE_CACHE=1)' },
|
|
33
|
+
{ name: '--crap-threshold', type: 'number', default: 30, description: 'CRAP threshold for WARN' },
|
|
34
|
+
{ name: '--coverage-file', type: 'string', default: null, description: 'Istanbul coverage JSON file path' },
|
|
35
|
+
{ name: '--auto-coverage', type: 'boolean', default: false, description: 'Auto-generate coverage artifact [experimental]' },
|
|
36
|
+
{ name: '--format', type: 'string', default: null, description: 'Output format: github, junit, sarif' },
|
|
37
|
+
{ name: '--provider-config', type: 'string', default: null, description: 'Path to checkchange.providers.json' },
|
|
38
|
+
{ name: '--allow-external-config', type: 'boolean', default: false, description: 'Authorize external provider config' },
|
|
39
|
+
{ name: '--verbose', type: 'boolean', default: false, description: 'Print diagnostic info to stderr' },
|
|
40
|
+
],
|
|
41
|
+
examples: [
|
|
42
|
+
'checkchange check --base main --json',
|
|
43
|
+
'checkchange check --crap-threshold 25 --format github',
|
|
44
|
+
'checkchange check --auto-coverage --verbose',
|
|
45
|
+
],
|
|
46
|
+
};
|
|
47
|
+
const DOCTOR = {
|
|
48
|
+
command: 'doctor',
|
|
49
|
+
description: 'Run environment health probes (git, base, providers, coverage)',
|
|
50
|
+
usage: 'checkchange doctor [--json] [--provider-config <path>] [--allow-external-config]',
|
|
51
|
+
head: `Usage: checkchange doctor [--json] [--provider-config <path>] [--allow-external-config]
|
|
52
|
+
|
|
53
|
+
Options:
|
|
54
|
+
--json Output JSON (default: false)
|
|
55
|
+
--provider-config <path> Path to checkchange.providers.json (overrides builtin defaults)
|
|
56
|
+
--allow-external-config Authorize an external provider config path (default: false)
|
|
57
|
+
|
|
58
|
+
Probes (always run):
|
|
59
|
+
gitExecutable git on PATH
|
|
60
|
+
gitRepo current directory is a git repo
|
|
61
|
+
defaultBase origin/main or main auto-detected
|
|
62
|
+
providerAvailability changed file extensions have registered providers
|
|
63
|
+
coverageArtifact Istanbul coverage JSON present and parseable`,
|
|
64
|
+
flags: [
|
|
65
|
+
{ name: '--json', type: 'boolean', default: false, description: 'Output JSON' },
|
|
66
|
+
{ name: '--provider-config', type: 'string', default: null, description: 'Path to checkchange.providers.json' },
|
|
67
|
+
{ name: '--allow-external-config', type: 'boolean', default: false, description: 'Authorize external provider config' },
|
|
68
|
+
],
|
|
69
|
+
examples: [
|
|
70
|
+
'checkchange doctor --json',
|
|
71
|
+
'checkchange doctor --provider-config ./custom.providers.json',
|
|
72
|
+
],
|
|
73
|
+
};
|
|
74
|
+
const EXPLAIN = {
|
|
75
|
+
command: 'explain',
|
|
76
|
+
description: 'Show derivation for a rule result (threshold, providers, fallbacks)',
|
|
77
|
+
usage: 'checkchange explain [options] [--base <ref>] [--crap-threshold <number>] [--coverage-file <path>] [ruleId]',
|
|
78
|
+
head: `Usage: checkchange explain [options] [--base <ref>] [--crap-threshold <number>] [--coverage-file <path>] [ruleId]
|
|
79
|
+
|
|
80
|
+
Options:
|
|
81
|
+
--json Output JSON (default: false)
|
|
82
|
+
--base <ref> Git base reference (default: auto-detect)
|
|
83
|
+
--crap-threshold <number> CRAP threshold for derivation (default: 30)
|
|
84
|
+
--coverage-file <path> Coverage artifact to use in derivation
|
|
85
|
+
|
|
86
|
+
Arguments:
|
|
87
|
+
ruleId Filter to specific rule (default: changed-function-high-crap)
|
|
88
|
+
|
|
89
|
+
Derivation includes:
|
|
90
|
+
threshold, base (raw + resolved), input counts, provider sources (complexity/coverage),
|
|
91
|
+
fallback reason, matching rule results`,
|
|
92
|
+
flags: [
|
|
93
|
+
{ name: '--json', type: 'boolean', default: false, description: 'Output JSON' },
|
|
94
|
+
{ name: '--base', type: 'string', default: 'auto-detect', description: 'Git base reference' },
|
|
95
|
+
{ name: '--crap-threshold', type: 'number', default: 30, description: 'CRAP threshold for derivation' },
|
|
96
|
+
{ name: '--coverage-file', type: 'string', default: null, description: 'Coverage artifact to use in derivation' },
|
|
97
|
+
],
|
|
98
|
+
examples: [
|
|
99
|
+
'checkchange explain --json',
|
|
100
|
+
'checkchange explain --base HEAD~5 --crap-threshold 20',
|
|
101
|
+
'checkchange explain changed-function-high-crap --coverage-file ./cov.json',
|
|
102
|
+
],
|
|
103
|
+
};
|
|
104
|
+
const TRACE = {
|
|
105
|
+
command: 'trace',
|
|
106
|
+
description: 'Run analysis with timing spans (correlation ID + stage durations)',
|
|
107
|
+
usage: 'checkchange trace [options] [--base <ref>] [--crap-threshold <number>] [--coverage-file <path>]',
|
|
108
|
+
head: `Usage: checkchange trace [options] [--base <ref>] [--crap-threshold <number>] [--coverage-file <path>]
|
|
109
|
+
|
|
110
|
+
Options:
|
|
111
|
+
--json Output JSON (default: false)
|
|
112
|
+
--base <ref> Git base reference (default: auto-detect)
|
|
113
|
+
--crap-threshold <number> CRAP threshold (default: 30)
|
|
114
|
+
--coverage-file <path> Coverage artifact path
|
|
115
|
+
|
|
116
|
+
Output (text):
|
|
117
|
+
trace: <correlationId> — N span(s)
|
|
118
|
+
warning: [source] message
|
|
119
|
+
|
|
120
|
+
Output (--json):
|
|
121
|
+
{ command: 'trace', correlationId, spans: [{stage, durationMs, status}], warnings }`,
|
|
122
|
+
flags: [
|
|
123
|
+
{ name: '--json', type: 'boolean', default: false, description: 'Output JSON' },
|
|
124
|
+
{ name: '--base', type: 'string', default: 'auto-detect', description: 'Git base reference' },
|
|
125
|
+
{ name: '--crap-threshold', type: 'number', default: 30, description: 'CRAP threshold' },
|
|
126
|
+
{ name: '--coverage-file', type: 'string', default: null, description: 'Coverage artifact path' },
|
|
127
|
+
],
|
|
128
|
+
examples: [
|
|
129
|
+
'checkchange trace --json',
|
|
130
|
+
'checkchange trace --base main --crap-threshold 25',
|
|
131
|
+
],
|
|
132
|
+
};
|
|
133
|
+
const DELTA = {
|
|
134
|
+
command: 'delta',
|
|
135
|
+
description: 'Compare two EvidenceOutput JSON files (gate transition summary)',
|
|
136
|
+
usage: 'checkchange delta --baseline <path> --current <path> [--json]',
|
|
137
|
+
head: `Usage: checkchange delta --baseline <path> --current <path> [--json]
|
|
138
|
+
|
|
139
|
+
Options:
|
|
140
|
+
--json Output JSON (default: false)
|
|
141
|
+
--baseline <path> Path to baseline EvidenceOutput JSON (required)
|
|
142
|
+
--current <path> Path to current EvidenceOutput JSON (required)
|
|
143
|
+
|
|
144
|
+
Output (text):
|
|
145
|
+
delta: added N, removed M, changed K, unchanged L
|
|
146
|
+
gate: <transition | unchanged>
|
|
147
|
+
|
|
148
|
+
Output (--json):
|
|
149
|
+
DeltaOutput schema (summary + added/removed/changed arrays + gateTransition)`,
|
|
150
|
+
flags: [
|
|
151
|
+
{ name: '--json', type: 'boolean', default: false, description: 'Output JSON' },
|
|
152
|
+
{ name: '--baseline', type: 'string', default: null, description: 'Path to baseline EvidenceOutput JSON (required)' },
|
|
153
|
+
{ name: '--current', type: 'string', default: null, description: 'Path to current EvidenceOutput JSON (required)' },
|
|
154
|
+
],
|
|
155
|
+
examples: [
|
|
156
|
+
'checkchange delta --baseline before.json --current after.json --json',
|
|
157
|
+
'checkchange delta --baseline ./artifacts/base.json --current ./artifacts/head.json',
|
|
158
|
+
],
|
|
159
|
+
};
|
|
160
|
+
const PREPARE_REPO = {
|
|
161
|
+
command: 'prepare-repo',
|
|
162
|
+
description: 'Detect stack, plan installs, approve, execute, verify',
|
|
163
|
+
usage: 'checkchange prepare-repo [--dry-run] [--json] [--yes]',
|
|
164
|
+
head: `Usage: checkchange prepare-repo [--dry-run] [--json] [--yes]
|
|
165
|
+
|
|
166
|
+
Options:
|
|
167
|
+
--dry-run Show install plan only (no execution, no TTY required)
|
|
168
|
+
--json Output JSON (default: false)
|
|
169
|
+
--yes Skip approval prompt (non-interactive execution)
|
|
170
|
+
|
|
171
|
+
Phases:
|
|
172
|
+
1. Detect — language stacks + registered providers
|
|
173
|
+
2. Plan — install actions + config updates per language
|
|
174
|
+
3. Approve — TTY prompt (skipped by --dry-run or --yes)
|
|
175
|
+
4. Install — execute actions; on full success, flush config updates
|
|
176
|
+
5. Verify — re-detect + run analyzers; report ok/unfixable
|
|
177
|
+
|
|
178
|
+
Exit codes:
|
|
179
|
+
0 dry-run plan printed OR full success
|
|
180
|
+
1 partial/unfixable install or verify, approval denied, no-TTY refusal`,
|
|
181
|
+
flags: [
|
|
182
|
+
{ name: '--dry-run', type: 'boolean', default: false, description: 'Show install plan only (no execution, no TTY required)' },
|
|
183
|
+
{ name: '--json', type: 'boolean', default: false, description: 'Output JSON' },
|
|
184
|
+
{ name: '--yes', type: 'boolean', default: false, description: 'Skip approval prompt (non-interactive execution)' },
|
|
185
|
+
],
|
|
186
|
+
examples: [
|
|
187
|
+
'checkchange prepare-repo --dry-run --json',
|
|
188
|
+
'checkchange prepare-repo --yes',
|
|
189
|
+
'checkchange prepare-repo --dry-run',
|
|
190
|
+
],
|
|
191
|
+
};
|
|
192
|
+
export const HELP_REGISTRY = {
|
|
193
|
+
check: CHECK,
|
|
194
|
+
doctor: DOCTOR,
|
|
195
|
+
explain: EXPLAIN,
|
|
196
|
+
trace: TRACE,
|
|
197
|
+
delta: DELTA,
|
|
198
|
+
'prepare-repo': PREPARE_REPO,
|
|
199
|
+
};
|
|
200
|
+
const OVERVIEW_DESCRIPTION = "Run 'checkchange <command> --help' for command-specific help.";
|
|
201
|
+
// ponytail: the Commands block is generated, not hand-maintained — an edited
|
|
202
|
+
// registry description can no longer leave the overview stale. padEnd(14) is the
|
|
203
|
+
// width of the literal it replaces; `--help` output stays byte-identical.
|
|
204
|
+
// The rest of the overview has no Examples block, so it stays literal.
|
|
205
|
+
const OVERVIEW_COMMANDS = Object.values(HELP_REGISTRY)
|
|
206
|
+
.map((entry) => ` ${entry.command.padEnd(14)}${entry.description}`)
|
|
207
|
+
.join('\n');
|
|
208
|
+
const OVERVIEW_TEXT = `Usage: checkchange <command> [options]
|
|
209
|
+
|
|
210
|
+
Commands:
|
|
211
|
+
${OVERVIEW_COMMANDS}
|
|
212
|
+
|
|
213
|
+
Options:
|
|
214
|
+
--help Show this help
|
|
215
|
+
--version Print version and exit
|
|
216
|
+
|
|
217
|
+
Run 'checkchange <command> --help' for command-specific help.
|
|
218
|
+
Tip: 'checkchange <command> --help --json' for machine-readable help.`;
|
|
219
|
+
function jsonTip(command) {
|
|
220
|
+
return `Tip: 'checkchange ${command} --help --json' for machine-readable help.`;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Render help for one subcommand, or the overview when `command` is undefined
|
|
224
|
+
* (bare `checkchange --help`). No trailing newline — the caller prints it.
|
|
225
|
+
*/
|
|
226
|
+
export function formatHelpText(command) {
|
|
227
|
+
if (command === undefined)
|
|
228
|
+
return OVERVIEW_TEXT;
|
|
229
|
+
const entry = HELP_REGISTRY[command];
|
|
230
|
+
return [
|
|
231
|
+
entry.head,
|
|
232
|
+
'',
|
|
233
|
+
'Examples:',
|
|
234
|
+
...entry.examples.map((example) => ` ${example}`),
|
|
235
|
+
'',
|
|
236
|
+
jsonTip(entry.command),
|
|
237
|
+
].join('\n');
|
|
238
|
+
}
|
|
239
|
+
/** Machine-readable help: same shape for every command (spec §J). */
|
|
240
|
+
export function formatHelpJson(command) {
|
|
241
|
+
if (command === undefined) {
|
|
242
|
+
return {
|
|
243
|
+
command: 'checkchange',
|
|
244
|
+
description: OVERVIEW_DESCRIPTION,
|
|
245
|
+
usage: 'checkchange <command> [options]',
|
|
246
|
+
commands: Object.keys(HELP_REGISTRY),
|
|
247
|
+
examples: [],
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
const entry = HELP_REGISTRY[command];
|
|
251
|
+
return {
|
|
252
|
+
command: entry.command,
|
|
253
|
+
description: entry.description,
|
|
254
|
+
usage: entry.usage,
|
|
255
|
+
flags: entry.flags,
|
|
256
|
+
examples: entry.examples,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* `checkchange <version>`, read from this package's package.json at runtime
|
|
261
|
+
* (never hardcoded — src/rules.ts:4 is the reason that matters). moduleDir is
|
|
262
|
+
* src/ or dist/, so the manifest sits one level up; same idiom as
|
|
263
|
+
* src/evidence.ts:74. Never throws: an unreadable manifest yields 'unknown'.
|
|
264
|
+
*/
|
|
265
|
+
export function formatVersion() {
|
|
266
|
+
try {
|
|
267
|
+
const moduleDir = path.dirname(fileURLToPath(import.meta.url));
|
|
268
|
+
const manifest = JSON.parse(fs.readFileSync(path.join(moduleDir, '..', 'package.json'), 'utf8'));
|
|
269
|
+
return `checkchange ${manifest.version ?? 'unknown'}`;
|
|
270
|
+
}
|
|
271
|
+
catch {
|
|
272
|
+
return 'checkchange unknown';
|
|
273
|
+
}
|
|
274
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { buildEvidenceOutput } from './evidence.js';
|
|
2
|
+
export { getChangedIntervals, resolveBaseRef, validateGitRepo } from './git.js';
|
|
3
|
+
export { readCoverage } from './coverage.js';
|
|
4
|
+
export { collectComplexity } from './complexity.js';
|
|
5
|
+
export type { ChangedFunction } from './evidence.js';
|
|
6
|
+
export type { CoverageResult } from './coverage.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/** Test runner definition for a provider language. */
|
|
2
|
+
export interface TestRunner {
|
|
3
|
+
name: string;
|
|
4
|
+
configFiles: string[];
|
|
5
|
+
binaryProbes: string[];
|
|
6
|
+
command: string[];
|
|
7
|
+
artifact: string;
|
|
8
|
+
/** Optional install spec consulted by prepare-repo (Task 1). */
|
|
9
|
+
install?: {
|
|
10
|
+
packages: string[];
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export interface ProviderEntry {
|
|
14
|
+
language: string;
|
|
15
|
+
extensions: string[];
|
|
16
|
+
complexityCmd?: string;
|
|
17
|
+
coverageFiles?: string[];
|
|
18
|
+
coverageCmd?: string;
|
|
19
|
+
testRunners?: TestRunner[];
|
|
20
|
+
}
|
|
21
|
+
export interface ProviderConfig {
|
|
22
|
+
version: number;
|
|
23
|
+
providers: ProviderEntry[];
|
|
24
|
+
allowlist?: string[];
|
|
25
|
+
}
|
|
26
|
+
export interface ResolvedProvider {
|
|
27
|
+
language: string;
|
|
28
|
+
extensions: string[];
|
|
29
|
+
complexityCmd: string | null;
|
|
30
|
+
coverageFiles: string[];
|
|
31
|
+
coverageCmd: string | null;
|
|
32
|
+
testRunners: TestRunner[] | null;
|
|
33
|
+
/** Whether this provider came from the builtin defaults or a loaded config file. */
|
|
34
|
+
source: 'builtin' | string;
|
|
35
|
+
}
|
|
36
|
+
export declare function builtinConfig(): ProviderConfig;
|
|
37
|
+
export declare function loadProviderConfig(cwd: string, explicitPath?: string, allowExternalConfig?: boolean): {
|
|
38
|
+
config: ProviderConfig;
|
|
39
|
+
source: 'explicit' | 'repo-root' | 'builtin';
|
|
40
|
+
};
|
|
41
|
+
export declare function deriveRegistry(config: ProviderConfig, source?: 'builtin' | string): Map<string, ResolvedProvider>;
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Config-driven provider registry.
|
|
3
|
+
* Precedence: explicit path > repo root ./checkchange.providers.json > builtin defaults.
|
|
4
|
+
*/
|
|
5
|
+
import * as fs from 'node:fs';
|
|
6
|
+
import * as path from 'node:path';
|
|
7
|
+
// ---- Builtin defaults (current hardcoded values as config) ----
|
|
8
|
+
const PY_EXTENSIONS = ['.py'];
|
|
9
|
+
const VITEST_RUNNER = {
|
|
10
|
+
name: 'vitest',
|
|
11
|
+
configFiles: ['vitest.config.*'],
|
|
12
|
+
binaryProbes: [],
|
|
13
|
+
command: ['npx', 'vitest', 'run', '--coverage'],
|
|
14
|
+
artifact: 'coverage/coverage-final.json',
|
|
15
|
+
};
|
|
16
|
+
const JEST_RUNNER = {
|
|
17
|
+
name: 'jest',
|
|
18
|
+
configFiles: ['jest.config.*'],
|
|
19
|
+
binaryProbes: [],
|
|
20
|
+
command: ['npx', 'jest', '--coverage'],
|
|
21
|
+
artifact: 'coverage/coverage-final.json',
|
|
22
|
+
};
|
|
23
|
+
const PYTEST_RUNNER = {
|
|
24
|
+
name: 'pytest',
|
|
25
|
+
configFiles: ['pyproject.toml', 'pytest.ini', 'setup.cfg', 'requirements.txt'],
|
|
26
|
+
binaryProbes: ['.venv/bin/pytest', 'VIRTUAL_ENV', 'python3 -m pytest'],
|
|
27
|
+
command: ['python3', '-m', 'pytest', '--cov', '--cov-report=xml'],
|
|
28
|
+
artifact: 'coverage.xml',
|
|
29
|
+
};
|
|
30
|
+
export function builtinConfig() {
|
|
31
|
+
return {
|
|
32
|
+
version: 1,
|
|
33
|
+
providers: [
|
|
34
|
+
{
|
|
35
|
+
language: 'javascript',
|
|
36
|
+
extensions: ['.js', '.jsx', '.mjs', '.cjs'],
|
|
37
|
+
coverageFiles: ['coverage/coverage-final.json', 'coverage/lcov.info'],
|
|
38
|
+
testRunners: [VITEST_RUNNER, JEST_RUNNER],
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
language: 'typescript',
|
|
42
|
+
extensions: ['.ts', '.tsx'],
|
|
43
|
+
coverageFiles: ['coverage/coverage-final.json', 'coverage/lcov.info'],
|
|
44
|
+
testRunners: [VITEST_RUNNER, JEST_RUNNER],
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
language: 'python',
|
|
48
|
+
extensions: PY_EXTENSIONS,
|
|
49
|
+
coverageFiles: ['.coverage', 'coverage.xml', 'coverage.json', 'coverage/coverage-final.json'],
|
|
50
|
+
coverageCmd: 'coverage json -o {out}',
|
|
51
|
+
testRunners: [PYTEST_RUNNER],
|
|
52
|
+
},
|
|
53
|
+
],
|
|
54
|
+
allowlist: ['./checkchange.providers.json', './.checkchange/providers.json'],
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
// ---- Config loading ----
|
|
58
|
+
function isRecord(v) {
|
|
59
|
+
return typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
60
|
+
}
|
|
61
|
+
// ponytail: extracted from validateConfig to reduce cyclomatic complexity / CRAP.
|
|
62
|
+
function validateTestRunner(runner, i, j) {
|
|
63
|
+
if (!isRecord(runner))
|
|
64
|
+
throw new Error(`config: providers[${i}].testRunners[${j}] not an object`);
|
|
65
|
+
if (typeof runner.name !== 'string')
|
|
66
|
+
throw new Error(`config: providers[${i}].testRunners[${j}].name missing`);
|
|
67
|
+
if (!Array.isArray(runner.configFiles))
|
|
68
|
+
throw new Error(`config: providers[${i}].testRunners[${j}].configFiles missing`);
|
|
69
|
+
if (!Array.isArray(runner.binaryProbes))
|
|
70
|
+
throw new Error(`config: providers[${i}].testRunners[${j}].binaryProbes missing`);
|
|
71
|
+
if (!Array.isArray(runner.command))
|
|
72
|
+
throw new Error(`config: providers[${i}].testRunners[${j}].command missing`);
|
|
73
|
+
if (typeof runner.artifact !== 'string')
|
|
74
|
+
throw new Error(`config: providers[${i}].testRunners[${j}].artifact missing`);
|
|
75
|
+
if (runner.install !== undefined)
|
|
76
|
+
validateInstallField(runner.install, i, j);
|
|
77
|
+
}
|
|
78
|
+
// ponytail: extracted install-field check from validateTestRunner (trivially separable).
|
|
79
|
+
function validateInstallField(install, i, j) {
|
|
80
|
+
if (!isRecord(install))
|
|
81
|
+
throw new Error(`config: providers[${i}].testRunners[${j}].install not an object`);
|
|
82
|
+
if (!Array.isArray(install.packages))
|
|
83
|
+
throw new Error(`config: providers[${i}].testRunners[${j}].install.packages must be an array`);
|
|
84
|
+
}
|
|
85
|
+
function validateConfig(raw) {
|
|
86
|
+
if (!isRecord(raw))
|
|
87
|
+
throw new Error('config: not an object');
|
|
88
|
+
if (raw.version !== 1)
|
|
89
|
+
throw new Error(`config: unsupported version ${String(raw.version)}`);
|
|
90
|
+
if (!Array.isArray(raw.providers))
|
|
91
|
+
throw new Error('config: missing providers array');
|
|
92
|
+
for (const [i, entry] of raw.providers.entries()) {
|
|
93
|
+
if (!isRecord(entry))
|
|
94
|
+
throw new Error(`config: providers[${i}] not an object`);
|
|
95
|
+
if (typeof entry.language !== 'string')
|
|
96
|
+
throw new Error(`config: providers[${i}].language missing`);
|
|
97
|
+
if (!Array.isArray(entry.extensions))
|
|
98
|
+
throw new Error(`config: providers[${i}].extensions missing`);
|
|
99
|
+
if (entry.testRunners !== undefined) {
|
|
100
|
+
if (!Array.isArray(entry.testRunners))
|
|
101
|
+
throw new Error(`config: providers[${i}].testRunners not an array`);
|
|
102
|
+
for (const [j, runner] of entry.testRunners.entries()) {
|
|
103
|
+
validateTestRunner(runner, i, j);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return raw;
|
|
108
|
+
}
|
|
109
|
+
const REPO_ROOT_NAMES = [
|
|
110
|
+
'checkchange.providers.json',
|
|
111
|
+
path.join('.checkchange', 'providers.json'),
|
|
112
|
+
];
|
|
113
|
+
function isRepoRootConfig(realPath, realCwd) {
|
|
114
|
+
return REPO_ROOT_NAMES.some((name) => realPath === path.join(realCwd, name));
|
|
115
|
+
}
|
|
116
|
+
export function loadProviderConfig(cwd, explicitPath, allowExternalConfig = false) {
|
|
117
|
+
if (explicitPath !== undefined) {
|
|
118
|
+
const resolved = path.isAbsolute(explicitPath)
|
|
119
|
+
? explicitPath
|
|
120
|
+
: path.resolve(cwd, explicitPath);
|
|
121
|
+
const real = fs.realpathSync(resolved);
|
|
122
|
+
if (!allowExternalConfig && !isRepoRootConfig(real, fs.realpathSync(cwd))) {
|
|
123
|
+
throw new Error('config: external config requires allowExternalConfig');
|
|
124
|
+
}
|
|
125
|
+
return { config: validateConfig(JSON.parse(fs.readFileSync(real, 'utf8'))), source: 'explicit' };
|
|
126
|
+
}
|
|
127
|
+
for (const name of REPO_ROOT_NAMES) {
|
|
128
|
+
const candidate = path.resolve(cwd, name);
|
|
129
|
+
let realCandidate;
|
|
130
|
+
try {
|
|
131
|
+
realCandidate = fs.realpathSync(candidate);
|
|
132
|
+
}
|
|
133
|
+
catch (err) {
|
|
134
|
+
if (typeof err === 'object' && err !== null && 'code' in err && err.code === 'ENOENT')
|
|
135
|
+
continue; // file missing — try next
|
|
136
|
+
throw err;
|
|
137
|
+
}
|
|
138
|
+
if (!allowExternalConfig && !isRepoRootConfig(realCandidate, fs.realpathSync(cwd))) {
|
|
139
|
+
throw new Error('config: external config requires allowExternalConfig');
|
|
140
|
+
}
|
|
141
|
+
let raw;
|
|
142
|
+
try {
|
|
143
|
+
raw = fs.readFileSync(realCandidate, 'utf8');
|
|
144
|
+
}
|
|
145
|
+
catch (err) {
|
|
146
|
+
if (typeof err === 'object' && err !== null && 'code' in err && err.code === 'ENOENT')
|
|
147
|
+
continue; // file disappeared — try next
|
|
148
|
+
throw err;
|
|
149
|
+
}
|
|
150
|
+
return { config: validateConfig(JSON.parse(raw)), source: 'repo-root' }; // throws on bad JSON or bad schema
|
|
151
|
+
}
|
|
152
|
+
return { config: builtinConfig(), source: 'builtin' };
|
|
153
|
+
}
|
|
154
|
+
// ---- Registry derivation ----
|
|
155
|
+
export function deriveRegistry(config, source = 'builtin') {
|
|
156
|
+
const map = new Map();
|
|
157
|
+
for (const entry of config.providers) {
|
|
158
|
+
const resolved = {
|
|
159
|
+
language: entry.language,
|
|
160
|
+
extensions: entry.extensions,
|
|
161
|
+
complexityCmd: entry.complexityCmd ?? null,
|
|
162
|
+
coverageFiles: entry.coverageFiles ?? [],
|
|
163
|
+
coverageCmd: entry.coverageCmd ?? null,
|
|
164
|
+
testRunners: entry.testRunners ?? null,
|
|
165
|
+
source,
|
|
166
|
+
};
|
|
167
|
+
for (const ext of entry.extensions) {
|
|
168
|
+
map.set(ext, resolved); // later entry overrides
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return map;
|
|
172
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { ResolvedProvider } from './config.js';
|
|
2
|
+
import type { ComplexityProvider } from '../complexity-providers.js';
|
|
3
|
+
export declare function createGenericCommandProvider(entry: ResolvedProvider, cwd: string, allowedArgv?: readonly (readonly string[])[]): ComplexityProvider | null;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic command provider — executes an allowlisted argv command to collect complexity.
|
|
3
|
+
* Tokens: {cwd} {out} {ext}. {files} is NOT supported — throws if present.
|
|
4
|
+
* Env: PATH only, no ambient authority. 30s timeout.
|
|
5
|
+
*/
|
|
6
|
+
import { execFile } from 'node:child_process';
|
|
7
|
+
const DEFAULT_TIMEOUT_MS = 30_000;
|
|
8
|
+
function buildArgv(command, cwd, out, ext) {
|
|
9
|
+
const argv = command.trim().split(/\s+/);
|
|
10
|
+
if (argv.length === 1 && argv[0] === '') {
|
|
11
|
+
throw new Error('genericCommand: complexityCmd is empty');
|
|
12
|
+
}
|
|
13
|
+
return argv.map((token) => token
|
|
14
|
+
.replaceAll('{cwd}', cwd)
|
|
15
|
+
.replaceAll('{out}', out)
|
|
16
|
+
.replaceAll('{ext}', ext));
|
|
17
|
+
}
|
|
18
|
+
function sameArgv(left, right) {
|
|
19
|
+
return left.length === right.length && left.every((value, index) => value === right[index]);
|
|
20
|
+
}
|
|
21
|
+
export function createGenericCommandProvider(entry, cwd, allowedArgv = []) {
|
|
22
|
+
if (!entry.complexityCmd)
|
|
23
|
+
return null;
|
|
24
|
+
// Throw early if template contains unsupported {files} token
|
|
25
|
+
if (entry.complexityCmd.includes('{files}')) {
|
|
26
|
+
throw new Error('genericCommand: {files} token is not supported in complexityCmd template; ' +
|
|
27
|
+
'use {cwd}, {out}, or {ext} instead');
|
|
28
|
+
}
|
|
29
|
+
const out = 'complexity-out.json';
|
|
30
|
+
const ext = entry.extensions[0] ?? '';
|
|
31
|
+
const argv = buildArgv(entry.complexityCmd, cwd, out, ext);
|
|
32
|
+
if (!allowedArgv.some((allowed) => sameArgv(argv, allowed))) {
|
|
33
|
+
throw new Error('genericCommand: command is not allowlisted');
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
extensions: [...entry.extensions],
|
|
37
|
+
async collectComplexity(dir) {
|
|
38
|
+
const timeout = Number(process.env.CHECKCHANGE_PROVIDER_TIMEOUT_MS) || DEFAULT_TIMEOUT_MS;
|
|
39
|
+
const stdout = await execPromise(argv, {
|
|
40
|
+
cwd: dir,
|
|
41
|
+
timeout,
|
|
42
|
+
env: { PATH: process.env.PATH ?? '' },
|
|
43
|
+
});
|
|
44
|
+
const parsed = JSON.parse(stdout);
|
|
45
|
+
if (!Array.isArray(parsed)) {
|
|
46
|
+
throw new Error('genericCommand: stdout is not a JSON array');
|
|
47
|
+
}
|
|
48
|
+
return parsed.map((item) => {
|
|
49
|
+
const info = item;
|
|
50
|
+
if (typeof item !== 'object' || item === null ||
|
|
51
|
+
typeof info.file !== 'string' ||
|
|
52
|
+
typeof info.method !== 'string' ||
|
|
53
|
+
typeof info.lineStart !== 'number' ||
|
|
54
|
+
typeof info.lineEnd !== 'number' ||
|
|
55
|
+
typeof info.cc !== 'number') {
|
|
56
|
+
throw new Error('genericCommand: invalid ComplexityInfo shape');
|
|
57
|
+
}
|
|
58
|
+
return info;
|
|
59
|
+
});
|
|
60
|
+
},
|
|
61
|
+
describe() {
|
|
62
|
+
return `genericCommand(${entry.complexityCmd})`;
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function execPromise(argv, opts) {
|
|
67
|
+
return new Promise((resolve, reject) => {
|
|
68
|
+
const [command, ...args] = argv;
|
|
69
|
+
if (!command) {
|
|
70
|
+
reject(new Error('genericCommand: executable is empty'));
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
execFile(command, args, { cwd: opts.cwd, timeout: opts.timeout, env: opts.env }, (error, stdout, stderr) => {
|
|
74
|
+
if (error) {
|
|
75
|
+
if ('code' in error && error.code !== undefined && error.code !== 0) {
|
|
76
|
+
reject(new Error(`genericCommand: exited ${error.code}: ${stderr || stdout || error.message}`));
|
|
77
|
+
}
|
|
78
|
+
else if (error.killed) {
|
|
79
|
+
reject(new Error('genericCommand: timed out'));
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
reject(new Error(`genericCommand: ${error.message}`));
|
|
83
|
+
}
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
resolve(stdout);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type { ProviderConfig, ProviderEntry, ResolvedProvider, TestRunner } from './config.js';
|
|
2
|
+
export { loadProviderConfig, deriveRegistry, builtinConfig } from './config.js';
|
|
3
|
+
export { createGenericCommandProvider } from './genericCommand.js';
|
|
4
|
+
export { resolveRunner } from './runner-detection.js';
|
|
5
|
+
export type { ResolvedRunner } from './runner-detection.js';
|
|
6
|
+
export type { PrepareOptions, DetectedStack, InstallActionKind, InstallAction, InstallPlan, PromptApprovalResult, PrepareReport, InstallActionResult, ExecuteResult, VerifyAnalyzer, VerifyReport, } from './prepare.js';
|
|
7
|
+
export { detectStack, createInstallPlan, promptApproval, buildPreamble, executeInstallPlan, flushConfigUpdates, verifyInstall, } from './prepare.js';
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { loadProviderConfig, deriveRegistry, builtinConfig } from './config.js';
|
|
2
|
+
export { createGenericCommandProvider } from './genericCommand.js';
|
|
3
|
+
export { resolveRunner } from './runner-detection.js';
|
|
4
|
+
export { detectStack, createInstallPlan, promptApproval, buildPreamble, executeInstallPlan, flushConfigUpdates, verifyInstall, } from './prepare.js';
|