@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
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Copy/paste detection with jscpd. Scans the configured paths; when given files (dev profile),
|
|
3
|
+
// fails only on clones that involve one of them, so existing duplication elsewhere does not
|
|
4
|
+
// block unrelated work.
|
|
5
|
+
import { spawnSync } from 'node:child_process';
|
|
6
|
+
import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
|
|
7
|
+
import { createRequire } from 'node:module';
|
|
8
|
+
import { tmpdir } from 'node:os';
|
|
9
|
+
import { dirname, join, relative, resolve } from 'node:path';
|
|
10
|
+
import { pathToFileURL } from 'node:url';
|
|
11
|
+
import { loadConfig } from '../runner/config.mjs';
|
|
12
|
+
|
|
13
|
+
const FORMATS = 'typescript,tsx,javascript,jsx,scss,css';
|
|
14
|
+
const DEFAULT_IGNORE = [
|
|
15
|
+
'**/*.test.*',
|
|
16
|
+
'**/*.spec.*',
|
|
17
|
+
'**/__mocks__/**',
|
|
18
|
+
'**/*.gen.*',
|
|
19
|
+
'**/dist/**',
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
const posix = (path) => path.split('\\').join('/');
|
|
23
|
+
|
|
24
|
+
export function jscpdBin(repo) {
|
|
25
|
+
try {
|
|
26
|
+
const require = createRequire(join(repo, 'package.json'));
|
|
27
|
+
const manifestPath = require.resolve('jscpd/package.json');
|
|
28
|
+
const { bin } = JSON.parse(readFileSync(manifestPath, 'utf8'));
|
|
29
|
+
const rel = typeof bin === 'string' ? bin : bin?.jscpd;
|
|
30
|
+
return rel ? join(dirname(manifestPath), rel) : undefined;
|
|
31
|
+
} catch {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Clones from a jscpd JSON report, optionally only those touching `changed` (repo-relative). */
|
|
37
|
+
export function relevantClones(report, repo, changed = []) {
|
|
38
|
+
const wanted = new Set(changed.map(posix));
|
|
39
|
+
const name = (file) => posix(relative(repo, resolve(repo, file.name)));
|
|
40
|
+
return (report.duplicates ?? [])
|
|
41
|
+
.map((clone) => ({
|
|
42
|
+
first: {
|
|
43
|
+
file: name(clone.firstFile),
|
|
44
|
+
start: clone.firstFile.start,
|
|
45
|
+
end: clone.firstFile.end,
|
|
46
|
+
},
|
|
47
|
+
second: {
|
|
48
|
+
file: name(clone.secondFile),
|
|
49
|
+
start: clone.secondFile.start,
|
|
50
|
+
end: clone.secondFile.end,
|
|
51
|
+
},
|
|
52
|
+
lines: clone.lines,
|
|
53
|
+
}))
|
|
54
|
+
.filter(
|
|
55
|
+
(clone) => !wanted.size || wanted.has(clone.first.file) || wanted.has(clone.second.file),
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function main() {
|
|
60
|
+
const repo = process.env.TOOLING_ROOT ?? process.cwd();
|
|
61
|
+
const { config } = loadConfig(repo);
|
|
62
|
+
const options = config.duplication ?? {};
|
|
63
|
+
const bin = jscpdBin(repo);
|
|
64
|
+
if (!bin) {
|
|
65
|
+
console.error('duplication: install jscpd as a devDependency');
|
|
66
|
+
return 1;
|
|
67
|
+
}
|
|
68
|
+
// Changed files: dev passes them as arguments, push/ci through a list file. Neither (an `--all`
|
|
69
|
+
// run) means there is no change to judge.
|
|
70
|
+
const listed = process.env.TOOLING_CHANGED_FILES
|
|
71
|
+
? readFileSync(process.env.TOOLING_CHANGED_FILES, 'utf8').split(/\r?\n/).filter(Boolean)
|
|
72
|
+
: undefined;
|
|
73
|
+
const argFiles = process.argv.slice(2).filter((arg) => !arg.startsWith('--'));
|
|
74
|
+
const given = argFiles.length ? argFiles : listed;
|
|
75
|
+
const changed = (given ?? []).map((file) => posix(relative(repo, resolve(repo, file))));
|
|
76
|
+
// scope "changed" (default): only clones touching a changed file fail; with no change list,
|
|
77
|
+
// clones are reported without failing. scope "all": every clone fails (for repos with none).
|
|
78
|
+
const scopeAll = options.scope === 'all';
|
|
79
|
+
const reportOnly = !scopeAll && given === undefined;
|
|
80
|
+
if (!scopeAll && given !== undefined && !changed.length) return 0;
|
|
81
|
+
const configured = options.paths ?? ['src'];
|
|
82
|
+
const paths = configured.filter((path) => existsSync(join(repo, path)));
|
|
83
|
+
if (!paths.length) {
|
|
84
|
+
console.error(`duplication: none of ${configured.join(', ')} exist; set duplication.paths`);
|
|
85
|
+
return 1;
|
|
86
|
+
}
|
|
87
|
+
const output = mkdtempSync(join(tmpdir(), 'tooling-jscpd-'));
|
|
88
|
+
try {
|
|
89
|
+
const result = spawnSync(
|
|
90
|
+
process.execPath,
|
|
91
|
+
[
|
|
92
|
+
bin,
|
|
93
|
+
...paths,
|
|
94
|
+
'--min-tokens',
|
|
95
|
+
String(options.minTokens ?? 70),
|
|
96
|
+
'--format',
|
|
97
|
+
FORMATS,
|
|
98
|
+
'--ignore',
|
|
99
|
+
[...DEFAULT_IGNORE, ...(options.ignore ?? [])].join(','),
|
|
100
|
+
'--reporters',
|
|
101
|
+
'json',
|
|
102
|
+
'--output',
|
|
103
|
+
output,
|
|
104
|
+
'--silent',
|
|
105
|
+
'--gitignore',
|
|
106
|
+
],
|
|
107
|
+
{ cwd: repo, encoding: 'utf8' },
|
|
108
|
+
);
|
|
109
|
+
const reportPath = join(output, 'jscpd-report.json');
|
|
110
|
+
if (!existsSync(reportPath)) {
|
|
111
|
+
console.error(result.stderr || result.stdout || 'duplication: jscpd produced no report');
|
|
112
|
+
return 1;
|
|
113
|
+
}
|
|
114
|
+
const report = JSON.parse(readFileSync(reportPath, 'utf8'));
|
|
115
|
+
const clones = relevantClones(report, repo, scopeAll ? [] : changed);
|
|
116
|
+
for (const { first, second, lines } of clones)
|
|
117
|
+
console.log(
|
|
118
|
+
`${first.file}:${first.start}-${first.end} duplicates ${second.file}:${second.start}-${second.end} (${lines} lines). Move it to one shared module.`,
|
|
119
|
+
);
|
|
120
|
+
if (!clones.length) return 0;
|
|
121
|
+
if (reportOnly) {
|
|
122
|
+
console.log(
|
|
123
|
+
`duplication: ${clones.length} existing clone(s), reported only (no changed files to judge; set duplication.scope "all" to fail on them)`,
|
|
124
|
+
);
|
|
125
|
+
return 0;
|
|
126
|
+
}
|
|
127
|
+
console.log(`duplication: ${clones.length} clone(s)`);
|
|
128
|
+
return 1;
|
|
129
|
+
} finally {
|
|
130
|
+
rmSync(output, { recursive: true, force: true });
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href)
|
|
135
|
+
process.exitCode = main();
|