@dcaldeira/tooling 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -34
- package/checks/a11y.mjs +206 -206
- package/checks/architecture.mjs +137 -137
- package/checks/audit.mjs +121 -121
- package/checks/bundle.mjs +128 -126
- package/checks/dependencies.mjs +277 -277
- package/checks/duplication.mjs +135 -0
- package/checks/lint-scss.mjs +363 -349
- package/checks/package.mjs +30 -30
- package/checks/playwright-summary.mjs +53 -53
- package/checks/sass.mjs +67 -67
- package/checks/scan-secrets.mjs +58 -58
- package/checks/shared.mjs +86 -86
- package/checks/translations.mjs +55 -55
- package/checks/visual-compare.mjs +148 -148
- package/checks/visual-report-html.mjs +470 -470
- package/checks/visual-report.mjs +13 -13
- package/config.schema.json +999 -927
- package/configs/eslint.mjs +27 -27
- package/configs/knip.json +11 -11
- package/configs/playwright.mjs +15 -15
- package/configs/vitest.mjs +16 -16
- package/package.json +105 -105
- package/release/finalize.mjs +65 -65
- package/release/github.mjs +64 -64
- package/release/prepare.mjs +30 -30
- package/runner/browsers.mjs +73 -73
- package/runner/cache.mjs +121 -121
- package/runner/ci-plan.mjs +54 -54
- package/runner/cli.mjs +250 -248
- package/runner/config.mjs +148 -148
- package/runner/docs-install.mjs +93 -93
- package/runner/execute.mjs +298 -286
- package/runner/generate.mjs +122 -122
- package/runner/git.mjs +122 -122
- package/runner/graph.mjs +250 -243
- package/runner/insights.mjs +313 -313
- package/runner/provision.mjs +53 -53
- package/runner/repo-standard.mjs +104 -0
- package/runner/report.mjs +35 -35
- package/runner/serve-static.mjs +76 -76
- package/runner/setup.mjs +241 -237
- package/runner/summary.mjs +112 -112
- package/runner/watch.mjs +82 -82
- package/templates/ci.yml +25 -25
- package/templates/dependabot-merge.yml +13 -13
- package/templates/dependabot.yml +26 -26
- package/templates/pre-push +13 -13
- package/templates/release.yml +23 -23
package/checks/translations.mjs
CHANGED
|
@@ -1,55 +1,55 @@
|
|
|
1
|
-
import { readFileSync, readdirSync } from 'node:fs';
|
|
2
|
-
import { join } from 'node:path';
|
|
3
|
-
import { pathToFileURL } from 'node:url';
|
|
4
|
-
import { loadConfig, inside } from '../runner/config.mjs';
|
|
5
|
-
|
|
6
|
-
export const flatten = (value, prefix = '') =>
|
|
7
|
-
Object.entries(value).flatMap(([key, child]) => {
|
|
8
|
-
const path = prefix ? `${prefix}.${key}` : key;
|
|
9
|
-
return child && typeof child === 'object' ? flatten(child, path) : [[path, child]];
|
|
10
|
-
});
|
|
11
|
-
const placeholders = (value) =>
|
|
12
|
-
[...String(value).matchAll(/\{\{\s*(-?\s*[\w.]+)(?:\s*,[^}]*)?\s*\}\}/g)]
|
|
13
|
-
.map((m) => m[1].trim())
|
|
14
|
-
.sort()
|
|
15
|
-
.join(',');
|
|
16
|
-
|
|
17
|
-
export function validateCatalogues(catalogues, source, { pluralExceptions = [] } = {}) {
|
|
18
|
-
const errors = [];
|
|
19
|
-
const base = new Map(flatten(catalogues[source]));
|
|
20
|
-
for (const [locale, catalogue] of Object.entries(catalogues)) {
|
|
21
|
-
const entries = new Map(flatten(catalogue));
|
|
22
|
-
for (const [key, value] of entries) {
|
|
23
|
-
if (typeof value !== 'string' || !value.trim())
|
|
24
|
-
errors.push(`${locale}: ${key} must be a non-empty string`);
|
|
25
|
-
if (!base.has(key)) errors.push(`${locale}: extra key ${key}`);
|
|
26
|
-
else if (placeholders(value) !== placeholders(base.get(key)))
|
|
27
|
-
errors.push(`${locale}: ${key} interpolation differs from ${source}`);
|
|
28
|
-
const plural = key.match(/^(.*)_(zero|one|two|few|many|other)$/);
|
|
29
|
-
if (plural && !pluralExceptions.includes(plural[1]) && !entries.has(`${plural[1]}_other`))
|
|
30
|
-
errors.push(`${locale}: ${plural[1]}_other is missing`);
|
|
31
|
-
}
|
|
32
|
-
for (const key of base.keys())
|
|
33
|
-
if (!entries.has(key)) errors.push(`${locale}: missing key ${key}`);
|
|
34
|
-
}
|
|
35
|
-
return errors;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
39
|
-
const root = process.env.TOOLING_ROOT ?? process.cwd();
|
|
40
|
-
const { config } = loadConfig(root);
|
|
41
|
-
if (!config.translations?.directory || !config.translations?.source)
|
|
42
|
-
throw new Error('Configure translations.directory and translations.source');
|
|
43
|
-
const directory = inside(root, config.translations.directory);
|
|
44
|
-
const catalogues = Object.fromEntries(
|
|
45
|
-
readdirSync(directory)
|
|
46
|
-
.filter((name) => name.endsWith('.json'))
|
|
47
|
-
.map((name) => [name.slice(0, -5), JSON.parse(readFileSync(join(directory, name), 'utf8'))]),
|
|
48
|
-
);
|
|
49
|
-
if (!catalogues[config.translations.source]) throw new Error('Authoritative catalogue not found');
|
|
50
|
-
const errors = validateCatalogues(catalogues, config.translations.source, config.translations);
|
|
51
|
-
if (errors.length) {
|
|
52
|
-
console.error(errors.join('\n'));
|
|
53
|
-
process.exitCode = 1;
|
|
54
|
-
} else console.log(`Translation parity passed (${Object.keys(catalogues).length} catalogues)`);
|
|
55
|
-
}
|
|
1
|
+
import { readFileSync, readdirSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { pathToFileURL } from 'node:url';
|
|
4
|
+
import { loadConfig, inside } from '../runner/config.mjs';
|
|
5
|
+
|
|
6
|
+
export const flatten = (value, prefix = '') =>
|
|
7
|
+
Object.entries(value).flatMap(([key, child]) => {
|
|
8
|
+
const path = prefix ? `${prefix}.${key}` : key;
|
|
9
|
+
return child && typeof child === 'object' ? flatten(child, path) : [[path, child]];
|
|
10
|
+
});
|
|
11
|
+
const placeholders = (value) =>
|
|
12
|
+
[...String(value).matchAll(/\{\{\s*(-?\s*[\w.]+)(?:\s*,[^}]*)?\s*\}\}/g)]
|
|
13
|
+
.map((m) => m[1].trim())
|
|
14
|
+
.sort()
|
|
15
|
+
.join(',');
|
|
16
|
+
|
|
17
|
+
export function validateCatalogues(catalogues, source, { pluralExceptions = [] } = {}) {
|
|
18
|
+
const errors = [];
|
|
19
|
+
const base = new Map(flatten(catalogues[source]));
|
|
20
|
+
for (const [locale, catalogue] of Object.entries(catalogues)) {
|
|
21
|
+
const entries = new Map(flatten(catalogue));
|
|
22
|
+
for (const [key, value] of entries) {
|
|
23
|
+
if (typeof value !== 'string' || !value.trim())
|
|
24
|
+
errors.push(`${locale}: ${key} must be a non-empty string`);
|
|
25
|
+
if (!base.has(key)) errors.push(`${locale}: extra key ${key}`);
|
|
26
|
+
else if (placeholders(value) !== placeholders(base.get(key)))
|
|
27
|
+
errors.push(`${locale}: ${key} interpolation differs from ${source}`);
|
|
28
|
+
const plural = key.match(/^(.*)_(zero|one|two|few|many|other)$/);
|
|
29
|
+
if (plural && !pluralExceptions.includes(plural[1]) && !entries.has(`${plural[1]}_other`))
|
|
30
|
+
errors.push(`${locale}: ${plural[1]}_other is missing`);
|
|
31
|
+
}
|
|
32
|
+
for (const key of base.keys())
|
|
33
|
+
if (!entries.has(key)) errors.push(`${locale}: missing key ${key}`);
|
|
34
|
+
}
|
|
35
|
+
return errors;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
39
|
+
const root = process.env.TOOLING_ROOT ?? process.cwd();
|
|
40
|
+
const { config } = loadConfig(root);
|
|
41
|
+
if (!config.translations?.directory || !config.translations?.source)
|
|
42
|
+
throw new Error('Configure translations.directory and translations.source');
|
|
43
|
+
const directory = inside(root, config.translations.directory);
|
|
44
|
+
const catalogues = Object.fromEntries(
|
|
45
|
+
readdirSync(directory)
|
|
46
|
+
.filter((name) => name.endsWith('.json'))
|
|
47
|
+
.map((name) => [name.slice(0, -5), JSON.parse(readFileSync(join(directory, name), 'utf8'))]),
|
|
48
|
+
);
|
|
49
|
+
if (!catalogues[config.translations.source]) throw new Error('Authoritative catalogue not found');
|
|
50
|
+
const errors = validateCatalogues(catalogues, config.translations.source, config.translations);
|
|
51
|
+
if (errors.length) {
|
|
52
|
+
console.error(errors.join('\n'));
|
|
53
|
+
process.exitCode = 1;
|
|
54
|
+
} else console.log(`Translation parity passed (${Object.keys(catalogues).length} catalogues)`);
|
|
55
|
+
}
|
|
@@ -1,148 +1,148 @@
|
|
|
1
|
-
import { execFileSync } from 'node:child_process';
|
|
2
|
-
import {
|
|
3
|
-
existsSync,
|
|
4
|
-
mkdirSync,
|
|
5
|
-
readFileSync,
|
|
6
|
-
readdirSync,
|
|
7
|
-
rmSync,
|
|
8
|
-
writeFileSync,
|
|
9
|
-
} from 'node:fs';
|
|
10
|
-
import { basename, dirname, isAbsolute, join, relative } from 'node:path';
|
|
11
|
-
|
|
12
|
-
import { buildHtml } from './visual-report-html.mjs';
|
|
13
|
-
import { inside } from '../runner/config.mjs';
|
|
14
|
-
|
|
15
|
-
const [baseRef, outDir = 'visual-diff', snapshotDir, resultsDir = 'test-results'] = process.argv.slice(2);
|
|
16
|
-
|
|
17
|
-
if (!baseRef || !snapshotDir || process.argv.length > 6) {
|
|
18
|
-
process.stderr.write('Usage: visual-compare.mjs <baseRef> <outDir> <snapshotDir> [resultsDir]\n');
|
|
19
|
-
process.exit(1);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const root = process.cwd();
|
|
23
|
-
const output = inside(root, outDir);
|
|
24
|
-
const snapshots = inside(root, snapshotDir);
|
|
25
|
-
const results = inside(root, resultsDir);
|
|
26
|
-
const overlaps = (a, b) => {
|
|
27
|
-
const rel = relative(a, b).replaceAll(String.fromCharCode(92), '/');
|
|
28
|
-
return rel === '' || (rel !== '..' && !rel.startsWith('../') && !isAbsolute(rel));
|
|
29
|
-
};
|
|
30
|
-
if (output === root || overlaps(output, snapshots) || overlaps(snapshots, output) ||
|
|
31
|
-
overlaps(output, results) || overlaps(results, output))
|
|
32
|
-
throw new Error('Report output must be separate from source snapshots and test results');
|
|
33
|
-
|
|
34
|
-
const git = (...args) =>
|
|
35
|
-
execFileSync('git', args, { maxBuffer: 256 * 1024 * 1024 });
|
|
36
|
-
|
|
37
|
-
// Paths relative to the snapshot directory, e.g. `desktop/blocks-table--default--light.png`.
|
|
38
|
-
const listBase = () =>
|
|
39
|
-
git('ls-tree', '-r', '--name-only', baseRef, '--', `./${snapshotDir}`)
|
|
40
|
-
.toString()
|
|
41
|
-
.split('\n')
|
|
42
|
-
.map((file) => file.trim())
|
|
43
|
-
.filter((file) => file.endsWith('.png'))
|
|
44
|
-
.map((file) =>
|
|
45
|
-
file.slice(file.indexOf(snapshotDir) + snapshotDir.length + 1)
|
|
46
|
-
);
|
|
47
|
-
|
|
48
|
-
const listHead = (dir = '') => {
|
|
49
|
-
const root = join(snapshotDir, dir);
|
|
50
|
-
if (!existsSync(root)) return [];
|
|
51
|
-
return readdirSync(root, { withFileTypes: true }).flatMap((entry) => {
|
|
52
|
-
const path = dir ? `${dir}/${entry.name}` : entry.name;
|
|
53
|
-
if (entry.isDirectory()) return listHead(path);
|
|
54
|
-
return entry.name.endsWith('.png') ? [path] : [];
|
|
55
|
-
});
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
const baseFiles = new Set(listBase());
|
|
59
|
-
const headFiles = new Set(listHead());
|
|
60
|
-
|
|
61
|
-
const readBase = (file) => git('show', `${baseRef}:./${snapshotDir}/${file}`);
|
|
62
|
-
const readHead = (file) => readFileSync(join(snapshotDir, file));
|
|
63
|
-
|
|
64
|
-
const changes = [];
|
|
65
|
-
|
|
66
|
-
for (const file of [...new Set([...baseFiles, ...headFiles])].sort()) {
|
|
67
|
-
const inBase = baseFiles.has(file);
|
|
68
|
-
const inHead = headFiles.has(file);
|
|
69
|
-
|
|
70
|
-
if (inBase && inHead) {
|
|
71
|
-
if (!readBase(file).equals(readHead(file)))
|
|
72
|
-
changes.push({ file, status: 'changed' });
|
|
73
|
-
} else {
|
|
74
|
-
changes.push({ file, status: inHead ? 'added' : 'removed' });
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// Failing tests leave `<test folder>-<project>/<name>-actual.png` next to the baseline they missed.
|
|
79
|
-
const listFailures = (dir = resultsDir) => {
|
|
80
|
-
if (!existsSync(dir)) return [];
|
|
81
|
-
return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
|
|
82
|
-
const path = join(dir, entry.name);
|
|
83
|
-
if (entry.isDirectory()) return listFailures(path);
|
|
84
|
-
return entry.name.endsWith('-actual.png') ? [path] : [];
|
|
85
|
-
});
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
const known = new Set(changes.map(({ file }) => file));
|
|
89
|
-
const projects = [...new Set([...headFiles].map((file) => file.split('/')[0]))]
|
|
90
|
-
.sort((a, b) => b.length - a.length);
|
|
91
|
-
|
|
92
|
-
for (const actual of listFailures()) {
|
|
93
|
-
const folder = basename(dirname(actual));
|
|
94
|
-
const project = projects.find((name) => folder.endsWith('-' + name));
|
|
95
|
-
if (!project) continue;
|
|
96
|
-
const file = `${project}/${basename(actual).replace(/-actual\.png$/, '.png')}`;
|
|
97
|
-
if (known.has(file) || !headFiles.has(file)) continue;
|
|
98
|
-
changes.push({ file, status: 'changed', failure: actual });
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
rmSync(outDir, { recursive: true, force: true });
|
|
102
|
-
|
|
103
|
-
if (changes.length === 0) {
|
|
104
|
-
process.stdout.write('No visual snapshot changes.\n');
|
|
105
|
-
process.exit(0);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const counts = { added: 0, changed: 0, removed: 0 };
|
|
109
|
-
for (const { status } of changes) counts[status] += 1;
|
|
110
|
-
|
|
111
|
-
mkdirSync(outDir, { recursive: true });
|
|
112
|
-
|
|
113
|
-
// A failing test compares the committed baseline with the image the test produced.
|
|
114
|
-
const beforeOf = ({ file, status, failure }) => {
|
|
115
|
-
if (failure) return readHead(file);
|
|
116
|
-
return status === 'added' ? null : readBase(file);
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
const afterOf = ({ file, status, failure }) => {
|
|
120
|
-
if (failure) return readFileSync(failure);
|
|
121
|
-
return status === 'removed' ? null : readHead(file);
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
const entries = changes.map((change) => ({
|
|
125
|
-
file: change.file,
|
|
126
|
-
status: change.status,
|
|
127
|
-
failure: Boolean(change.failure),
|
|
128
|
-
before: beforeOf(change),
|
|
129
|
-
after: afterOf(change),
|
|
130
|
-
}));
|
|
131
|
-
writeFileSync(
|
|
132
|
-
join(outDir, 'index.html'),
|
|
133
|
-
buildHtml({ entries, counts, baseRef })
|
|
134
|
-
);
|
|
135
|
-
|
|
136
|
-
const summary = `${counts.added} new (no previous snapshot), ${counts.changed} changed, ${counts.removed} removed.`;
|
|
137
|
-
|
|
138
|
-
// Short PR comment: counts only. Images live in the interactive report, not in the comment.
|
|
139
|
-
writeFileSync(
|
|
140
|
-
join(outDir, 'comment.md'),
|
|
141
|
-
['<!-- visual-snapshots -->', '## Visual snapshots', '', summary, ''].join(
|
|
142
|
-
'\n'
|
|
143
|
-
)
|
|
144
|
-
);
|
|
145
|
-
|
|
146
|
-
process.stdout.write(
|
|
147
|
-
`${counts.added} new, ${counts.changed} changed, ${counts.removed} removed. Report: ${join(outDir, 'index.html')}\n`
|
|
148
|
-
);
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import {
|
|
3
|
+
existsSync,
|
|
4
|
+
mkdirSync,
|
|
5
|
+
readFileSync,
|
|
6
|
+
readdirSync,
|
|
7
|
+
rmSync,
|
|
8
|
+
writeFileSync,
|
|
9
|
+
} from 'node:fs';
|
|
10
|
+
import { basename, dirname, isAbsolute, join, relative } from 'node:path';
|
|
11
|
+
|
|
12
|
+
import { buildHtml } from './visual-report-html.mjs';
|
|
13
|
+
import { inside } from '../runner/config.mjs';
|
|
14
|
+
|
|
15
|
+
const [baseRef, outDir = 'visual-diff', snapshotDir, resultsDir = 'test-results'] = process.argv.slice(2);
|
|
16
|
+
|
|
17
|
+
if (!baseRef || !snapshotDir || process.argv.length > 6) {
|
|
18
|
+
process.stderr.write('Usage: visual-compare.mjs <baseRef> <outDir> <snapshotDir> [resultsDir]\n');
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const root = process.cwd();
|
|
23
|
+
const output = inside(root, outDir);
|
|
24
|
+
const snapshots = inside(root, snapshotDir);
|
|
25
|
+
const results = inside(root, resultsDir);
|
|
26
|
+
const overlaps = (a, b) => {
|
|
27
|
+
const rel = relative(a, b).replaceAll(String.fromCharCode(92), '/');
|
|
28
|
+
return rel === '' || (rel !== '..' && !rel.startsWith('../') && !isAbsolute(rel));
|
|
29
|
+
};
|
|
30
|
+
if (output === root || overlaps(output, snapshots) || overlaps(snapshots, output) ||
|
|
31
|
+
overlaps(output, results) || overlaps(results, output))
|
|
32
|
+
throw new Error('Report output must be separate from source snapshots and test results');
|
|
33
|
+
|
|
34
|
+
const git = (...args) =>
|
|
35
|
+
execFileSync('git', args, { maxBuffer: 256 * 1024 * 1024 });
|
|
36
|
+
|
|
37
|
+
// Paths relative to the snapshot directory, e.g. `desktop/blocks-table--default--light.png`.
|
|
38
|
+
const listBase = () =>
|
|
39
|
+
git('ls-tree', '-r', '--name-only', baseRef, '--', `./${snapshotDir}`)
|
|
40
|
+
.toString()
|
|
41
|
+
.split('\n')
|
|
42
|
+
.map((file) => file.trim())
|
|
43
|
+
.filter((file) => file.endsWith('.png'))
|
|
44
|
+
.map((file) =>
|
|
45
|
+
file.slice(file.indexOf(snapshotDir) + snapshotDir.length + 1)
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
const listHead = (dir = '') => {
|
|
49
|
+
const root = join(snapshotDir, dir);
|
|
50
|
+
if (!existsSync(root)) return [];
|
|
51
|
+
return readdirSync(root, { withFileTypes: true }).flatMap((entry) => {
|
|
52
|
+
const path = dir ? `${dir}/${entry.name}` : entry.name;
|
|
53
|
+
if (entry.isDirectory()) return listHead(path);
|
|
54
|
+
return entry.name.endsWith('.png') ? [path] : [];
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const baseFiles = new Set(listBase());
|
|
59
|
+
const headFiles = new Set(listHead());
|
|
60
|
+
|
|
61
|
+
const readBase = (file) => git('show', `${baseRef}:./${snapshotDir}/${file}`);
|
|
62
|
+
const readHead = (file) => readFileSync(join(snapshotDir, file));
|
|
63
|
+
|
|
64
|
+
const changes = [];
|
|
65
|
+
|
|
66
|
+
for (const file of [...new Set([...baseFiles, ...headFiles])].sort()) {
|
|
67
|
+
const inBase = baseFiles.has(file);
|
|
68
|
+
const inHead = headFiles.has(file);
|
|
69
|
+
|
|
70
|
+
if (inBase && inHead) {
|
|
71
|
+
if (!readBase(file).equals(readHead(file)))
|
|
72
|
+
changes.push({ file, status: 'changed' });
|
|
73
|
+
} else {
|
|
74
|
+
changes.push({ file, status: inHead ? 'added' : 'removed' });
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Failing tests leave `<test folder>-<project>/<name>-actual.png` next to the baseline they missed.
|
|
79
|
+
const listFailures = (dir = resultsDir) => {
|
|
80
|
+
if (!existsSync(dir)) return [];
|
|
81
|
+
return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
|
|
82
|
+
const path = join(dir, entry.name);
|
|
83
|
+
if (entry.isDirectory()) return listFailures(path);
|
|
84
|
+
return entry.name.endsWith('-actual.png') ? [path] : [];
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const known = new Set(changes.map(({ file }) => file));
|
|
89
|
+
const projects = [...new Set([...headFiles].map((file) => file.split('/')[0]))]
|
|
90
|
+
.sort((a, b) => b.length - a.length);
|
|
91
|
+
|
|
92
|
+
for (const actual of listFailures()) {
|
|
93
|
+
const folder = basename(dirname(actual));
|
|
94
|
+
const project = projects.find((name) => folder.endsWith('-' + name));
|
|
95
|
+
if (!project) continue;
|
|
96
|
+
const file = `${project}/${basename(actual).replace(/-actual\.png$/, '.png')}`;
|
|
97
|
+
if (known.has(file) || !headFiles.has(file)) continue;
|
|
98
|
+
changes.push({ file, status: 'changed', failure: actual });
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
rmSync(outDir, { recursive: true, force: true });
|
|
102
|
+
|
|
103
|
+
if (changes.length === 0) {
|
|
104
|
+
process.stdout.write('No visual snapshot changes.\n');
|
|
105
|
+
process.exit(0);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const counts = { added: 0, changed: 0, removed: 0 };
|
|
109
|
+
for (const { status } of changes) counts[status] += 1;
|
|
110
|
+
|
|
111
|
+
mkdirSync(outDir, { recursive: true });
|
|
112
|
+
|
|
113
|
+
// A failing test compares the committed baseline with the image the test produced.
|
|
114
|
+
const beforeOf = ({ file, status, failure }) => {
|
|
115
|
+
if (failure) return readHead(file);
|
|
116
|
+
return status === 'added' ? null : readBase(file);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const afterOf = ({ file, status, failure }) => {
|
|
120
|
+
if (failure) return readFileSync(failure);
|
|
121
|
+
return status === 'removed' ? null : readHead(file);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const entries = changes.map((change) => ({
|
|
125
|
+
file: change.file,
|
|
126
|
+
status: change.status,
|
|
127
|
+
failure: Boolean(change.failure),
|
|
128
|
+
before: beforeOf(change),
|
|
129
|
+
after: afterOf(change),
|
|
130
|
+
}));
|
|
131
|
+
writeFileSync(
|
|
132
|
+
join(outDir, 'index.html'),
|
|
133
|
+
buildHtml({ entries, counts, baseRef })
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
const summary = `${counts.added} new (no previous snapshot), ${counts.changed} changed, ${counts.removed} removed.`;
|
|
137
|
+
|
|
138
|
+
// Short PR comment: counts only. Images live in the interactive report, not in the comment.
|
|
139
|
+
writeFileSync(
|
|
140
|
+
join(outDir, 'comment.md'),
|
|
141
|
+
['<!-- visual-snapshots -->', '## Visual snapshots', '', summary, ''].join(
|
|
142
|
+
'\n'
|
|
143
|
+
)
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
process.stdout.write(
|
|
147
|
+
`${counts.added} new, ${counts.changed} changed, ${counts.removed} removed. Report: ${join(outDir, 'index.html')}\n`
|
|
148
|
+
);
|