ouch4ts 0.0.0-stage → 0.1.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 +45 -2
- package/dist/cache.d.ts +33 -0
- package/dist/cache.js +29 -0
- package/dist/changes.d.ts +6 -0
- package/dist/changes.js +63 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +3 -0
- package/dist/cognitive.d.ts +9 -0
- package/dist/cognitive.js +192 -0
- package/dist/command.d.ts +4 -0
- package/dist/command.js +217 -0
- package/dist/comparison.d.ts +92 -0
- package/dist/comparison.js +243 -0
- package/dist/configuration.d.ts +9 -0
- package/dist/configuration.js +56 -0
- package/dist/cycles.d.ts +5 -0
- package/dist/cycles.js +73 -0
- package/dist/discovery.d.ts +1 -0
- package/dist/discovery.js +27 -0
- package/dist/files.d.ts +9 -0
- package/dist/files.js +53 -0
- package/dist/format.d.ts +5 -0
- package/dist/format.js +28 -0
- package/dist/git.d.ts +1 -0
- package/dist/git.js +13 -0
- package/dist/index.d.ts +60 -0
- package/dist/index.js +207 -0
- package/dist/lcov.d.ts +1 -0
- package/dist/lcov.js +49 -0
- package/dist/manifest.d.ts +3 -0
- package/dist/manifest.js +19 -0
- package/dist/package-manager.d.ts +2 -0
- package/dist/package-manager.js +121 -0
- package/dist/process.d.ts +1 -0
- package/dist/process.js +9 -0
- package/dist/project.d.ts +39 -0
- package/dist/project.js +54 -0
- package/dist/recursion.d.ts +3 -0
- package/dist/recursion.js +340 -0
- package/dist/report.d.ts +30 -0
- package/dist/report.js +57 -0
- package/dist/syntax.d.ts +21 -0
- package/dist/syntax.js +93 -0
- package/package.json +49 -4
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { ProjectOptions } from "./project.js";
|
|
2
|
+
type ComparisonOptions = ProjectOptions & {
|
|
3
|
+
setupCommand: string | undefined;
|
|
4
|
+
refresh: boolean;
|
|
5
|
+
outputDir: string;
|
|
6
|
+
};
|
|
7
|
+
export declare function compareProject(reference: string, options: ComparisonOptions): Promise<{
|
|
8
|
+
base: string;
|
|
9
|
+
head: string;
|
|
10
|
+
entries: {
|
|
11
|
+
name: string;
|
|
12
|
+
file: string;
|
|
13
|
+
status: string;
|
|
14
|
+
base: {
|
|
15
|
+
coverage: null;
|
|
16
|
+
crap: null;
|
|
17
|
+
ouch: null;
|
|
18
|
+
name: string;
|
|
19
|
+
file: string;
|
|
20
|
+
line: number;
|
|
21
|
+
endLine: number;
|
|
22
|
+
complexity: number;
|
|
23
|
+
cognitive: number;
|
|
24
|
+
} | {
|
|
25
|
+
coverage: number;
|
|
26
|
+
crap: number;
|
|
27
|
+
ouch: number;
|
|
28
|
+
name: string;
|
|
29
|
+
file: string;
|
|
30
|
+
line: number;
|
|
31
|
+
endLine: number;
|
|
32
|
+
complexity: number;
|
|
33
|
+
cognitive: number;
|
|
34
|
+
} | null;
|
|
35
|
+
head: {
|
|
36
|
+
coverage: null;
|
|
37
|
+
crap: null;
|
|
38
|
+
ouch: null;
|
|
39
|
+
name: string;
|
|
40
|
+
file: string;
|
|
41
|
+
line: number;
|
|
42
|
+
endLine: number;
|
|
43
|
+
complexity: number;
|
|
44
|
+
cognitive: number;
|
|
45
|
+
} | {
|
|
46
|
+
coverage: number;
|
|
47
|
+
crap: number;
|
|
48
|
+
ouch: number;
|
|
49
|
+
name: string;
|
|
50
|
+
file: string;
|
|
51
|
+
line: number;
|
|
52
|
+
endLine: number;
|
|
53
|
+
complexity: number;
|
|
54
|
+
cognitive: number;
|
|
55
|
+
} | null;
|
|
56
|
+
delta: {
|
|
57
|
+
complexity: null;
|
|
58
|
+
cognitive: null;
|
|
59
|
+
coverage: null;
|
|
60
|
+
crap: null;
|
|
61
|
+
ouch: null;
|
|
62
|
+
} | {
|
|
63
|
+
complexity: number;
|
|
64
|
+
cognitive: number;
|
|
65
|
+
coverage: number | null;
|
|
66
|
+
crap: number | null;
|
|
67
|
+
ouch: number | null;
|
|
68
|
+
};
|
|
69
|
+
}[];
|
|
70
|
+
files: {
|
|
71
|
+
file: string;
|
|
72
|
+
status: string;
|
|
73
|
+
base: {
|
|
74
|
+
complexity: number;
|
|
75
|
+
cognitive: number;
|
|
76
|
+
functions: number;
|
|
77
|
+
file: string;
|
|
78
|
+
} | null;
|
|
79
|
+
head: {
|
|
80
|
+
complexity: number;
|
|
81
|
+
cognitive: number;
|
|
82
|
+
functions: number;
|
|
83
|
+
file: string;
|
|
84
|
+
} | null;
|
|
85
|
+
delta: {
|
|
86
|
+
complexity: number | null;
|
|
87
|
+
cognitive: number | null;
|
|
88
|
+
functions: number | null;
|
|
89
|
+
};
|
|
90
|
+
}[];
|
|
91
|
+
}>;
|
|
92
|
+
export {};
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { mkdtemp, readdir, readFile, realpath, rm } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { extname, join, relative, resolve, sep } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import ts from "typescript";
|
|
7
|
+
import { readCache, writeCache } from "./cache.js";
|
|
8
|
+
import { canonicalRepositoryPath } from "./files.js";
|
|
9
|
+
import { git } from "./git.js";
|
|
10
|
+
import { analyzeProject, warnUnknownCoverage } from "./project.js";
|
|
11
|
+
import { runCommand } from "./process.js";
|
|
12
|
+
import { defaultSetupCommand } from "./package-manager.js";
|
|
13
|
+
export async function compareProject(reference, options) {
|
|
14
|
+
const root = git(["rev-parse", "--show-toplevel"], options.cwd).trimEnd();
|
|
15
|
+
const head = git(["rev-parse", "--verify", "HEAD^{commit}"], options.cwd).trim();
|
|
16
|
+
const target = git(["rev-parse", "--verify", "--end-of-options", `${reference}^{commit}`], options.cwd).trim();
|
|
17
|
+
const base = git(["merge-base", target, head], options.cwd).trim();
|
|
18
|
+
const paths = await comparisonPaths(options, root);
|
|
19
|
+
const normalized = { ...options, ...paths, exclude: [...new Set(options.exclude)].sort() };
|
|
20
|
+
const settingsKey = await settingsFingerprint(normalized, root);
|
|
21
|
+
const snapshotOptions = { ...normalized, root, settingsKey };
|
|
22
|
+
const before = await snapshot(base, snapshotOptions);
|
|
23
|
+
const after = base === head ? before : await snapshot(head, snapshotOptions);
|
|
24
|
+
warnUnknownCoverage(before.entries, " at the merge base");
|
|
25
|
+
if (base !== head)
|
|
26
|
+
warnUnknownCoverage(after.entries, " at HEAD");
|
|
27
|
+
const renames = renamedFiles(base, head, root, options.cwd);
|
|
28
|
+
return {
|
|
29
|
+
base, head, entries: compareEntries(before.entries, after.entries, renames),
|
|
30
|
+
files: compareFiles(before.files, after.files, renames),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
async function comparisonPaths(options, root) {
|
|
34
|
+
async function projectPath(path) {
|
|
35
|
+
const absolute = await canonicalRepositoryPath(resolve(options.cwd, path), root);
|
|
36
|
+
return relative(options.cwd, absolute) || ".";
|
|
37
|
+
}
|
|
38
|
+
await projectPath(".");
|
|
39
|
+
return {
|
|
40
|
+
sourceRoots: [...new Set(await Promise.all(options.sourceRoots.map(projectPath)))].sort(),
|
|
41
|
+
lcov: await projectPath(options.lcov),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
async function settingsFingerprint(options, root) {
|
|
45
|
+
const analyzer = createHash("sha256");
|
|
46
|
+
const directory = fileURLToPath(new URL("./", import.meta.url));
|
|
47
|
+
const extension = extname(import.meta.url);
|
|
48
|
+
const files = (await readdir(directory, { recursive: true, withFileTypes: true }))
|
|
49
|
+
.filter(file => file.isFile() && extname(file.name) === extension)
|
|
50
|
+
.map(file => relative(directory, join(file.parentPath, file.name))).sort();
|
|
51
|
+
for (const file of files) {
|
|
52
|
+
analyzer.update(JSON.stringify([file, await readFile(join(directory, file), "utf8")]));
|
|
53
|
+
}
|
|
54
|
+
return createHash("sha256").update(JSON.stringify({
|
|
55
|
+
version: 3, analyzer: analyzer.digest("hex"), typescript: ts.version,
|
|
56
|
+
node: process.version, platform: process.platform, arch: process.arch,
|
|
57
|
+
project: relative(root, options.cwd), sourceRoots: options.sourceRoots, exclude: options.exclude, lcov: options.lcov,
|
|
58
|
+
setupCommand: options.setupCommand, coverageCommand: options.coverageCommand,
|
|
59
|
+
})).digest("hex");
|
|
60
|
+
}
|
|
61
|
+
async function snapshot(commit, options) {
|
|
62
|
+
const cachePath = join(options.outputDir, "cache", `${commit}-${options.settingsKey}.json`);
|
|
63
|
+
const cached = await reusableSnapshot(cachePath, commit, options);
|
|
64
|
+
if (cached)
|
|
65
|
+
return cached;
|
|
66
|
+
const directory = await mkdtemp(join(tmpdir(), "ouch4ts-compare-"));
|
|
67
|
+
const checkout = join(directory, "checkout");
|
|
68
|
+
let added = false;
|
|
69
|
+
try {
|
|
70
|
+
git(["-c", "core.hooksPath=/dev/null", "worktree", "add", "--detach", checkout, commit], options.root);
|
|
71
|
+
added = true;
|
|
72
|
+
const repositoryRoot = await realpath(checkout);
|
|
73
|
+
const cwd = await realpath(resolve(checkout, relative(options.root, options.cwd)));
|
|
74
|
+
await comparisonPaths({ ...options, cwd }, repositoryRoot);
|
|
75
|
+
console.error(`Analyzing ${commit}`);
|
|
76
|
+
runCommand(options.setupCommand ?? await defaultSetupCommand(cwd), cwd, "Setup");
|
|
77
|
+
const report = await analyzeProject({ ...options, cwd, repositoryRoot, useExistingCoverage: false });
|
|
78
|
+
await writeCache(cachePath, commit, options.settingsKey, report);
|
|
79
|
+
return report;
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
if (added)
|
|
83
|
+
git(["worktree", "remove", "--force", checkout], options.root);
|
|
84
|
+
await rm(directory, { recursive: true, force: true });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
async function reusableSnapshot(path, commit, options) {
|
|
88
|
+
if (options.refresh)
|
|
89
|
+
return null;
|
|
90
|
+
const cached = await readCache(path, commit, options.settingsKey);
|
|
91
|
+
if (cached)
|
|
92
|
+
console.error(`Using cached report for ${commit}`);
|
|
93
|
+
return cached;
|
|
94
|
+
}
|
|
95
|
+
function renamedFiles(base, head, root, cwd) {
|
|
96
|
+
const renames = new Map();
|
|
97
|
+
const paths = git(["diff", "--no-ext-diff", "--no-textconv", "--no-relative", "--name-status", "-z",
|
|
98
|
+
"--find-renames", base, head, "--"], cwd).split("\0").slice(0, -1);
|
|
99
|
+
for (let index = 0; index < paths.length;) {
|
|
100
|
+
const status = paths[index++];
|
|
101
|
+
const from = paths[index++];
|
|
102
|
+
const to = hasDestinationPath(status) ? paths[index++] : undefined;
|
|
103
|
+
recordRename(renames, status, from, to, root, cwd);
|
|
104
|
+
}
|
|
105
|
+
return renames;
|
|
106
|
+
}
|
|
107
|
+
function hasDestinationPath(status) {
|
|
108
|
+
return status.startsWith("R") || status.startsWith("C");
|
|
109
|
+
}
|
|
110
|
+
function recordRename(renames, status, from, to, root, cwd) {
|
|
111
|
+
if (status.startsWith("R") && to !== undefined) {
|
|
112
|
+
renames.set(relative(cwd, resolve(root, from)).split(sep).join("/"), relative(cwd, resolve(root, to)).split(sep).join("/"));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function compareEntries(before, after, renames) {
|
|
116
|
+
const groups = new Map();
|
|
117
|
+
function add(entry, side) {
|
|
118
|
+
const file = side === "base" ? renames.get(entry.file) ?? entry.file : entry.file;
|
|
119
|
+
const identity = `${file}\0${entry.name.replace(/<anonymous@\d+:\d+>/g, "<anonymous>")}`;
|
|
120
|
+
let group = groups.get(identity);
|
|
121
|
+
if (!group) {
|
|
122
|
+
group = { base: [], head: [] };
|
|
123
|
+
groups.set(identity, group);
|
|
124
|
+
}
|
|
125
|
+
group[side].push(entry);
|
|
126
|
+
}
|
|
127
|
+
before.forEach(entry => add(entry, "base"));
|
|
128
|
+
after.forEach(entry => add(entry, "head"));
|
|
129
|
+
return [...groups.values()].flatMap(compareGroup).filter(entry => entry !== null).sort(compareChanges);
|
|
130
|
+
}
|
|
131
|
+
function compareGroup(group) {
|
|
132
|
+
if (isUniqueGroup(group))
|
|
133
|
+
return [comparisonEntry(group.base[0], group.head[0])];
|
|
134
|
+
if (unchangedGroup(group))
|
|
135
|
+
return [];
|
|
136
|
+
return [...group.base.map(entry => comparisonEntry(entry, null)), ...group.head.map(entry => comparisonEntry(null, entry))];
|
|
137
|
+
}
|
|
138
|
+
function isUniqueGroup(group) {
|
|
139
|
+
return group.base.length === 1 && group.head.length === 1;
|
|
140
|
+
}
|
|
141
|
+
function unchangedGroup(group) {
|
|
142
|
+
return group.base[0]?.file === group.head[0]?.file && groupScores(group.base) === groupScores(group.head);
|
|
143
|
+
}
|
|
144
|
+
function groupScores(entries) {
|
|
145
|
+
const scores = entries.map(entry => {
|
|
146
|
+
const values = scoreFields.map(field => entry[field]);
|
|
147
|
+
return JSON.stringify(values);
|
|
148
|
+
});
|
|
149
|
+
return scores.sort().join("\0");
|
|
150
|
+
}
|
|
151
|
+
const scoreFields = ["complexity", "cognitive", "coverage", "crap", "ouch"];
|
|
152
|
+
function sameScores(base, head) {
|
|
153
|
+
return sameFieldValues(base, head, scoreFields);
|
|
154
|
+
}
|
|
155
|
+
function comparisonEntry(base, head) {
|
|
156
|
+
const entry = head ?? base;
|
|
157
|
+
if (!entry)
|
|
158
|
+
return null;
|
|
159
|
+
if (unchangedEntry(base, head, scoreFields))
|
|
160
|
+
return null;
|
|
161
|
+
return {
|
|
162
|
+
name: entry.name, file: entry.file, status: comparisonStatus(base, head, sameScores(base, head)), base, head,
|
|
163
|
+
delta: metricDeltas(base, head),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
function unchangedEntry(base, head, fields) {
|
|
167
|
+
return sameFieldValues(base, head, fields) && base?.file === head?.file;
|
|
168
|
+
}
|
|
169
|
+
function comparisonStatus(base, head, scoresUnchanged) {
|
|
170
|
+
return unmatchedEntryStatus(base, head) ?? (scoresUnchanged ? "renamed" : "changed");
|
|
171
|
+
}
|
|
172
|
+
function unmatchedEntryStatus(base, head) {
|
|
173
|
+
if (base === null)
|
|
174
|
+
return "added";
|
|
175
|
+
if (head === null)
|
|
176
|
+
return "removed";
|
|
177
|
+
}
|
|
178
|
+
function metricDeltas(base, head) {
|
|
179
|
+
if (base === null || head === null)
|
|
180
|
+
return { complexity: null, cognitive: null, coverage: null, crap: null, ouch: null };
|
|
181
|
+
return {
|
|
182
|
+
complexity: head.complexity - base.complexity,
|
|
183
|
+
cognitive: head.cognitive - base.cognitive,
|
|
184
|
+
coverage: scoreDelta(base.coverage, head.coverage),
|
|
185
|
+
crap: scoreDelta(base.crap, head.crap),
|
|
186
|
+
ouch: scoreDelta(base.ouch, head.ouch),
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
function scoreDelta(base, head) {
|
|
190
|
+
return base === null || head === null ? null : head - base;
|
|
191
|
+
}
|
|
192
|
+
function compareChanges(a, b) {
|
|
193
|
+
return compareScoreChanges(a, b) || a.file.localeCompare(b.file) || a.name.localeCompare(b.name);
|
|
194
|
+
}
|
|
195
|
+
function compareScoreChanges(a, b) {
|
|
196
|
+
return (b.delta.ouch ?? 0) - (a.delta.ouch ?? 0) || currentOrPreviousScore(b) - currentOrPreviousScore(a);
|
|
197
|
+
}
|
|
198
|
+
function currentOrPreviousScore(entry) {
|
|
199
|
+
return entry.head?.ouch ?? entry.base?.ouch ?? -1;
|
|
200
|
+
}
|
|
201
|
+
function compareFiles(before, after, renames) {
|
|
202
|
+
const base = new Map(before.map(entry => [renames.get(entry.file) ?? entry.file, entry]));
|
|
203
|
+
const head = new Map(after.map(entry => [entry.file, entry]));
|
|
204
|
+
return [...new Set([...base.keys(), ...head.keys()])]
|
|
205
|
+
.map(file => fileComparisonEntry(base.get(file) ?? null, head.get(file) ?? null))
|
|
206
|
+
.filter(entry => entry !== null)
|
|
207
|
+
.sort(compareFileChanges);
|
|
208
|
+
}
|
|
209
|
+
function compareFileChanges(a, b) {
|
|
210
|
+
return fileCognitiveDelta(b) - fileCognitiveDelta(a)
|
|
211
|
+
|| fileCognitive(b) - fileCognitive(a) || a.file.localeCompare(b.file);
|
|
212
|
+
}
|
|
213
|
+
function fileCognitiveDelta(entry) {
|
|
214
|
+
return entry.delta.cognitive ?? 0;
|
|
215
|
+
}
|
|
216
|
+
function fileCognitive(entry) {
|
|
217
|
+
return entry.head?.cognitive ?? entry.base?.cognitive ?? 0;
|
|
218
|
+
}
|
|
219
|
+
function fileComparisonEntry(base, head) {
|
|
220
|
+
const entry = head ?? base;
|
|
221
|
+
if (entry === null)
|
|
222
|
+
return null;
|
|
223
|
+
if (unchangedEntry(base, head, fileScoreFields))
|
|
224
|
+
return null;
|
|
225
|
+
return {
|
|
226
|
+
file: entry.file, status: comparisonStatus(base, head, sameFileScores(base, head)), base, head,
|
|
227
|
+
delta: {
|
|
228
|
+
complexity: fileScoreDelta(base, head, "complexity"),
|
|
229
|
+
cognitive: fileScoreDelta(base, head, "cognitive"),
|
|
230
|
+
functions: fileScoreDelta(base, head, "functions"),
|
|
231
|
+
},
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
const fileScoreFields = ["complexity", "cognitive", "functions"];
|
|
235
|
+
function sameFileScores(base, head) {
|
|
236
|
+
return sameFieldValues(base, head, fileScoreFields);
|
|
237
|
+
}
|
|
238
|
+
function sameFieldValues(base, head, fields) {
|
|
239
|
+
return base !== null && head !== null && fields.every(field => base[field] === head[field]);
|
|
240
|
+
}
|
|
241
|
+
function fileScoreDelta(base, head, field) {
|
|
242
|
+
return scoreDelta(base?.[field] ?? null, head?.[field] ?? null);
|
|
243
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Load only the selected project's config, with paths left relative to that project. */
|
|
2
|
+
export declare function loadConfiguration(cwd: string, explicitPath: string | undefined, disabled: boolean): Promise<{
|
|
3
|
+
sourceRoots: string[] | undefined;
|
|
4
|
+
exclude: string[] | undefined;
|
|
5
|
+
lcov: string | undefined;
|
|
6
|
+
coverageCommand: string | undefined;
|
|
7
|
+
setupCommand: string | undefined;
|
|
8
|
+
outputDir: string | undefined;
|
|
9
|
+
}>;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import { isManifestObject, readOptionalFile } from "./manifest.js";
|
|
3
|
+
/** Load only the selected project's config, with paths left relative to that project. */
|
|
4
|
+
export async function loadConfiguration(cwd, explicitPath, disabled) {
|
|
5
|
+
const path = explicitPath ?? resolve(cwd, "ouch4ts.config.json");
|
|
6
|
+
const text = disabled ? null : await readOptionalFile(path);
|
|
7
|
+
if (!disabled && explicitPath !== undefined && text === null)
|
|
8
|
+
throw new Error(`Config not found: ${path}`);
|
|
9
|
+
return parseConfiguration(text, path);
|
|
10
|
+
}
|
|
11
|
+
function parseConfiguration(text, path) {
|
|
12
|
+
let data;
|
|
13
|
+
try {
|
|
14
|
+
data = text === null ? {} : JSON.parse(text);
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
if (error instanceof SyntaxError)
|
|
18
|
+
throw new Error(`Invalid JSON config ${path}: ${error.message}`);
|
|
19
|
+
throw error;
|
|
20
|
+
}
|
|
21
|
+
if (!isManifestObject(data))
|
|
22
|
+
throw new Error(`Expected a config object in ${path}`);
|
|
23
|
+
const keys = new Set(["sourceRoots", "exclude", "lcov", "coverageCommand", "setupCommand", "outputDir"]);
|
|
24
|
+
for (const key of Object.keys(data)) {
|
|
25
|
+
if (!keys.has(key))
|
|
26
|
+
throw new Error(`Unknown config property ${key} in ${path}`);
|
|
27
|
+
}
|
|
28
|
+
const sourceRoots = stringList(data.sourceRoots, `${path}: sourceRoots`);
|
|
29
|
+
if (sourceRoots?.length === 0)
|
|
30
|
+
throw new Error(`${path}: sourceRoots must not be empty`);
|
|
31
|
+
return {
|
|
32
|
+
sourceRoots, exclude: stringList(data.exclude, `${path}: exclude`),
|
|
33
|
+
lcov: stringSetting(data.lcov, `${path}: lcov`),
|
|
34
|
+
coverageCommand: stringSetting(data.coverageCommand, `${path}: coverageCommand`),
|
|
35
|
+
setupCommand: stringSetting(data.setupCommand, `${path}: setupCommand`),
|
|
36
|
+
outputDir: stringSetting(data.outputDir, `${path}: outputDir`),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function stringSetting(value, label) {
|
|
40
|
+
if (value === undefined)
|
|
41
|
+
return undefined;
|
|
42
|
+
return nonEmptyString(value, label);
|
|
43
|
+
}
|
|
44
|
+
function nonEmptyString(value, label) {
|
|
45
|
+
if (typeof value !== "string" || value.trim() === "" || value.includes("\0")) {
|
|
46
|
+
throw new Error(`${label} must be a non-empty string without NUL characters`);
|
|
47
|
+
}
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
function stringList(value, label) {
|
|
51
|
+
if (value === undefined)
|
|
52
|
+
return undefined;
|
|
53
|
+
if (!Array.isArray(value))
|
|
54
|
+
throw new Error(`${label} must be a list of strings`);
|
|
55
|
+
return value.map((item) => nonEmptyString(item, label));
|
|
56
|
+
}
|
package/dist/cycles.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type ts from "typescript";
|
|
2
|
+
type CallGraph = ReadonlyMap<ts.SignatureDeclaration, ReadonlySet<ts.SignatureDeclaration>>;
|
|
3
|
+
/** Find cyclic components once with iterative Tarjan traversal, including long call chains. */
|
|
4
|
+
export declare function recursiveCallNodes(graph: CallGraph): Set<ts.SignatureDeclaration>;
|
|
5
|
+
export {};
|
package/dist/cycles.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/** Find cyclic components once with iterative Tarjan traversal, including long call chains. */
|
|
2
|
+
export function recursiveCallNodes(graph) {
|
|
3
|
+
return new CallCycleFinder(graph).find();
|
|
4
|
+
}
|
|
5
|
+
class CallCycleFinder {
|
|
6
|
+
graph;
|
|
7
|
+
visits = new Map();
|
|
8
|
+
pending = [];
|
|
9
|
+
componentStack = [];
|
|
10
|
+
recursive = new Set();
|
|
11
|
+
constructor(graph) {
|
|
12
|
+
this.graph = graph;
|
|
13
|
+
}
|
|
14
|
+
find() {
|
|
15
|
+
for (const node of this.graph.keys())
|
|
16
|
+
this.visitUnseen(node);
|
|
17
|
+
return this.recursive;
|
|
18
|
+
}
|
|
19
|
+
visitUnseen(node) {
|
|
20
|
+
if (this.visits.has(node))
|
|
21
|
+
return;
|
|
22
|
+
this.enter(node);
|
|
23
|
+
while (this.pending.length)
|
|
24
|
+
this.advance(this.pending.at(-1));
|
|
25
|
+
}
|
|
26
|
+
enter(node) {
|
|
27
|
+
const visit = {
|
|
28
|
+
node, index: this.visits.size, earliestReachableIndex: this.visits.size, active: true,
|
|
29
|
+
targets: (this.graph.get(node) ?? []).values(),
|
|
30
|
+
};
|
|
31
|
+
this.visits.set(node, visit);
|
|
32
|
+
this.pending.push(visit);
|
|
33
|
+
this.componentStack.push(visit);
|
|
34
|
+
}
|
|
35
|
+
advance(visit) {
|
|
36
|
+
const target = visit.targets.next();
|
|
37
|
+
if (target.done)
|
|
38
|
+
this.finishVisit(visit);
|
|
39
|
+
else
|
|
40
|
+
this.visitTarget(visit, target.value);
|
|
41
|
+
}
|
|
42
|
+
visitTarget(caller, target) {
|
|
43
|
+
const visit = this.visits.get(target);
|
|
44
|
+
if (!visit)
|
|
45
|
+
return this.enter(target);
|
|
46
|
+
if (visit.active)
|
|
47
|
+
caller.earliestReachableIndex = Math.min(caller.earliestReachableIndex, visit.index);
|
|
48
|
+
}
|
|
49
|
+
finishVisit(visit) {
|
|
50
|
+
this.pending.pop();
|
|
51
|
+
const caller = this.pending.at(-1);
|
|
52
|
+
if (caller)
|
|
53
|
+
caller.earliestReachableIndex = Math.min(caller.earliestReachableIndex, visit.earliestReachableIndex);
|
|
54
|
+
if (visit.earliestReachableIndex === visit.index)
|
|
55
|
+
this.recordComponent(visit);
|
|
56
|
+
}
|
|
57
|
+
recordComponent(root) {
|
|
58
|
+
const component = this.takeComponent(root);
|
|
59
|
+
if (component.length > 1 || this.graph.get(root.node)?.has(root.node)) {
|
|
60
|
+
component.forEach(node => this.recursive.add(node));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
takeComponent(root) {
|
|
64
|
+
const component = [];
|
|
65
|
+
let visit;
|
|
66
|
+
do {
|
|
67
|
+
visit = this.componentStack.pop();
|
|
68
|
+
visit.active = false;
|
|
69
|
+
component.push(visit.node);
|
|
70
|
+
} while (visit !== root);
|
|
71
|
+
return component;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function discoverSources(root: string, isExcluded: (path: string) => boolean): Promise<string[]>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { readdir } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
export async function discoverSources(root, isExcluded) {
|
|
4
|
+
if (isExcluded(root))
|
|
5
|
+
return [];
|
|
6
|
+
const paths = [];
|
|
7
|
+
for (const entry of await readdir(root, { withFileTypes: true })) {
|
|
8
|
+
paths.push(...await discoverEntrySources(root, entry, isExcluded));
|
|
9
|
+
}
|
|
10
|
+
return paths;
|
|
11
|
+
}
|
|
12
|
+
async function discoverEntrySources(root, entry, isExcluded) {
|
|
13
|
+
const path = join(root, entry.name);
|
|
14
|
+
if (isSourceDirectory(entry))
|
|
15
|
+
return discoverSources(path, isExcluded);
|
|
16
|
+
return isIncludedSource(path, entry, isExcluded) ? [path] : [];
|
|
17
|
+
}
|
|
18
|
+
function isSourceDirectory(entry) {
|
|
19
|
+
return entry.isDirectory() && !excludedDirectories.has(entry.name);
|
|
20
|
+
}
|
|
21
|
+
function isIncludedSource(path, entry, isExcluded) {
|
|
22
|
+
return entry.isFile() && isSourceFile(entry.name) && !isExcluded(path);
|
|
23
|
+
}
|
|
24
|
+
function isSourceFile(name) {
|
|
25
|
+
return /\.(?:ts|tsx|mts|cts|js|jsx|mjs|cjs)$/.test(name) && !/\.d\.[cm]?ts$|\.(?:test|spec)\.[cm]?[jt]sx?$/.test(name);
|
|
26
|
+
}
|
|
27
|
+
const excludedDirectories = new Set(["node_modules", ".git", "dist", "build", "coverage", "test", "tests", "__tests__", "__mocks__"]);
|
package/dist/files.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Publish complete JSON with an atomic rename in the destination directory. */
|
|
2
|
+
export declare function writeJson(path: string, value: unknown): Promise<void>;
|
|
3
|
+
export declare function reportStamp(path: string): Promise<string | null>;
|
|
4
|
+
export declare function isMissingFile(error: unknown): boolean;
|
|
5
|
+
export declare function missingFileOrThrow(error: unknown): null;
|
|
6
|
+
/** Resolve symlinks through the nearest existing ancestor, including for a future LCOV file. */
|
|
7
|
+
export declare function canonicalPath(path: string): Promise<string>;
|
|
8
|
+
/** Resolve a comparison input and reject symlinks or paths outside its checkout. */
|
|
9
|
+
export declare function canonicalRepositoryPath(path: string, root: string): Promise<string>;
|
package/dist/files.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { mkdir, realpath, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
3
|
+
import { basename, dirname, isAbsolute, join, relative, sep } from "node:path";
|
|
4
|
+
/** Publish complete JSON with an atomic rename in the destination directory. */
|
|
5
|
+
export async function writeJson(path, value) {
|
|
6
|
+
await mkdir(dirname(path), { recursive: true });
|
|
7
|
+
const temporary = `${path}.${randomUUID()}.tmp`;
|
|
8
|
+
try {
|
|
9
|
+
await writeFile(temporary, JSON.stringify(value, null, 2) + "\n");
|
|
10
|
+
await rename(temporary, path);
|
|
11
|
+
}
|
|
12
|
+
finally {
|
|
13
|
+
await rm(temporary, { force: true });
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export async function reportStamp(path) {
|
|
17
|
+
try {
|
|
18
|
+
const info = await stat(path, { bigint: true });
|
|
19
|
+
return `${info.ino}:${info.mtimeNs}:${info.ctimeNs}:${info.size}`;
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
return missingFileOrThrow(error);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export function isMissingFile(error) {
|
|
26
|
+
return error instanceof Error && "code" in error && error.code === "ENOENT";
|
|
27
|
+
}
|
|
28
|
+
export function missingFileOrThrow(error) {
|
|
29
|
+
if (isMissingFile(error))
|
|
30
|
+
return null;
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
/** Resolve symlinks through the nearest existing ancestor, including for a future LCOV file. */
|
|
34
|
+
export async function canonicalPath(path) {
|
|
35
|
+
return realpath(path).catch(error => canonicalMissingPath(path, error));
|
|
36
|
+
}
|
|
37
|
+
/** Resolve a comparison input and reject symlinks or paths outside its checkout. */
|
|
38
|
+
export async function canonicalRepositoryPath(path, root) {
|
|
39
|
+
const absolute = await canonicalPath(path);
|
|
40
|
+
const fromRoot = relative(root, absolute);
|
|
41
|
+
if (fromRoot === ".." || fromRoot.startsWith(`..${sep}`) || isAbsolute(fromRoot)) {
|
|
42
|
+
throw new Error(`Comparison paths must stay inside the Git repository: ${path}`);
|
|
43
|
+
}
|
|
44
|
+
return absolute;
|
|
45
|
+
}
|
|
46
|
+
async function canonicalMissingPath(path, error) {
|
|
47
|
+
if (!canResolveMissingPath(path, error))
|
|
48
|
+
throw error;
|
|
49
|
+
return join(await canonicalPath(dirname(path)), basename(path));
|
|
50
|
+
}
|
|
51
|
+
function canResolveMissingPath(path, error) {
|
|
52
|
+
return isMissingFile(error) && dirname(path) !== path;
|
|
53
|
+
}
|
package/dist/format.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function formatScore(value: number | null): string;
|
|
2
|
+
export declare function formatCoverage(value: number | null): string;
|
|
3
|
+
export declare function formatPair(base: number | undefined, head: number | undefined): string;
|
|
4
|
+
export declare function formatDelta(value: number | null): string;
|
|
5
|
+
export declare function formatTable(title: string, rows: string[][], leftColumns?: number): string;
|
package/dist/format.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export function formatScore(value) {
|
|
2
|
+
return value === null ? "N/A" : value.toFixed(1);
|
|
3
|
+
}
|
|
4
|
+
export function formatCoverage(value) {
|
|
5
|
+
return value === null ? "N/A" : value.toFixed(1) + "%";
|
|
6
|
+
}
|
|
7
|
+
export function formatPair(base, head) {
|
|
8
|
+
return `${base ?? "-"} / ${head ?? "-"}`;
|
|
9
|
+
}
|
|
10
|
+
export function formatDelta(value) {
|
|
11
|
+
if (value === null)
|
|
12
|
+
return "-";
|
|
13
|
+
return (value > 0 ? "+" : "") + value.toFixed(1);
|
|
14
|
+
}
|
|
15
|
+
export function formatTable(title, rows, leftColumns = 2) {
|
|
16
|
+
const header = rows[0];
|
|
17
|
+
if (header === undefined)
|
|
18
|
+
throw new Error("Table must include a header row");
|
|
19
|
+
if (rows.some(row => row.length !== header.length)) {
|
|
20
|
+
throw new Error("Table rows must match the header column count");
|
|
21
|
+
}
|
|
22
|
+
const widths = header.map((_, column) => rows.reduce((width, row) => Math.max(width, row[column].length), 0));
|
|
23
|
+
const formatted = rows.map(row => {
|
|
24
|
+
const columns = row.map((value, column) => column < leftColumns ? value.padEnd(widths[column]) : value.padStart(widths[column]));
|
|
25
|
+
return columns.join(" ").trimEnd();
|
|
26
|
+
});
|
|
27
|
+
return [title, "=".repeat(title.length), ...formatted].join("\n");
|
|
28
|
+
}
|
package/dist/git.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function git(args: string[], cwd?: string): string;
|
package/dist/git.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
export function git(args, cwd = process.cwd()) {
|
|
3
|
+
const result = spawnSync("git", args, { cwd, encoding: "utf8", maxBuffer: 64 * 1024 * 1024 });
|
|
4
|
+
if (result.error)
|
|
5
|
+
throw new Error(`Cannot run Git: ${result.error.message}. Install Git and ensure it is on PATH.`);
|
|
6
|
+
if (result.status !== 0) {
|
|
7
|
+
const hint = args[0] === "rev-parse" || args[0] === "merge-base"
|
|
8
|
+
? " Check the selected project's Git repository and reference names. Fetch missing references or history, including in shallow checkouts."
|
|
9
|
+
: "";
|
|
10
|
+
throw new Error(`git ${args[0]} failed: ${result.stderr.trim()}${hint}`);
|
|
11
|
+
}
|
|
12
|
+
return result.stdout;
|
|
13
|
+
}
|