@dcaldeira/tooling 0.1.0 → 0.2.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/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 +126 -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 +977 -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/runner/setup.mjs
CHANGED
|
@@ -1,237 +1,241 @@
|
|
|
1
|
-
import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
-
import { dirname, join } from 'node:path';
|
|
3
|
-
import { inside, loadConfig, validateConfig } from './config.mjs';
|
|
4
|
-
import { builtins, plan } from './graph.mjs';
|
|
5
|
-
import { executable, packageManifest } from './execute.mjs';
|
|
6
|
-
import { git } from './git.mjs';
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
if (
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
'check:
|
|
59
|
-
'
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
files.set('.github/workflows/
|
|
73
|
-
files.set('.github/dependabot.yml', template('dependabot.yml'));
|
|
74
|
-
files.set('.
|
|
75
|
-
files.set(
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
'prettier.config.
|
|
85
|
-
'prettier.config.
|
|
86
|
-
'.
|
|
87
|
-
'.prettierrc
|
|
88
|
-
'.prettierrc.
|
|
89
|
-
'.prettierrc.
|
|
90
|
-
'.prettierrc.
|
|
91
|
-
'.prettierrc.
|
|
92
|
-
'.prettierrc.
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
.
|
|
103
|
-
)
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
'
|
|
113
|
-
'.
|
|
114
|
-
'
|
|
115
|
-
'
|
|
116
|
-
'
|
|
117
|
-
'
|
|
118
|
-
'
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
pkg.
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
const
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
command[
|
|
181
|
-
|
|
182
|
-
!existsSync(command[1])
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
1
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { inside, loadConfig, validateConfig } from './config.mjs';
|
|
4
|
+
import { builtins, plan } from './graph.mjs';
|
|
5
|
+
import { executable, packageManifest } from './execute.mjs';
|
|
6
|
+
import { git } from './git.mjs';
|
|
7
|
+
import { repoStandardFindings } from './repo-standard.mjs';
|
|
8
|
+
|
|
9
|
+
const manifest = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
|
|
10
|
+
const readJSON = (path) => JSON.parse(readFileSync(path, 'utf8'));
|
|
11
|
+
const json = (value) => JSON.stringify(value, null, 2) + '\n';
|
|
12
|
+
|
|
13
|
+
export function starterConfig({ preset = 'react-vite', app = 'frontend', scripts = {} } = {}) {
|
|
14
|
+
if (!['react-vite', 'workspace'].includes(preset))
|
|
15
|
+
throw new Error('Preset must be react-vite or workspace');
|
|
16
|
+
if (!/^[\w-]+(?:\/[\w-]+)*$/.test(app))
|
|
17
|
+
throw new Error('App must be a repository-relative directory');
|
|
18
|
+
const workspace = preset === 'workspace';
|
|
19
|
+
const cwd = workspace ? app : '.';
|
|
20
|
+
const enabled = ['format', 'lint', 'types', 'unit', 'build'];
|
|
21
|
+
const checks = Object.fromEntries(
|
|
22
|
+
Object.keys(builtins).map((id) => [id, enabled.includes(id) ? {} : false]),
|
|
23
|
+
);
|
|
24
|
+
const adapters = { lint: 'lint', types: 'typecheck', unit: 'test:coverage', build: 'build' };
|
|
25
|
+
for (const id of ['lint', 'types', 'unit', 'build']) {
|
|
26
|
+
checks[id].cwd = cwd;
|
|
27
|
+
if (workspace) checks[id].workspaces = ['app'];
|
|
28
|
+
if (scripts[adapters[id]]) checks[id].command = ['npm', 'run', adapters[id]];
|
|
29
|
+
}
|
|
30
|
+
const config = {
|
|
31
|
+
$schema: './node_modules/@dcaldeira/tooling/config.schema.json',
|
|
32
|
+
schemaVersion: 1,
|
|
33
|
+
execution: { concurrency: 2 },
|
|
34
|
+
...(workspace
|
|
35
|
+
? {
|
|
36
|
+
paths: { app: [`${app}/src/**`, 'shared/src/**'] },
|
|
37
|
+
workspaces: {
|
|
38
|
+
app: { paths: [`${app}/**`], dependsOn: ['shared'] },
|
|
39
|
+
shared: { paths: ['shared/**'] },
|
|
40
|
+
},
|
|
41
|
+
}
|
|
42
|
+
: {}),
|
|
43
|
+
checks,
|
|
44
|
+
};
|
|
45
|
+
validateConfig(config);
|
|
46
|
+
return config;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function initialize(root, { preset = 'react-vite', app = 'frontend', toolingSha } = {}) {
|
|
50
|
+
if (toolingSha && !/^[a-f\d]{40}$/.test(toolingSha))
|
|
51
|
+
throw new Error('--tooling-sha must be a full 40-character commit SHA');
|
|
52
|
+
const packagePath = join(root, 'package.json');
|
|
53
|
+
const pkg = readJSON(packagePath);
|
|
54
|
+
const appPackage =
|
|
55
|
+
preset === 'workspace' ? readJSON(join(inside(root, app), 'package.json')) : pkg;
|
|
56
|
+
const config = starterConfig({ preset, app, scripts: appPackage.scripts });
|
|
57
|
+
const aliases = {
|
|
58
|
+
'check:dev': 'tooling --profile=dev --changed',
|
|
59
|
+
'check:push': 'tooling --profile=push --changed --bail',
|
|
60
|
+
'ci:local': 'tooling --profile=ci --all',
|
|
61
|
+
};
|
|
62
|
+
for (const [key, value] of Object.entries(aliases)) {
|
|
63
|
+
if (pkg.scripts?.[key] && pkg.scripts[key] !== value)
|
|
64
|
+
throw new Error(`Existing script ${key}; adapt it before initializing`);
|
|
65
|
+
}
|
|
66
|
+
const files = new Map([['tooling.config.json', json(config)]]);
|
|
67
|
+
const template = (name) =>
|
|
68
|
+
readFileSync(new URL(`../templates/${name}`, import.meta.url), 'utf8').replaceAll(
|
|
69
|
+
'REPLACE_WITH_TOOLING_COMMIT_SHA',
|
|
70
|
+
toolingSha ?? 'REPLACE_WITH_TOOLING_COMMIT_SHA',
|
|
71
|
+
);
|
|
72
|
+
files.set('.github/workflows/ci.yml', template('ci.yml'));
|
|
73
|
+
files.set('.github/workflows/dependabot-merge.yml', template('dependabot-merge.yml'));
|
|
74
|
+
files.set('.github/dependabot.yml', template('dependabot.yml'));
|
|
75
|
+
files.set('.githooks/pre-push', template('pre-push'));
|
|
76
|
+
files.set(
|
|
77
|
+
'prettier.config.mjs',
|
|
78
|
+
"export { default } from '@dcaldeira/tooling/configs/prettier.mjs';\n",
|
|
79
|
+
);
|
|
80
|
+
files.set('.nvmrc', '22.23.1\n');
|
|
81
|
+
// Preserve existing formatter configuration and Node version files.
|
|
82
|
+
if (
|
|
83
|
+
[
|
|
84
|
+
'prettier.config.mjs',
|
|
85
|
+
'prettier.config.js',
|
|
86
|
+
'prettier.config.cjs',
|
|
87
|
+
'.prettierrc',
|
|
88
|
+
'.prettierrc.json',
|
|
89
|
+
'.prettierrc.yml',
|
|
90
|
+
'.prettierrc.yaml',
|
|
91
|
+
'.prettierrc.js',
|
|
92
|
+
'.prettierrc.cjs',
|
|
93
|
+
'.prettierrc.toml',
|
|
94
|
+
].some((name) => existsSync(join(root, name))) ||
|
|
95
|
+
pkg.prettier
|
|
96
|
+
)
|
|
97
|
+
files.delete('prettier.config.mjs');
|
|
98
|
+
if (files.has('prettier.config.mjs'))
|
|
99
|
+
files.set(
|
|
100
|
+
'.github/workflows/ci.yml',
|
|
101
|
+
files
|
|
102
|
+
.get('.github/workflows/ci.yml')
|
|
103
|
+
.replace(' with:\n', ' with:\n lightweight-docs: true\n'),
|
|
104
|
+
);
|
|
105
|
+
if (existsSync(join(root, '.nvmrc'))) files.delete('.nvmrc');
|
|
106
|
+
for (const path of files.keys())
|
|
107
|
+
if (existsSync(join(root, path))) throw new Error(`Refusing to overwrite ${path}`);
|
|
108
|
+
for (const name of ['.gitignore', '.prettierignore']) {
|
|
109
|
+
const path = join(root, name);
|
|
110
|
+
let text = existsSync(path) ? readFileSync(path, 'utf8') : '';
|
|
111
|
+
for (const entry of [
|
|
112
|
+
'node_modules/',
|
|
113
|
+
'debug.log',
|
|
114
|
+
'.tooling/',
|
|
115
|
+
'dist/',
|
|
116
|
+
'coverage/',
|
|
117
|
+
'playwright-report/',
|
|
118
|
+
'test-results/',
|
|
119
|
+
'*.tsbuildinfo',
|
|
120
|
+
]) {
|
|
121
|
+
if (!text.split(/\r?\n/).includes(entry))
|
|
122
|
+
text += `${text && !text.endsWith('\n') ? '\n' : ''}${entry}\n`;
|
|
123
|
+
}
|
|
124
|
+
files.set(name, text);
|
|
125
|
+
}
|
|
126
|
+
pkg.scripts = { ...pkg.scripts, ...aliases };
|
|
127
|
+
pkg.devDependencies = { ...pkg.devDependencies, [manifest.name]: manifest.version };
|
|
128
|
+
files.set('package.json', json(pkg));
|
|
129
|
+
for (const [path, text] of files) {
|
|
130
|
+
const target = join(root, path);
|
|
131
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
132
|
+
writeFileSync(target, text);
|
|
133
|
+
}
|
|
134
|
+
chmodSync(join(root, '.githooks/pre-push'), 0o755);
|
|
135
|
+
return [...files.keys()];
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function diagnose(root, configPath) {
|
|
139
|
+
const findings = [];
|
|
140
|
+
const add = (status, message) => findings.push({ status, message });
|
|
141
|
+
let config;
|
|
142
|
+
try {
|
|
143
|
+
config = loadConfig(root, configPath).config;
|
|
144
|
+
add('passed', 'Configuration is valid');
|
|
145
|
+
} catch (error) {
|
|
146
|
+
add('failed', error.message);
|
|
147
|
+
return findings;
|
|
148
|
+
}
|
|
149
|
+
let selection;
|
|
150
|
+
try {
|
|
151
|
+
selection = plan(config, { profile: 'ci' });
|
|
152
|
+
add('passed', 'Check dependencies form a valid graph');
|
|
153
|
+
} catch (error) {
|
|
154
|
+
add('failed', error.message);
|
|
155
|
+
return findings;
|
|
156
|
+
}
|
|
157
|
+
if (!existsSync(join(root, 'package-lock.json')))
|
|
158
|
+
add('failed', 'Run npm install and commit package-lock.json');
|
|
159
|
+
if (!existsSync(join(root, '.nvmrc'))) add('failed', 'Add .nvmrc for the shared workflow');
|
|
160
|
+
try {
|
|
161
|
+
git(root, 'rev-parse', '--verify', 'HEAD');
|
|
162
|
+
if (git(root, 'config', '--get', 'core.hooksPath') !== '.githooks')
|
|
163
|
+
add('failed', 'Enable the hook: git config core.hooksPath .githooks');
|
|
164
|
+
} catch {
|
|
165
|
+
add('failed', 'Initialize Git, commit the project and enable .githooks');
|
|
166
|
+
}
|
|
167
|
+
for (const check of selection.checks) {
|
|
168
|
+
const cwd = inside(root, check.cwd ?? '.');
|
|
169
|
+
if (!existsSync(cwd)) {
|
|
170
|
+
add('failed', `${check.id}: missing directory ${check.cwd}`);
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
const commands = [check.command, check.devCommand, check.devAllCommand].filter(Array.isArray);
|
|
174
|
+
const tools = new Set([...commands.map((command) => command[0]), ...(check.tools ?? [])]);
|
|
175
|
+
for (const tool of tools)
|
|
176
|
+
if (!executable(cwd, tool))
|
|
177
|
+
add('failed', `${check.id}: missing ${tool} in ${check.cwd ?? '.'}`);
|
|
178
|
+
for (const command of commands) {
|
|
179
|
+
if (
|
|
180
|
+
command[0] === 'node' &&
|
|
181
|
+
command[1]?.endsWith('.mjs') &&
|
|
182
|
+
!existsSync(join(cwd, command[1])) &&
|
|
183
|
+
!existsSync(command[1])
|
|
184
|
+
)
|
|
185
|
+
add('failed', `${check.id}: missing script ${command[1]}`);
|
|
186
|
+
if (command[0] === 'npm' && command[1] === 'run') {
|
|
187
|
+
const pkgPath = join(cwd, 'package.json');
|
|
188
|
+
if (!existsSync(pkgPath) || !readJSON(pkgPath).scripts?.[command[2]])
|
|
189
|
+
add('failed', `${check.id}: missing npm script ${command[2]}`);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (check.platforms && !check.platforms.includes(process.platform))
|
|
193
|
+
add('notice', `${check.id}: use ${check.platforms.join('/')} for canonical validation`);
|
|
194
|
+
if ((check.kind ?? check.id) === 'styles' && !packageManifest(root, 'postcss-scss'))
|
|
195
|
+
add('failed', 'styles: install postcss-scss and its postcss peer at the consumer root');
|
|
196
|
+
if (
|
|
197
|
+
['architecture', 'dependency-consistency'].includes(check.kind ?? check.id) &&
|
|
198
|
+
!packageManifest(cwd, 'typescript')
|
|
199
|
+
)
|
|
200
|
+
add('failed', `${check.id}: install TypeScript for import analysis`);
|
|
201
|
+
if ((check.kind ?? check.id) === 'a11y') {
|
|
202
|
+
if (!config.a11y?.routes?.length)
|
|
203
|
+
add('failed', `${check.id}: configure a11y.baseURL and routes`);
|
|
204
|
+
for (const name of ['@playwright/test', '@axe-core/playwright'])
|
|
205
|
+
if (!packageManifest(cwd, name)) add('failed', `${check.id}: install ${name}`);
|
|
206
|
+
}
|
|
207
|
+
if (
|
|
208
|
+
(check.kind ?? check.id) === 'unit' &&
|
|
209
|
+
commands.some((command) => command.includes('--coverage')) &&
|
|
210
|
+
!packageManifest(cwd, '@vitest/coverage-v8')
|
|
211
|
+
)
|
|
212
|
+
add('failed', 'unit: install @vitest/coverage-v8 matching your Vitest version');
|
|
213
|
+
}
|
|
214
|
+
if (config.repoStandard) findings.push(...repoStandardFindings(root));
|
|
215
|
+
for (const name of ['ci.yml', 'dependabot-merge.yml']) {
|
|
216
|
+
const path = join(root, '.github/workflows', name);
|
|
217
|
+
if (!existsSync(path)) add('failed', `Missing .github/workflows/${name}`);
|
|
218
|
+
else if (readFileSync(path, 'utf8').includes('REPLACE_WITH_TOOLING_COMMIT_SHA'))
|
|
219
|
+
add('failed', `Pin .github/workflows/${name} to the tooling commit SHA`);
|
|
220
|
+
}
|
|
221
|
+
return findings;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export async function setupCommand(command, root, options) {
|
|
225
|
+
if (command === 'init') {
|
|
226
|
+
const paths = initialize(root, {
|
|
227
|
+
preset: options.preset,
|
|
228
|
+
app: options.app,
|
|
229
|
+
toolingSha: options['tooling-sha'],
|
|
230
|
+
});
|
|
231
|
+
console.log(
|
|
232
|
+
`Created/adapted: ${paths.join(', ')}\nRun npm install, install the peer tools for your enabled checks, then tooling doctor.\nEnable the hook: git config core.hooksPath .githooks\nPin the workflow SHA if you did not pass --tooling-sha.`,
|
|
233
|
+
);
|
|
234
|
+
return 0;
|
|
235
|
+
}
|
|
236
|
+
const findings = options['repo-standard']
|
|
237
|
+
? repoStandardFindings(root)
|
|
238
|
+
: diagnose(root, options.config);
|
|
239
|
+
for (const finding of findings) console.log(`${finding.status}: ${finding.message}`);
|
|
240
|
+
return findings.some((finding) => finding.status === 'failed') ? 1 : 0;
|
|
241
|
+
}
|