@smoothbricks/nx-plugin 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/README.md +35 -8
  2. package/dist/bounded-test-policy.d.ts +32 -0
  3. package/dist/bounded-test-policy.d.ts.map +1 -0
  4. package/dist/bounded-test-policy.js +184 -0
  5. package/dist/executors/bounded-exec/executor.d.ts +15 -0
  6. package/dist/executors/bounded-exec/executor.d.ts.map +1 -0
  7. package/dist/executors/bounded-exec/executor.js +179 -0
  8. package/dist/executors/bounded-exec/schema.d.ts +11 -0
  9. package/dist/executors/bounded-exec/schema.d.ts.map +1 -0
  10. package/dist/executors/bounded-exec/schema.js +1 -0
  11. package/dist/generators/bounded-test-targets/generator.d.ts +7 -0
  12. package/dist/generators/bounded-test-targets/generator.d.ts.map +1 -0
  13. package/dist/generators/bounded-test-targets/generator.js +40 -0
  14. package/dist/generators/bun-test-tracing/generator.d.ts.map +1 -1
  15. package/dist/generators/bun-test-tracing/generator.js +5 -4
  16. package/dist/generators/create-package/generator.d.ts +4 -0
  17. package/dist/generators/create-package/generator.d.ts.map +1 -0
  18. package/dist/generators/create-package/generator.js +204 -0
  19. package/dist/generators/make-public/generator.d.ts +4 -0
  20. package/dist/generators/make-public/generator.d.ts.map +1 -0
  21. package/dist/generators/make-public/generator.js +55 -0
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +78 -7
  24. package/dist/package-target-policy.d.ts +31 -0
  25. package/dist/package-target-policy.d.ts.map +1 -0
  26. package/dist/package-target-policy.js +937 -0
  27. package/dist/release-config-policy.d.ts +32 -0
  28. package/dist/release-config-policy.d.ts.map +1 -0
  29. package/dist/release-config-policy.js +225 -0
  30. package/dist/typecheck-test-policy.d.ts +56 -0
  31. package/dist/typecheck-test-policy.d.ts.map +1 -0
  32. package/dist/typecheck-test-policy.js +640 -0
  33. package/dist/version-actions.cjs +78 -0
  34. package/dist/version-actions.d.cts +6 -0
  35. package/dist/version-actions.d.cts.map +1 -0
  36. package/dist/workspace-config-policy.d.ts +33 -0
  37. package/dist/workspace-config-policy.d.ts.map +1 -0
  38. package/dist/workspace-config-policy.js +334 -0
  39. package/executors.json +9 -0
  40. package/generators.json +15 -0
  41. package/package.json +45 -11
  42. package/src/bounded-test-policy.test.ts +257 -0
  43. package/src/bounded-test-policy.ts +257 -0
  44. package/src/bun-bootstrap.test.ts +52 -0
  45. package/src/executors/bounded-exec/executor.test.ts +258 -0
  46. package/src/executors/bounded-exec/executor.ts +228 -0
  47. package/src/executors/bounded-exec/schema.json +68 -0
  48. package/src/executors/bounded-exec/schema.ts +10 -0
  49. package/src/generators/bounded-test-targets/generator.test.ts +136 -0
  50. package/src/generators/bounded-test-targets/generator.ts +67 -0
  51. package/src/generators/bounded-test-targets/schema.d.ts +3 -0
  52. package/src/generators/bounded-test-targets/schema.json +14 -0
  53. package/src/generators/bun-test-tracing/generator.test.ts +25 -1
  54. package/src/generators/bun-test-tracing/generator.ts +6 -5
  55. package/src/generators/create-package/generator.test.ts +206 -0
  56. package/src/generators/create-package/generator.ts +251 -0
  57. package/src/generators/create-package/schema.d.ts +5 -0
  58. package/src/generators/create-package/schema.json +24 -0
  59. package/src/generators/make-public/generator.test.ts +137 -0
  60. package/src/generators/make-public/generator.ts +83 -0
  61. package/src/generators/make-public/schema.d.ts +3 -0
  62. package/src/generators/make-public/schema.json +13 -0
  63. package/src/index.test.ts +85 -18
  64. package/src/index.ts +99 -8
  65. package/src/package-target-policy.test.ts +785 -0
  66. package/src/package-target-policy.ts +1140 -0
  67. package/src/release-config-policy.test.ts +280 -0
  68. package/src/release-config-policy.ts +258 -0
  69. package/src/typecheck-test-policy.test.ts +797 -0
  70. package/src/typecheck-test-policy.ts +752 -0
  71. package/src/version-actions.cts +94 -0
  72. package/src/workspace-config-policy.test.ts +376 -0
  73. package/src/workspace-config-policy.ts +390 -0
  74. package/src/test-suite-tracer.ts +0 -0
@@ -0,0 +1,67 @@
1
+ import {
2
+ getProjects,
3
+ joinPathFragments,
4
+ normalizePath,
5
+ readJson,
6
+ type Tree,
7
+ updateJson,
8
+ } from 'nx/src/devkit-exports.js';
9
+
10
+ import {
11
+ applyBoundedTestTargetPolicy,
12
+ type BoundedTestPolicyPackageJson,
13
+ type BoundedTestPolicyProjectJson,
14
+ } from '../../bounded-test-policy.js';
15
+
16
+ interface BoundedTestTargetsGeneratorSchema {
17
+ project: string;
18
+ }
19
+
20
+ export default async function generator(tree: Tree, schema: BoundedTestTargetsGeneratorSchema): Promise<void> {
21
+ const resolved = resolveProject(tree, schema.project);
22
+ const packageJsonPath = joinPathFragments(resolved.root, 'package.json');
23
+ const projectJsonPath = joinPathFragments(resolved.root, 'project.json');
24
+
25
+ if (!tree.exists(packageJsonPath)) {
26
+ throw new Error(`Project ${resolved.name} is missing ${packageJsonPath}`);
27
+ }
28
+
29
+ const projectJson = tree.exists(projectJsonPath)
30
+ ? readJson<BoundedTestPolicyProjectJson>(tree, projectJsonPath)
31
+ : undefined;
32
+
33
+ updateJson<BoundedTestPolicyPackageJson>(tree, packageJsonPath, (packageJson) => {
34
+ applyBoundedTestTargetPolicy(packageJson, { projectName: resolved.name, projectJson });
35
+ return packageJson;
36
+ });
37
+
38
+ if (projectJson) {
39
+ updateJson<BoundedTestPolicyProjectJson>(tree, projectJsonPath, () => projectJson);
40
+ }
41
+ }
42
+
43
+ function resolveProject(tree: Tree, projectInput: string): { name: string; root: string } {
44
+ const normalizedInput = normalizeLookupValue(projectInput);
45
+
46
+ for (const [name, config] of getProjects(tree)) {
47
+ if (normalizeLookupValue(name) === normalizedInput || normalizeLookupValue(config.root) === normalizedInput) {
48
+ return { name, root: config.root };
49
+ }
50
+
51
+ const packageJsonPath = joinPathFragments(config.root, 'package.json');
52
+ if (!tree.exists(packageJsonPath)) {
53
+ continue;
54
+ }
55
+
56
+ const packageJson = readJson<{ name?: string }>(tree, packageJsonPath);
57
+ if (packageJson.name && normalizeLookupValue(packageJson.name) === normalizedInput) {
58
+ return { name, root: config.root };
59
+ }
60
+ }
61
+
62
+ throw new Error(`Could not resolve project ${projectInput}`);
63
+ }
64
+
65
+ function normalizeLookupValue(value: string): string {
66
+ return normalizePath(value.replace(/^\.\//, ''));
67
+ }
@@ -0,0 +1,3 @@
1
+ export interface BoundedTestTargetsGeneratorSchema {
2
+ project: string;
3
+ }
@@ -0,0 +1,14 @@
1
+ {
2
+ "$schema": "http://json-schema.org/schema",
3
+ "cli": "nx",
4
+ "title": "Configure bounded test targets",
5
+ "description": "Normalize a package test target to the workspace bounded command executor",
6
+ "type": "object",
7
+ "properties": {
8
+ "project": {
9
+ "type": "string",
10
+ "description": "Nx project name, package name, or project root to configure"
11
+ }
12
+ },
13
+ "required": ["project"]
14
+ }
@@ -2,6 +2,7 @@ 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 { BOUNDED_TEST_KILL_AFTER_MS, BOUNDED_TEST_TIMEOUT_MS } from '../../bounded-test-policy.js';
5
6
  import generator from './generator.js';
6
7
 
7
8
  function referencePaths(references: Array<{ path: string }> | undefined): string[] {
@@ -66,7 +67,17 @@ describe('bun-test-tracing generator', () => {
66
67
  );
67
68
 
68
69
  const packageJson = readJson(tree, 'packages/example/package.json');
69
- expect(packageJson.scripts?.test).toBe('bun test');
70
+ expect(packageJson.scripts?.test).toBe('nx run example:test --outputStyle=stream');
71
+ expect(packageJson.nx?.targets?.test).toEqual({
72
+ executor: '@smoothbricks/nx-plugin:bounded-exec',
73
+ dependsOn: ['typecheck-tests', '^build'],
74
+ options: {
75
+ command: 'bun test',
76
+ cwd: '{projectRoot}',
77
+ timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
78
+ killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
79
+ },
80
+ });
70
81
  expect(
71
82
  packageJson.dependencies?.['@smoothbricks/lmao'] ?? packageJson.devDependencies?.['@smoothbricks/lmao'],
72
83
  ).toBe('workspace:*');
@@ -148,6 +159,16 @@ describe('bun-test-tracing generator', () => {
148
159
  },
149
160
  nx: {
150
161
  targets: {
162
+ test: {
163
+ executor: '@smoothbricks/nx-plugin:bounded-exec',
164
+ dependsOn: ['typecheck-tests', '^build'],
165
+ options: {
166
+ command: 'bun test',
167
+ cwd: '{projectRoot}',
168
+ timeoutMs: 600000,
169
+ killAfterMs: 10000,
170
+ },
171
+ },
151
172
  lint: {},
152
173
  },
153
174
  },
@@ -176,6 +197,9 @@ describe('bun-test-tracing generator', () => {
176
197
  expect(bunfig).toContain('@smoothbricks/lmao/bun/preload');
177
198
  expect(bunfig).toContain('@smoothbricks/lmao/bun/trace-preload');
178
199
 
200
+ const packageJson = readJson(tree, 'packages/example/package.json');
201
+ expect(packageJson.scripts?.test).toBe('nx run example:test --outputStyle=stream');
202
+
179
203
  const tsconfigTest = readJson(tree, 'packages/example/tsconfig.test.json') as {
180
204
  extends?: string;
181
205
  compilerOptions?: {
@@ -9,6 +9,8 @@ import {
9
9
  updateJson,
10
10
  } from 'nx/src/devkit-exports.js';
11
11
 
12
+ import { applyBoundedTestTargetPolicy } from '../../bounded-test-policy.js';
13
+
12
14
  interface BunTestTracingGeneratorSchema {
13
15
  project: string;
14
16
  opContextModule: string;
@@ -66,7 +68,7 @@ export default async function generator(tree: Tree, schema: BunTestTracingGenera
66
68
  throw new Error(`Project ${resolved.name} is missing ${libTsconfigPath}`);
67
69
  }
68
70
 
69
- updatePackageJson(tree, packageJsonPath);
71
+ updatePackageJson(tree, packageJsonPath, resolved.name);
70
72
  writeBunfig(tree, resolved.root);
71
73
  tree.write(joinPathFragments(resolved.root, 'src/test-suite-tracer.ts'), renderSuiteTracer(options));
72
74
  updateTsconfigTest(tree, resolved.root, packageJsonPath, tsconfigPath, libTsconfigPath);
@@ -107,13 +109,12 @@ function normalizeLookupValue(value: string): string {
107
109
  return value.replace(/^\.\//, '').replace(/\\/g, '/');
108
110
  }
109
111
 
110
- function updatePackageJson(tree: Tree, packageJsonPath: string): void {
112
+ function updatePackageJson(tree: Tree, packageJsonPath: string, projectName: string): void {
111
113
  updateJson<PackageJson>(tree, packageJsonPath, (packageJson: PackageJson) => {
112
- packageJson.scripts ??= {};
113
- packageJson.scripts.test ??= 'bun test';
114
-
115
114
  packageJson.nx ??= {};
116
115
  packageJson.nx.targets ??= {};
116
+ packageJson.nx.targets.test ??= { dependsOn: ['typecheck-tests', '^build'] };
117
+ applyBoundedTestTargetPolicy(packageJson, { projectName });
117
118
  packageJson.nx.targets.lint ??= {};
118
119
 
119
120
  const hasLmaoDependency = Boolean(
@@ -0,0 +1,206 @@
1
+ import { describe, expect, it } from 'bun:test';
2
+ import { readJson, type Tree } from 'nx/src/devkit-exports.js';
3
+ import { createTreeWithEmptyWorkspace } from 'nx/src/devkit-testing-exports.js';
4
+
5
+ import generator from './generator.js';
6
+
7
+ function setupTree(rootName = '@smoothbricks/codebase'): Tree {
8
+ const tree = createTreeWithEmptyWorkspace();
9
+ tree.write(
10
+ 'package.json',
11
+ JSON.stringify(
12
+ {
13
+ name: rootName,
14
+ version: '0.0.0',
15
+ private: true,
16
+ repository: { type: 'git', url: 'git+https://github.com/smoothbricks/codebase.git' },
17
+ workspaces: ['packages/*'],
18
+ },
19
+ null,
20
+ 2,
21
+ ),
22
+ );
23
+ return tree;
24
+ }
25
+
26
+ describe('create-package generator', () => {
27
+ describe('ts-lib variant', () => {
28
+ it('creates basic package structure', async () => {
29
+ const tree = setupTree();
30
+ await generator(tree, { name: 'duration', variant: 'ts-lib' });
31
+
32
+ // package.json
33
+ const pkg = readJson(tree, 'packages/duration/package.json');
34
+ expect(pkg.name).toBe('@smoothbricks/duration');
35
+ expect(pkg.version).toBe('0.0.0');
36
+ expect(pkg.private).toBe(true);
37
+ expect(pkg.type).toBe('module');
38
+ expect(pkg.sideEffects).toBe(false);
39
+ expect(pkg.main).toBe('./dist/index.js');
40
+ expect(pkg.module).toBe('./dist/index.js');
41
+ expect(pkg.types).toBe('./dist/index.d.ts');
42
+ expect(pkg.exports).toEqual({
43
+ './package.json': './package.json',
44
+ '.': {
45
+ types: './dist/index.d.ts',
46
+ development: './src/index.ts',
47
+ import: './dist/index.js',
48
+ default: './dist/index.js',
49
+ },
50
+ });
51
+ expect(pkg.dependencies).toEqual({ tslib: '^2.8.1' });
52
+ expect(pkg.devDependencies).toEqual({ '@smoothbricks/validation': 'workspace:*' });
53
+ expect(pkg.scripts?.test).toBe('nx run duration:test --outputStyle=stream');
54
+ expect(pkg.nx?.name).toBe('duration');
55
+ expect(pkg.nx?.targets?.lint).toEqual({});
56
+ expect(pkg.nx?.targets?.test?.executor).toBe('@smoothbricks/nx-plugin:bounded-exec');
57
+ expect(pkg.nx?.targets?.test?.options?.command).toBe('bun test');
58
+
59
+ // tsconfig.json (wrapper)
60
+ const tsconfig = readJson(tree, 'packages/duration/tsconfig.json');
61
+ expect(tsconfig.extends).toBe('../../tsconfig.base.json');
62
+ expect(tsconfig.files).toEqual([]);
63
+ expect(tsconfig.include).toEqual([]);
64
+ expect(tsconfig.references).toEqual([{ path: './tsconfig.lib.json' }]);
65
+
66
+ // tsconfig.lib.json
67
+ const tsconfigLib = readJson(tree, 'packages/duration/tsconfig.lib.json');
68
+ expect(tsconfigLib.extends).toBe('../../tsconfig.base.json');
69
+ expect(tsconfigLib.compilerOptions.rootDir).toBe('src');
70
+ expect(tsconfigLib.compilerOptions.outDir).toBe('dist');
71
+ expect(tsconfigLib.compilerOptions.types).toEqual([]);
72
+ expect(tsconfigLib.include).toEqual(['src/**/*.ts']);
73
+ expect(tsconfigLib.exclude).toEqual(['src/**/*.test.ts', 'src/**/__tests__/**']);
74
+
75
+ // tsconfig.test.json
76
+ const tsconfigTest = readJson(tree, 'packages/duration/tsconfig.test.json');
77
+ expect(tsconfigTest.extends).toBe('../../tsconfig.base.json');
78
+ expect(tsconfigTest.compilerOptions.types).toEqual(['bun']);
79
+ expect(tsconfigTest.compilerOptions.composite).toBe(false);
80
+ expect(tsconfigTest.compilerOptions.noEmit).toBe(true);
81
+ expect(tsconfigTest.include).toContain('src/**/*.test.ts');
82
+ expect(tsconfigTest.references).toEqual([{ path: './tsconfig.lib.json' }]);
83
+
84
+ // bunfig.toml
85
+ const bunfig = tree.read('packages/duration/bunfig.toml', 'utf-8');
86
+ expect(bunfig).toContain('[test]');
87
+ expect(bunfig).toContain('timeout = 30000');
88
+ expect(bunfig).toContain('preload = ["@smoothbricks/validation/bun/preload"]');
89
+
90
+ // src/index.ts
91
+ expect(tree.exists('packages/duration/src/index.ts')).toBe(true);
92
+ });
93
+
94
+ it('adds publication metadata when public', async () => {
95
+ const tree = setupTree();
96
+ await generator(tree, { name: 'mylib', variant: 'ts-lib', public: true });
97
+
98
+ const pkg = readJson(tree, 'packages/mylib/package.json');
99
+ expect(pkg.private).toBeUndefined();
100
+ expect(pkg.license).toBe('MIT');
101
+ expect(pkg.publishConfig).toEqual({ access: 'public' });
102
+ expect(pkg.repository).toEqual({
103
+ type: 'git',
104
+ url: 'git+https://github.com/smoothbricks/codebase.git',
105
+ directory: 'packages/mylib',
106
+ });
107
+ expect(pkg.nx?.tags).toEqual(['npm:public']);
108
+ });
109
+
110
+ it('derives scope from root package.json', async () => {
111
+ const tree = setupTree('@other/monorepo');
112
+ await generator(tree, { name: 'mylib', variant: 'ts-lib' });
113
+
114
+ const pkg = readJson(tree, 'packages/mylib/package.json');
115
+ expect(pkg.name).toBe('@other/mylib');
116
+ });
117
+
118
+ it('works without scope', async () => {
119
+ const tree = setupTree('my-monorepo');
120
+ await generator(tree, { name: 'mylib', variant: 'ts-lib' });
121
+
122
+ const pkg = readJson(tree, 'packages/mylib/package.json');
123
+ expect(pkg.name).toBe('mylib');
124
+ });
125
+ });
126
+
127
+ describe('ts-zig variant', () => {
128
+ it('creates zig scaffold on top of ts-lib', async () => {
129
+ const tree = setupTree();
130
+ await generator(tree, { name: 'columine', variant: 'ts-zig' });
131
+
132
+ // Zig files exist
133
+ expect(tree.exists('packages/columine/build.zig')).toBe(true);
134
+ expect(tree.exists('packages/columine/build.zig.zon')).toBe(true);
135
+ expect(tree.exists('packages/columine/src/columine.zig')).toBe(true);
136
+
137
+ // build.zig contains wasm step
138
+ const buildZig = tree.read('packages/columine/build.zig', 'utf-8') ?? '';
139
+ expect(buildZig).toContain('b.step("wasm", "Build WASM artifact")');
140
+ expect(buildZig).toContain('.name = "columine"');
141
+ expect(buildZig).toContain('b.path("src/columine.zig")');
142
+
143
+ // build.zig.zon
144
+ const buildZigZon = tree.read('packages/columine/build.zig.zon', 'utf-8') ?? '';
145
+ expect(buildZigZon).toContain('.name = .columine');
146
+
147
+ // src/<name>.zig
148
+ const zigSrc = tree.read('packages/columine/src/columine.zig', 'utf-8') ?? '';
149
+ expect(zigSrc).toContain('export fn add');
150
+
151
+ // package.json has wasm export and build scripts
152
+ const pkg = readJson(tree, 'packages/columine/package.json');
153
+ expect(pkg.exports['./wasm']).toBe('./dist/columine.wasm');
154
+ expect(pkg.exports['.'].development).toBeUndefined();
155
+ expect(pkg.scripts['build:zig']).toBe('nx run columine:zig-wasm');
156
+ expect(pkg.scripts['build:ts']).toBe('nx run columine:tsc-js');
157
+ expect(pkg.scripts.build).toBe('bun run build:ts && bun run build:zig');
158
+ expect(pkg.files).toEqual(['dist']);
159
+
160
+ // tsconfig.lib.json has webworker lib
161
+ const tsconfigLib = readJson(tree, 'packages/columine/tsconfig.lib.json');
162
+ expect(tsconfigLib.compilerOptions.lib).toEqual(['es2022', 'webworker']);
163
+ expect(tsconfigLib.compilerOptions.declaration).toBe(true);
164
+ expect(tsconfigLib.compilerOptions.sourceMap).toBe(true);
165
+
166
+ // tsconfig.test.json has webworker lib
167
+ const tsconfigTest = readJson(tree, 'packages/columine/tsconfig.test.json');
168
+ expect(tsconfigTest.compilerOptions.lib).toEqual(['es2022', 'webworker']);
169
+ });
170
+
171
+ it('combines zig and publication metadata when public', async () => {
172
+ const tree = setupTree();
173
+ await generator(tree, { name: 'zigpub', variant: 'ts-zig', public: true });
174
+
175
+ const pkg = readJson(tree, 'packages/zigpub/package.json');
176
+
177
+ // Has zig files
178
+ expect(tree.exists('packages/zigpub/build.zig')).toBe(true);
179
+ expect(tree.exists('packages/zigpub/build.zig.zon')).toBe(true);
180
+ expect(tree.exists('packages/zigpub/src/zigpub.zig')).toBe(true);
181
+
182
+ // Has publication metadata
183
+ expect(pkg.private).toBeUndefined();
184
+ expect(pkg.license).toBe('MIT');
185
+ expect(pkg.publishConfig).toEqual({ access: 'public' });
186
+ expect(pkg.repository).toEqual({
187
+ type: 'git',
188
+ url: 'git+https://github.com/smoothbricks/codebase.git',
189
+ directory: 'packages/zigpub',
190
+ });
191
+ expect(pkg.nx?.tags).toEqual(['npm:public']);
192
+
193
+ // Has wasm export
194
+ expect(pkg.exports['./wasm']).toBe('./dist/zigpub.wasm');
195
+ });
196
+ });
197
+
198
+ it('rejects existing package', async () => {
199
+ const tree = setupTree();
200
+ tree.write('packages/existing/package.json', '{}');
201
+
202
+ await expect(generator(tree, { name: 'existing', variant: 'ts-lib' })).rejects.toThrow(
203
+ 'Package already exists at packages/existing',
204
+ );
205
+ });
206
+ });
@@ -0,0 +1,251 @@
1
+ // Scaffold a new workspace package. Invoked by `smoo g ts-lib|ts-zig <name>`.
2
+ // CLI wiring: packages/cli/src/generate/index.ts (variant registry)
3
+ import { readJson, type Tree, writeJson } from 'nx/src/devkit-exports.js';
4
+
5
+ import type { CreatePackageGeneratorSchema } from './schema.js';
6
+
7
+ export default async function generator(tree: Tree, schema: CreatePackageGeneratorSchema): Promise<void> {
8
+ const rootPkg = readJson<{ name?: string; repository?: unknown }>(tree, 'package.json');
9
+ const scope = extractScope(rootPkg.name);
10
+ const packageName = scope ? `${scope}/${schema.name}` : schema.name;
11
+ const projectRoot = `packages/${schema.name}`;
12
+
13
+ if (tree.exists(`${projectRoot}/package.json`)) {
14
+ throw new Error(`Package already exists at ${projectRoot}`);
15
+ }
16
+
17
+ writeCommonFiles(tree, schema, packageName, projectRoot, rootPkg);
18
+
19
+ if (schema.variant === 'ts-zig') {
20
+ writeZigFiles(tree, schema, projectRoot);
21
+ }
22
+ }
23
+
24
+ function extractScope(name: string | undefined): string | null {
25
+ if (!name) return null;
26
+ const match = /^(@[^/]+)\//.exec(name);
27
+ return match?.[1] ?? null;
28
+ }
29
+
30
+ function writeCommonFiles(
31
+ tree: Tree,
32
+ schema: CreatePackageGeneratorSchema,
33
+ packageName: string,
34
+ projectRoot: string,
35
+ rootPkg: { name?: string; repository?: unknown },
36
+ ): void {
37
+ const packageJson = buildPackageJson(schema, packageName, projectRoot, rootPkg);
38
+ writeJson(tree, `${projectRoot}/package.json`, packageJson);
39
+
40
+ writeJson(tree, `${projectRoot}/tsconfig.json`, {
41
+ extends: '../../tsconfig.base.json',
42
+ files: [],
43
+ include: [],
44
+ references: [{ path: './tsconfig.lib.json' }],
45
+ });
46
+
47
+ writeJson(tree, `${projectRoot}/tsconfig.lib.json`, {
48
+ extends: '../../tsconfig.base.json',
49
+ compilerOptions: {
50
+ rootDir: 'src',
51
+ outDir: 'dist',
52
+ tsBuildInfoFile: 'dist/tsconfig.lib.tsbuildinfo',
53
+ emitDeclarationOnly: false,
54
+ forceConsistentCasingInFileNames: true,
55
+ types: [],
56
+ },
57
+ include: ['src/**/*.ts'],
58
+ exclude: ['src/**/*.test.ts', 'src/**/__tests__/**'],
59
+ });
60
+
61
+ writeJson(tree, `${projectRoot}/tsconfig.test.json`, {
62
+ extends: '../../tsconfig.base.json',
63
+ compilerOptions: {
64
+ types: ['bun'],
65
+ composite: false,
66
+ declaration: false,
67
+ declarationMap: false,
68
+ emitDeclarationOnly: false,
69
+ noEmit: true,
70
+ },
71
+ include: [
72
+ 'src/**/*.test.ts',
73
+ 'src/**/*.spec.ts',
74
+ 'src/**/__tests__/**/*.ts',
75
+ 'src/**/__tests__/**/*.tsx',
76
+ 'src/test-suite-tracer.ts',
77
+ ],
78
+ references: [{ path: './tsconfig.lib.json' }],
79
+ });
80
+
81
+ tree.write(
82
+ `${projectRoot}/bunfig.toml`,
83
+ '[test]\ntimeout = 30000\npreload = ["@smoothbricks/validation/bun/preload"]\n',
84
+ );
85
+
86
+ tree.write(`${projectRoot}/src/index.ts`, '');
87
+ }
88
+
89
+ function buildPackageJson(
90
+ schema: CreatePackageGeneratorSchema,
91
+ packageName: string,
92
+ projectRoot: string,
93
+ rootPkg: { name?: string; repository?: unknown },
94
+ ): Record<string, unknown> {
95
+ const packageJson: Record<string, unknown> = {
96
+ name: packageName,
97
+ version: '0.0.0',
98
+ type: 'module',
99
+ sideEffects: false,
100
+ main: './dist/index.js',
101
+ module: './dist/index.js',
102
+ types: './dist/index.d.ts',
103
+ exports: {
104
+ './package.json': './package.json',
105
+ '.': {
106
+ types: './dist/index.d.ts',
107
+ development: './src/index.ts',
108
+ import: './dist/index.js',
109
+ default: './dist/index.js',
110
+ },
111
+ },
112
+ files: ['dist', 'src', '!**/*.tsbuildinfo'],
113
+ scripts: {
114
+ test: `nx run ${schema.name}:test --outputStyle=stream`,
115
+ },
116
+ dependencies: {
117
+ tslib: '^2.8.1',
118
+ },
119
+ devDependencies: {
120
+ '@smoothbricks/validation': 'workspace:*',
121
+ },
122
+ nx: {
123
+ name: schema.name,
124
+ targets: {
125
+ lint: {},
126
+ test: {
127
+ executor: '@smoothbricks/nx-plugin:bounded-exec',
128
+ options: {
129
+ command: 'bun test',
130
+ cwd: '{projectRoot}',
131
+ timeoutMs: 120000,
132
+ killAfterMs: 10000,
133
+ },
134
+ },
135
+ },
136
+ },
137
+ };
138
+
139
+ if (schema.public) {
140
+ packageJson.license = 'MIT';
141
+ packageJson.publishConfig = { access: 'public' };
142
+ const rootRepository = rootPkg.repository;
143
+ if (rootRepository && typeof rootRepository === 'object') {
144
+ packageJson.repository = {
145
+ type: (rootRepository as Record<string, unknown>).type ?? 'git',
146
+ url: (rootRepository as Record<string, unknown>).url,
147
+ directory: projectRoot,
148
+ };
149
+ }
150
+ (packageJson.nx as Record<string, unknown>).tags = ['npm:public'];
151
+ } else {
152
+ packageJson.private = true;
153
+ }
154
+
155
+ return packageJson;
156
+ }
157
+
158
+ function writeZigFiles(tree: Tree, schema: CreatePackageGeneratorSchema, projectRoot: string): void {
159
+ // Modify package.json for ts-zig variant
160
+ const packageJson = readJson<Record<string, unknown>>(tree, `${projectRoot}/package.json`);
161
+
162
+ // Add wasm export and remove development condition
163
+ const exports = packageJson.exports as Record<string, unknown>;
164
+ exports['./wasm'] = `./dist/${schema.name}.wasm`;
165
+ const dotExport = exports['.'] as Record<string, unknown>;
166
+ delete dotExport.development;
167
+
168
+ // Add build scripts
169
+ const scripts = packageJson.scripts as Record<string, string>;
170
+ scripts['build:zig'] = `nx run ${schema.name}:zig-wasm`;
171
+ scripts['build:ts'] = `nx run ${schema.name}:tsc-js`;
172
+ scripts.build = 'bun run build:ts && bun run build:zig';
173
+
174
+ // Change files for zig packages (no src published)
175
+ packageJson.files = ['dist'];
176
+
177
+ writeJson(tree, `${projectRoot}/package.json`, packageJson);
178
+
179
+ // Modify tsconfig.lib.json for ts-zig
180
+ const tsconfigLib = readJson<Record<string, unknown>>(tree, `${projectRoot}/tsconfig.lib.json`);
181
+ const libCompilerOptions = tsconfigLib.compilerOptions as Record<string, unknown>;
182
+ libCompilerOptions.lib = ['es2022', 'webworker'];
183
+ libCompilerOptions.declaration = true;
184
+ libCompilerOptions.sourceMap = true;
185
+ libCompilerOptions.skipLibCheck = true;
186
+ writeJson(tree, `${projectRoot}/tsconfig.lib.json`, tsconfigLib);
187
+
188
+ // Modify tsconfig.test.json for ts-zig
189
+ const tsconfigTest = readJson<Record<string, unknown>>(tree, `${projectRoot}/tsconfig.test.json`);
190
+ const testCompilerOptions = tsconfigTest.compilerOptions as Record<string, unknown>;
191
+ testCompilerOptions.lib = ['es2022', 'webworker'];
192
+ writeJson(tree, `${projectRoot}/tsconfig.test.json`, tsconfigTest);
193
+
194
+ // Write build.zig
195
+ tree.write(
196
+ `${projectRoot}/build.zig`,
197
+ `const std = @import("std");
198
+
199
+ pub fn build(b: *std.Build) void {
200
+ const optimize = b.standardOptimizeOption(.{});
201
+
202
+ const wasm_step = b.step("wasm", "Build WASM artifact");
203
+
204
+ const wasm_target = b.resolveTargetQuery(.{
205
+ .cpu_arch = .wasm32,
206
+ .os_tag = .freestanding,
207
+ });
208
+
209
+ const lib = b.addExecutable(.{
210
+ .name = "${schema.name}",
211
+ .root_source_file = b.path("src/${schema.name}.zig"),
212
+ .target = wasm_target,
213
+ .optimize = optimize,
214
+ });
215
+ lib.entry = .disabled;
216
+ lib.rdynamic = true;
217
+
218
+ const install = b.addInstallArtifact(lib, .{
219
+ .dest_dir = .{ .override = .{ .custom = "../dist" } },
220
+ });
221
+ wasm_step.dependOn(&install.step);
222
+ }
223
+ `,
224
+ );
225
+
226
+ // Write build.zig.zon
227
+ tree.write(
228
+ `${projectRoot}/build.zig.zon`,
229
+ `.{
230
+ .name = .${schema.name},
231
+ .version = "0.0.0",
232
+ .minimum_zig_version = "0.14.0",
233
+ .dependencies = .{},
234
+ .paths = .{
235
+ "build.zig",
236
+ "build.zig.zon",
237
+ "src",
238
+ },
239
+ }
240
+ `,
241
+ );
242
+
243
+ // Write src/<name>.zig
244
+ tree.write(
245
+ `${projectRoot}/src/${schema.name}.zig`,
246
+ `export fn add(a: i32, b: i32) i32 {
247
+ return a + b;
248
+ }
249
+ `,
250
+ );
251
+ }
@@ -0,0 +1,5 @@
1
+ export interface CreatePackageGeneratorSchema {
2
+ name: string;
3
+ variant: 'ts-lib' | 'ts-zig';
4
+ public?: boolean;
5
+ }
@@ -0,0 +1,24 @@
1
+ {
2
+ "$schema": "http://json-schema.org/schema",
3
+ "id": "CreatePackage",
4
+ "title": "Create a new workspace package",
5
+ "type": "object",
6
+ "properties": {
7
+ "name": {
8
+ "type": "string",
9
+ "description": "Unscoped package name (e.g. 'duration'). Scope is derived from root package.json."
10
+ },
11
+ "variant": {
12
+ "type": "string",
13
+ "description": "Package variant to scaffold.",
14
+ "enum": ["ts-lib", "ts-zig"],
15
+ "default": "ts-lib"
16
+ },
17
+ "public": {
18
+ "type": "boolean",
19
+ "description": "Configure for npm publication (license, publishConfig, npm:public tag).",
20
+ "default": false
21
+ }
22
+ },
23
+ "required": ["name", "variant"]
24
+ }