@dcaldeira/tooling 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 +9 -0
- package/README.md +34 -3
- package/checks/a11y.mjs +206 -0
- package/checks/architecture.mjs +137 -0
- package/checks/audit.mjs +121 -0
- package/checks/bundle.mjs +126 -0
- package/checks/dependencies.mjs +277 -0
- package/checks/lint-scss.mjs +349 -0
- package/checks/package.mjs +30 -0
- package/checks/playwright-summary.mjs +53 -0
- package/checks/sass.mjs +67 -0
- package/checks/scan-secrets.mjs +58 -0
- package/checks/shared.mjs +86 -0
- package/checks/style-property-order.json +498 -0
- package/checks/translations.mjs +55 -0
- package/checks/visual-compare.mjs +148 -0
- package/checks/visual-report-html.mjs +470 -0
- package/checks/visual-report.mjs +13 -0
- package/config.schema.json +927 -0
- package/configs/eslint.mjs +27 -0
- package/configs/knip.json +11 -0
- package/configs/playwright.mjs +15 -0
- package/configs/prettier.mjs +1 -0
- package/configs/tsconfig.json +16 -0
- package/configs/vitest.mjs +16 -0
- package/package.json +105 -6
- package/release/evidence.mjs +62 -0
- package/release/finalize.mjs +65 -0
- package/release/gh.mjs +25 -0
- package/release/github.mjs +64 -0
- package/release/prepare.mjs +30 -0
- package/runner/browsers.mjs +73 -0
- package/runner/cache.mjs +121 -0
- package/runner/ci-plan.mjs +54 -0
- package/runner/cli.mjs +249 -0
- package/runner/config.mjs +148 -0
- package/runner/docs-install.mjs +93 -0
- package/runner/execute.mjs +286 -0
- package/runner/generate.mjs +122 -0
- package/runner/git.mjs +122 -0
- package/runner/graph.mjs +243 -0
- package/runner/index.mjs +4 -0
- package/runner/insights.mjs +313 -0
- package/runner/provision.mjs +53 -0
- package/runner/report.mjs +35 -0
- package/runner/serve-static.mjs +76 -0
- package/runner/setup.mjs +237 -0
- package/runner/summary.mjs +112 -0
- package/runner/watch.mjs +82 -0
- package/templates/ci.yml +25 -0
- package/templates/dependabot-merge.yml +13 -0
- package/templates/dependabot.yml +26 -0
- package/templates/pre-push +13 -0
- package/templates/release.yml +23 -0
package/runner/graph.mjs
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { defaults } from './config.mjs';
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
|
|
4
|
+
const script = (name) => ['node', fileURLToPath(new URL(`../checks/${name}.mjs`, import.meta.url))];
|
|
5
|
+
const patterns = new Map();
|
|
6
|
+
|
|
7
|
+
function compile(pattern) {
|
|
8
|
+
let regex = '^';
|
|
9
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
10
|
+
const c = pattern[i];
|
|
11
|
+
if (c === '*' && pattern[i + 1] === '*') {
|
|
12
|
+
i++;
|
|
13
|
+
if (pattern[i + 1] === '/') {
|
|
14
|
+
i++;
|
|
15
|
+
regex += '(?:.*/)?';
|
|
16
|
+
} else regex += '.*';
|
|
17
|
+
} else if (c === '*') regex += '[^/]*';
|
|
18
|
+
else if (c === '?') regex += '[^/]';
|
|
19
|
+
else regex += c.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
20
|
+
}
|
|
21
|
+
return new RegExp(`${regex}$`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function matches(file, pattern) {
|
|
25
|
+
let regex = patterns.get(pattern);
|
|
26
|
+
if (!regex) {
|
|
27
|
+
regex = compile(pattern);
|
|
28
|
+
if (patterns.size >= 2048) patterns.clear();
|
|
29
|
+
patterns.set(pattern, regex);
|
|
30
|
+
}
|
|
31
|
+
return regex.test(file.replaceAll('\\', '/'));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function workspaceClosure(config, names) {
|
|
35
|
+
const closure = new Set();
|
|
36
|
+
const visit = (name, chain = []) => {
|
|
37
|
+
const workspace = config.workspaces?.[name];
|
|
38
|
+
if (!workspace) throw new Error(`Unknown workspace: ${name}`);
|
|
39
|
+
if (chain.includes(name))
|
|
40
|
+
throw new Error(`Cyclic workspaces: ${[...chain, name].join(' -> ')}`);
|
|
41
|
+
if (closure.has(name)) return;
|
|
42
|
+
for (const dependency of workspace.dependsOn ?? []) visit(dependency, [...chain, name]);
|
|
43
|
+
closure.add(name);
|
|
44
|
+
};
|
|
45
|
+
names.forEach((name) => visit(name));
|
|
46
|
+
return [...closure];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function workspaceFiles(config, names, files) {
|
|
50
|
+
if (files === 'all' || !names?.length) return files;
|
|
51
|
+
const paths = workspaceClosure(config, names).flatMap((name) => config.workspaces[name].paths);
|
|
52
|
+
return files.filter((file) => {
|
|
53
|
+
if (paths.some((pattern) => matches(file, pattern))) return true;
|
|
54
|
+
const owned = Object.values(config.workspaces ?? {}).some((workspace) =>
|
|
55
|
+
workspace.paths.some((pattern) => matches(file, pattern)),
|
|
56
|
+
);
|
|
57
|
+
// Unassigned source is conservatively shared; root documentation remains formatting-only.
|
|
58
|
+
return !owned && classify([file], config).some((group) => group !== 'docs');
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function classify(files, config = {}) {
|
|
63
|
+
if (files === 'all') return ['all'];
|
|
64
|
+
const paths = { ...defaults, ...config.paths };
|
|
65
|
+
const groups = new Set();
|
|
66
|
+
for (const file of files) {
|
|
67
|
+
const found = Object.entries(paths)
|
|
68
|
+
.filter(([, patterns]) => patterns.some((p) => matches(file, p)))
|
|
69
|
+
.map(([key]) => key);
|
|
70
|
+
if (found.includes('docs')) groups.add('docs');
|
|
71
|
+
else if (found.includes('styles')) groups.add('styles');
|
|
72
|
+
else if (found.includes('translations')) groups.add('translations');
|
|
73
|
+
else if (found.length) found.forEach((g) => groups.add(g));
|
|
74
|
+
else if (
|
|
75
|
+
Object.values(config.workspaces ?? {}).some((workspace) =>
|
|
76
|
+
workspace.paths.some((pattern) => matches(file, pattern)),
|
|
77
|
+
)
|
|
78
|
+
)
|
|
79
|
+
groups.add('app');
|
|
80
|
+
else groups.add('unknown');
|
|
81
|
+
}
|
|
82
|
+
return [...groups].sort();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const full = ['push', 'ci'];
|
|
86
|
+
const all = ['dev', ...full];
|
|
87
|
+
const check = (command, groups, extra = {}) => ({ command, groups, profiles: full, ...extra });
|
|
88
|
+
export const builtins = {
|
|
89
|
+
format: check(
|
|
90
|
+
['prettier', '--check', '.'],
|
|
91
|
+
['docs', 'styles', 'translations', 'app', 'database', 'functions', 'workflows'],
|
|
92
|
+
{
|
|
93
|
+
profiles: all,
|
|
94
|
+
devCommand: ['prettier', '--check', '--ignore-unknown', '--'],
|
|
95
|
+
devFiles: ['**/*'],
|
|
96
|
+
},
|
|
97
|
+
),
|
|
98
|
+
lint: check(
|
|
99
|
+
['eslint', '.', '--max-warnings', '0', '--cache', '--cache-strategy', 'content'],
|
|
100
|
+
['app', 'functions'],
|
|
101
|
+
{
|
|
102
|
+
profiles: all,
|
|
103
|
+
devCommand: ['eslint', '--max-warnings', '0', '--cache', '--cache-strategy', 'content', '--'],
|
|
104
|
+
devFiles: ['**/*.ts', '**/*.tsx', '**/*.js', '**/*.mjs'],
|
|
105
|
+
},
|
|
106
|
+
),
|
|
107
|
+
styles: check(script('lint-scss'), ['styles', 'app'], {
|
|
108
|
+
profiles: all,
|
|
109
|
+
devCommand: script('lint-scss'),
|
|
110
|
+
devFiles: ['**/*.scss'],
|
|
111
|
+
}),
|
|
112
|
+
types: check(['tsc', '--build', '--pretty', 'false'], ['app', 'functions'], {
|
|
113
|
+
resources: ['build'],
|
|
114
|
+
}),
|
|
115
|
+
unit: check(['vitest', 'run', '--coverage'], ['app', 'functions'], {
|
|
116
|
+
profiles: all,
|
|
117
|
+
devCommand: ['vitest', 'related', '--run', '--passWithNoTests'],
|
|
118
|
+
devAllCommand: ['vitest', 'run'],
|
|
119
|
+
devFiles: ['**/*.ts', '**/*.tsx', '**/*.js', '**/*.mjs'],
|
|
120
|
+
}),
|
|
121
|
+
translations: check(script('translations'), ['translations', 'app'], { profiles: all }),
|
|
122
|
+
build: check(['npm', 'run', 'build'], ['app'], { resources: ['build'] }),
|
|
123
|
+
size: check(['npm', 'run', 'size'], ['app'], { dependsOn: ['build'] }),
|
|
124
|
+
deadcode: check(['knip'], ['app']),
|
|
125
|
+
security: check(
|
|
126
|
+
'npm audit --audit-level=high && gitleaks git --redact',
|
|
127
|
+
['app', 'database', 'functions'],
|
|
128
|
+
{ tools: ['gitleaks'] },
|
|
129
|
+
),
|
|
130
|
+
e2e: check(['playwright', 'test', '--grep-invert', '@visual'], ['app'], {
|
|
131
|
+
dependsOn: ['build'],
|
|
132
|
+
platforms: ['linux'],
|
|
133
|
+
resources: ['browser'],
|
|
134
|
+
}),
|
|
135
|
+
visual: check(['playwright', 'test', '--grep', '@visual'], ['app'], {
|
|
136
|
+
dependsOn: ['build'],
|
|
137
|
+
platforms: ['linux'],
|
|
138
|
+
resources: ['browser'],
|
|
139
|
+
}),
|
|
140
|
+
lighthouse: check(['lhci', 'autorun'], ['app'], {
|
|
141
|
+
dependsOn: ['build'],
|
|
142
|
+
tools: ['lhci'],
|
|
143
|
+
platforms: ['linux'],
|
|
144
|
+
resources: ['browser'],
|
|
145
|
+
}),
|
|
146
|
+
workflows: check(['actionlint', '-color'], ['workflows'], { tools: ['actionlint'] }),
|
|
147
|
+
sass: check(script('sass'), ['styles', 'app'], { profiles: all }),
|
|
148
|
+
architecture: check(script('architecture'), ['app', 'functions'], { profiles: all }),
|
|
149
|
+
'dependency-consistency': check(script('dependencies'), ['app', 'functions', 'dependencies']),
|
|
150
|
+
a11y: check(script('a11y'), ['app'], {
|
|
151
|
+
dependsOn: ['build'],
|
|
152
|
+
resources: ['browser'],
|
|
153
|
+
platforms: ['linux'],
|
|
154
|
+
}),
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const optIn = {
|
|
158
|
+
sass: 'sass',
|
|
159
|
+
architecture: 'architecture',
|
|
160
|
+
'dependency-consistency': 'dependencyConsistency',
|
|
161
|
+
a11y: 'a11y',
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
export function plan(config, { files = 'all', profile = 'ci', only = [], skip = [] } = {}) {
|
|
165
|
+
if (!['dev', 'push', 'ci'].includes(profile)) throw new Error(`Unknown profile: ${profile}`);
|
|
166
|
+
const checks = Object.fromEntries(
|
|
167
|
+
Object.entries({ ...builtins, ...config.checks })
|
|
168
|
+
.filter(
|
|
169
|
+
([id, c]) =>
|
|
170
|
+
c !== false &&
|
|
171
|
+
(!optIn[c.extends ?? id] ||
|
|
172
|
+
config[optIn[c.extends ?? id]] ||
|
|
173
|
+
Object.hasOwn(config.checks ?? {}, id)),
|
|
174
|
+
)
|
|
175
|
+
.map(([id, c]) => {
|
|
176
|
+
if (c.extends && !builtins[c.extends])
|
|
177
|
+
throw new Error(`Unknown check preset: ${c.extends}`);
|
|
178
|
+
return [id, { ...builtins[c.extends ?? id], ...c, kind: c.extends ?? id }];
|
|
179
|
+
}),
|
|
180
|
+
);
|
|
181
|
+
for (const id of [...only, ...skip]) if (!checks[id]) throw new Error(`Unknown check: ${id}`);
|
|
182
|
+
for (const [id, c] of Object.entries(checks))
|
|
183
|
+
if (!c.command || !c.groups || !c.profiles)
|
|
184
|
+
throw new Error(`Custom check ${id} needs command, groups and profiles`);
|
|
185
|
+
const groups = classify(files, config);
|
|
186
|
+
const broad = groups.some((g) => ['all', 'tooling', 'dependencies', 'unknown'].includes(g));
|
|
187
|
+
const workspaces = Object.keys(config.workspaces ?? {});
|
|
188
|
+
workspaceClosure(config, workspaces);
|
|
189
|
+
for (const check of Object.values(checks)) workspaceClosure(config, check.workspaces ?? []);
|
|
190
|
+
const affectedWorkspaces = workspaces.filter(
|
|
191
|
+
(name) => broad || workspaceFiles(config, [name], files).length,
|
|
192
|
+
);
|
|
193
|
+
const scopedFiles = (c) => (broad ? files : workspaceFiles(config, c.workspaces, files));
|
|
194
|
+
const required = new Set(
|
|
195
|
+
Object.entries(checks)
|
|
196
|
+
.filter(
|
|
197
|
+
([id, c]) =>
|
|
198
|
+
c.profiles.includes(profile) &&
|
|
199
|
+
(profile !== 'dev' ||
|
|
200
|
+
c.kind !== 'translations' ||
|
|
201
|
+
broad ||
|
|
202
|
+
groups.includes('translations')) &&
|
|
203
|
+
(broad || c.groups.some((g) => classify(scopedFiles(c), config).includes(g))),
|
|
204
|
+
)
|
|
205
|
+
.map(([id]) => id),
|
|
206
|
+
);
|
|
207
|
+
const wanted = new Set(only.length ? only : required);
|
|
208
|
+
const expanded = new Set();
|
|
209
|
+
const visit = (id, chain = []) => {
|
|
210
|
+
if (!checks[id]) throw new Error(`Missing dependency: ${id}`);
|
|
211
|
+
if (chain.includes(id)) throw new Error(`Cyclic checks: ${[...chain, id].join(' -> ')}`);
|
|
212
|
+
if (expanded.has(id)) return;
|
|
213
|
+
for (const dep of checks[id].dependsOn ?? []) {
|
|
214
|
+
wanted.add(dep);
|
|
215
|
+
visit(dep, [...chain, id]);
|
|
216
|
+
}
|
|
217
|
+
expanded.add(id);
|
|
218
|
+
};
|
|
219
|
+
[...wanted].forEach((id) => visit(id));
|
|
220
|
+
const ordered = [];
|
|
221
|
+
const seen = new Set();
|
|
222
|
+
const order = (id) => {
|
|
223
|
+
if (seen.has(id)) return;
|
|
224
|
+
for (const dep of checks[id].dependsOn ?? []) order(dep);
|
|
225
|
+
seen.add(id);
|
|
226
|
+
ordered.push(id);
|
|
227
|
+
};
|
|
228
|
+
[...wanted].forEach(order);
|
|
229
|
+
return {
|
|
230
|
+
profile,
|
|
231
|
+
groups,
|
|
232
|
+
files,
|
|
233
|
+
affectedWorkspaces,
|
|
234
|
+
checks: ordered.map((id) => ({
|
|
235
|
+
id,
|
|
236
|
+
...checks[id],
|
|
237
|
+
files: scopedFiles(checks[id]),
|
|
238
|
+
excluded: skip.includes(id),
|
|
239
|
+
})),
|
|
240
|
+
required: [...required],
|
|
241
|
+
excluded: [...required].filter((id) => !wanted.has(id) || skip.includes(id)),
|
|
242
|
+
};
|
|
243
|
+
}
|
package/runner/index.mjs
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import {
|
|
3
|
+
mkdirSync,
|
|
4
|
+
openSync,
|
|
5
|
+
closeSync,
|
|
6
|
+
readFileSync,
|
|
7
|
+
statSync,
|
|
8
|
+
writeFileSync,
|
|
9
|
+
renameSync,
|
|
10
|
+
unlinkSync,
|
|
11
|
+
} from 'node:fs';
|
|
12
|
+
import { join, resolve } from 'node:path';
|
|
13
|
+
import { inside } from './config.mjs';
|
|
14
|
+
|
|
15
|
+
const finite = (value) => typeof value === 'number' && Number.isFinite(value) && value >= 0;
|
|
16
|
+
const median = (values) => {
|
|
17
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
18
|
+
if (!sorted.length) return null;
|
|
19
|
+
const middle = Math.floor(sorted.length / 2);
|
|
20
|
+
return sorted.length % 2 ? sorted[middle] : (sorted[middle - 1] + sorted[middle]) / 2;
|
|
21
|
+
};
|
|
22
|
+
const readFresh = (path, since) => {
|
|
23
|
+
try {
|
|
24
|
+
const stat = statSync(path);
|
|
25
|
+
if (stat.size > 20 * 1024 * 1024 || stat.mtimeMs < since - 1000) return null;
|
|
26
|
+
return JSON.parse(readFileSync(path, 'utf8'));
|
|
27
|
+
} catch (error) {
|
|
28
|
+
if (error.code === 'ENOENT') return null;
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export function collectMetrics(root, output, report, settings = {}) {
|
|
34
|
+
const since = Date.parse(report.startedAt) || Date.now();
|
|
35
|
+
const metrics = { slowTests: [] };
|
|
36
|
+
const threshold = settings.slowTestMilliseconds ?? 500;
|
|
37
|
+
const seen = new Set();
|
|
38
|
+
const read = (path) => {
|
|
39
|
+
const full = resolve(path);
|
|
40
|
+
if (seen.has(full)) return null;
|
|
41
|
+
seen.add(full);
|
|
42
|
+
return readFresh(full, since);
|
|
43
|
+
};
|
|
44
|
+
const totals = {};
|
|
45
|
+
function coverage(data) {
|
|
46
|
+
if (!data?.total) return;
|
|
47
|
+
for (const key of ['lines', 'branches', 'functions', 'statements']) {
|
|
48
|
+
const value = data.total[key];
|
|
49
|
+
if (!value) continue;
|
|
50
|
+
if (finite(value.total) && finite(value.covered)) {
|
|
51
|
+
const sum = totals[key] ?? { total: 0, covered: 0 };
|
|
52
|
+
sum.total += value.total;
|
|
53
|
+
sum.covered += value.covered;
|
|
54
|
+
totals[key] = sum;
|
|
55
|
+
metrics.coverage ??= {};
|
|
56
|
+
metrics.coverage[key] = sum.total ? (100 * sum.covered) / sum.total : 100;
|
|
57
|
+
} else if (finite(value.pct) && !totals[key]) {
|
|
58
|
+
metrics.coverage ??= {};
|
|
59
|
+
metrics.coverage[key] = value.pct;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function unit(data) {
|
|
64
|
+
for (const file of data.testResults ?? [])
|
|
65
|
+
for (const result of file.assertionResults ?? []) {
|
|
66
|
+
if (finite(result.duration) && result.duration >= threshold)
|
|
67
|
+
metrics.slowTests.push({
|
|
68
|
+
name: [
|
|
69
|
+
...(result.ancestorTitles ?? []),
|
|
70
|
+
result.title ?? result.fullName ?? file.name,
|
|
71
|
+
].join(' > '),
|
|
72
|
+
milliseconds: result.duration,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function playwright(data) {
|
|
77
|
+
const stats = data.stats ?? {};
|
|
78
|
+
metrics.browser ??= { passed: 0, failed: 0, flaky: 0, skipped: 0 };
|
|
79
|
+
for (const [key, field] of Object.entries({
|
|
80
|
+
passed: 'expected',
|
|
81
|
+
failed: 'unexpected',
|
|
82
|
+
flaky: 'flaky',
|
|
83
|
+
skipped: 'skipped',
|
|
84
|
+
}))
|
|
85
|
+
if (finite(stats[field])) metrics.browser[key] += stats[field];
|
|
86
|
+
function visit(suite, parents = []) {
|
|
87
|
+
for (const spec of suite.specs ?? [])
|
|
88
|
+
for (const test of spec.tests ?? []) {
|
|
89
|
+
const duration = Math.max(
|
|
90
|
+
0,
|
|
91
|
+
...(test.results ?? []).map((result) => result.duration ?? 0),
|
|
92
|
+
);
|
|
93
|
+
if (finite(duration) && duration >= threshold)
|
|
94
|
+
metrics.slowTests.push({
|
|
95
|
+
name: [...parents, spec.title].join(' > '),
|
|
96
|
+
milliseconds: duration,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
for (const child of suite.suites ?? []) visit(child, [...parents, child.title]);
|
|
100
|
+
}
|
|
101
|
+
for (const suite of data.suites ?? []) visit(suite, [suite.title]);
|
|
102
|
+
}
|
|
103
|
+
for (const check of report.results) {
|
|
104
|
+
if (check.cached || ['excluded', 'unavailable'].includes(check.status)) continue;
|
|
105
|
+
const kind = check.kind ?? check.id;
|
|
106
|
+
const details = read(join(output, `${check.id}.details.json`));
|
|
107
|
+
if (details?.bundle) metrics.bundle = details.bundle;
|
|
108
|
+
if (details?.a11y)
|
|
109
|
+
metrics.a11y = {
|
|
110
|
+
pages: details.a11y.pages?.length ?? 0,
|
|
111
|
+
violations: details.a11y.failures ?? 0,
|
|
112
|
+
errors: details.a11y.errors?.length ?? 0,
|
|
113
|
+
};
|
|
114
|
+
if (details?.architecture)
|
|
115
|
+
metrics.architecture = {
|
|
116
|
+
files: details.architecture.files,
|
|
117
|
+
issues: details.architecture.issues?.length ?? 0,
|
|
118
|
+
};
|
|
119
|
+
if (details?.dependencies)
|
|
120
|
+
metrics.dependencies = {
|
|
121
|
+
packages: details.dependencies.packages,
|
|
122
|
+
issues: details.dependencies.issues?.length ?? 0,
|
|
123
|
+
duplicates: details.dependencies.warnings?.length ?? 0,
|
|
124
|
+
};
|
|
125
|
+
if (kind === 'unit') {
|
|
126
|
+
const data = read(join(output, `${check.id}-results/vitest.json`));
|
|
127
|
+
if (data) unit(data);
|
|
128
|
+
coverage(read(join(output, `${check.id}-results/coverage/coverage-summary.json`)));
|
|
129
|
+
}
|
|
130
|
+
if (['e2e', 'visual'].includes(kind)) {
|
|
131
|
+
const data = read(join(output, `${check.id}-results/results.json`));
|
|
132
|
+
if (data) playwright(data);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
for (const path of settings.coverage ?? []) {
|
|
136
|
+
if (
|
|
137
|
+
!report.results.some(
|
|
138
|
+
(check) =>
|
|
139
|
+
(check.kind ?? check.id) === 'unit' &&
|
|
140
|
+
!check.cached &&
|
|
141
|
+
!['excluded', 'unavailable'].includes(check.status),
|
|
142
|
+
)
|
|
143
|
+
)
|
|
144
|
+
break;
|
|
145
|
+
const data = read(inside(root, path));
|
|
146
|
+
coverage(data);
|
|
147
|
+
}
|
|
148
|
+
for (const path of settings.unit ?? []) {
|
|
149
|
+
if (
|
|
150
|
+
report.results.some(
|
|
151
|
+
(check) =>
|
|
152
|
+
(check.kind ?? check.id) === 'unit' &&
|
|
153
|
+
!check.cached &&
|
|
154
|
+
!['excluded', 'unavailable'].includes(check.status),
|
|
155
|
+
)
|
|
156
|
+
) {
|
|
157
|
+
const data = read(inside(root, path));
|
|
158
|
+
if (data) unit(data);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
for (const path of settings.playwright ?? []) {
|
|
162
|
+
if (
|
|
163
|
+
report.results.some(
|
|
164
|
+
(check) =>
|
|
165
|
+
['e2e', 'visual'].includes(check.kind ?? check.id) &&
|
|
166
|
+
!['excluded', 'unavailable'].includes(check.status),
|
|
167
|
+
)
|
|
168
|
+
) {
|
|
169
|
+
const data = read(inside(root, path));
|
|
170
|
+
if (data) playwright(data);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
metrics.slowTests.sort((a, b) => b.milliseconds - a.milliseconds);
|
|
174
|
+
metrics.slowTests = metrics.slowTests.slice(0, 20);
|
|
175
|
+
return metrics;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function readHistory(root) {
|
|
179
|
+
try {
|
|
180
|
+
const path = join(root, '.tooling/history/runs.json');
|
|
181
|
+
if (statSync(path).size > 10 * 1024 * 1024) return [];
|
|
182
|
+
const data = JSON.parse(readFileSync(path, 'utf8'));
|
|
183
|
+
if (data.schemaVersion !== 1 || !Array.isArray(data.runs)) return [];
|
|
184
|
+
return data.runs
|
|
185
|
+
.filter(
|
|
186
|
+
(run) =>
|
|
187
|
+
typeof run.context === 'string' &&
|
|
188
|
+
typeof run.finishedAt === 'string' &&
|
|
189
|
+
typeof run.profile === 'string' &&
|
|
190
|
+
typeof run.commit === 'string' &&
|
|
191
|
+
typeof run.complete === 'boolean' &&
|
|
192
|
+
finite(run.wallMilliseconds) &&
|
|
193
|
+
Array.isArray(run.checks) &&
|
|
194
|
+
run.checks.every((check) => typeof check.id === 'string' && finite(check.milliseconds)),
|
|
195
|
+
)
|
|
196
|
+
.slice(-1000);
|
|
197
|
+
} catch {
|
|
198
|
+
return [];
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export function performanceHistory(root, report, settings = {}) {
|
|
203
|
+
const runs = readHistory(root);
|
|
204
|
+
const context = createHash('sha256')
|
|
205
|
+
.update(
|
|
206
|
+
JSON.stringify({
|
|
207
|
+
profile: report.profile,
|
|
208
|
+
groups: [...report.groups].sort(),
|
|
209
|
+
checks: [...report.required].sort(),
|
|
210
|
+
node: report.node,
|
|
211
|
+
platform: report.platform,
|
|
212
|
+
configuration: report.configuration,
|
|
213
|
+
}),
|
|
214
|
+
)
|
|
215
|
+
.digest('hex');
|
|
216
|
+
const checks = report.results.map((result) => ({
|
|
217
|
+
id: result.id,
|
|
218
|
+
status: result.status,
|
|
219
|
+
cached: Boolean(result.cached),
|
|
220
|
+
milliseconds: finite(result.milliseconds) ? result.milliseconds : 0,
|
|
221
|
+
}));
|
|
222
|
+
const prior = runs.filter((run) => run.context === context && run.complete).slice(-10);
|
|
223
|
+
const comparable = prior.filter(
|
|
224
|
+
(run) =>
|
|
225
|
+
run.checks.map((check) => `${check.id}:${check.cached}:${check.status}`).join() ===
|
|
226
|
+
checks.map((check) => `${check.id}:${check.cached}:${check.status}`).join(),
|
|
227
|
+
);
|
|
228
|
+
const previousMs = median(comparable.map((run) => run.wallMilliseconds));
|
|
229
|
+
const regressions = checks
|
|
230
|
+
.filter((check) => check.status === 'passed' && !check.cached)
|
|
231
|
+
.flatMap((check) => {
|
|
232
|
+
const samples = prior.flatMap((run) =>
|
|
233
|
+
run.checks
|
|
234
|
+
.filter(
|
|
235
|
+
(previous) =>
|
|
236
|
+
previous.id === check.id && previous.status === 'passed' && !previous.cached,
|
|
237
|
+
)
|
|
238
|
+
.map((previous) => previous.milliseconds),
|
|
239
|
+
);
|
|
240
|
+
const baseline = median(samples);
|
|
241
|
+
return samples.length >= 3 &&
|
|
242
|
+
baseline > 0 &&
|
|
243
|
+
check.milliseconds > baseline * (1 + (settings.regressionPercent ?? 25) / 100)
|
|
244
|
+
? [{ id: check.id, previousMs: baseline, currentMs: check.milliseconds }]
|
|
245
|
+
: [];
|
|
246
|
+
});
|
|
247
|
+
const result = {
|
|
248
|
+
runs: Math.min(runs.length + 1, settings.limit ?? 200),
|
|
249
|
+
cacheHits: checks.filter((check) => check.cached).length,
|
|
250
|
+
previousMs,
|
|
251
|
+
deltaMs: previousMs === null ? null : report.wallMilliseconds - previousMs,
|
|
252
|
+
regressions,
|
|
253
|
+
bundleDeltaKB:
|
|
254
|
+
finite(report.metrics?.bundle?.initialKB) && finite(prior.at(-1)?.bundleKB)
|
|
255
|
+
? report.metrics.bundle.initialKB - prior.at(-1).bundleKB
|
|
256
|
+
: null,
|
|
257
|
+
};
|
|
258
|
+
if (settings.enabled === false) return { ...result, runs: 0 };
|
|
259
|
+
const record = {
|
|
260
|
+
context,
|
|
261
|
+
finishedAt: report.finishedAt,
|
|
262
|
+
commit: report.commit,
|
|
263
|
+
profile: report.profile,
|
|
264
|
+
complete: report.complete,
|
|
265
|
+
wallMilliseconds: report.wallMilliseconds,
|
|
266
|
+
checks,
|
|
267
|
+
bundleKB: report.metrics?.bundle?.initialKB,
|
|
268
|
+
coverage: report.metrics?.coverage,
|
|
269
|
+
slowTests: report.metrics?.slowTests ?? [],
|
|
270
|
+
};
|
|
271
|
+
const directory = join(root, '.tooling/history');
|
|
272
|
+
mkdirSync(directory, { recursive: true });
|
|
273
|
+
const lockPath = join(directory, 'write.lock');
|
|
274
|
+
let lock;
|
|
275
|
+
try {
|
|
276
|
+
try {
|
|
277
|
+
if (Date.now() - statSync(lockPath).mtimeMs > 120000) unlinkSync(lockPath);
|
|
278
|
+
} catch (error) {
|
|
279
|
+
if (error.code !== 'ENOENT') throw error;
|
|
280
|
+
}
|
|
281
|
+
lock = openSync(lockPath, 'wx');
|
|
282
|
+
const target = join(directory, 'runs.json'),
|
|
283
|
+
temporary = `${target}.${randomUUID()}.tmp`;
|
|
284
|
+
writeFileSync(
|
|
285
|
+
temporary,
|
|
286
|
+
JSON.stringify({
|
|
287
|
+
schemaVersion: 1,
|
|
288
|
+
runs: [...readHistory(root), record].slice(-(settings.limit ?? 200)),
|
|
289
|
+
}),
|
|
290
|
+
);
|
|
291
|
+
renameSync(temporary, target);
|
|
292
|
+
} catch (error) {
|
|
293
|
+
if (error.code !== 'EEXIST') throw error;
|
|
294
|
+
} finally {
|
|
295
|
+
if (lock !== undefined) {
|
|
296
|
+
closeSync(lock);
|
|
297
|
+
unlinkSync(lockPath);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return result;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
export function historyText(root, limit = 20) {
|
|
304
|
+
const runs = readHistory(root).slice(-limit);
|
|
305
|
+
return runs.length
|
|
306
|
+
? runs
|
|
307
|
+
.map(
|
|
308
|
+
(run) =>
|
|
309
|
+
`${run.finishedAt} ${run.profile.padEnd(4)} ${run.complete ? 'passed' : 'failed'} ${(run.wallMilliseconds / 1000).toFixed(2)}s ${run.checks.filter((check) => check.cached).length}/${run.checks.length} cached ${run.commit?.slice(0, 8) ?? ''}`,
|
|
310
|
+
)
|
|
311
|
+
.join('\n')
|
|
312
|
+
: 'No validation history yet.';
|
|
313
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { chmodSync, mkdirSync, readFileSync, writeFileSync, appendFileSync } from 'node:fs';
|
|
3
|
+
import { join, resolve } from 'node:path';
|
|
4
|
+
import { invoke } from './execute.mjs';
|
|
5
|
+
|
|
6
|
+
const selection = JSON.parse(readFileSync(process.argv[2], 'utf8'));
|
|
7
|
+
const selected = selection.checks.filter((c) => !c.excluded);
|
|
8
|
+
const ids = selected.map((c) => c.kind ?? c.id);
|
|
9
|
+
const tools = new Set(selected.flatMap((c) => c.tools ?? []));
|
|
10
|
+
const root = process.cwd();
|
|
11
|
+
const bin = resolve('.tooling/bin');
|
|
12
|
+
mkdirSync(bin, { recursive: true });
|
|
13
|
+
async function requirePass(command) {
|
|
14
|
+
const result = await invoke(command, root);
|
|
15
|
+
if (result.status !== 'passed')
|
|
16
|
+
throw new Error(`Prerequisite installation failed: ${command.join(' ')}`);
|
|
17
|
+
}
|
|
18
|
+
if (process.platform !== 'linux')
|
|
19
|
+
throw new Error('CI prerequisite provisioning requires Linux; use WSL or a container');
|
|
20
|
+
if (ids.includes('database')) {
|
|
21
|
+
await requirePass(['sudo', 'apt-get', 'update']);
|
|
22
|
+
await requirePass(['sudo', 'apt-get', 'install', '-y', 'postgresql-16', 'postgresql-16-pgtap']);
|
|
23
|
+
if (process.env.GITHUB_ENV)
|
|
24
|
+
appendFileSync(process.env.GITHUB_ENV, 'PG_BIN=/usr/lib/postgresql/16/bin\n');
|
|
25
|
+
}
|
|
26
|
+
for (const [name, image] of [
|
|
27
|
+
['actionlint', 'rhysd/actionlint:1.7.7'],
|
|
28
|
+
['gitleaks', 'zricethezav/gitleaks:v8.24.3'],
|
|
29
|
+
['deno', 'denoland/deno:2.5.6'],
|
|
30
|
+
]) {
|
|
31
|
+
if (!tools.has(name)) continue;
|
|
32
|
+
await requirePass(['docker', 'pull', image]);
|
|
33
|
+
const path = join(bin, name);
|
|
34
|
+
writeFileSync(path, `#!/bin/sh\nexec docker run --rm -v "$PWD:/repo" -w /repo ${image} "$@"\n`);
|
|
35
|
+
chmodSync(path, 0o755);
|
|
36
|
+
}
|
|
37
|
+
if (tools.has('lhci')) {
|
|
38
|
+
await requirePass([
|
|
39
|
+
'npm',
|
|
40
|
+
'install',
|
|
41
|
+
'--prefix',
|
|
42
|
+
'.tooling/tools',
|
|
43
|
+
'--no-save',
|
|
44
|
+
'@lhci/cli@0.15.1',
|
|
45
|
+
]);
|
|
46
|
+
const path = join(bin, 'lhci');
|
|
47
|
+
writeFileSync(
|
|
48
|
+
path,
|
|
49
|
+
`#!/bin/sh\nexec node "${resolve('.tooling/tools/node_modules/@lhci/cli/src/cli.js')}" "$@"\n`,
|
|
50
|
+
);
|
|
51
|
+
chmodSync(path, 0o755);
|
|
52
|
+
}
|
|
53
|
+
if (process.env.GITHUB_PATH) appendFileSync(process.env.GITHUB_PATH, `${bin}\n`);
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { markdownSummary } from './summary.mjs';
|
|
4
|
+
|
|
5
|
+
const escape = (value) =>
|
|
6
|
+
String(value ?? '').replace(
|
|
7
|
+
/[&<>"']/g,
|
|
8
|
+
(c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c],
|
|
9
|
+
);
|
|
10
|
+
export function writeReport(directory, report) {
|
|
11
|
+
mkdirSync(directory, { recursive: true });
|
|
12
|
+
writeFileSync(join(directory, 'summary.json'), `${JSON.stringify(report, null, 2)}\n`);
|
|
13
|
+
writeFileSync(join(directory, 'summary.md'), markdownSummary(report));
|
|
14
|
+
const rows = report.results
|
|
15
|
+
.map(
|
|
16
|
+
(r) =>
|
|
17
|
+
`<tr><td>${escape(r.id)}</td><td>${escape(r.status)}</td><td>${escape(r.reason)}</td><td>${((r.milliseconds ?? 0) / 1000).toFixed(2)}s</td></tr>`,
|
|
18
|
+
)
|
|
19
|
+
.join('');
|
|
20
|
+
const browserLinks = report.results
|
|
21
|
+
.filter(
|
|
22
|
+
(result) =>
|
|
23
|
+
['e2e', 'visual'].includes(result.kind ?? result.id) &&
|
|
24
|
+
existsSync(join(directory, result.id, 'index.html')),
|
|
25
|
+
)
|
|
26
|
+
.map(
|
|
27
|
+
(result) =>
|
|
28
|
+
`<a href="${encodeURIComponent(result.id)}/index.html">${escape(result.id)} report</a>`,
|
|
29
|
+
)
|
|
30
|
+
.join(' · ');
|
|
31
|
+
writeFileSync(
|
|
32
|
+
join(directory, 'index.html'),
|
|
33
|
+
`<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>Repository checks</title><style>body{font:16px system-ui;max-width:1100px;margin:3rem auto;padding:0 1rem;color:#172033}table{width:100%;border-collapse:collapse}td,th{padding:.8rem;text-align:left;border-bottom:1px solid #ccd3df}code{overflow-wrap:anywhere}</style><h1>${report.complete ? 'Validation passed' : 'Validation incomplete or failed'}</h1><p>${escape(report.profile)} · ${escape(report.groups.join(', '))}</p><p>Commit: <code>${escape(report.commit)}</code></p><p>Tooling: ${escape(report.version)}</p><table><thead><tr><th>Check</th><th>Result</th><th>Details</th><th>Duration</th></tr></thead><tbody>${rows}</tbody></table><p><a href="summary.json" download>Download JSON</a>${browserLinks ? ` · ${browserLinks}` : ''}</p></html>`,
|
|
34
|
+
);
|
|
35
|
+
}
|