@smoothbricks/nx-plugin 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 +35 -8
- package/dist/bounded-test-policy.d.ts +32 -0
- package/dist/bounded-test-policy.d.ts.map +1 -0
- package/dist/bounded-test-policy.js +184 -0
- package/dist/executors/bounded-exec/executor.d.ts +15 -0
- package/dist/executors/bounded-exec/executor.d.ts.map +1 -0
- package/dist/executors/bounded-exec/executor.js +179 -0
- package/dist/executors/bounded-exec/schema.d.ts +11 -0
- package/dist/executors/bounded-exec/schema.d.ts.map +1 -0
- package/dist/executors/bounded-exec/schema.js +1 -0
- package/dist/generators/bounded-test-targets/generator.d.ts +7 -0
- package/dist/generators/bounded-test-targets/generator.d.ts.map +1 -0
- package/dist/generators/bounded-test-targets/generator.js +40 -0
- package/dist/generators/bun-test-tracing/generator.d.ts.map +1 -1
- package/dist/generators/bun-test-tracing/generator.js +5 -4
- package/dist/generators/create-package/generator.d.ts +4 -0
- package/dist/generators/create-package/generator.d.ts.map +1 -0
- package/dist/generators/create-package/generator.js +204 -0
- package/dist/generators/make-public/generator.d.ts +4 -0
- package/dist/generators/make-public/generator.d.ts.map +1 -0
- package/dist/generators/make-public/generator.js +55 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +78 -7
- package/dist/package-target-policy.d.ts +31 -0
- package/dist/package-target-policy.d.ts.map +1 -0
- package/dist/package-target-policy.js +937 -0
- package/dist/release-config-policy.d.ts +32 -0
- package/dist/release-config-policy.d.ts.map +1 -0
- package/dist/release-config-policy.js +225 -0
- package/dist/typecheck-test-policy.d.ts +56 -0
- package/dist/typecheck-test-policy.d.ts.map +1 -0
- package/dist/typecheck-test-policy.js +640 -0
- package/dist/version-actions.cjs +78 -0
- package/dist/version-actions.d.cts +6 -0
- package/dist/version-actions.d.cts.map +1 -0
- package/dist/workspace-config-policy.d.ts +33 -0
- package/dist/workspace-config-policy.d.ts.map +1 -0
- package/dist/workspace-config-policy.js +334 -0
- package/executors.json +9 -0
- package/generators.json +15 -0
- package/package.json +45 -11
- package/src/bounded-test-policy.test.ts +257 -0
- package/src/bounded-test-policy.ts +257 -0
- package/src/bun-bootstrap.test.ts +52 -0
- package/src/executors/bounded-exec/executor.test.ts +258 -0
- package/src/executors/bounded-exec/executor.ts +228 -0
- package/src/executors/bounded-exec/schema.json +68 -0
- package/src/executors/bounded-exec/schema.ts +10 -0
- package/src/generators/bounded-test-targets/generator.test.ts +136 -0
- package/src/generators/bounded-test-targets/generator.ts +67 -0
- package/src/generators/bounded-test-targets/schema.d.ts +3 -0
- package/src/generators/bounded-test-targets/schema.json +14 -0
- package/src/generators/bun-test-tracing/generator.test.ts +25 -1
- package/src/generators/bun-test-tracing/generator.ts +6 -5
- package/src/generators/create-package/generator.test.ts +206 -0
- package/src/generators/create-package/generator.ts +251 -0
- package/src/generators/create-package/schema.d.ts +5 -0
- package/src/generators/create-package/schema.json +24 -0
- package/src/generators/make-public/generator.test.ts +137 -0
- package/src/generators/make-public/generator.ts +83 -0
- package/src/generators/make-public/schema.d.ts +3 -0
- package/src/generators/make-public/schema.json +13 -0
- package/src/index.test.ts +85 -18
- package/src/index.ts +99 -8
- package/src/package-target-policy.test.ts +785 -0
- package/src/package-target-policy.ts +1140 -0
- package/src/release-config-policy.test.ts +280 -0
- package/src/release-config-policy.ts +258 -0
- package/src/typecheck-test-policy.test.ts +797 -0
- package/src/typecheck-test-policy.ts +752 -0
- package/src/version-actions.cts +94 -0
- package/src/workspace-config-policy.test.ts +376 -0
- package/src/workspace-config-policy.ts +390 -0
- package/src/test-suite-tracer.ts +0 -0
|
@@ -0,0 +1,640 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { join, relative } from 'node:path';
|
|
3
|
+
import { getProjects, readJson, readProjectConfiguration, updateJson, writeJson } from 'nx/src/devkit-exports.js';
|
|
4
|
+
const workspaceDependencyFields = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'];
|
|
5
|
+
/**
|
|
6
|
+
* Detect test runners from a package.json object and optional project targets
|
|
7
|
+
* (from project.json or nx config).
|
|
8
|
+
*/
|
|
9
|
+
export function detectPackageTestRunners(pkg, projectTargets) {
|
|
10
|
+
const runners = new Set();
|
|
11
|
+
// Check package.json scripts
|
|
12
|
+
const scripts = recordProperty(pkg, 'scripts');
|
|
13
|
+
if (scripts) {
|
|
14
|
+
for (const command of Object.values(scripts)) {
|
|
15
|
+
if (typeof command !== 'string') {
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
const runner = detectTestRunnerFromCommand(command);
|
|
19
|
+
if (runner) {
|
|
20
|
+
runners.add(runner);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
// Check package.json nx.targets
|
|
25
|
+
const nxTargets = recordProperty(recordProperty(pkg, 'nx'), 'targets');
|
|
26
|
+
if (nxTargets) {
|
|
27
|
+
for (const target of Object.values(nxTargets)) {
|
|
28
|
+
if (!isRecord(target)) {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
const options = recordProperty(target, 'options');
|
|
32
|
+
const command = options ? stringProperty(options, 'command') : null;
|
|
33
|
+
if (command) {
|
|
34
|
+
const runner = detectTestRunnerFromCommand(command);
|
|
35
|
+
if (runner) {
|
|
36
|
+
runners.add(runner);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// Check project.json / project config targets
|
|
42
|
+
if (projectTargets) {
|
|
43
|
+
for (const target of Object.values(projectTargets)) {
|
|
44
|
+
if (!isRecord(target)) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
const options = recordProperty(target, 'options');
|
|
48
|
+
const command = options ? stringProperty(options, 'command') : null;
|
|
49
|
+
if (command) {
|
|
50
|
+
const runner = detectTestRunnerFromCommand(command);
|
|
51
|
+
if (runner) {
|
|
52
|
+
runners.add(runner);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return runners;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Validate tsconfig.test.json contents (in-memory object).
|
|
61
|
+
* Returns policy issues found.
|
|
62
|
+
*/
|
|
63
|
+
export function checkTypecheckTestConfig(tsconfigTest, packagePath) {
|
|
64
|
+
if (!tsconfigTest) {
|
|
65
|
+
return [];
|
|
66
|
+
}
|
|
67
|
+
const issues = [];
|
|
68
|
+
const tsconfigTestPath = join(packagePath, 'tsconfig.test.json');
|
|
69
|
+
const compilerOptions = recordProperty(tsconfigTest, 'compilerOptions');
|
|
70
|
+
if (!compilerOptions || compilerOptions.noEmit !== true) {
|
|
71
|
+
issues.push({
|
|
72
|
+
path: tsconfigTestPath,
|
|
73
|
+
message: 'compilerOptions.noEmit must be true',
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
if (compilerOptions?.composite === true) {
|
|
77
|
+
issues.push({
|
|
78
|
+
path: tsconfigTestPath,
|
|
79
|
+
message: 'must not set compilerOptions.composite = true. ' +
|
|
80
|
+
'Bun test typechecking is a no-emit validation pass, not a TypeScript build-mode project.',
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
if (compilerOptions?.declaration === true) {
|
|
84
|
+
issues.push({
|
|
85
|
+
path: tsconfigTestPath,
|
|
86
|
+
message: 'must not set compilerOptions.declaration = true',
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
if (compilerOptions?.declarationMap === true) {
|
|
90
|
+
issues.push({
|
|
91
|
+
path: tsconfigTestPath,
|
|
92
|
+
message: 'must not set compilerOptions.declarationMap = true',
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
if (compilerOptions?.outDir === 'dist-test') {
|
|
96
|
+
issues.push({
|
|
97
|
+
path: tsconfigTestPath,
|
|
98
|
+
message: 'must not emit to dist-test',
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
if (typeof compilerOptions?.tsBuildInfoFile === 'string' && compilerOptions.tsBuildInfoFile.includes('dist-test')) {
|
|
102
|
+
issues.push({
|
|
103
|
+
path: tsconfigTestPath,
|
|
104
|
+
message: 'must not write tsbuildinfo under dist-test',
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
return issues;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Check if tsconfig.json has a bad reference to ./tsconfig.test.json.
|
|
111
|
+
*/
|
|
112
|
+
export function checkTsconfigTestReference(projectTsconfig, packagePath) {
|
|
113
|
+
if (!projectTsconfigHasTestReference(projectTsconfig)) {
|
|
114
|
+
return [];
|
|
115
|
+
}
|
|
116
|
+
return [
|
|
117
|
+
{
|
|
118
|
+
path: join(packagePath, 'tsconfig.json'),
|
|
119
|
+
message: 'must not reference ./tsconfig.test.json. ' +
|
|
120
|
+
'Test typechecking is run by the inferred typecheck-tests target with tsc --noEmit, not TypeScript build mode.',
|
|
121
|
+
},
|
|
122
|
+
];
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Apply defaults to a tsconfig.test.json object (in-memory mutation).
|
|
126
|
+
* Returns whether anything changed.
|
|
127
|
+
*/
|
|
128
|
+
export function applyTypecheckTestDefaults(tsconfigTest, options) {
|
|
129
|
+
const extendsValue = options.tsconfigLibExtends ?? '../../tsconfig.base.json';
|
|
130
|
+
let changed = setMissingStringProperty(tsconfigTest, 'extends', extendsValue);
|
|
131
|
+
const compilerOptions = getOrCreateRecord(tsconfigTest, 'compilerOptions');
|
|
132
|
+
// Copy relevant compiler options from lib tsconfig
|
|
133
|
+
if (options.libCompilerOptions) {
|
|
134
|
+
for (const key of ['baseUrl', 'module', 'moduleResolution', 'jsx', 'lib']) {
|
|
135
|
+
if (Object.hasOwn(options.libCompilerOptions, key) && compilerOptions[key] !== options.libCompilerOptions[key]) {
|
|
136
|
+
compilerOptions[key] = options.libCompilerOptions[key];
|
|
137
|
+
changed = true;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
changed = setBooleanProperty(compilerOptions, 'composite', false) || changed;
|
|
142
|
+
changed = setBooleanProperty(compilerOptions, 'declaration', false) || changed;
|
|
143
|
+
changed = setBooleanProperty(compilerOptions, 'declarationMap', false) || changed;
|
|
144
|
+
changed = setBooleanProperty(compilerOptions, 'emitDeclarationOnly', false) || changed;
|
|
145
|
+
changed = setBooleanProperty(compilerOptions, 'noEmit', true) || changed;
|
|
146
|
+
if (options.testRunners.has('bun')) {
|
|
147
|
+
changed = mergeStringListProperty(compilerOptions, 'types', ['bun']) || changed;
|
|
148
|
+
}
|
|
149
|
+
if ('outDir' in compilerOptions) {
|
|
150
|
+
delete compilerOptions.outDir;
|
|
151
|
+
changed = true;
|
|
152
|
+
}
|
|
153
|
+
if ('tsBuildInfoFile' in compilerOptions) {
|
|
154
|
+
delete compilerOptions.tsBuildInfoFile;
|
|
155
|
+
changed = true;
|
|
156
|
+
}
|
|
157
|
+
changed =
|
|
158
|
+
mergeStringListProperty(tsconfigTest, 'include', [
|
|
159
|
+
'src/**/*.test.ts',
|
|
160
|
+
'src/**/*.spec.ts',
|
|
161
|
+
'src/**/__tests__/**/*.ts',
|
|
162
|
+
'src/**/__tests__/**/*.tsx',
|
|
163
|
+
'src/test-suite-tracer.ts',
|
|
164
|
+
]) || changed;
|
|
165
|
+
for (const referencePath of options.referencePaths) {
|
|
166
|
+
changed = addTsconfigReference(tsconfigTest, referencePath) || changed;
|
|
167
|
+
}
|
|
168
|
+
return changed;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Remove the ./tsconfig.test.json reference from a tsconfig.json object.
|
|
172
|
+
* Returns whether anything changed.
|
|
173
|
+
*/
|
|
174
|
+
export function removeTsconfigTestReference(projectTsconfig) {
|
|
175
|
+
if (!projectTsconfigHasTestReference(projectTsconfig)) {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
const references = Array.isArray(projectTsconfig.references) ? projectTsconfig.references : [];
|
|
179
|
+
projectTsconfig.references = references.filter((entry) => !isRecord(entry) || entry.path !== './tsconfig.test.json');
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Check all workspace packages for typecheck test policy compliance using an Nx Tree.
|
|
184
|
+
*/
|
|
185
|
+
export function checkTypecheckTestPolicyTree(tree) {
|
|
186
|
+
const issues = [];
|
|
187
|
+
for (const [projectName, config] of getProjects(tree)) {
|
|
188
|
+
const pkgPath = `${config.root}/package.json`;
|
|
189
|
+
if (!tree.exists(pkgPath))
|
|
190
|
+
continue;
|
|
191
|
+
if (config.root === '.' || config.root === '')
|
|
192
|
+
continue;
|
|
193
|
+
const pkg = readJson(tree, pkgPath);
|
|
194
|
+
// Get targets from project config (handles both package.json and project.json)
|
|
195
|
+
const projectConfig = readProjectConfiguration(tree, projectName);
|
|
196
|
+
const projectTargets = projectConfig.targets ?? {};
|
|
197
|
+
const testRunners = detectPackageTestRunners(pkg, projectTargets);
|
|
198
|
+
if (testRunners.size === 0)
|
|
199
|
+
continue;
|
|
200
|
+
// Check tsconfig.test.json exists
|
|
201
|
+
const tsconfigTestPath = `${config.root}/tsconfig.test.json`;
|
|
202
|
+
if (!tree.exists(tsconfigTestPath)) {
|
|
203
|
+
issues.push({
|
|
204
|
+
path: tsconfigTestPath,
|
|
205
|
+
message: `${formatTestRunnerList(testRunners)} requires tsconfig.test.json because those runners do not typecheck test files by default`,
|
|
206
|
+
});
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
// Check tsconfig.test.json contents
|
|
210
|
+
const tsconfigTest = readJson(tree, tsconfigTestPath);
|
|
211
|
+
issues.push(...checkTypecheckTestConfig(tsconfigTest, config.root));
|
|
212
|
+
// Check tsconfig.json reference
|
|
213
|
+
const tsconfigPath = `${config.root}/tsconfig.json`;
|
|
214
|
+
if (tree.exists(tsconfigPath)) {
|
|
215
|
+
const tsconfig = readJson(tree, tsconfigPath);
|
|
216
|
+
issues.push(...checkTsconfigTestReference(tsconfig, config.root));
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return issues;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Fix all workspace packages' tsconfig.test.json configuration using an Nx Tree.
|
|
223
|
+
* Returns whether any files changed.
|
|
224
|
+
*/
|
|
225
|
+
export function applyTypecheckTestPolicyTree(tree) {
|
|
226
|
+
let changed = false;
|
|
227
|
+
const workspacePackages = collectWorkspacePackagesTree(tree);
|
|
228
|
+
for (const [projectName, config] of getProjects(tree)) {
|
|
229
|
+
const pkgPath = `${config.root}/package.json`;
|
|
230
|
+
if (!tree.exists(pkgPath))
|
|
231
|
+
continue;
|
|
232
|
+
if (config.root === '.' || config.root === '')
|
|
233
|
+
continue;
|
|
234
|
+
const pkg = readJson(tree, pkgPath);
|
|
235
|
+
const projectConfig = readProjectConfiguration(tree, projectName);
|
|
236
|
+
const projectTargets = projectConfig.targets ?? {};
|
|
237
|
+
const testRunners = detectPackageTestRunners(pkg, projectTargets);
|
|
238
|
+
if (testRunners.size === 0)
|
|
239
|
+
continue;
|
|
240
|
+
// Apply tsconfig.test.json defaults
|
|
241
|
+
const tsconfigTestPath = `${config.root}/tsconfig.test.json`;
|
|
242
|
+
const isNew = !tree.exists(tsconfigTestPath);
|
|
243
|
+
const tsconfigTest = isNew ? {} : readJson(tree, tsconfigTestPath);
|
|
244
|
+
// Read lib tsconfig for extends and compiler options
|
|
245
|
+
const libTsconfigPath = `${config.root}/tsconfig.lib.json`;
|
|
246
|
+
const libTsconfig = tree.exists(libTsconfigPath) ? readJson(tree, libTsconfigPath) : null;
|
|
247
|
+
const tsconfigLibExtends = (libTsconfig ? stringProperty(libTsconfig, 'extends') : null) ?? '../../tsconfig.base.json';
|
|
248
|
+
const libCompilerOptions = libTsconfig ? recordProperty(libTsconfig, 'compilerOptions') : null;
|
|
249
|
+
// Collect reference paths
|
|
250
|
+
const referencePaths = collectReferencePathsTree(tree, config.root, pkg, workspacePackages);
|
|
251
|
+
if (applyTypecheckTestDefaults(tsconfigTest, {
|
|
252
|
+
testRunners,
|
|
253
|
+
tsconfigLibExtends,
|
|
254
|
+
libCompilerOptions: libCompilerOptions ?? undefined,
|
|
255
|
+
referencePaths,
|
|
256
|
+
}) ||
|
|
257
|
+
isNew) {
|
|
258
|
+
writeJson(tree, tsconfigTestPath, tsconfigTest);
|
|
259
|
+
changed = true;
|
|
260
|
+
}
|
|
261
|
+
// Remove test reference from tsconfig.json
|
|
262
|
+
const tsconfigPath = `${config.root}/tsconfig.json`;
|
|
263
|
+
if (tree.exists(tsconfigPath)) {
|
|
264
|
+
updateJson(tree, tsconfigPath, (tsconfig) => {
|
|
265
|
+
if (removeTsconfigTestReference(tsconfig)) {
|
|
266
|
+
changed = true;
|
|
267
|
+
}
|
|
268
|
+
return tsconfig;
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return changed;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Check all workspace packages for typecheck test policy compliance.
|
|
276
|
+
*
|
|
277
|
+
* For each package that uses bun test or vitest:
|
|
278
|
+
* - tsconfig.test.json must exist
|
|
279
|
+
* - tsconfig.test.json must be noEmit, not composite, no declaration, no dist-test output
|
|
280
|
+
* - tsconfig.json must NOT reference ./tsconfig.test.json
|
|
281
|
+
*/
|
|
282
|
+
export function checkTypecheckTestPolicy(root) {
|
|
283
|
+
const issues = [];
|
|
284
|
+
for (const packageJsonPath of listWorkspacePackageJsonPaths(root)) {
|
|
285
|
+
const pkg = readJsonObject(packageJsonPath);
|
|
286
|
+
if (!pkg) {
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
const packageDir = packageJsonPath.slice(0, -'/package.json'.length);
|
|
290
|
+
const packagePath = relative(root, packageDir);
|
|
291
|
+
if (packagePath === '' || packagePath === '.') {
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
const testRunners = detectPackageTestRunners(pkg);
|
|
295
|
+
if (testRunners.size === 0) {
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
// tsconfig.test.json must exist
|
|
299
|
+
const tsconfigTestPath = join(root, packagePath, 'tsconfig.test.json');
|
|
300
|
+
if (!existsSync(tsconfigTestPath)) {
|
|
301
|
+
issues.push({
|
|
302
|
+
path: tsconfigTestPath,
|
|
303
|
+
message: `${formatTestRunnerList(testRunners)} requires tsconfig.test.json because those runners do not typecheck test files by default`,
|
|
304
|
+
});
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
// tsconfig.test.json must have correct settings
|
|
308
|
+
const tsconfig = readJsonObject(tsconfigTestPath);
|
|
309
|
+
if (tsconfig) {
|
|
310
|
+
// Use absolute path for filesystem layer
|
|
311
|
+
const absoluteIssues = checkTypecheckTestConfig(tsconfig, join(root, packagePath));
|
|
312
|
+
issues.push(...absoluteIssues);
|
|
313
|
+
}
|
|
314
|
+
// tsconfig.json must NOT reference ./tsconfig.test.json
|
|
315
|
+
const projectTsconfigPath = join(root, packagePath, 'tsconfig.json');
|
|
316
|
+
const projectTsconfig = readJsonObject(projectTsconfigPath);
|
|
317
|
+
const refIssues = checkTsconfigTestReference(projectTsconfig, join(root, packagePath));
|
|
318
|
+
issues.push(...refIssues);
|
|
319
|
+
}
|
|
320
|
+
return issues;
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Fix all workspace packages' tsconfig.test.json configuration.
|
|
324
|
+
* Returns whether any files changed.
|
|
325
|
+
*/
|
|
326
|
+
export function applyTypecheckTestPolicy(root) {
|
|
327
|
+
const workspaceNames = getWorkspacePackageNames(root);
|
|
328
|
+
let changed = false;
|
|
329
|
+
for (const packageJsonPath of listWorkspacePackageJsonPaths(root)) {
|
|
330
|
+
const pkg = readJsonObject(packageJsonPath);
|
|
331
|
+
if (!pkg) {
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
const packageDir = packageJsonPath.slice(0, -'/package.json'.length);
|
|
335
|
+
const packagePath = relative(root, packageDir);
|
|
336
|
+
if (packagePath === '' || packagePath === '.') {
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
const testRunners = detectPackageTestRunners(pkg);
|
|
340
|
+
if (testRunners.size === 0) {
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
// Create/update tsconfig.test.json
|
|
344
|
+
const tsconfigTestPath = join(root, packagePath, 'tsconfig.test.json');
|
|
345
|
+
const existing = readJsonObject(tsconfigTestPath);
|
|
346
|
+
const tsconfigTest = existing ?? {};
|
|
347
|
+
let fileChanged = existing === null;
|
|
348
|
+
// Read lib tsconfig for extends and compiler options
|
|
349
|
+
const libTsconfig = readJsonObject(join(root, packagePath, 'tsconfig.lib.json'));
|
|
350
|
+
const tsconfigLibExtends = stringProperty(libTsconfig ?? {}, 'extends') ?? '../../tsconfig.base.json';
|
|
351
|
+
const libCompilerOptions = libTsconfig ? recordProperty(libTsconfig, 'compilerOptions') : null;
|
|
352
|
+
// Collect reference paths
|
|
353
|
+
const referencePaths = collectTsconfigTestReferencePaths(root, packagePath, pkg, workspaceNames);
|
|
354
|
+
fileChanged =
|
|
355
|
+
applyTypecheckTestDefaults(tsconfigTest, {
|
|
356
|
+
testRunners,
|
|
357
|
+
tsconfigLibExtends,
|
|
358
|
+
libCompilerOptions: libCompilerOptions ?? undefined,
|
|
359
|
+
referencePaths,
|
|
360
|
+
}) || fileChanged;
|
|
361
|
+
if (fileChanged) {
|
|
362
|
+
writeJsonObjectFs(tsconfigTestPath, tsconfigTest);
|
|
363
|
+
changed = true;
|
|
364
|
+
}
|
|
365
|
+
// Remove ./tsconfig.test.json from tsconfig.json references if present
|
|
366
|
+
const projectTsconfigPath = join(root, packagePath, 'tsconfig.json');
|
|
367
|
+
const projectTsconfig = readJsonObject(projectTsconfigPath);
|
|
368
|
+
if (projectTsconfig && removeTsconfigTestReference(projectTsconfig)) {
|
|
369
|
+
writeJsonObjectFs(projectTsconfigPath, projectTsconfig);
|
|
370
|
+
changed = true;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
return changed;
|
|
374
|
+
}
|
|
375
|
+
function detectTestRunnerFromCommand(command) {
|
|
376
|
+
const trimmed = parseEnvPrefixedCommand(command).command.trim();
|
|
377
|
+
if (/^bun\s+test(?:\s|$)/.test(trimmed)) {
|
|
378
|
+
return 'bun';
|
|
379
|
+
}
|
|
380
|
+
if (/^vitest(?:\s|$)/.test(trimmed)) {
|
|
381
|
+
return 'vitest';
|
|
382
|
+
}
|
|
383
|
+
return null;
|
|
384
|
+
}
|
|
385
|
+
function formatTestRunnerList(testRunners) {
|
|
386
|
+
const labels = [];
|
|
387
|
+
if (testRunners.has('bun')) {
|
|
388
|
+
labels.push('bun test');
|
|
389
|
+
}
|
|
390
|
+
if (testRunners.has('vitest')) {
|
|
391
|
+
labels.push('vitest');
|
|
392
|
+
}
|
|
393
|
+
return labels.join(' or ');
|
|
394
|
+
}
|
|
395
|
+
function parseEnvPrefixedCommand(command) {
|
|
396
|
+
const env = {};
|
|
397
|
+
let rest = command.trimStart();
|
|
398
|
+
while (true) {
|
|
399
|
+
const match = /^([A-Za-z_][A-Za-z0-9_]*)=/.exec(rest);
|
|
400
|
+
if (!match?.[1]) {
|
|
401
|
+
return { command: rest.trim(), env };
|
|
402
|
+
}
|
|
403
|
+
let index = match[0].length;
|
|
404
|
+
let value = '';
|
|
405
|
+
const quote = rest[index];
|
|
406
|
+
if (quote === '"' || quote === "'") {
|
|
407
|
+
index += 1;
|
|
408
|
+
const end = rest.indexOf(quote, index);
|
|
409
|
+
if (end === -1) {
|
|
410
|
+
return { command: command.trim(), env: {} };
|
|
411
|
+
}
|
|
412
|
+
value = rest.slice(index, end);
|
|
413
|
+
index = end + 1;
|
|
414
|
+
}
|
|
415
|
+
else {
|
|
416
|
+
const end = rest.slice(index).search(/\s/);
|
|
417
|
+
const valueEnd = end === -1 ? rest.length : index + end;
|
|
418
|
+
value = rest.slice(index, valueEnd);
|
|
419
|
+
index = valueEnd;
|
|
420
|
+
}
|
|
421
|
+
if (index < rest.length && !/\s/.test(rest[index] ?? '')) {
|
|
422
|
+
return { command: command.trim(), env: {} };
|
|
423
|
+
}
|
|
424
|
+
env[match[1]] = value;
|
|
425
|
+
rest = rest.slice(index).trimStart();
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
function projectTsconfigHasTestReference(projectTsconfig) {
|
|
429
|
+
return Boolean(projectTsconfig &&
|
|
430
|
+
Array.isArray(projectTsconfig.references) &&
|
|
431
|
+
projectTsconfig.references.some((entry) => isRecord(entry) && entry.path === './tsconfig.test.json'));
|
|
432
|
+
}
|
|
433
|
+
function collectWorkspacePackagesTree(tree) {
|
|
434
|
+
const packages = [];
|
|
435
|
+
for (const [, config] of getProjects(tree)) {
|
|
436
|
+
const pkgPath = `${config.root}/package.json`;
|
|
437
|
+
if (!tree.exists(pkgPath))
|
|
438
|
+
continue;
|
|
439
|
+
const pkg = readJson(tree, pkgPath);
|
|
440
|
+
if (pkg.name) {
|
|
441
|
+
packages.push({ name: pkg.name, path: config.root });
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
return packages;
|
|
445
|
+
}
|
|
446
|
+
function collectReferencePathsTree(tree, packageRoot, pkg, workspacePackages) {
|
|
447
|
+
const paths = [];
|
|
448
|
+
const libTsconfigPath = `${packageRoot}/tsconfig.lib.json`;
|
|
449
|
+
if (tree.exists(libTsconfigPath)) {
|
|
450
|
+
paths.push('./tsconfig.lib.json');
|
|
451
|
+
}
|
|
452
|
+
const packagesByName = new Map(workspacePackages.map((p) => [p.name, p]));
|
|
453
|
+
for (const field of workspaceDependencyFields) {
|
|
454
|
+
const deps = recordProperty(pkg, field);
|
|
455
|
+
if (!deps)
|
|
456
|
+
continue;
|
|
457
|
+
for (const depName of Object.keys(deps)) {
|
|
458
|
+
const depPkg = packagesByName.get(depName);
|
|
459
|
+
if (!depPkg)
|
|
460
|
+
continue;
|
|
461
|
+
const depTsconfig = `${depPkg.path}/tsconfig.lib.json`;
|
|
462
|
+
if (!tree.exists(depTsconfig))
|
|
463
|
+
continue;
|
|
464
|
+
const refPath = relative(packageRoot, depTsconfig).replaceAll('\\', '/');
|
|
465
|
+
if (!paths.includes(refPath))
|
|
466
|
+
paths.push(refPath);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
return paths;
|
|
470
|
+
}
|
|
471
|
+
function listWorkspacePackageJsonPaths(root) {
|
|
472
|
+
const rootPackagePath = join(root, 'package.json');
|
|
473
|
+
if (!existsSync(rootPackagePath)) {
|
|
474
|
+
return [];
|
|
475
|
+
}
|
|
476
|
+
const rootPackage = readJsonObject(rootPackagePath);
|
|
477
|
+
if (!rootPackage) {
|
|
478
|
+
return [];
|
|
479
|
+
}
|
|
480
|
+
const workspacePatterns = Array.isArray(rootPackage.workspaces)
|
|
481
|
+
? rootPackage.workspaces.filter((entry) => typeof entry === 'string')
|
|
482
|
+
: [];
|
|
483
|
+
const paths = [];
|
|
484
|
+
for (const pattern of workspacePatterns) {
|
|
485
|
+
if (!pattern.endsWith('/*')) {
|
|
486
|
+
continue;
|
|
487
|
+
}
|
|
488
|
+
const parent = join(root, pattern.slice(0, -2));
|
|
489
|
+
if (!existsSync(parent)) {
|
|
490
|
+
continue;
|
|
491
|
+
}
|
|
492
|
+
for (const entry of readdirSync(parent, { withFileTypes: true })) {
|
|
493
|
+
if (!entry.isDirectory()) {
|
|
494
|
+
continue;
|
|
495
|
+
}
|
|
496
|
+
const packageJsonPath = join(parent, entry.name, 'package.json');
|
|
497
|
+
if (existsSync(packageJsonPath)) {
|
|
498
|
+
paths.push(packageJsonPath);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
return paths.sort((a, b) => a.localeCompare(b));
|
|
503
|
+
}
|
|
504
|
+
function getWorkspacePackageNames(root) {
|
|
505
|
+
const names = new Set();
|
|
506
|
+
for (const packageJsonPath of listWorkspacePackageJsonPaths(root)) {
|
|
507
|
+
const pkg = readJsonObject(packageJsonPath);
|
|
508
|
+
if (pkg) {
|
|
509
|
+
const name = stringProperty(pkg, 'name');
|
|
510
|
+
if (name) {
|
|
511
|
+
names.add(name);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
return names;
|
|
516
|
+
}
|
|
517
|
+
function getWorkspacePackages(root) {
|
|
518
|
+
const packages = [];
|
|
519
|
+
for (const packageJsonPath of listWorkspacePackageJsonPaths(root)) {
|
|
520
|
+
const pkg = readJsonObject(packageJsonPath);
|
|
521
|
+
if (!pkg) {
|
|
522
|
+
continue;
|
|
523
|
+
}
|
|
524
|
+
const name = stringProperty(pkg, 'name');
|
|
525
|
+
if (!name) {
|
|
526
|
+
continue;
|
|
527
|
+
}
|
|
528
|
+
const packageDir = packageJsonPath.slice(0, -'/package.json'.length);
|
|
529
|
+
packages.push({ name, path: relative(root, packageDir) });
|
|
530
|
+
}
|
|
531
|
+
return packages;
|
|
532
|
+
}
|
|
533
|
+
function collectTsconfigTestReferencePaths(root, packagePath, pkg, workspaceNames) {
|
|
534
|
+
const paths = existsSync(join(root, packagePath, 'tsconfig.lib.json')) ? ['./tsconfig.lib.json'] : [];
|
|
535
|
+
const packagesByName = new Map(getWorkspacePackages(root).map((workspacePkg) => [workspacePkg.name, workspacePkg]));
|
|
536
|
+
for (const field of workspaceDependencyFields) {
|
|
537
|
+
const dependencies = recordProperty(pkg, field);
|
|
538
|
+
if (!dependencies) {
|
|
539
|
+
continue;
|
|
540
|
+
}
|
|
541
|
+
for (const dependencyName of Object.keys(dependencies)) {
|
|
542
|
+
if (!workspaceNames.has(dependencyName)) {
|
|
543
|
+
continue;
|
|
544
|
+
}
|
|
545
|
+
const dependencyPackage = packagesByName.get(dependencyName);
|
|
546
|
+
if (!dependencyPackage) {
|
|
547
|
+
continue;
|
|
548
|
+
}
|
|
549
|
+
const dependencyTsconfig = join(root, dependencyPackage.path, 'tsconfig.lib.json');
|
|
550
|
+
if (!existsSync(dependencyTsconfig)) {
|
|
551
|
+
continue;
|
|
552
|
+
}
|
|
553
|
+
const refPath = relative(join(root, packagePath), dependencyTsconfig).replaceAll('\\', '/');
|
|
554
|
+
if (!paths.includes(refPath)) {
|
|
555
|
+
paths.push(refPath);
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
return paths;
|
|
560
|
+
}
|
|
561
|
+
function readJsonObject(path) {
|
|
562
|
+
if (!existsSync(path)) {
|
|
563
|
+
return null;
|
|
564
|
+
}
|
|
565
|
+
try {
|
|
566
|
+
const parsed = JSON.parse(readFileSync(path, 'utf8'));
|
|
567
|
+
return isRecord(parsed) ? parsed : null;
|
|
568
|
+
}
|
|
569
|
+
catch {
|
|
570
|
+
return null;
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
function writeJsonObjectFs(path, value) {
|
|
574
|
+
writeFileSync(path, `${JSON.stringify(value, null, 2)}\n`);
|
|
575
|
+
}
|
|
576
|
+
function isRecord(value) {
|
|
577
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
578
|
+
}
|
|
579
|
+
function stringProperty(record, key) {
|
|
580
|
+
const value = record[key];
|
|
581
|
+
return typeof value === 'string' && value.length > 0 ? value : null;
|
|
582
|
+
}
|
|
583
|
+
function recordProperty(record, key) {
|
|
584
|
+
if (!record)
|
|
585
|
+
return null;
|
|
586
|
+
const value = record[key];
|
|
587
|
+
return isRecord(value) ? value : null;
|
|
588
|
+
}
|
|
589
|
+
function getOrCreateRecord(record, key) {
|
|
590
|
+
const value = record[key];
|
|
591
|
+
if (isRecord(value)) {
|
|
592
|
+
return value;
|
|
593
|
+
}
|
|
594
|
+
const next = {};
|
|
595
|
+
record[key] = next;
|
|
596
|
+
return next;
|
|
597
|
+
}
|
|
598
|
+
function setMissingStringProperty(record, key, value) {
|
|
599
|
+
if (typeof record[key] === 'string') {
|
|
600
|
+
return false;
|
|
601
|
+
}
|
|
602
|
+
record[key] = value;
|
|
603
|
+
return true;
|
|
604
|
+
}
|
|
605
|
+
function setBooleanProperty(record, key, value) {
|
|
606
|
+
if (record[key] === value) {
|
|
607
|
+
return false;
|
|
608
|
+
}
|
|
609
|
+
record[key] = value;
|
|
610
|
+
return true;
|
|
611
|
+
}
|
|
612
|
+
function mergeStringListProperty(record, key, values) {
|
|
613
|
+
const rawCurrent = record[key];
|
|
614
|
+
const current = Array.isArray(rawCurrent)
|
|
615
|
+
? rawCurrent.filter((entry) => typeof entry === 'string')
|
|
616
|
+
: [];
|
|
617
|
+
const next = [...current];
|
|
618
|
+
for (const value of values) {
|
|
619
|
+
if (!next.includes(value)) {
|
|
620
|
+
next.push(value);
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
if (Array.isArray(rawCurrent) &&
|
|
624
|
+
next.length === rawCurrent.length &&
|
|
625
|
+
next.every((entry, index) => entry === rawCurrent[index])) {
|
|
626
|
+
return false;
|
|
627
|
+
}
|
|
628
|
+
record[key] = next;
|
|
629
|
+
return true;
|
|
630
|
+
}
|
|
631
|
+
function addTsconfigReference(tsconfig, path) {
|
|
632
|
+
const current = Array.isArray(tsconfig.references)
|
|
633
|
+
? tsconfig.references.filter((entry) => isRecord(entry))
|
|
634
|
+
: [];
|
|
635
|
+
if (current.some((entry) => entry.path === path)) {
|
|
636
|
+
return false;
|
|
637
|
+
}
|
|
638
|
+
tsconfig.references = [...current, { path }];
|
|
639
|
+
return true;
|
|
640
|
+
}
|