@smoothbricks/nx-plugin 0.2.2 → 0.2.4
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/dist/bounded-test-policy.d.ts +2 -0
- package/dist/bounded-test-policy.d.ts.map +1 -1
- package/dist/bounded-test-policy.js +29 -0
- package/dist/executors/clean-outputs/executor.d.ts +26 -0
- package/dist/executors/clean-outputs/executor.d.ts.map +1 -0
- package/dist/executors/clean-outputs/executor.js +68 -0
- package/dist/executors/clean-outputs/schema.d.ts +4 -0
- package/dist/executors/clean-outputs/schema.d.ts.map +1 -0
- package/dist/executors/clean-outputs/schema.js +1 -0
- package/dist/generators/bun-test-tracing/generator.d.ts.map +1 -1
- package/dist/generators/bun-test-tracing/generator.js +18 -22
- package/dist/index.js +4 -0
- package/dist/release-config-policy.d.ts.map +1 -1
- package/dist/release-config-policy.js +4 -0
- package/dist/workspace-config-policy.d.ts.map +1 -1
- package/dist/workspace-config-policy.js +28 -0
- package/executors.json +5 -0
- package/package.json +2 -2
- package/src/bounded-test-policy.test.ts +24 -7
- package/src/bounded-test-policy.ts +36 -0
- package/src/bun-bootstrap.test.ts +2 -0
- package/src/executors/clean-outputs/executor.test.ts +20 -0
- package/src/executors/clean-outputs/executor.ts +116 -0
- package/src/executors/clean-outputs/schema.json +15 -0
- package/src/executors/clean-outputs/schema.ts +3 -0
- package/src/generators/bounded-test-targets/generator.test.ts +7 -3
- package/src/generators/bun-test-tracing/generator.test.ts +12 -5
- package/src/generators/bun-test-tracing/generator.ts +18 -27
- package/src/index.test.ts +4 -1
- package/src/index.ts +4 -0
- package/src/release-config-policy.test.ts +3 -0
- package/src/release-config-policy.ts +4 -0
- package/src/workspace-config-policy.test.ts +27 -15
- package/src/workspace-config-policy.ts +36 -0
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export declare const BOUNDED_TEST_EXECUTOR = "@smoothbricks/nx-plugin:bounded-exec";
|
|
2
2
|
export declare const BOUNDED_TEST_TIMEOUT_MS = 120000;
|
|
3
3
|
export declare const BOUNDED_TEST_KILL_AFTER_MS = 10000;
|
|
4
|
+
export declare const BOUNDED_TEST_PER_TEST_TIMEOUT_MS = 30000;
|
|
4
5
|
export interface BoundedTestPolicyPackageJson {
|
|
5
6
|
name?: string;
|
|
6
7
|
workspaces?: unknown;
|
|
@@ -29,4 +30,5 @@ export declare function checkWorkspaceBoundedTestTargetPolicy(root: string): Bou
|
|
|
29
30
|
export declare function checkBoundedTestTargetPolicy(packageJson: BoundedTestPolicyPackageJson, options: ApplyBoundedTestTargetPolicyOptions): boolean;
|
|
30
31
|
export declare function boundedTestScriptAlias(projectName: string): string;
|
|
31
32
|
export declare function resolveTestCommand(packageJson: BoundedTestPolicyPackageJson, defaultCommand?: string, projectJson?: BoundedTestPolicyProjectJson): string;
|
|
33
|
+
export declare function ensureBunTestTimeoutFlag(command: string, timeoutMs?: number): string;
|
|
32
34
|
//# sourceMappingURL=bounded-test-policy.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bounded-test-policy.d.ts","sourceRoot":"","sources":["../src/bounded-test-policy.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,qBAAqB,yCAAyC,CAAC;AAC5E,eAAO,MAAM,uBAAuB,SAAU,CAAC;AAC/C,eAAO,MAAM,0BAA0B,QAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"bounded-test-policy.d.ts","sourceRoot":"","sources":["../src/bounded-test-policy.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,qBAAqB,yCAAyC,CAAC;AAC5E,eAAO,MAAM,uBAAuB,SAAU,CAAC;AAC/C,eAAO,MAAM,0BAA0B,QAAS,CAAC;AAKjD,eAAO,MAAM,gCAAgC,QAAS,CAAC;AAEvD,MAAM,WAAW,4BAA4B;IAC3C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,EAAE,CAAC,EAAE;QACH,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;KACnD,CAAC;CACH;AAED,MAAM,WAAW,4BAA4B;IAC3C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;CACnD;AAED,MAAM,WAAW,sBAAsB;IACrC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,mCAAmC;IAClD,WAAW,EAAE,MAAM,CAAC;IACpB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,WAAW,CAAC,EAAE,4BAA4B,CAAC;CAC5C;AAED,wBAAgB,4BAA4B,CAC1C,WAAW,EAAE,4BAA4B,EACzC,OAAO,EAAE,mCAAmC,GAC3C,IAAI,CAmBN;AAED,wBAAgB,qCAAqC,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CA0B3E;AAED,wBAAgB,qCAAqC,CAAC,IAAI,EAAE,MAAM,GAAG,sBAAsB,EAAE,CAyB5F;AAED,wBAAgB,4BAA4B,CAC1C,WAAW,EAAE,4BAA4B,EACzC,OAAO,EAAE,mCAAmC,GAC3C,OAAO,CAmBT;AAED,wBAAgB,sBAAsB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAElE;AAED,wBAAgB,kBAAkB,CAChC,WAAW,EAAE,4BAA4B,EACzC,cAAc,SAAa,EAC3B,WAAW,CAAC,EAAE,4BAA4B,GACzC,MAAM,CAER;AA+CD,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,SAAmC,GAAG,MAAM,CAa9G"}
|
|
@@ -3,6 +3,11 @@ import { dirname, join } from 'node:path';
|
|
|
3
3
|
export const BOUNDED_TEST_EXECUTOR = '@smoothbricks/nx-plugin:bounded-exec';
|
|
4
4
|
export const BOUNDED_TEST_TIMEOUT_MS = 120_000;
|
|
5
5
|
export const BOUNDED_TEST_KILL_AFTER_MS = 10_000;
|
|
6
|
+
// Per-test timeout passed to `bun test --timeout=<ms>`. Bun's default is
|
|
7
|
+
// 5000ms which is too tight for git-fixture tests on slower CI runners
|
|
8
|
+
// (Actions). The bunfig.toml `[test] timeout` key is silently ignored by
|
|
9
|
+
// Bun — the only working surfaces are this CLI flag and `setDefaultTimeout()`.
|
|
10
|
+
export const BOUNDED_TEST_PER_TEST_TIMEOUT_MS = 30_000;
|
|
6
11
|
export function applyBoundedTestTargetPolicy(packageJson, options) {
|
|
7
12
|
const command = resolveTestCommand(packageJson, options.defaultCommand ?? 'bun test', options.projectJson);
|
|
8
13
|
const targetOwner = options.projectJson ?? (packageJson.nx ??= {});
|
|
@@ -83,6 +88,7 @@ export function checkBoundedTestTargetPolicy(packageJson, options) {
|
|
|
83
88
|
typeof targetOptions.command !== 'string' ||
|
|
84
89
|
targetOptions.command.length === 0 ||
|
|
85
90
|
isPackageTestScriptRunnerCommand(targetOptions.command) ||
|
|
91
|
+
targetOptions.command !== ensureBunTestTimeoutFlag(targetOptions.command) ||
|
|
86
92
|
targetOptions.cwd !== '{projectRoot}' ||
|
|
87
93
|
targetOptions.timeoutMs !== BOUNDED_TEST_TIMEOUT_MS ||
|
|
88
94
|
targetOptions.killAfterMs !== BOUNDED_TEST_KILL_AFTER_MS) {
|
|
@@ -94,6 +100,9 @@ export function boundedTestScriptAlias(projectName) {
|
|
|
94
100
|
return `nx run ${projectName}:test --outputStyle=stream`;
|
|
95
101
|
}
|
|
96
102
|
export function resolveTestCommand(packageJson, defaultCommand = 'bun test', projectJson) {
|
|
103
|
+
return ensureBunTestTimeoutFlag(resolveRawTestCommand(packageJson, defaultCommand, projectJson));
|
|
104
|
+
}
|
|
105
|
+
function resolveRawTestCommand(packageJson, defaultCommand, projectJson) {
|
|
97
106
|
const commandFromProjectTarget = resolveTargetCommand(projectJson?.targets?.test);
|
|
98
107
|
if (commandFromProjectTarget) {
|
|
99
108
|
return commandFromProjectTarget;
|
|
@@ -120,6 +129,26 @@ function resolveTargetCommand(existingTarget) {
|
|
|
120
129
|
}
|
|
121
130
|
return null;
|
|
122
131
|
}
|
|
132
|
+
const BUN_TEST_PREFIX = /^bun\s+test(?=\s|$)/;
|
|
133
|
+
const EXISTING_BUN_TIMEOUT = /(^|\s)--timeout(?:=|\s+)(\S+)/;
|
|
134
|
+
// Bun's `[test] timeout` bunfig key is silently ignored — only the CLI flag
|
|
135
|
+
// and `setDefaultTimeout()` actually take effect. Normalize every `bun test`
|
|
136
|
+
// command so the policy enforces the workspace per-test timeout in the one
|
|
137
|
+
// place where Bun will honor it.
|
|
138
|
+
export function ensureBunTestTimeoutFlag(command, timeoutMs = BOUNDED_TEST_PER_TEST_TIMEOUT_MS) {
|
|
139
|
+
if (!BUN_TEST_PREFIX.test(command)) {
|
|
140
|
+
return command;
|
|
141
|
+
}
|
|
142
|
+
const flag = `--timeout=${timeoutMs}`;
|
|
143
|
+
const existing = EXISTING_BUN_TIMEOUT.exec(command);
|
|
144
|
+
if (!existing) {
|
|
145
|
+
return command.replace(BUN_TEST_PREFIX, `bun test ${flag}`);
|
|
146
|
+
}
|
|
147
|
+
if (existing[2] === String(timeoutMs)) {
|
|
148
|
+
return command;
|
|
149
|
+
}
|
|
150
|
+
return command.replace(EXISTING_BUN_TIMEOUT, `${existing[1]}${flag}`);
|
|
151
|
+
}
|
|
123
152
|
function isPackageTestScriptRunnerCommand(command) {
|
|
124
153
|
return /^(?:bun\s+run|npm(?:\s+run)?|pnpm(?:\s+run)?|yarn(?:\s+run)?)\s+test(?:\s|$)/.test(command.trim());
|
|
125
154
|
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { CleanOutputsOptions } from './schema.js';
|
|
2
|
+
interface CleanOutputsContext {
|
|
3
|
+
root: string;
|
|
4
|
+
projectName?: string;
|
|
5
|
+
projectsConfigurations?: {
|
|
6
|
+
projects: Record<string, NxProject>;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
interface CleanOutputsResult {
|
|
10
|
+
success: boolean;
|
|
11
|
+
}
|
|
12
|
+
interface NxProject {
|
|
13
|
+
root?: string;
|
|
14
|
+
targets?: Record<string, {
|
|
15
|
+
outputs?: string[];
|
|
16
|
+
}>;
|
|
17
|
+
}
|
|
18
|
+
export default function cleanOutputsExecutor(options: CleanOutputsOptions, context: CleanOutputsContext): Promise<CleanOutputsResult>;
|
|
19
|
+
export declare function resolveOutputDirs(options: {
|
|
20
|
+
outputs: string[];
|
|
21
|
+
projectName: string;
|
|
22
|
+
projectRoot: string;
|
|
23
|
+
workspaceRoot: string;
|
|
24
|
+
}): string[];
|
|
25
|
+
export {};
|
|
26
|
+
//# sourceMappingURL=executor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"executor.d.ts","sourceRoot":"","sources":["../../../src/executors/clean-outputs/executor.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAEvD,UAAU,mBAAmB;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,sBAAsB,CAAC,EAAE;QACvB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;KACrC,CAAC;CACH;AAED,UAAU,kBAAkB;IAC1B,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,UAAU,SAAS;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE;QAAE,OAAO,CAAC,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,CAAC;CAClD;AAED,wBAA8B,oBAAoB,CAChD,OAAO,EAAE,mBAAmB,EAC5B,OAAO,EAAE,mBAAmB,GAC3B,OAAO,CAAC,kBAAkB,CAAC,CA8B7B;AAED,wBAAgB,iBAAiB,CAAC,OAAO,EAAE;IACzC,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,aAAa,EAAE,MAAM,CAAC;CACvB,GAAG,MAAM,EAAE,CAOX"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { rm } from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
export default async function cleanOutputsExecutor(options, context) {
|
|
5
|
+
const projectName = context.projectName;
|
|
6
|
+
if (!projectName) {
|
|
7
|
+
throw new Error('@smoothbricks/nx-plugin:clean-outputs requires a project context.');
|
|
8
|
+
}
|
|
9
|
+
const workspaceRoot = context.root;
|
|
10
|
+
const project = context.projectsConfigurations?.projects[projectName];
|
|
11
|
+
if (!project) {
|
|
12
|
+
throw new Error(`Project ${projectName} was not found in the Nx project graph.`);
|
|
13
|
+
}
|
|
14
|
+
const outputDirs = resolveOutputDirs({
|
|
15
|
+
outputs: options.outputs ?? project.targets?.build?.outputs ?? ['{projectRoot}/dist'],
|
|
16
|
+
projectName,
|
|
17
|
+
projectRoot: project.root ?? '.',
|
|
18
|
+
workspaceRoot,
|
|
19
|
+
});
|
|
20
|
+
for (const outputDir of outputDirs) {
|
|
21
|
+
assertSafeOutputDir(outputDir, project.root ?? '.', workspaceRoot);
|
|
22
|
+
if (containsTrackedFiles(outputDir, workspaceRoot)) {
|
|
23
|
+
console.log(`skipped ${path.relative(workspaceRoot, outputDir)} (contains tracked files)`);
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
await rm(outputDir, { force: true, recursive: true });
|
|
27
|
+
console.log(`removed ${path.relative(workspaceRoot, outputDir)}`);
|
|
28
|
+
}
|
|
29
|
+
return { success: true };
|
|
30
|
+
}
|
|
31
|
+
export function resolveOutputDirs(options) {
|
|
32
|
+
const concreteOutputDirs = options.outputs.map((output) => outputDir(output, options));
|
|
33
|
+
const dedupedOutputDirs = [...new Set(concreteOutputDirs)].sort((a, b) => a.length - b.length);
|
|
34
|
+
return dedupedOutputDirs.filter((candidate, index) => !dedupedOutputDirs.some((other, otherIndex) => otherIndex < index && isWithin(candidate, other)));
|
|
35
|
+
}
|
|
36
|
+
function outputDir(output, options) {
|
|
37
|
+
const interpolated = output
|
|
38
|
+
.replaceAll('{workspaceRoot}', '.')
|
|
39
|
+
.replaceAll('{projectRoot}', options.projectRoot)
|
|
40
|
+
.replaceAll('{projectName}', options.projectName);
|
|
41
|
+
const segments = interpolated.split(/[\\/]/);
|
|
42
|
+
const globIndex = segments.findIndex((segment) => segment.includes('*'));
|
|
43
|
+
const dirSegments = globIndex === -1 ? segments : segments.slice(0, globIndex);
|
|
44
|
+
const outputPath = dirSegments.join(path.sep);
|
|
45
|
+
return path.resolve(options.workspaceRoot, outputPath);
|
|
46
|
+
}
|
|
47
|
+
function assertSafeOutputDir(outputDir, projectRoot, workspaceRoot) {
|
|
48
|
+
const absoluteWorkspaceRoot = path.resolve(workspaceRoot);
|
|
49
|
+
const absoluteProjectRoot = path.resolve(workspaceRoot, projectRoot);
|
|
50
|
+
if (!isWithin(outputDir, absoluteWorkspaceRoot)) {
|
|
51
|
+
throw new Error(`Refusing to remove output outside workspace: ${outputDir}`);
|
|
52
|
+
}
|
|
53
|
+
if (outputDir === absoluteWorkspaceRoot || outputDir === absoluteProjectRoot) {
|
|
54
|
+
throw new Error(`Refusing to remove unsafe output directory: ${path.relative(workspaceRoot, outputDir) || '.'}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function isWithin(candidate, parent) {
|
|
58
|
+
const relative = path.relative(parent, candidate);
|
|
59
|
+
return relative === '' || (!relative.startsWith('..') && !path.isAbsolute(relative));
|
|
60
|
+
}
|
|
61
|
+
function containsTrackedFiles(outputDir, workspaceRoot) {
|
|
62
|
+
const relativeOutputDir = path.relative(workspaceRoot, outputDir);
|
|
63
|
+
const trackedFiles = execFileSync('git', ['ls-files', '--', relativeOutputDir], {
|
|
64
|
+
cwd: workspaceRoot,
|
|
65
|
+
encoding: 'utf8',
|
|
66
|
+
});
|
|
67
|
+
return trackedFiles.trim().length > 0;
|
|
68
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../../src/executors/clean-outputs/schema.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,mBAAmB;IAClC,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;CACpB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../../../src/generators/bun-test-tracing/generator.ts"],"names":[],"mappings":"AACA,OAAO,EAML,KAAK,IAAI,EAEV,MAAM,0BAA0B,CAAC;AAIlC,UAAU,6BAA6B;IACrC,OAAO,EAAE,MAAM,CAAC;IAChB,eAAe,EAAE,MAAM,CAAC;IACxB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;
|
|
1
|
+
{"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../../../src/generators/bun-test-tracing/generator.ts"],"names":[],"mappings":"AACA,OAAO,EAML,KAAK,IAAI,EAEV,MAAM,0BAA0B,CAAC;AAIlC,UAAU,6BAA6B;IACrC,OAAO,EAAE,MAAM,CAAC;IAChB,eAAe,EAAE,MAAM,CAAC;IACxB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAgCD,wBAA8B,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,6BAA6B,GAAG,OAAO,CAAC,IAAI,CAAC,CAuBxG"}
|
|
@@ -9,7 +9,6 @@ const DEFAULT_TEST_INCLUDES = [
|
|
|
9
9
|
'src/test-suite-tracer.ts',
|
|
10
10
|
];
|
|
11
11
|
const COPIED_COMPILER_OPTIONS = ['baseUrl', 'module', 'moduleResolution', 'jsx', 'lib'];
|
|
12
|
-
const DEFAULT_BUN_TEST_TIMEOUT_MS = 30_000;
|
|
13
12
|
export default async function generator(tree, schema) {
|
|
14
13
|
const options = normalizeOptions(schema);
|
|
15
14
|
const resolved = resolveProject(tree, options.project);
|
|
@@ -77,39 +76,36 @@ function writeBunfig(tree, projectRoot) {
|
|
|
77
76
|
const bunfigPath = joinPathFragments(projectRoot, 'bunfig.toml');
|
|
78
77
|
const preloadEntries = '"@smoothbricks/lmao/bun/preload", "@smoothbricks/lmao/bun/trace-preload"';
|
|
79
78
|
if (!tree.exists(bunfigPath)) {
|
|
80
|
-
tree.write(bunfigPath, `[test]\
|
|
79
|
+
tree.write(bunfigPath, `[test]\npreload = [${preloadEntries}]\n`);
|
|
81
80
|
return;
|
|
82
81
|
}
|
|
83
|
-
|
|
84
|
-
|
|
82
|
+
// Bun silently ignores `[test] timeout` in bunfig.toml — only the `--timeout`
|
|
83
|
+
// CLI flag and `setDefaultTimeout()` actually take effect. Strip any stale
|
|
84
|
+
// `timeout = N` lines so the file isn't lying about what's enforced; the real
|
|
85
|
+
// per-test timeout is wired into `bun test --timeout=N` by the bounded-test
|
|
86
|
+
// policy (see BOUNDED_TEST_PER_TEST_TIMEOUT_MS).
|
|
87
|
+
const current = stripBunTestTimeoutLine(tree.read(bunfigPath, 'utf-8') ?? '');
|
|
85
88
|
// Already has the package specifier preloads
|
|
86
|
-
if (
|
|
87
|
-
if (
|
|
88
|
-
tree.write(bunfigPath,
|
|
89
|
+
if (current.includes('@smoothbricks/lmao/bun/preload')) {
|
|
90
|
+
if (current !== (tree.read(bunfigPath, 'utf-8') ?? '')) {
|
|
91
|
+
tree.write(bunfigPath, current);
|
|
89
92
|
}
|
|
90
93
|
return;
|
|
91
94
|
}
|
|
92
95
|
// Replace old patterns with the package specifier preloads
|
|
93
|
-
if (
|
|
94
|
-
tree.write(bunfigPath,
|
|
96
|
+
if (current.includes('preload = [')) {
|
|
97
|
+
tree.write(bunfigPath, current.replace(/preload\s*=\s*\[.*?\]/s, `preload = [${preloadEntries}]`));
|
|
95
98
|
return;
|
|
96
99
|
}
|
|
97
|
-
if (next.includes('[test]')) {
|
|
98
|
-
tree.write(bunfigPath, next.replace('[test]', `[test]\npreload = [${preloadEntries}]`));
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
const suffix = next.endsWith('\n') ? '' : '\n';
|
|
102
|
-
tree.write(bunfigPath, `${next}${suffix}[test]\ntimeout = ${DEFAULT_BUN_TEST_TIMEOUT_MS}\npreload = [${preloadEntries}]\n`);
|
|
103
|
-
}
|
|
104
|
-
function ensureBunTestTimeout(current) {
|
|
105
|
-
if (/(^|\n)\s*timeout\s*=/.test(current)) {
|
|
106
|
-
return current;
|
|
107
|
-
}
|
|
108
100
|
if (current.includes('[test]')) {
|
|
109
|
-
|
|
101
|
+
tree.write(bunfigPath, current.replace('[test]', `[test]\npreload = [${preloadEntries}]`));
|
|
102
|
+
return;
|
|
110
103
|
}
|
|
111
104
|
const suffix = current.endsWith('\n') ? '' : '\n';
|
|
112
|
-
|
|
105
|
+
tree.write(bunfigPath, `${current}${suffix}[test]\npreload = [${preloadEntries}]\n`);
|
|
106
|
+
}
|
|
107
|
+
function stripBunTestTimeoutLine(current) {
|
|
108
|
+
return current.replace(/^\s*timeout\s*=.*\n?/gm, '');
|
|
113
109
|
}
|
|
114
110
|
function renderSuiteTracer(options) {
|
|
115
111
|
return [
|
package/dist/index.js
CHANGED
|
@@ -98,6 +98,10 @@ async function createProjectTargets(packageJsonPath, workspaceRoot) {
|
|
|
98
98
|
cache: true,
|
|
99
99
|
dependsOn: ['^build', ...BUILD_OUTPUT_DEPENDENCIES],
|
|
100
100
|
};
|
|
101
|
+
targets.clean = {
|
|
102
|
+
executor: '@smoothbricks/nx-plugin:clean-outputs',
|
|
103
|
+
cache: false,
|
|
104
|
+
};
|
|
101
105
|
}
|
|
102
106
|
if (targets['typecheck-tests']) {
|
|
103
107
|
if (hasLibTsconfig) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"release-config-policy.d.ts","sourceRoot":"","sources":["../src/release-config-policy.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,0BAA0B,CAAC;AAGrD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAElE,eAAO,MAAM,uBAAuB,4CAA4C,CAAC;AACjF,eAAO,MAAM,2BAA2B,4BAA4B,CAAC;AAErE;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,aAAa,EAAE,
|
|
1
|
+
{"version":3,"file":"release-config-policy.d.ts","sourceRoot":"","sources":["../src/release-config-policy.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,0BAA0B,CAAC;AAGrD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAElE,eAAO,MAAM,uBAAuB,4CAA4C,CAAC;AACjF,eAAO,MAAM,2BAA2B,4BAA4B,CAAC;AAErE;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,aAAa,EAAE,CAuFnF;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAuC3E;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,IAAI,GAAG,aAAa,EAAE,CAKlE;AAED;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAU1D;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa,EAAE,CAYtE;AAED;;GAEG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAW9D"}
|
|
@@ -55,6 +55,9 @@ export function checkReleaseConfig(nxJson) {
|
|
|
55
55
|
if (!changelog) {
|
|
56
56
|
issues.push({ path: 'nx.json', message: 'release.changelog config is missing' });
|
|
57
57
|
}
|
|
58
|
+
if (changelog && changelog.automaticFromRef !== true) {
|
|
59
|
+
issues.push({ path: 'nx.json', message: 'release.changelog.automaticFromRef must be true' });
|
|
60
|
+
}
|
|
58
61
|
if (changelog && changelog.workspaceChangelog !== false) {
|
|
59
62
|
issues.push({ path: 'nx.json', message: 'release.changelog.workspaceChangelog must be false' });
|
|
60
63
|
}
|
|
@@ -103,6 +106,7 @@ export function applyReleaseConfig(nxJson) {
|
|
|
103
106
|
const releaseTag = getOrCreateRecord(release, 'releaseTag');
|
|
104
107
|
changed = setStringProperty(releaseTag, 'pattern', SMOO_NX_RELEASE_TAG_PATTERN) || changed;
|
|
105
108
|
const changelog = getOrCreateRecord(release, 'changelog');
|
|
109
|
+
changed = setBooleanProperty(changelog, 'automaticFromRef', true) || changed;
|
|
106
110
|
changed = setBooleanProperty(changelog, 'workspaceChangelog', false) || changed;
|
|
107
111
|
const projectChangelogs = getOrCreateRecord(changelog, 'projectChangelogs');
|
|
108
112
|
changed = setBooleanProperty(projectChangelogs, 'createRelease', false) || changed;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"workspace-config-policy.d.ts","sourceRoot":"","sources":["../src/workspace-config-policy.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,0BAA0B,CAAC;AAGrD,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,eAAO,MAAM,yBAAyB,UAWrC,CAAC;
|
|
1
|
+
{"version":3,"file":"workspace-config-policy.d.ts","sourceRoot":"","sources":["../src/workspace-config-policy.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,0BAA0B,CAAC;AAGrD,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,eAAO,MAAM,yBAAyB,UAWrC,CAAC;AAeF;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,aAAa,EAAE,CAuDrF;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAe7E;AAED;;GAEG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,IAAI,GAAG,aAAa,EAAE,CAKpE;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAU5D;AAED;;GAEG;AACH,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa,EAAE,CAYxE;AAED;;GAEG;AACH,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAWhE"}
|
|
@@ -15,6 +15,7 @@ export const BUILD_OUTPUT_DEPENDENCIES = [
|
|
|
15
15
|
];
|
|
16
16
|
const nxJsTypescriptPlugin = '@nx/js/typescript';
|
|
17
17
|
const smoothBricksNxPlugin = '@smoothbricks/nx-plugin';
|
|
18
|
+
const cleanOutputsExecutor = '@smoothbricks/nx-plugin:clean-outputs';
|
|
18
19
|
const expectedSharedGlobalsNamedInput = ['{workspaceRoot}/.github/workflows/ci.yml'];
|
|
19
20
|
const defaultProductionNamedInput = [
|
|
20
21
|
'{projectRoot}/src/**/*',
|
|
@@ -44,6 +45,8 @@ export function checkWorkspaceConfig(nxJson) {
|
|
|
44
45
|
}
|
|
45
46
|
// Build target default
|
|
46
47
|
validateBuildTargetDefault(nxJson, 'nx.json', issues);
|
|
48
|
+
// Clean target default
|
|
49
|
+
validateCleanTargetDefault(nxJson, 'nx.json', issues);
|
|
47
50
|
// Named input defaults
|
|
48
51
|
validateNamedInputDefaults(nxJson, 'nx.json', issues);
|
|
49
52
|
// Plugin configuration
|
|
@@ -79,6 +82,7 @@ export function checkWorkspaceConfig(nxJson) {
|
|
|
79
82
|
export function applyWorkspaceConfig(nxJson) {
|
|
80
83
|
let changed = removeColonTargetDefaults(nxJson);
|
|
81
84
|
changed = applyBuildTargetDefault(nxJson) || changed;
|
|
85
|
+
changed = applyCleanTargetDefault(nxJson) || changed;
|
|
82
86
|
changed = applyNamedInputDefaults(nxJson) || changed;
|
|
83
87
|
const currentPlugins = Array.isArray(nxJson.plugins) ? nxJson.plugins : [];
|
|
84
88
|
const nextPlugins = upsertNxPlugin(upsertNxPlugin(currentPlugins, expectedNxJsTypescriptPlugin()), smoothBricksNxPlugin);
|
|
@@ -154,6 +158,16 @@ function validateBuildTargetDefault(nxJson, nxJsonPath, issues) {
|
|
|
154
158
|
issues.push({ path: nxJsonPath, message: 'targetDefaults.build.outputs must be ["{projectRoot}/dist"]' });
|
|
155
159
|
}
|
|
156
160
|
}
|
|
161
|
+
function validateCleanTargetDefault(nxJson, nxJsonPath, issues) {
|
|
162
|
+
const targetDefaults = recordProperty(nxJson, 'targetDefaults');
|
|
163
|
+
const clean = targetDefaults ? recordProperty(targetDefaults, 'clean') : null;
|
|
164
|
+
if (!clean || clean.executor !== cleanOutputsExecutor) {
|
|
165
|
+
issues.push({ path: nxJsonPath, message: `targetDefaults.clean.executor must be ${cleanOutputsExecutor}` });
|
|
166
|
+
}
|
|
167
|
+
if (!clean || clean.cache !== false) {
|
|
168
|
+
issues.push({ path: nxJsonPath, message: 'targetDefaults.clean.cache must be false' });
|
|
169
|
+
}
|
|
170
|
+
}
|
|
157
171
|
function validateNamedInputDefaults(nxJson, nxJsonPath, issues) {
|
|
158
172
|
const namedInputs = recordProperty(nxJson, 'namedInputs');
|
|
159
173
|
if (!namedInputs) {
|
|
@@ -197,6 +211,13 @@ function applyBuildTargetDefault(nxJson) {
|
|
|
197
211
|
changed = setStringArrayProperty(build, 'outputs', ['{projectRoot}/dist']) || changed;
|
|
198
212
|
return changed;
|
|
199
213
|
}
|
|
214
|
+
function applyCleanTargetDefault(nxJson) {
|
|
215
|
+
const targetDefaults = getOrCreateRecord(nxJson, 'targetDefaults');
|
|
216
|
+
const clean = getOrCreateRecord(targetDefaults, 'clean');
|
|
217
|
+
let changed = setStringProperty(clean, 'executor', cleanOutputsExecutor);
|
|
218
|
+
changed = setBooleanProperty(clean, 'cache', false) || changed;
|
|
219
|
+
return changed;
|
|
220
|
+
}
|
|
200
221
|
function applyNamedInputDefaults(nxJson) {
|
|
201
222
|
const namedInputs = getOrCreateRecord(nxJson, 'namedInputs');
|
|
202
223
|
let changed = false;
|
|
@@ -321,6 +342,13 @@ function setBooleanProperty(record, key, value) {
|
|
|
321
342
|
record[key] = value;
|
|
322
343
|
return true;
|
|
323
344
|
}
|
|
345
|
+
function setStringProperty(record, key, value) {
|
|
346
|
+
if (record[key] === value) {
|
|
347
|
+
return false;
|
|
348
|
+
}
|
|
349
|
+
record[key] = value;
|
|
350
|
+
return true;
|
|
351
|
+
}
|
|
324
352
|
function setStringArrayProperty(record, key, value) {
|
|
325
353
|
const current = record[key];
|
|
326
354
|
if (stringArrayEquals(current, value)) {
|
package/executors.json
CHANGED
|
@@ -4,6 +4,11 @@
|
|
|
4
4
|
"implementation": "./dist/executors/bounded-exec/executor.js",
|
|
5
5
|
"schema": "./src/executors/bounded-exec/schema.json",
|
|
6
6
|
"description": "Run a command with bounded timeout and force-kill behavior"
|
|
7
|
+
},
|
|
8
|
+
"clean-outputs": {
|
|
9
|
+
"implementation": "./dist/executors/clean-outputs/executor.js",
|
|
10
|
+
"schema": "./src/executors/clean-outputs/schema.json",
|
|
11
|
+
"description": "Remove declared build output directories unless they contain tracked files"
|
|
7
12
|
}
|
|
8
13
|
}
|
|
9
14
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smoothbricks/nx-plugin",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./loader.js",
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
"test": {
|
|
62
62
|
"executor": "@smoothbricks/nx-plugin:bounded-exec",
|
|
63
63
|
"options": {
|
|
64
|
-
"command": "bun test",
|
|
64
|
+
"command": "bun test --timeout=30000",
|
|
65
65
|
"cwd": "{projectRoot}",
|
|
66
66
|
"timeoutMs": 120000,
|
|
67
67
|
"killAfterMs": 1e4
|
|
@@ -8,15 +8,19 @@ import {
|
|
|
8
8
|
applyWorkspaceBoundedTestTargetPolicy,
|
|
9
9
|
BOUNDED_TEST_EXECUTOR,
|
|
10
10
|
BOUNDED_TEST_KILL_AFTER_MS,
|
|
11
|
+
BOUNDED_TEST_PER_TEST_TIMEOUT_MS,
|
|
11
12
|
BOUNDED_TEST_TIMEOUT_MS,
|
|
12
13
|
type BoundedTestPolicyPackageJson,
|
|
13
14
|
type BoundedTestPolicyProjectJson,
|
|
14
15
|
boundedTestScriptAlias,
|
|
15
16
|
checkBoundedTestTargetPolicy,
|
|
16
17
|
checkWorkspaceBoundedTestTargetPolicy,
|
|
18
|
+
ensureBunTestTimeoutFlag,
|
|
17
19
|
resolveTestCommand,
|
|
18
20
|
} from './bounded-test-policy.js';
|
|
19
21
|
|
|
22
|
+
const TIMEOUT_FLAG = `--timeout=${BOUNDED_TEST_PER_TEST_TIMEOUT_MS}`;
|
|
23
|
+
|
|
20
24
|
describe('bounded test target policy', () => {
|
|
21
25
|
it('preserves an existing nx:run-commands test command', () => {
|
|
22
26
|
const packageJson: BoundedTestPolicyPackageJson = {
|
|
@@ -38,7 +42,7 @@ describe('bounded test target policy', () => {
|
|
|
38
42
|
executor: BOUNDED_TEST_EXECUTOR,
|
|
39
43
|
dependsOn: ['typecheck-tests'],
|
|
40
44
|
options: {
|
|
41
|
-
command:
|
|
45
|
+
command: `bun test ${TIMEOUT_FLAG} --coverage`,
|
|
42
46
|
cwd: '{projectRoot}',
|
|
43
47
|
timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
|
|
44
48
|
killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
|
|
@@ -57,7 +61,7 @@ describe('bounded test target policy', () => {
|
|
|
57
61
|
expect(packageJson.nx?.targets?.test).toEqual({
|
|
58
62
|
executor: BOUNDED_TEST_EXECUTOR,
|
|
59
63
|
options: {
|
|
60
|
-
command:
|
|
64
|
+
command: `bun test ${TIMEOUT_FLAG} --pass-with-no-tests`,
|
|
61
65
|
cwd: '{projectRoot}',
|
|
62
66
|
timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
|
|
63
67
|
killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
|
|
@@ -94,7 +98,7 @@ describe('bounded test target policy', () => {
|
|
|
94
98
|
executor: BOUNDED_TEST_EXECUTOR,
|
|
95
99
|
dependsOn: ['typecheck-tests'],
|
|
96
100
|
options: {
|
|
97
|
-
command:
|
|
101
|
+
command: `bun test ${TIMEOUT_FLAG} --project-target`,
|
|
98
102
|
cwd: '{projectRoot}',
|
|
99
103
|
timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
|
|
100
104
|
killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
|
|
@@ -120,7 +124,7 @@ describe('bounded test target policy', () => {
|
|
|
120
124
|
expect(projectJson.targets?.test).toEqual({
|
|
121
125
|
executor: BOUNDED_TEST_EXECUTOR,
|
|
122
126
|
options: {
|
|
123
|
-
command:
|
|
127
|
+
command: `bun test ${TIMEOUT_FLAG} --script`,
|
|
124
128
|
cwd: '{projectRoot}',
|
|
125
129
|
timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
|
|
126
130
|
killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
|
|
@@ -157,7 +161,20 @@ describe('bounded test target policy', () => {
|
|
|
157
161
|
scripts: { test: boundedTestScriptAlias('example') },
|
|
158
162
|
};
|
|
159
163
|
|
|
160
|
-
expect(resolveTestCommand(packageJson)).toBe(
|
|
164
|
+
expect(resolveTestCommand(packageJson)).toBe(`bun test ${TIMEOUT_FLAG}`);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('idempotently appends --timeout to bare bun test commands', () => {
|
|
168
|
+
expect(ensureBunTestTimeoutFlag('bun test')).toBe(`bun test ${TIMEOUT_FLAG}`);
|
|
169
|
+
expect(ensureBunTestTimeoutFlag('bun test --pass-with-no-tests')).toBe(
|
|
170
|
+
`bun test ${TIMEOUT_FLAG} --pass-with-no-tests`,
|
|
171
|
+
);
|
|
172
|
+
expect(ensureBunTestTimeoutFlag(`bun test ${TIMEOUT_FLAG}`)).toBe(`bun test ${TIMEOUT_FLAG}`);
|
|
173
|
+
expect(ensureBunTestTimeoutFlag('bun test --timeout=10')).toBe(`bun test ${TIMEOUT_FLAG}`);
|
|
174
|
+
expect(ensureBunTestTimeoutFlag('bun test --timeout 10 --bail')).toBe(`bun test ${TIMEOUT_FLAG} --bail`);
|
|
175
|
+
// Non-bun-test commands are left alone (vitest, custom runners, etc.).
|
|
176
|
+
expect(ensureBunTestTimeoutFlag('vitest run')).toBe('vitest run');
|
|
177
|
+
expect(ensureBunTestTimeoutFlag('bun run test')).toBe('bun run test');
|
|
161
178
|
});
|
|
162
179
|
|
|
163
180
|
it('checks and fixes workspace package test targets', async () => {
|
|
@@ -188,7 +205,7 @@ describe('bounded test target policy', () => {
|
|
|
188
205
|
expect(app.nx.targets.test).toEqual({
|
|
189
206
|
executor: BOUNDED_TEST_EXECUTOR,
|
|
190
207
|
options: {
|
|
191
|
-
command:
|
|
208
|
+
command: `bun test ${TIMEOUT_FLAG} --pass-with-no-tests`,
|
|
192
209
|
cwd: '{projectRoot}',
|
|
193
210
|
timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
|
|
194
211
|
killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
|
|
@@ -238,7 +255,7 @@ describe('bounded test target policy', () => {
|
|
|
238
255
|
expect(appProject.targets.test).toEqual({
|
|
239
256
|
executor: BOUNDED_TEST_EXECUTOR,
|
|
240
257
|
options: {
|
|
241
|
-
command:
|
|
258
|
+
command: `bun test ${TIMEOUT_FLAG} --project`,
|
|
242
259
|
cwd: '{projectRoot}',
|
|
243
260
|
timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
|
|
244
261
|
killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
|
|
@@ -4,6 +4,11 @@ import { dirname, join } from 'node:path';
|
|
|
4
4
|
export const BOUNDED_TEST_EXECUTOR = '@smoothbricks/nx-plugin:bounded-exec';
|
|
5
5
|
export const BOUNDED_TEST_TIMEOUT_MS = 120_000;
|
|
6
6
|
export const BOUNDED_TEST_KILL_AFTER_MS = 10_000;
|
|
7
|
+
// Per-test timeout passed to `bun test --timeout=<ms>`. Bun's default is
|
|
8
|
+
// 5000ms which is too tight for git-fixture tests on slower CI runners
|
|
9
|
+
// (Actions). The bunfig.toml `[test] timeout` key is silently ignored by
|
|
10
|
+
// Bun — the only working surfaces are this CLI flag and `setDefaultTimeout()`.
|
|
11
|
+
export const BOUNDED_TEST_PER_TEST_TIMEOUT_MS = 30_000;
|
|
7
12
|
|
|
8
13
|
export interface BoundedTestPolicyPackageJson {
|
|
9
14
|
name?: string;
|
|
@@ -124,6 +129,7 @@ export function checkBoundedTestTargetPolicy(
|
|
|
124
129
|
typeof targetOptions.command !== 'string' ||
|
|
125
130
|
targetOptions.command.length === 0 ||
|
|
126
131
|
isPackageTestScriptRunnerCommand(targetOptions.command) ||
|
|
132
|
+
targetOptions.command !== ensureBunTestTimeoutFlag(targetOptions.command) ||
|
|
127
133
|
targetOptions.cwd !== '{projectRoot}' ||
|
|
128
134
|
targetOptions.timeoutMs !== BOUNDED_TEST_TIMEOUT_MS ||
|
|
129
135
|
targetOptions.killAfterMs !== BOUNDED_TEST_KILL_AFTER_MS
|
|
@@ -141,6 +147,14 @@ export function resolveTestCommand(
|
|
|
141
147
|
packageJson: BoundedTestPolicyPackageJson,
|
|
142
148
|
defaultCommand = 'bun test',
|
|
143
149
|
projectJson?: BoundedTestPolicyProjectJson,
|
|
150
|
+
): string {
|
|
151
|
+
return ensureBunTestTimeoutFlag(resolveRawTestCommand(packageJson, defaultCommand, projectJson));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function resolveRawTestCommand(
|
|
155
|
+
packageJson: BoundedTestPolicyPackageJson,
|
|
156
|
+
defaultCommand: string,
|
|
157
|
+
projectJson: BoundedTestPolicyProjectJson | undefined,
|
|
144
158
|
): string {
|
|
145
159
|
const commandFromProjectTarget = resolveTargetCommand(projectJson?.targets?.test);
|
|
146
160
|
if (commandFromProjectTarget) {
|
|
@@ -175,6 +189,28 @@ function resolveTargetCommand(existingTarget: unknown): string | null {
|
|
|
175
189
|
return null;
|
|
176
190
|
}
|
|
177
191
|
|
|
192
|
+
const BUN_TEST_PREFIX = /^bun\s+test(?=\s|$)/;
|
|
193
|
+
const EXISTING_BUN_TIMEOUT = /(^|\s)--timeout(?:=|\s+)(\S+)/;
|
|
194
|
+
|
|
195
|
+
// Bun's `[test] timeout` bunfig key is silently ignored — only the CLI flag
|
|
196
|
+
// and `setDefaultTimeout()` actually take effect. Normalize every `bun test`
|
|
197
|
+
// command so the policy enforces the workspace per-test timeout in the one
|
|
198
|
+
// place where Bun will honor it.
|
|
199
|
+
export function ensureBunTestTimeoutFlag(command: string, timeoutMs = BOUNDED_TEST_PER_TEST_TIMEOUT_MS): string {
|
|
200
|
+
if (!BUN_TEST_PREFIX.test(command)) {
|
|
201
|
+
return command;
|
|
202
|
+
}
|
|
203
|
+
const flag = `--timeout=${timeoutMs}`;
|
|
204
|
+
const existing = EXISTING_BUN_TIMEOUT.exec(command);
|
|
205
|
+
if (!existing) {
|
|
206
|
+
return command.replace(BUN_TEST_PREFIX, `bun test ${flag}`);
|
|
207
|
+
}
|
|
208
|
+
if (existing[2] === String(timeoutMs)) {
|
|
209
|
+
return command;
|
|
210
|
+
}
|
|
211
|
+
return command.replace(EXISTING_BUN_TIMEOUT, `${existing[1]}${flag}`);
|
|
212
|
+
}
|
|
213
|
+
|
|
178
214
|
function isPackageTestScriptRunnerCommand(command: string): boolean {
|
|
179
215
|
return /^(?:bun\s+run|npm(?:\s+run)?|pnpm(?:\s+run)?|yarn(?:\s+run)?)\s+test(?:\s|$)/.test(command.trim());
|
|
180
216
|
}
|
|
@@ -15,9 +15,11 @@ describe('Bun bootstrap imports', () => {
|
|
|
15
15
|
expect(typeof policy.checkBoundedTestTargetPolicy).toBe('function');
|
|
16
16
|
expect(typeof policy.boundedTestScriptAlias).toBe('function');
|
|
17
17
|
expect(typeof policy.resolveTestCommand).toBe('function');
|
|
18
|
+
expect(typeof policy.ensureBunTestTimeoutFlag).toBe('function');
|
|
18
19
|
expect(typeof policy.BOUNDED_TEST_EXECUTOR).toBe('string');
|
|
19
20
|
expect(typeof policy.BOUNDED_TEST_TIMEOUT_MS).toBe('number');
|
|
20
21
|
expect(typeof policy.BOUNDED_TEST_KILL_AFTER_MS).toBe('number');
|
|
22
|
+
expect(typeof policy.BOUNDED_TEST_PER_TEST_TIMEOUT_MS).toBe('number');
|
|
21
23
|
});
|
|
22
24
|
|
|
23
25
|
it('imports workspace-config-policy', async () => {
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { tmpdir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
|
|
5
|
+
import { resolveOutputDirs } from './executor.js';
|
|
6
|
+
|
|
7
|
+
describe('clean-outputs executor', () => {
|
|
8
|
+
it('resolves declared output patterns to safe concrete directories', () => {
|
|
9
|
+
const workspaceRoot = join(tmpdir(), 'smoothbricks-clean-outputs');
|
|
10
|
+
|
|
11
|
+
expect(
|
|
12
|
+
resolveOutputDirs({
|
|
13
|
+
outputs: ['{projectRoot}/dist/**/*.js', '{projectRoot}/dist/types', '{projectRoot}/dist-wasm-web'],
|
|
14
|
+
projectName: 'example',
|
|
15
|
+
projectRoot: 'packages/example',
|
|
16
|
+
workspaceRoot,
|
|
17
|
+
}).map((outputDir) => outputDir.slice(workspaceRoot.length + 1)),
|
|
18
|
+
).toEqual(['packages/example/dist', 'packages/example/dist-wasm-web']);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { rm } from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
import type { CleanOutputsOptions } from './schema.js';
|
|
6
|
+
|
|
7
|
+
interface CleanOutputsContext {
|
|
8
|
+
root: string;
|
|
9
|
+
projectName?: string;
|
|
10
|
+
projectsConfigurations?: {
|
|
11
|
+
projects: Record<string, NxProject>;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface CleanOutputsResult {
|
|
16
|
+
success: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface NxProject {
|
|
20
|
+
root?: string;
|
|
21
|
+
targets?: Record<string, { outputs?: string[] }>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default async function cleanOutputsExecutor(
|
|
25
|
+
options: CleanOutputsOptions,
|
|
26
|
+
context: CleanOutputsContext,
|
|
27
|
+
): Promise<CleanOutputsResult> {
|
|
28
|
+
const projectName = context.projectName;
|
|
29
|
+
if (!projectName) {
|
|
30
|
+
throw new Error('@smoothbricks/nx-plugin:clean-outputs requires a project context.');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const workspaceRoot = context.root;
|
|
34
|
+
const project = context.projectsConfigurations?.projects[projectName];
|
|
35
|
+
if (!project) {
|
|
36
|
+
throw new Error(`Project ${projectName} was not found in the Nx project graph.`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const outputDirs = resolveOutputDirs({
|
|
40
|
+
outputs: options.outputs ?? project.targets?.build?.outputs ?? ['{projectRoot}/dist'],
|
|
41
|
+
projectName,
|
|
42
|
+
projectRoot: project.root ?? '.',
|
|
43
|
+
workspaceRoot,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
for (const outputDir of outputDirs) {
|
|
47
|
+
assertSafeOutputDir(outputDir, project.root ?? '.', workspaceRoot);
|
|
48
|
+
if (containsTrackedFiles(outputDir, workspaceRoot)) {
|
|
49
|
+
console.log(`skipped ${path.relative(workspaceRoot, outputDir)} (contains tracked files)`);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
await rm(outputDir, { force: true, recursive: true });
|
|
53
|
+
console.log(`removed ${path.relative(workspaceRoot, outputDir)}`);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return { success: true };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function resolveOutputDirs(options: {
|
|
60
|
+
outputs: string[];
|
|
61
|
+
projectName: string;
|
|
62
|
+
projectRoot: string;
|
|
63
|
+
workspaceRoot: string;
|
|
64
|
+
}): string[] {
|
|
65
|
+
const concreteOutputDirs = options.outputs.map((output) => outputDir(output, options));
|
|
66
|
+
const dedupedOutputDirs = [...new Set(concreteOutputDirs)].sort((a, b) => a.length - b.length);
|
|
67
|
+
return dedupedOutputDirs.filter(
|
|
68
|
+
(candidate, index) =>
|
|
69
|
+
!dedupedOutputDirs.some((other, otherIndex) => otherIndex < index && isWithin(candidate, other)),
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function outputDir(
|
|
74
|
+
output: string,
|
|
75
|
+
options: { projectName: string; projectRoot: string; workspaceRoot: string },
|
|
76
|
+
): string {
|
|
77
|
+
const interpolated = output
|
|
78
|
+
.replaceAll('{workspaceRoot}', '.')
|
|
79
|
+
.replaceAll('{projectRoot}', options.projectRoot)
|
|
80
|
+
.replaceAll('{projectName}', options.projectName);
|
|
81
|
+
|
|
82
|
+
const segments = interpolated.split(/[\\/]/);
|
|
83
|
+
const globIndex = segments.findIndex((segment) => segment.includes('*'));
|
|
84
|
+
const dirSegments = globIndex === -1 ? segments : segments.slice(0, globIndex);
|
|
85
|
+
const outputPath = dirSegments.join(path.sep);
|
|
86
|
+
|
|
87
|
+
return path.resolve(options.workspaceRoot, outputPath);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function assertSafeOutputDir(outputDir: string, projectRoot: string, workspaceRoot: string): void {
|
|
91
|
+
const absoluteWorkspaceRoot = path.resolve(workspaceRoot);
|
|
92
|
+
const absoluteProjectRoot = path.resolve(workspaceRoot, projectRoot);
|
|
93
|
+
|
|
94
|
+
if (!isWithin(outputDir, absoluteWorkspaceRoot)) {
|
|
95
|
+
throw new Error(`Refusing to remove output outside workspace: ${outputDir}`);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (outputDir === absoluteWorkspaceRoot || outputDir === absoluteProjectRoot) {
|
|
99
|
+
throw new Error(`Refusing to remove unsafe output directory: ${path.relative(workspaceRoot, outputDir) || '.'}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function isWithin(candidate: string, parent: string): boolean {
|
|
104
|
+
const relative = path.relative(parent, candidate);
|
|
105
|
+
return relative === '' || (!relative.startsWith('..') && !path.isAbsolute(relative));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function containsTrackedFiles(outputDir: string, workspaceRoot: string): boolean {
|
|
109
|
+
const relativeOutputDir = path.relative(workspaceRoot, outputDir);
|
|
110
|
+
const trackedFiles = execFileSync('git', ['ls-files', '--', relativeOutputDir], {
|
|
111
|
+
cwd: workspaceRoot,
|
|
112
|
+
encoding: 'utf8',
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
return trackedFiles.trim().length > 0;
|
|
116
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/schema",
|
|
3
|
+
"version": 2,
|
|
4
|
+
"title": "Clean Outputs Executor",
|
|
5
|
+
"description": "Remove declared build output directories unless they contain tracked files.",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"properties": {
|
|
8
|
+
"outputs": {
|
|
9
|
+
"type": "array",
|
|
10
|
+
"items": { "type": "string" },
|
|
11
|
+
"description": "Optional output patterns. Defaults to the project's build outputs, then {projectRoot}/dist."
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"additionalProperties": false
|
|
15
|
+
}
|
|
@@ -5,8 +5,12 @@ import { createTreeWithEmptyWorkspace } from 'nx/src/devkit-testing-exports.js';
|
|
|
5
5
|
import {
|
|
6
6
|
BOUNDED_TEST_EXECUTOR,
|
|
7
7
|
BOUNDED_TEST_KILL_AFTER_MS,
|
|
8
|
+
BOUNDED_TEST_PER_TEST_TIMEOUT_MS,
|
|
8
9
|
BOUNDED_TEST_TIMEOUT_MS,
|
|
9
10
|
} from '../../bounded-test-policy.js';
|
|
11
|
+
|
|
12
|
+
const TIMEOUT_FLAG = `--timeout=${BOUNDED_TEST_PER_TEST_TIMEOUT_MS}`;
|
|
13
|
+
|
|
10
14
|
import generator from './generator.js';
|
|
11
15
|
|
|
12
16
|
describe('bounded-test-targets generator', () => {
|
|
@@ -41,7 +45,7 @@ describe('bounded-test-targets generator', () => {
|
|
|
41
45
|
executor: BOUNDED_TEST_EXECUTOR,
|
|
42
46
|
dependsOn: ['typecheck-tests', '^build'],
|
|
43
47
|
options: {
|
|
44
|
-
command:
|
|
48
|
+
command: `bun test ${TIMEOUT_FLAG} --target`,
|
|
45
49
|
cwd: '{projectRoot}',
|
|
46
50
|
timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
|
|
47
51
|
killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
|
|
@@ -64,7 +68,7 @@ describe('bounded-test-targets generator', () => {
|
|
|
64
68
|
expect(packageJson.nx?.targets?.test).toEqual({
|
|
65
69
|
executor: BOUNDED_TEST_EXECUTOR,
|
|
66
70
|
options: {
|
|
67
|
-
command:
|
|
71
|
+
command: `bun test ${TIMEOUT_FLAG} --script`,
|
|
68
72
|
cwd: '{projectRoot}',
|
|
69
73
|
timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
|
|
70
74
|
killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
|
|
@@ -110,7 +114,7 @@ describe('bounded-test-targets generator', () => {
|
|
|
110
114
|
executor: BOUNDED_TEST_EXECUTOR,
|
|
111
115
|
dependsOn: ['typecheck-tests'],
|
|
112
116
|
options: {
|
|
113
|
-
command:
|
|
117
|
+
command: `bun test ${TIMEOUT_FLAG} --project-target`,
|
|
114
118
|
cwd: '{projectRoot}',
|
|
115
119
|
timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
|
|
116
120
|
killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
|
|
@@ -2,7 +2,11 @@ import { beforeEach, describe, expect, it } from 'bun:test';
|
|
|
2
2
|
import { addProjectConfiguration, readJson, type Tree } from 'nx/src/devkit-exports.js';
|
|
3
3
|
import { createTreeWithEmptyWorkspace } from 'nx/src/devkit-testing-exports.js';
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
BOUNDED_TEST_KILL_AFTER_MS,
|
|
7
|
+
BOUNDED_TEST_PER_TEST_TIMEOUT_MS,
|
|
8
|
+
BOUNDED_TEST_TIMEOUT_MS,
|
|
9
|
+
} from '../../bounded-test-policy.js';
|
|
6
10
|
import generator from './generator.js';
|
|
7
11
|
|
|
8
12
|
function referencePaths(references: Array<{ path: string }> | undefined): string[] {
|
|
@@ -57,7 +61,8 @@ describe('bun-test-tracing generator', () => {
|
|
|
57
61
|
expect(tree.read('packages/example/bunfig.toml', 'utf-8')).toContain(
|
|
58
62
|
'preload = ["@smoothbricks/lmao/bun/preload", "@smoothbricks/lmao/bun/trace-preload"]',
|
|
59
63
|
);
|
|
60
|
-
|
|
64
|
+
// Bun ignores `[test] timeout` in bunfig — per-test timeout lives on the bun CLI flag.
|
|
65
|
+
expect(tree.read('packages/example/bunfig.toml', 'utf-8')).not.toContain('timeout =');
|
|
61
66
|
|
|
62
67
|
const tracerFile = tree.read('packages/example/src/test-suite-tracer.ts', 'utf-8');
|
|
63
68
|
expect(tracerFile).toContain("import { opContext } from '@smoothbricks/lmao';");
|
|
@@ -72,7 +77,7 @@ describe('bun-test-tracing generator', () => {
|
|
|
72
77
|
executor: '@smoothbricks/nx-plugin:bounded-exec',
|
|
73
78
|
dependsOn: ['typecheck-tests', '^build'],
|
|
74
79
|
options: {
|
|
75
|
-
command:
|
|
80
|
+
command: `bun test --timeout=${BOUNDED_TEST_PER_TEST_TIMEOUT_MS}`,
|
|
76
81
|
cwd: '{projectRoot}',
|
|
77
82
|
timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
|
|
78
83
|
killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
|
|
@@ -142,7 +147,7 @@ describe('bun-test-tracing generator', () => {
|
|
|
142
147
|
|
|
143
148
|
const bunfig = tree.read('packages/example/bunfig.toml', 'utf-8') ?? '';
|
|
144
149
|
expect(bunfig).toContain('[test]');
|
|
145
|
-
expect(bunfig).toContain('timeout =
|
|
150
|
+
expect(bunfig).not.toContain('timeout =');
|
|
146
151
|
expect(bunfig).toContain('preload');
|
|
147
152
|
});
|
|
148
153
|
|
|
@@ -193,7 +198,9 @@ describe('bun-test-tracing generator', () => {
|
|
|
193
198
|
});
|
|
194
199
|
|
|
195
200
|
const bunfig = tree.read('packages/example/bunfig.toml', 'utf-8') ?? '';
|
|
196
|
-
|
|
201
|
+
// Stale `[test] timeout` lines are stripped — Bun ignores them and they
|
|
202
|
+
// mislead readers about what's actually enforced.
|
|
203
|
+
expect(bunfig).not.toContain('timeout =');
|
|
197
204
|
expect(bunfig).toContain('@smoothbricks/lmao/bun/preload');
|
|
198
205
|
expect(bunfig).toContain('@smoothbricks/lmao/bun/trace-preload');
|
|
199
206
|
|
|
@@ -47,7 +47,6 @@ const DEFAULT_TEST_INCLUDES = [
|
|
|
47
47
|
];
|
|
48
48
|
|
|
49
49
|
const COPIED_COMPILER_OPTIONS = ['baseUrl', 'module', 'moduleResolution', 'jsx', 'lib'];
|
|
50
|
-
const DEFAULT_BUN_TEST_TIMEOUT_MS = 30_000;
|
|
51
50
|
|
|
52
51
|
export default async function generator(tree: Tree, schema: BunTestTracingGeneratorSchema): Promise<void> {
|
|
53
52
|
const options = normalizeOptions(schema);
|
|
@@ -135,50 +134,42 @@ function writeBunfig(tree: Tree, projectRoot: string): void {
|
|
|
135
134
|
const preloadEntries = '"@smoothbricks/lmao/bun/preload", "@smoothbricks/lmao/bun/trace-preload"';
|
|
136
135
|
|
|
137
136
|
if (!tree.exists(bunfigPath)) {
|
|
138
|
-
tree.write(bunfigPath, `[test]\
|
|
137
|
+
tree.write(bunfigPath, `[test]\npreload = [${preloadEntries}]\n`);
|
|
139
138
|
return;
|
|
140
139
|
}
|
|
141
140
|
|
|
142
|
-
|
|
143
|
-
|
|
141
|
+
// Bun silently ignores `[test] timeout` in bunfig.toml — only the `--timeout`
|
|
142
|
+
// CLI flag and `setDefaultTimeout()` actually take effect. Strip any stale
|
|
143
|
+
// `timeout = N` lines so the file isn't lying about what's enforced; the real
|
|
144
|
+
// per-test timeout is wired into `bun test --timeout=N` by the bounded-test
|
|
145
|
+
// policy (see BOUNDED_TEST_PER_TEST_TIMEOUT_MS).
|
|
146
|
+
const current = stripBunTestTimeoutLine(tree.read(bunfigPath, 'utf-8') ?? '');
|
|
144
147
|
|
|
145
148
|
// Already has the package specifier preloads
|
|
146
|
-
if (
|
|
147
|
-
if (
|
|
148
|
-
tree.write(bunfigPath,
|
|
149
|
+
if (current.includes('@smoothbricks/lmao/bun/preload')) {
|
|
150
|
+
if (current !== (tree.read(bunfigPath, 'utf-8') ?? '')) {
|
|
151
|
+
tree.write(bunfigPath, current);
|
|
149
152
|
}
|
|
150
153
|
return;
|
|
151
154
|
}
|
|
152
155
|
|
|
153
156
|
// Replace old patterns with the package specifier preloads
|
|
154
|
-
if (
|
|
155
|
-
tree.write(bunfigPath,
|
|
157
|
+
if (current.includes('preload = [')) {
|
|
158
|
+
tree.write(bunfigPath, current.replace(/preload\s*=\s*\[.*?\]/s, `preload = [${preloadEntries}]`));
|
|
156
159
|
return;
|
|
157
160
|
}
|
|
158
161
|
|
|
159
|
-
if (
|
|
160
|
-
tree.write(bunfigPath,
|
|
162
|
+
if (current.includes('[test]')) {
|
|
163
|
+
tree.write(bunfigPath, current.replace('[test]', `[test]\npreload = [${preloadEntries}]`));
|
|
161
164
|
return;
|
|
162
165
|
}
|
|
163
166
|
|
|
164
|
-
const suffix =
|
|
165
|
-
tree.write(
|
|
166
|
-
bunfigPath,
|
|
167
|
-
`${next}${suffix}[test]\ntimeout = ${DEFAULT_BUN_TEST_TIMEOUT_MS}\npreload = [${preloadEntries}]\n`,
|
|
168
|
-
);
|
|
167
|
+
const suffix = current.endsWith('\n') ? '' : '\n';
|
|
168
|
+
tree.write(bunfigPath, `${current}${suffix}[test]\npreload = [${preloadEntries}]\n`);
|
|
169
169
|
}
|
|
170
170
|
|
|
171
|
-
function
|
|
172
|
-
|
|
173
|
-
return current;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if (current.includes('[test]')) {
|
|
177
|
-
return current.replace('[test]', `[test]\ntimeout = ${DEFAULT_BUN_TEST_TIMEOUT_MS}`);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
const suffix = current.endsWith('\n') ? '' : '\n';
|
|
181
|
-
return `${current}${suffix}[test]\ntimeout = ${DEFAULT_BUN_TEST_TIMEOUT_MS}\n`;
|
|
171
|
+
function stripBunTestTimeoutLine(current: string): string {
|
|
172
|
+
return current.replace(/^\s*timeout\s*=.*\n?/gm, '');
|
|
182
173
|
}
|
|
183
174
|
|
|
184
175
|
function renderSuiteTracer(options: Required<BunTestTracingGeneratorSchema>): string {
|
package/src/index.test.ts
CHANGED
|
@@ -38,6 +38,8 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
|
|
|
38
38
|
expect(targets.build?.executor).toBe('nx:noop');
|
|
39
39
|
expect(targets.build?.cache).toBe(true);
|
|
40
40
|
expect(targets.build?.dependsOn).toEqual(buildOutputDependencies);
|
|
41
|
+
expect(targets.clean?.executor).toBe('@smoothbricks/nx-plugin:clean-outputs');
|
|
42
|
+
expect(targets.clean?.cache).toBe(false);
|
|
41
43
|
|
|
42
44
|
expect(targets['typecheck-tests']?.executor).toBe('nx:run-commands');
|
|
43
45
|
expect(targets['typecheck-tests']?.cache).toBe(true);
|
|
@@ -112,7 +114,7 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
|
|
|
112
114
|
|
|
113
115
|
const targets = await inferProjectTargets(workspace, 'packages/ziggy/package.json');
|
|
114
116
|
|
|
115
|
-
expect(Object.keys(targets).sort()).toEqual(['build', 'zig-native', 'zig-wasm']);
|
|
117
|
+
expect(Object.keys(targets).sort()).toEqual(['build', 'clean', 'zig-native', 'zig-wasm']);
|
|
116
118
|
expect(targets.build?.dependsOn).toEqual(buildOutputDependencies);
|
|
117
119
|
expect(targets.build?.cache).toBe(true);
|
|
118
120
|
expect(targets['zig-wasm']?.cache).toBe(true);
|
|
@@ -144,6 +146,7 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
|
|
|
144
146
|
expect(targets['tsdown-js']).toBeUndefined();
|
|
145
147
|
expect(targets.build?.executor).toBe('nx:noop');
|
|
146
148
|
expect(targets.build?.dependsOn).toEqual(buildOutputDependencies);
|
|
149
|
+
expect(targets.clean?.executor).toBe('@smoothbricks/nx-plugin:clean-outputs');
|
|
147
150
|
} finally {
|
|
148
151
|
await workspace.cleanup();
|
|
149
152
|
}
|
package/src/index.ts
CHANGED
|
@@ -118,6 +118,10 @@ async function createProjectTargets(packageJsonPath: string, workspaceRoot: stri
|
|
|
118
118
|
cache: true,
|
|
119
119
|
dependsOn: ['^build', ...BUILD_OUTPUT_DEPENDENCIES],
|
|
120
120
|
};
|
|
121
|
+
targets.clean = {
|
|
122
|
+
executor: '@smoothbricks/nx-plugin:clean-outputs',
|
|
123
|
+
cache: false,
|
|
124
|
+
};
|
|
121
125
|
}
|
|
122
126
|
|
|
123
127
|
if (targets['typecheck-tests']) {
|
|
@@ -35,6 +35,7 @@ function validReleaseNxJson(): Record<string, unknown> {
|
|
|
35
35
|
pattern: SMOO_NX_RELEASE_TAG_PATTERN,
|
|
36
36
|
},
|
|
37
37
|
changelog: {
|
|
38
|
+
automaticFromRef: true,
|
|
38
39
|
workspaceChangelog: false,
|
|
39
40
|
projectChangelogs: {
|
|
40
41
|
createRelease: false,
|
|
@@ -104,6 +105,7 @@ describe('pure core: checkReleaseConfig', () => {
|
|
|
104
105
|
'release.version.preVersionCommand must not be defined; smoo builds npm-missing packages before publish',
|
|
105
106
|
);
|
|
106
107
|
expect(messages).toContainEqual(`release.releaseTag.pattern must be ${SMOO_NX_RELEASE_TAG_PATTERN}`);
|
|
108
|
+
expect(messages).toContainEqual('release.changelog.automaticFromRef must be true');
|
|
107
109
|
expect(messages).toContainEqual('release.changelog.workspaceChangelog must be false');
|
|
108
110
|
expect(messages).toContainEqual('release.changelog.projectChangelogs.createRelease must be false');
|
|
109
111
|
expect(messages).toContainEqual('release.changelog.projectChangelogs.file must be false');
|
|
@@ -131,6 +133,7 @@ describe('pure core: applyReleaseConfig', () => {
|
|
|
131
133
|
const releaseTag = release.releaseTag as Record<string, unknown>;
|
|
132
134
|
expect(releaseTag.pattern).toBe(SMOO_NX_RELEASE_TAG_PATTERN);
|
|
133
135
|
const changelog = release.changelog as Record<string, unknown>;
|
|
136
|
+
expect(changelog.automaticFromRef).toBe(true);
|
|
134
137
|
expect(changelog.workspaceChangelog).toBe(false);
|
|
135
138
|
const projectChangelogs = changelog.projectChangelogs as Record<string, unknown>;
|
|
136
139
|
expect(projectChangelogs.createRelease).toBe(false);
|
|
@@ -65,6 +65,9 @@ export function checkReleaseConfig(nxJson: Record<string, unknown>): NxPolicyIss
|
|
|
65
65
|
if (!changelog) {
|
|
66
66
|
issues.push({ path: 'nx.json', message: 'release.changelog config is missing' });
|
|
67
67
|
}
|
|
68
|
+
if (changelog && changelog.automaticFromRef !== true) {
|
|
69
|
+
issues.push({ path: 'nx.json', message: 'release.changelog.automaticFromRef must be true' });
|
|
70
|
+
}
|
|
68
71
|
if (changelog && changelog.workspaceChangelog !== false) {
|
|
69
72
|
issues.push({ path: 'nx.json', message: 'release.changelog.workspaceChangelog must be false' });
|
|
70
73
|
}
|
|
@@ -122,6 +125,7 @@ export function applyReleaseConfig(nxJson: Record<string, unknown>): boolean {
|
|
|
122
125
|
changed = setStringProperty(releaseTag, 'pattern', SMOO_NX_RELEASE_TAG_PATTERN) || changed;
|
|
123
126
|
|
|
124
127
|
const changelog = getOrCreateRecord(release, 'changelog');
|
|
128
|
+
changed = setBooleanProperty(changelog, 'automaticFromRef', true) || changed;
|
|
125
129
|
changed = setBooleanProperty(changelog, 'workspaceChangelog', false) || changed;
|
|
126
130
|
|
|
127
131
|
const projectChangelogs = getOrCreateRecord(changelog, 'projectChangelogs');
|
|
@@ -36,13 +36,18 @@ function validNxJson(): Record<string, unknown> {
|
|
|
36
36
|
},
|
|
37
37
|
'@smoothbricks/nx-plugin',
|
|
38
38
|
],
|
|
39
|
-
targetDefaults:
|
|
40
|
-
build: { cache: true, outputs: ['{projectRoot}/dist'] },
|
|
41
|
-
},
|
|
39
|
+
targetDefaults: validTargetDefaults(),
|
|
42
40
|
namedInputs: validNamedInputs(),
|
|
43
41
|
};
|
|
44
42
|
}
|
|
45
43
|
|
|
44
|
+
function validTargetDefaults(): Record<string, unknown> {
|
|
45
|
+
return {
|
|
46
|
+
build: { cache: true, outputs: ['{projectRoot}/dist'] },
|
|
47
|
+
clean: { executor: '@smoothbricks/nx-plugin:clean-outputs', cache: false },
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
46
51
|
function validNamedInputs(): Record<string, unknown> {
|
|
47
52
|
return {
|
|
48
53
|
default: ['{projectRoot}/**/*', 'sharedGlobals'],
|
|
@@ -95,7 +100,7 @@ describe('pure core: checkWorkspaceConfig', () => {
|
|
|
95
100
|
it('detects missing plugins', () => {
|
|
96
101
|
const issues = checkWorkspaceConfig({
|
|
97
102
|
plugins: [],
|
|
98
|
-
targetDefaults:
|
|
103
|
+
targetDefaults: validTargetDefaults(),
|
|
99
104
|
namedInputs: validNamedInputs(),
|
|
100
105
|
});
|
|
101
106
|
expect(issues.length).toBe(2);
|
|
@@ -107,7 +112,7 @@ describe('pure core: checkWorkspaceConfig', () => {
|
|
|
107
112
|
const issues = checkWorkspaceConfig({
|
|
108
113
|
...validNxJson(),
|
|
109
114
|
targetDefaults: {
|
|
110
|
-
|
|
115
|
+
...validTargetDefaults(),
|
|
111
116
|
'build:wasm': { cache: true },
|
|
112
117
|
},
|
|
113
118
|
});
|
|
@@ -121,7 +126,7 @@ describe('pure core: checkWorkspaceConfig', () => {
|
|
|
121
126
|
{ plugin: '@nx/js/typescript', options: { build: { targetName: 'build' } } },
|
|
122
127
|
'@smoothbricks/nx-plugin',
|
|
123
128
|
],
|
|
124
|
-
targetDefaults:
|
|
129
|
+
targetDefaults: validTargetDefaults(),
|
|
125
130
|
namedInputs: validNamedInputs(),
|
|
126
131
|
});
|
|
127
132
|
expect(issues.some((i) => i.message.includes('build.targetName must be tsc-js'))).toBe(true);
|
|
@@ -130,7 +135,7 @@ describe('pure core: checkWorkspaceConfig', () => {
|
|
|
130
135
|
it('detects imprecise production inputs', () => {
|
|
131
136
|
const issues = checkWorkspaceConfig({
|
|
132
137
|
plugins: validPlugins(),
|
|
133
|
-
targetDefaults:
|
|
138
|
+
targetDefaults: validTargetDefaults(),
|
|
134
139
|
namedInputs: {
|
|
135
140
|
...validNamedInputs(),
|
|
136
141
|
production: ['default'],
|
|
@@ -142,7 +147,10 @@ describe('pure core: checkWorkspaceConfig', () => {
|
|
|
142
147
|
it('detects missing build cache', () => {
|
|
143
148
|
const issues = checkWorkspaceConfig({
|
|
144
149
|
plugins: validPlugins(),
|
|
145
|
-
targetDefaults: {
|
|
150
|
+
targetDefaults: {
|
|
151
|
+
build: { cache: false },
|
|
152
|
+
clean: { executor: '@smoothbricks/nx-plugin:clean-outputs', cache: false },
|
|
153
|
+
},
|
|
146
154
|
namedInputs: validNamedInputs(),
|
|
147
155
|
});
|
|
148
156
|
expect(issues.some((i) => i.message.includes('build.cache must be true'))).toBe(true);
|
|
@@ -152,7 +160,7 @@ describe('pure core: checkWorkspaceConfig', () => {
|
|
|
152
160
|
it('detects missing sharedGlobals', () => {
|
|
153
161
|
const issues = checkWorkspaceConfig({
|
|
154
162
|
plugins: validPlugins(),
|
|
155
|
-
targetDefaults:
|
|
163
|
+
targetDefaults: validTargetDefaults(),
|
|
156
164
|
namedInputs: {
|
|
157
165
|
...validNamedInputs(),
|
|
158
166
|
sharedGlobals: [],
|
|
@@ -171,7 +179,7 @@ describe('pure core: applyWorkspaceConfig', () => {
|
|
|
171
179
|
it('fixes missing plugins', () => {
|
|
172
180
|
const nxJson: Record<string, unknown> = {
|
|
173
181
|
plugins: [],
|
|
174
|
-
targetDefaults:
|
|
182
|
+
targetDefaults: validTargetDefaults(),
|
|
175
183
|
namedInputs: validNamedInputs(),
|
|
176
184
|
};
|
|
177
185
|
expect(applyWorkspaceConfig(nxJson)).toBe(true);
|
|
@@ -186,7 +194,7 @@ describe('pure core: applyWorkspaceConfig', () => {
|
|
|
186
194
|
const nxJson = {
|
|
187
195
|
...validNxJson(),
|
|
188
196
|
targetDefaults: {
|
|
189
|
-
|
|
197
|
+
...validTargetDefaults(),
|
|
190
198
|
'build:wasm': { cache: true },
|
|
191
199
|
},
|
|
192
200
|
};
|
|
@@ -197,7 +205,7 @@ describe('pure core: applyWorkspaceConfig', () => {
|
|
|
197
205
|
it('fixes imprecise production inputs', () => {
|
|
198
206
|
const nxJson: Record<string, unknown> = {
|
|
199
207
|
plugins: validPlugins(),
|
|
200
|
-
targetDefaults:
|
|
208
|
+
targetDefaults: validTargetDefaults(),
|
|
201
209
|
namedInputs: {
|
|
202
210
|
...validNamedInputs(),
|
|
203
211
|
production: ['default', '{projectRoot}/**/*'],
|
|
@@ -217,7 +225,7 @@ describe('pure core: applyWorkspaceConfig', () => {
|
|
|
217
225
|
it('accepts custom precise production inputs unchanged', () => {
|
|
218
226
|
const nxJson: Record<string, unknown> = {
|
|
219
227
|
plugins: validPlugins(),
|
|
220
|
-
targetDefaults:
|
|
228
|
+
targetDefaults: validTargetDefaults(),
|
|
221
229
|
namedInputs: {
|
|
222
230
|
...validNamedInputs(),
|
|
223
231
|
production: ['{projectRoot}/src/**/*.rs', '{projectRoot}/Cargo.toml', '!{projectRoot}/**/*.test.*'],
|
|
@@ -245,7 +253,7 @@ describe('Tree: checkWorkspaceConfigTree', () => {
|
|
|
245
253
|
const tree = createTreeWithEmptyWorkspace();
|
|
246
254
|
writeJson(tree, 'nx.json', {
|
|
247
255
|
plugins: [],
|
|
248
|
-
targetDefaults:
|
|
256
|
+
targetDefaults: validTargetDefaults(),
|
|
249
257
|
namedInputs: validNamedInputs(),
|
|
250
258
|
});
|
|
251
259
|
|
|
@@ -274,7 +282,7 @@ describe('Tree: applyWorkspaceConfigTree', () => {
|
|
|
274
282
|
const tree = createTreeWithEmptyWorkspace();
|
|
275
283
|
writeJson(tree, 'nx.json', {
|
|
276
284
|
plugins: [],
|
|
277
|
-
targetDefaults:
|
|
285
|
+
targetDefaults: validTargetDefaults(),
|
|
278
286
|
namedInputs: validNamedInputs(),
|
|
279
287
|
});
|
|
280
288
|
|
|
@@ -313,6 +321,10 @@ describe('Tree: applyWorkspaceConfigTree', () => {
|
|
|
313
321
|
cache: true,
|
|
314
322
|
outputs: ['{projectRoot}/dist'],
|
|
315
323
|
});
|
|
324
|
+
expect((nxJson.targetDefaults as Record<string, unknown>).clean).toEqual({
|
|
325
|
+
executor: '@smoothbricks/nx-plugin:clean-outputs',
|
|
326
|
+
cache: false,
|
|
327
|
+
});
|
|
316
328
|
});
|
|
317
329
|
});
|
|
318
330
|
|
|
@@ -23,6 +23,7 @@ export const BUILD_OUTPUT_DEPENDENCIES = [
|
|
|
23
23
|
|
|
24
24
|
const nxJsTypescriptPlugin = '@nx/js/typescript';
|
|
25
25
|
const smoothBricksNxPlugin = '@smoothbricks/nx-plugin';
|
|
26
|
+
const cleanOutputsExecutor = '@smoothbricks/nx-plugin:clean-outputs';
|
|
26
27
|
const expectedSharedGlobalsNamedInput = ['{workspaceRoot}/.github/workflows/ci.yml'];
|
|
27
28
|
const defaultProductionNamedInput = [
|
|
28
29
|
'{projectRoot}/src/**/*',
|
|
@@ -57,6 +58,9 @@ export function checkWorkspaceConfig(nxJson: Record<string, unknown>): NxPolicyI
|
|
|
57
58
|
// Build target default
|
|
58
59
|
validateBuildTargetDefault(nxJson, 'nx.json', issues);
|
|
59
60
|
|
|
61
|
+
// Clean target default
|
|
62
|
+
validateCleanTargetDefault(nxJson, 'nx.json', issues);
|
|
63
|
+
|
|
60
64
|
// Named input defaults
|
|
61
65
|
validateNamedInputDefaults(nxJson, 'nx.json', issues);
|
|
62
66
|
|
|
@@ -97,6 +101,7 @@ export function checkWorkspaceConfig(nxJson: Record<string, unknown>): NxPolicyI
|
|
|
97
101
|
export function applyWorkspaceConfig(nxJson: Record<string, unknown>): boolean {
|
|
98
102
|
let changed = removeColonTargetDefaults(nxJson);
|
|
99
103
|
changed = applyBuildTargetDefault(nxJson) || changed;
|
|
104
|
+
changed = applyCleanTargetDefault(nxJson) || changed;
|
|
100
105
|
changed = applyNamedInputDefaults(nxJson) || changed;
|
|
101
106
|
const currentPlugins = Array.isArray(nxJson.plugins) ? nxJson.plugins : [];
|
|
102
107
|
const nextPlugins = upsertNxPlugin(
|
|
@@ -185,6 +190,21 @@ function validateBuildTargetDefault(
|
|
|
185
190
|
}
|
|
186
191
|
}
|
|
187
192
|
|
|
193
|
+
function validateCleanTargetDefault(
|
|
194
|
+
nxJson: Record<string, unknown>,
|
|
195
|
+
nxJsonPath: string,
|
|
196
|
+
issues: NxPolicyIssue[],
|
|
197
|
+
): void {
|
|
198
|
+
const targetDefaults = recordProperty(nxJson, 'targetDefaults');
|
|
199
|
+
const clean = targetDefaults ? recordProperty(targetDefaults, 'clean') : null;
|
|
200
|
+
if (!clean || clean.executor !== cleanOutputsExecutor) {
|
|
201
|
+
issues.push({ path: nxJsonPath, message: `targetDefaults.clean.executor must be ${cleanOutputsExecutor}` });
|
|
202
|
+
}
|
|
203
|
+
if (!clean || clean.cache !== false) {
|
|
204
|
+
issues.push({ path: nxJsonPath, message: 'targetDefaults.clean.cache must be false' });
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
188
208
|
function validateNamedInputDefaults(
|
|
189
209
|
nxJson: Record<string, unknown>,
|
|
190
210
|
nxJsonPath: string,
|
|
@@ -235,6 +255,14 @@ function applyBuildTargetDefault(nxJson: Record<string, unknown>): boolean {
|
|
|
235
255
|
return changed;
|
|
236
256
|
}
|
|
237
257
|
|
|
258
|
+
function applyCleanTargetDefault(nxJson: Record<string, unknown>): boolean {
|
|
259
|
+
const targetDefaults = getOrCreateRecord(nxJson, 'targetDefaults');
|
|
260
|
+
const clean = getOrCreateRecord(targetDefaults, 'clean');
|
|
261
|
+
let changed = setStringProperty(clean, 'executor', cleanOutputsExecutor);
|
|
262
|
+
changed = setBooleanProperty(clean, 'cache', false) || changed;
|
|
263
|
+
return changed;
|
|
264
|
+
}
|
|
265
|
+
|
|
238
266
|
function applyNamedInputDefaults(nxJson: Record<string, unknown>): boolean {
|
|
239
267
|
const namedInputs = getOrCreateRecord(nxJson, 'namedInputs');
|
|
240
268
|
let changed = false;
|
|
@@ -374,6 +402,14 @@ function setBooleanProperty(record: Record<string, unknown>, key: string, value:
|
|
|
374
402
|
return true;
|
|
375
403
|
}
|
|
376
404
|
|
|
405
|
+
function setStringProperty(record: Record<string, unknown>, key: string, value: string): boolean {
|
|
406
|
+
if (record[key] === value) {
|
|
407
|
+
return false;
|
|
408
|
+
}
|
|
409
|
+
record[key] = value;
|
|
410
|
+
return true;
|
|
411
|
+
}
|
|
412
|
+
|
|
377
413
|
function setStringArrayProperty(record: Record<string, unknown>, key: string, value: string[]): boolean {
|
|
378
414
|
const current = record[key];
|
|
379
415
|
if (stringArrayEquals(current, value)) {
|