@smoothbricks/nx-plugin 0.0.0-bootstrap.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +66 -2
- package/dist/generators/bun-test-tracing/generator.d.ts +10 -0
- package/dist/generators/bun-test-tracing/generator.d.ts.map +1 -0
- package/dist/generators/bun-test-tracing/generator.js +246 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +126 -0
- package/generators.json +9 -0
- package/loader.js +16 -0
- package/package.json +55 -4
- package/src/generators/bun-test-tracing/generator.test.ts +219 -0
- package/src/generators/bun-test-tracing/generator.ts +347 -0
- package/src/generators/bun-test-tracing/schema.d.ts +8 -0
- package/src/generators/bun-test-tracing/schema.json +28 -0
- package/src/index.test.ts +133 -0
- package/src/index.ts +151 -0
- package/src/test-suite-tracer.ts +0 -0
package/README.md
CHANGED
|
@@ -1,3 +1,67 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Nx Plugin
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Local Nx plugin for workspace-standard package setup and missing inferred targets.
|
|
4
|
+
|
|
5
|
+
## Target Ownership
|
|
6
|
+
|
|
7
|
+
Official `@nx/js/typescript` owns TypeScript library inference. A package `tsconfig.lib.json` becomes the tool-output
|
|
8
|
+
target `tsc-js`; this plugin must not duplicate or rename that target.
|
|
9
|
+
|
|
10
|
+
`@smoothbricks/nx-plugin` owns only inferred targets Nx does not provide here:
|
|
11
|
+
|
|
12
|
+
- `typecheck-tests` from `tsconfig.test.json` for packages that use Bun test
|
|
13
|
+
- `zig-*` targets from `build.zig` steps such as `zig-wasm`
|
|
14
|
+
- aggregate `build` and `lint` targets
|
|
15
|
+
|
|
16
|
+
## Nx Target Naming
|
|
17
|
+
|
|
18
|
+
Target names are tool-output names. Use names like `tsc-js` and `zig-wasm`; `build` and `lint` are aggregates.
|
|
19
|
+
|
|
20
|
+
Concrete targets come from concrete files:
|
|
21
|
+
|
|
22
|
+
- `typecheck-tests` is inferred from `tsconfig.test.json`. Smoo requires that config for packages that use `bun test`
|
|
23
|
+
because Bun executes tests without typechecking.
|
|
24
|
+
- `zig-*` is inferred from `build.zig`; each explicit `b.step("name", ...)` becomes `zig-name`.
|
|
25
|
+
- `build` is inferred only when the project has at least one concrete build target to run, such as `tsc-js` from the
|
|
26
|
+
official TypeScript plugin or a `zig-*` target from this plugin.
|
|
27
|
+
|
|
28
|
+
This is why Zig appears in the convention: the plugin is not guessing from arbitrary Zig source. SmoothBricks requires
|
|
29
|
+
`build.zig` to expose named build steps so Nx can create cacheable, addressable targets from those steps.
|
|
30
|
+
|
|
31
|
+
Do not use colon-style Nx target names such as `build:wasm` or `lint:fix`. Nx CLI syntax already uses colons for
|
|
32
|
+
`project:target:configuration`, so colon target names are hard to read, easy to confuse with configurations, and awkward
|
|
33
|
+
to expose through package scripts. Package scripts may still use names like `build:wasm`; they should delegate to a real
|
|
34
|
+
target such as `nx run pkg:zig-wasm`.
|
|
35
|
+
|
|
36
|
+
There is no Nx `lint:fix` target; repository formatting is handled by the root `lint:fix` script.
|
|
37
|
+
|
|
38
|
+
`typecheck-tests` is inferred only when `tsconfig.test.json` exists. It runs TypeScript with `noEmit`; test typechecking
|
|
39
|
+
must not emit `dist-test`. Smoo validation creates/requires this config for Bun test packages because `bun test` is not
|
|
40
|
+
a typecheck command. Other test runners can satisfy test typechecking through their own toolchain.
|
|
41
|
+
|
|
42
|
+
`tsconfig.test.json` is not a TypeScript build-mode project. It should reference library tsconfigs it needs to typecheck
|
|
43
|
+
against, but the package root `tsconfig.json` should not reference `./tsconfig.test.json`. Nx runs test typechecking
|
|
44
|
+
through the inferred `typecheck-tests` target, not through `tsc --build`.
|
|
45
|
+
|
|
46
|
+
Zig targets are inferred only when `build.zig` declares at least one `b.step(...)`. Each step becomes a `zig-*` target,
|
|
47
|
+
so a package `build.zig` must have at least one step.
|
|
48
|
+
|
|
49
|
+
## Bun Test Tracing Generator
|
|
50
|
+
|
|
51
|
+
Configure a package for the Bun test tracing + no-emit test typechecking pattern used in this repo.
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
nx generate ./packages/nx-plugin:bun-test-tracing \
|
|
55
|
+
--project @smoothbricks/my-package \
|
|
56
|
+
--opContextModule @smoothbricks/lmao \
|
|
57
|
+
--opContextExport lmaoOpContext \
|
|
58
|
+
--tracerModule @smoothbricks/lmao/testing/bun
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
What it wires:
|
|
62
|
+
|
|
63
|
+
- `bunfig.toml` preloads for the LMAO Bun test tracing setup
|
|
64
|
+
- `src/test-suite-tracer.ts`
|
|
65
|
+
- `tsconfig.test.json` with `noEmit` for inferred `typecheck-tests`
|
|
66
|
+
- direct test config references to library tsconfigs; package root `tsconfig.json` is left out of the test config graph
|
|
67
|
+
- package `package.json` test/lint/devDependency wiring needed for the standard pattern
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Tree } from 'nx/src/devkit-exports.js';
|
|
2
|
+
interface BunTestTracingGeneratorSchema {
|
|
3
|
+
project: string;
|
|
4
|
+
opContextModule: string;
|
|
5
|
+
opContextExport?: string;
|
|
6
|
+
tracerModule?: string;
|
|
7
|
+
}
|
|
8
|
+
export default function generator(tree: Tree, schema: BunTestTracingGeneratorSchema): Promise<void>;
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=generator.d.ts.map
|
|
@@ -0,0 +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;AAElC,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;AAiCD,wBAA8B,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,6BAA6B,GAAG,OAAO,CAAC,IAAI,CAAC,CAuBxG"}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { posix as path } from 'node:path';
|
|
2
|
+
import { getProjects, joinPathFragments, normalizePath, readJson, updateJson, } from 'nx/src/devkit-exports.js';
|
|
3
|
+
const DEFAULT_TEST_INCLUDES = [
|
|
4
|
+
'src/**/*.test.ts',
|
|
5
|
+
'src/**/*.spec.ts',
|
|
6
|
+
'src/**/__tests__/**/*.ts',
|
|
7
|
+
'src/**/__tests__/**/*.tsx',
|
|
8
|
+
'src/test-suite-tracer.ts',
|
|
9
|
+
];
|
|
10
|
+
const COPIED_COMPILER_OPTIONS = ['baseUrl', 'module', 'moduleResolution', 'jsx', 'lib'];
|
|
11
|
+
const DEFAULT_BUN_TEST_TIMEOUT_MS = 30_000;
|
|
12
|
+
export default async function generator(tree, schema) {
|
|
13
|
+
const options = normalizeOptions(schema);
|
|
14
|
+
const resolved = resolveProject(tree, options.project);
|
|
15
|
+
const packageJsonPath = joinPathFragments(resolved.root, 'package.json');
|
|
16
|
+
const tsconfigPath = joinPathFragments(resolved.root, 'tsconfig.json');
|
|
17
|
+
const libTsconfigPath = joinPathFragments(resolved.root, 'tsconfig.lib.json');
|
|
18
|
+
if (!tree.exists(packageJsonPath)) {
|
|
19
|
+
throw new Error(`Project ${resolved.name} is missing ${packageJsonPath}`);
|
|
20
|
+
}
|
|
21
|
+
if (!tree.exists(tsconfigPath)) {
|
|
22
|
+
throw new Error(`Project ${resolved.name} is missing ${tsconfigPath}`);
|
|
23
|
+
}
|
|
24
|
+
if (!tree.exists(libTsconfigPath)) {
|
|
25
|
+
throw new Error(`Project ${resolved.name} is missing ${libTsconfigPath}`);
|
|
26
|
+
}
|
|
27
|
+
updatePackageJson(tree, packageJsonPath);
|
|
28
|
+
writeBunfig(tree, resolved.root);
|
|
29
|
+
tree.write(joinPathFragments(resolved.root, 'src/test-suite-tracer.ts'), renderSuiteTracer(options));
|
|
30
|
+
updateTsconfigTest(tree, resolved.root, packageJsonPath, tsconfigPath, libTsconfigPath);
|
|
31
|
+
}
|
|
32
|
+
function normalizeOptions(schema) {
|
|
33
|
+
return {
|
|
34
|
+
project: schema.project,
|
|
35
|
+
opContextModule: schema.opContextModule,
|
|
36
|
+
opContextExport: schema.opContextExport ?? 'opContext',
|
|
37
|
+
tracerModule: schema.tracerModule ?? '@smoothbricks/lmao/testing/bun',
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function resolveProject(tree, projectInput) {
|
|
41
|
+
const normalizedInput = normalizeLookupValue(projectInput);
|
|
42
|
+
for (const [name, config] of getProjects(tree)) {
|
|
43
|
+
if (normalizeLookupValue(name) === normalizedInput || normalizeLookupValue(config.root) === normalizedInput) {
|
|
44
|
+
return { name, root: config.root };
|
|
45
|
+
}
|
|
46
|
+
const packageJsonPath = joinPathFragments(config.root, 'package.json');
|
|
47
|
+
if (!tree.exists(packageJsonPath)) {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
const packageJson = readJson(tree, packageJsonPath);
|
|
51
|
+
if (packageJson.name && normalizeLookupValue(packageJson.name) === normalizedInput) {
|
|
52
|
+
return { name, root: config.root };
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
throw new Error(`Could not resolve project ${projectInput}`);
|
|
56
|
+
}
|
|
57
|
+
function normalizeLookupValue(value) {
|
|
58
|
+
return value.replace(/^\.\//, '').replace(/\\/g, '/');
|
|
59
|
+
}
|
|
60
|
+
function updatePackageJson(tree, packageJsonPath) {
|
|
61
|
+
updateJson(tree, packageJsonPath, (packageJson) => {
|
|
62
|
+
packageJson.scripts ??= {};
|
|
63
|
+
packageJson.scripts.test ??= 'bun test';
|
|
64
|
+
packageJson.nx ??= {};
|
|
65
|
+
packageJson.nx.targets ??= {};
|
|
66
|
+
packageJson.nx.targets.lint ??= {};
|
|
67
|
+
const hasLmaoDependency = Boolean(packageJson.dependencies?.['@smoothbricks/lmao'] || packageJson.devDependencies?.['@smoothbricks/lmao']);
|
|
68
|
+
if (!hasLmaoDependency) {
|
|
69
|
+
packageJson.devDependencies ??= {};
|
|
70
|
+
packageJson.devDependencies['@smoothbricks/lmao'] = 'workspace:*';
|
|
71
|
+
}
|
|
72
|
+
return packageJson;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
function writeBunfig(tree, projectRoot) {
|
|
76
|
+
const bunfigPath = joinPathFragments(projectRoot, 'bunfig.toml');
|
|
77
|
+
const preloadEntries = '"@smoothbricks/lmao/bun/preload", "@smoothbricks/lmao/bun/trace-preload"';
|
|
78
|
+
if (!tree.exists(bunfigPath)) {
|
|
79
|
+
tree.write(bunfigPath, `[test]\ntimeout = ${DEFAULT_BUN_TEST_TIMEOUT_MS}\npreload = [${preloadEntries}]\n`);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const current = tree.read(bunfigPath, 'utf-8') ?? '';
|
|
83
|
+
const next = ensureBunTestTimeout(current);
|
|
84
|
+
// Already has the package specifier preloads
|
|
85
|
+
if (next.includes('@smoothbricks/lmao/bun/preload')) {
|
|
86
|
+
if (next !== current) {
|
|
87
|
+
tree.write(bunfigPath, next);
|
|
88
|
+
}
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
// Replace old patterns with the package specifier preloads
|
|
92
|
+
if (next.includes('preload = [')) {
|
|
93
|
+
tree.write(bunfigPath, next.replace(/preload\s*=\s*\[.*?\]/s, `preload = [${preloadEntries}]`));
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (next.includes('[test]')) {
|
|
97
|
+
tree.write(bunfigPath, next.replace('[test]', `[test]\npreload = [${preloadEntries}]`));
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const suffix = next.endsWith('\n') ? '' : '\n';
|
|
101
|
+
tree.write(bunfigPath, `${next}${suffix}[test]\ntimeout = ${DEFAULT_BUN_TEST_TIMEOUT_MS}\npreload = [${preloadEntries}]\n`);
|
|
102
|
+
}
|
|
103
|
+
function ensureBunTestTimeout(current) {
|
|
104
|
+
if (/(^|\n)\s*timeout\s*=/.test(current)) {
|
|
105
|
+
return current;
|
|
106
|
+
}
|
|
107
|
+
if (current.includes('[test]')) {
|
|
108
|
+
return current.replace('[test]', `[test]\ntimeout = ${DEFAULT_BUN_TEST_TIMEOUT_MS}`);
|
|
109
|
+
}
|
|
110
|
+
const suffix = current.endsWith('\n') ? '' : '\n';
|
|
111
|
+
return `${current}${suffix}[test]\ntimeout = ${DEFAULT_BUN_TEST_TIMEOUT_MS}\n`;
|
|
112
|
+
}
|
|
113
|
+
function renderSuiteTracer(options) {
|
|
114
|
+
return [
|
|
115
|
+
`import { ${options.opContextExport} } from '${options.opContextModule}';`,
|
|
116
|
+
`import { defineTestTracer } from '${options.tracerModule}';`,
|
|
117
|
+
'',
|
|
118
|
+
`export const { useTestSpan, opContext, extraTestColumns } = defineTestTracer(${options.opContextExport});`,
|
|
119
|
+
'',
|
|
120
|
+
].join('\n');
|
|
121
|
+
}
|
|
122
|
+
function updateTsconfigTest(tree, projectRoot, packageJsonPath, tsconfigPath, libTsconfigPath) {
|
|
123
|
+
const tsconfigTestPath = joinPathFragments(projectRoot, 'tsconfig.test.json');
|
|
124
|
+
const packageJson = readJson(tree, packageJsonPath);
|
|
125
|
+
const projectTsconfig = readJson(tree, tsconfigPath);
|
|
126
|
+
const libTsconfig = readJson(tree, libTsconfigPath);
|
|
127
|
+
const extendsPath = chooseTsconfigTestExtends(projectTsconfig, libTsconfig);
|
|
128
|
+
const copiedCompilerOptions = getCopiedCompilerOptions(libTsconfig);
|
|
129
|
+
const referencePaths = collectReferencePaths(tree, projectRoot, packageJson);
|
|
130
|
+
if (!tree.exists(tsconfigTestPath)) {
|
|
131
|
+
const compilerOptions = {
|
|
132
|
+
...copiedCompilerOptions,
|
|
133
|
+
types: mergeStringArray([], ['bun']),
|
|
134
|
+
composite: false,
|
|
135
|
+
declaration: false,
|
|
136
|
+
declarationMap: false,
|
|
137
|
+
emitDeclarationOnly: false,
|
|
138
|
+
noEmit: true,
|
|
139
|
+
};
|
|
140
|
+
const tsconfigTest = {
|
|
141
|
+
extends: extendsPath,
|
|
142
|
+
compilerOptions,
|
|
143
|
+
include: [...DEFAULT_TEST_INCLUDES],
|
|
144
|
+
references: referencePaths.map((refPath) => ({ path: refPath })),
|
|
145
|
+
};
|
|
146
|
+
tree.write(tsconfigTestPath, `${JSON.stringify(tsconfigTest, null, 2)}\n`);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
updateJson(tree, tsconfigTestPath, (tsconfigTest) => {
|
|
150
|
+
tsconfigTest.extends ??= extendsPath;
|
|
151
|
+
tsconfigTest.compilerOptions = {
|
|
152
|
+
...copiedCompilerOptions,
|
|
153
|
+
...tsconfigTest.compilerOptions,
|
|
154
|
+
types: mergeStringArray(readStringArray(tsconfigTest.compilerOptions?.types), ['bun']),
|
|
155
|
+
composite: false,
|
|
156
|
+
declaration: false,
|
|
157
|
+
declarationMap: false,
|
|
158
|
+
emitDeclarationOnly: false,
|
|
159
|
+
noEmit: true,
|
|
160
|
+
};
|
|
161
|
+
delete tsconfigTest.compilerOptions.outDir;
|
|
162
|
+
delete tsconfigTest.compilerOptions.tsBuildInfoFile;
|
|
163
|
+
tsconfigTest.include = mergeStringArray(tsconfigTest.include, DEFAULT_TEST_INCLUDES);
|
|
164
|
+
tsconfigTest.references = mergeReferences(tsconfigTest.references, referencePaths);
|
|
165
|
+
return tsconfigTest;
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
function chooseTsconfigTestExtends(projectTsconfig, libTsconfig) {
|
|
169
|
+
return isWrapperTsconfig(projectTsconfig) ? (libTsconfig.extends ?? '../../tsconfig.base.json') : './tsconfig.json';
|
|
170
|
+
}
|
|
171
|
+
function isWrapperTsconfig(tsconfig) {
|
|
172
|
+
const includeLength = Array.isArray(tsconfig.include) ? tsconfig.include.length : 0;
|
|
173
|
+
const filesLength = Array.isArray(tsconfig.files) ? tsconfig.files.length : 0;
|
|
174
|
+
const compilerOptionKeys = Object.keys(tsconfig.compilerOptions ?? {});
|
|
175
|
+
return includeLength === 0 && filesLength === 0 && compilerOptionKeys.length === 0;
|
|
176
|
+
}
|
|
177
|
+
function getCopiedCompilerOptions(libTsconfig) {
|
|
178
|
+
const result = {};
|
|
179
|
+
const compilerOptions = libTsconfig.compilerOptions ?? {};
|
|
180
|
+
for (const key of COPIED_COMPILER_OPTIONS) {
|
|
181
|
+
if (key in compilerOptions) {
|
|
182
|
+
result[key] = compilerOptions[key];
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return result;
|
|
186
|
+
}
|
|
187
|
+
function collectReferencePaths(tree, projectRoot, packageJson) {
|
|
188
|
+
const referencePaths = ['./tsconfig.lib.json'];
|
|
189
|
+
const workspacePackages = buildWorkspacePackageMap(tree);
|
|
190
|
+
const dependencyNames = new Set([
|
|
191
|
+
...Object.keys(packageJson.dependencies ?? {}),
|
|
192
|
+
...Object.keys(packageJson.devDependencies ?? {}),
|
|
193
|
+
]);
|
|
194
|
+
dependencyNames.add('@smoothbricks/lmao');
|
|
195
|
+
for (const dependencyName of dependencyNames) {
|
|
196
|
+
const dependencyProject = workspacePackages.get(dependencyName);
|
|
197
|
+
if (!dependencyProject) {
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
const dependencyTsconfig = joinPathFragments(dependencyProject.root, 'tsconfig.lib.json');
|
|
201
|
+
if (!tree.exists(dependencyTsconfig)) {
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
const relativePath = normalizePath(path.relative(projectRoot, dependencyTsconfig));
|
|
205
|
+
if (!referencePaths.includes(relativePath)) {
|
|
206
|
+
referencePaths.push(relativePath);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return referencePaths;
|
|
210
|
+
}
|
|
211
|
+
function buildWorkspacePackageMap(tree) {
|
|
212
|
+
const packageMap = new Map();
|
|
213
|
+
for (const [, config] of getProjects(tree)) {
|
|
214
|
+
const packageJsonPath = joinPathFragments(config.root, 'package.json');
|
|
215
|
+
if (!tree.exists(packageJsonPath)) {
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
const packageJson = readJson(tree, packageJsonPath);
|
|
219
|
+
if (packageJson.name) {
|
|
220
|
+
packageMap.set(packageJson.name, config);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return packageMap;
|
|
224
|
+
}
|
|
225
|
+
function mergeStringArray(a, b) {
|
|
226
|
+
const aArray = a ?? [];
|
|
227
|
+
const set = new Set([...aArray, ...b]);
|
|
228
|
+
return Array.from(set);
|
|
229
|
+
}
|
|
230
|
+
function readStringArray(value) {
|
|
231
|
+
if (Array.isArray(value)) {
|
|
232
|
+
return value.filter((item) => typeof item === 'string');
|
|
233
|
+
}
|
|
234
|
+
return [];
|
|
235
|
+
}
|
|
236
|
+
function mergeReferences(existing, newPaths) {
|
|
237
|
+
const existingPaths = existing ?? [];
|
|
238
|
+
const existingSet = new Set(existingPaths.map((ref) => ref.path));
|
|
239
|
+
const merged = [...existingPaths];
|
|
240
|
+
for (const path of newPaths) {
|
|
241
|
+
if (!existingSet.has(path)) {
|
|
242
|
+
merged.push({ path });
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return merged;
|
|
246
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAuB,aAAa,EAAuB,MAAM,0BAA0B,CAAC;AAOxG,eAAO,MAAM,aAAa,EAAE,aAsB3B,CAAC;;;;AAEF,wBAAiC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { readFile } from 'node:fs/promises';
|
|
3
|
+
import { dirname, join } from 'node:path';
|
|
4
|
+
import { AggregateCreateNodesError } from 'nx/src/project-graph/error-types.js';
|
|
5
|
+
const RESERVED_ZIG_STEPS = new Set(['all', 'clean', 'install', 'test']);
|
|
6
|
+
const ZIG_STEP_PATTERN = /\bb\.step\(\s*["']([^"']+)["']\s*,/g;
|
|
7
|
+
const VALID_ZIG_STEP_NAME = /^[A-Za-z0-9_-]+$/;
|
|
8
|
+
export const createNodesV2 = [
|
|
9
|
+
'**/package.json',
|
|
10
|
+
async (projectConfigurationFiles, _options, context) => {
|
|
11
|
+
const results = [];
|
|
12
|
+
const errors = [];
|
|
13
|
+
await Promise.all(projectConfigurationFiles.map(async (packageJsonPath) => {
|
|
14
|
+
try {
|
|
15
|
+
results.push([packageJsonPath, await createProjectTargets(packageJsonPath, context.workspaceRoot)]);
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
errors.push([packageJsonPath, error instanceof Error ? error : new Error(String(error))]);
|
|
19
|
+
}
|
|
20
|
+
}));
|
|
21
|
+
if (errors.length > 0) {
|
|
22
|
+
throw new AggregateCreateNodesError(errors, results);
|
|
23
|
+
}
|
|
24
|
+
return results;
|
|
25
|
+
},
|
|
26
|
+
];
|
|
27
|
+
export default { createNodesV2 };
|
|
28
|
+
async function createProjectTargets(packageJsonPath, workspaceRoot) {
|
|
29
|
+
const projectRoot = dirname(packageJsonPath);
|
|
30
|
+
const absoluteProjectRoot = join(workspaceRoot, projectRoot);
|
|
31
|
+
const targets = {};
|
|
32
|
+
const buildComponents = [];
|
|
33
|
+
const validationTargets = [];
|
|
34
|
+
const hasLibTsconfig = existsSync(join(absoluteProjectRoot, 'tsconfig.lib.json'));
|
|
35
|
+
if (hasLibTsconfig) {
|
|
36
|
+
buildComponents.push('tsc-js');
|
|
37
|
+
}
|
|
38
|
+
const hasTestTsconfig = existsSync(join(absoluteProjectRoot, 'tsconfig.test.json'));
|
|
39
|
+
if (hasTestTsconfig) {
|
|
40
|
+
targets['typecheck-tests'] = {
|
|
41
|
+
executor: 'nx:run-commands',
|
|
42
|
+
cache: true,
|
|
43
|
+
options: {
|
|
44
|
+
command: 'tsc --noEmit -p tsconfig.test.json',
|
|
45
|
+
cwd: projectRoot,
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
validationTargets.push('typecheck-tests');
|
|
49
|
+
}
|
|
50
|
+
else if (hasLibTsconfig) {
|
|
51
|
+
validationTargets.push('typecheck');
|
|
52
|
+
}
|
|
53
|
+
const zigSteps = await readZigSteps(absoluteProjectRoot, projectRoot);
|
|
54
|
+
for (const step of zigSteps) {
|
|
55
|
+
const targetName = `zig-${step}`;
|
|
56
|
+
targets[targetName] = {
|
|
57
|
+
executor: 'nx:run-commands',
|
|
58
|
+
cache: true,
|
|
59
|
+
inputs: ['{projectRoot}/src/**/*.zig', '{projectRoot}/build.zig', '{projectRoot}/build.zig.zon'],
|
|
60
|
+
outputs: [
|
|
61
|
+
'{projectRoot}/dist/**/*.wasm',
|
|
62
|
+
'{projectRoot}/dist/**/*.node',
|
|
63
|
+
'{projectRoot}/dist/**/*.dylib',
|
|
64
|
+
'{projectRoot}/dist/**/*.so',
|
|
65
|
+
'{projectRoot}/dist/**/*.dll',
|
|
66
|
+
'{projectRoot}/dist/**/*.a',
|
|
67
|
+
],
|
|
68
|
+
options: {
|
|
69
|
+
command: `zig build ${step}`,
|
|
70
|
+
cwd: projectRoot,
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
buildComponents.push(targetName);
|
|
74
|
+
}
|
|
75
|
+
if (buildComponents.length > 0) {
|
|
76
|
+
targets.build = {
|
|
77
|
+
executor: 'nx:noop',
|
|
78
|
+
cache: true,
|
|
79
|
+
dependsOn: ['^build', ...buildComponents],
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
if (targets['typecheck-tests']) {
|
|
83
|
+
if (hasLibTsconfig) {
|
|
84
|
+
targets['typecheck-tests'].dependsOn = ['typecheck'];
|
|
85
|
+
}
|
|
86
|
+
else if (targets.build) {
|
|
87
|
+
targets['typecheck-tests'].dependsOn = ['build'];
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (validationTargets.length > 0) {
|
|
91
|
+
targets.lint = {
|
|
92
|
+
executor: 'nx:noop',
|
|
93
|
+
cache: true,
|
|
94
|
+
dependsOn: validationTargets,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
projects: {
|
|
99
|
+
[projectRoot]: { targets },
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
async function readZigSteps(absoluteProjectRoot, projectRoot) {
|
|
104
|
+
const buildZigPath = join(absoluteProjectRoot, 'build.zig');
|
|
105
|
+
if (!existsSync(buildZigPath)) {
|
|
106
|
+
return [];
|
|
107
|
+
}
|
|
108
|
+
const buildZig = await readFile(buildZigPath, 'utf-8');
|
|
109
|
+
const declaredSteps = Array.from(buildZig.matchAll(ZIG_STEP_PATTERN), (match) => match[1]);
|
|
110
|
+
if (declaredSteps.length === 0) {
|
|
111
|
+
return [];
|
|
112
|
+
}
|
|
113
|
+
const steps = [];
|
|
114
|
+
for (const step of declaredSteps) {
|
|
115
|
+
if (RESERVED_ZIG_STEPS.has(step)) {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
if (!VALID_ZIG_STEP_NAME.test(step)) {
|
|
119
|
+
throw new Error(`${projectRoot}/build.zig declares unsupported Zig step "${step}"; inferred target names cannot contain ':' or other special characters`);
|
|
120
|
+
}
|
|
121
|
+
if (!steps.includes(step)) {
|
|
122
|
+
steps.push(step);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return steps;
|
|
126
|
+
}
|
package/generators.json
ADDED
package/loader.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
|
|
3
|
+
const realPluginPath = new URL('./dist/index.js', import.meta.url);
|
|
4
|
+
|
|
5
|
+
export const createNodesV2 = [
|
|
6
|
+
'**/package.json',
|
|
7
|
+
async (...args) => {
|
|
8
|
+
if (!existsSync(realPluginPath)) {
|
|
9
|
+
return [];
|
|
10
|
+
}
|
|
11
|
+
const plugin = await import(realPluginPath.href);
|
|
12
|
+
return plugin.createNodesV2[1](...args);
|
|
13
|
+
},
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
export default { createNodesV2 };
|
package/package.json
CHANGED
|
@@ -1,14 +1,65 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smoothbricks/nx-plugin",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"main": "./loader.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
"./package.json": "./package.json",
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"development": "./src/index.ts",
|
|
14
|
+
"import": "./loader.js",
|
|
15
|
+
"default": "./loader.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"generators": "./generators.json",
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "nx run nx-plugin:test"
|
|
21
|
+
},
|
|
22
|
+
"nx": {
|
|
23
|
+
"targets": {
|
|
24
|
+
"lint": {},
|
|
25
|
+
"test": {
|
|
26
|
+
"executor": "nx:run-commands",
|
|
27
|
+
"dependsOn": [
|
|
28
|
+
"^build"
|
|
29
|
+
],
|
|
30
|
+
"options": {
|
|
31
|
+
"command": "bun test",
|
|
32
|
+
"cwd": "{projectRoot}"
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"tags": [
|
|
37
|
+
"npm:public"
|
|
38
|
+
],
|
|
39
|
+
"name": "nx-plugin"
|
|
40
|
+
},
|
|
41
|
+
"files": [
|
|
42
|
+
"dist",
|
|
43
|
+
"src",
|
|
44
|
+
"README.md",
|
|
45
|
+
"loader.js",
|
|
46
|
+
"generators.json",
|
|
47
|
+
"!**/*.tsbuildinfo"
|
|
48
|
+
],
|
|
5
49
|
"license": "MIT",
|
|
50
|
+
"publishConfig": {
|
|
51
|
+
"access": "public"
|
|
52
|
+
},
|
|
6
53
|
"repository": {
|
|
7
54
|
"type": "git",
|
|
8
55
|
"url": "git+https://github.com/smoothbricks/codebase.git",
|
|
9
56
|
"directory": "packages/nx-plugin"
|
|
10
57
|
},
|
|
11
|
-
"
|
|
12
|
-
"
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"nx": "22.5.4",
|
|
60
|
+
"tslib": "^2.8.1"
|
|
61
|
+
},
|
|
62
|
+
"devDependencies": {
|
|
63
|
+
"@smoothbricks/lmao": "0.1.2"
|
|
13
64
|
}
|
|
14
65
|
}
|