@smoothbricks/nx-plugin 0.0.0-bootstrap.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +93 -2
- package/dist/bounded-test-policy.d.ts +32 -0
- package/dist/bounded-test-policy.d.ts.map +1 -0
- package/dist/bounded-test-policy.js +184 -0
- package/dist/executors/bounded-exec/executor.d.ts +15 -0
- package/dist/executors/bounded-exec/executor.d.ts.map +1 -0
- package/dist/executors/bounded-exec/executor.js +179 -0
- package/dist/executors/bounded-exec/schema.d.ts +11 -0
- package/dist/executors/bounded-exec/schema.d.ts.map +1 -0
- package/dist/executors/bounded-exec/schema.js +1 -0
- package/dist/generators/bounded-test-targets/generator.d.ts +7 -0
- package/dist/generators/bounded-test-targets/generator.d.ts.map +1 -0
- package/dist/generators/bounded-test-targets/generator.js +40 -0
- package/dist/generators/bun-test-tracing/generator.d.ts +10 -0
- package/dist/generators/bun-test-tracing/generator.d.ts.map +1 -0
- package/dist/generators/bun-test-tracing/generator.js +247 -0
- package/dist/generators/create-package/generator.d.ts +4 -0
- package/dist/generators/create-package/generator.d.ts.map +1 -0
- package/dist/generators/create-package/generator.js +204 -0
- package/dist/generators/make-public/generator.d.ts +4 -0
- package/dist/generators/make-public/generator.d.ts.map +1 -0
- package/dist/generators/make-public/generator.js +55 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +197 -0
- package/dist/package-target-policy.d.ts +31 -0
- package/dist/package-target-policy.d.ts.map +1 -0
- package/dist/package-target-policy.js +930 -0
- package/dist/release-config-policy.d.ts +32 -0
- package/dist/release-config-policy.d.ts.map +1 -0
- package/dist/release-config-policy.js +225 -0
- package/dist/typecheck-test-policy.d.ts +56 -0
- package/dist/typecheck-test-policy.d.ts.map +1 -0
- package/dist/typecheck-test-policy.js +640 -0
- package/dist/version-actions.cjs +78 -0
- package/dist/version-actions.d.cts +6 -0
- package/dist/version-actions.d.cts.map +1 -0
- package/dist/workspace-config-policy.d.ts +33 -0
- package/dist/workspace-config-policy.d.ts.map +1 -0
- package/dist/workspace-config-policy.js +334 -0
- package/executors.json +9 -0
- package/generators.json +24 -0
- package/loader.js +16 -0
- package/package.json +89 -4
- package/src/bounded-test-policy.test.ts +257 -0
- package/src/bounded-test-policy.ts +257 -0
- package/src/bun-bootstrap.test.ts +52 -0
- package/src/executors/bounded-exec/executor.test.ts +258 -0
- package/src/executors/bounded-exec/executor.ts +228 -0
- package/src/executors/bounded-exec/schema.json +68 -0
- package/src/executors/bounded-exec/schema.ts +10 -0
- package/src/generators/bounded-test-targets/generator.test.ts +136 -0
- package/src/generators/bounded-test-targets/generator.ts +67 -0
- package/src/generators/bounded-test-targets/schema.d.ts +3 -0
- package/src/generators/bounded-test-targets/schema.json +14 -0
- package/src/generators/bun-test-tracing/generator.test.ts +243 -0
- package/src/generators/bun-test-tracing/generator.ts +348 -0
- package/src/generators/bun-test-tracing/schema.d.ts +8 -0
- package/src/generators/bun-test-tracing/schema.json +28 -0
- package/src/generators/create-package/generator.test.ts +206 -0
- package/src/generators/create-package/generator.ts +251 -0
- package/src/generators/create-package/schema.d.ts +5 -0
- package/src/generators/create-package/schema.json +24 -0
- package/src/generators/make-public/generator.test.ts +137 -0
- package/src/generators/make-public/generator.ts +83 -0
- package/src/generators/make-public/schema.d.ts +3 -0
- package/src/generators/make-public/schema.json +13 -0
- package/src/index.test.ts +200 -0
- package/src/index.ts +242 -0
- package/src/package-target-policy.test.ts +778 -0
- package/src/package-target-policy.ts +1127 -0
- package/src/release-config-policy.test.ts +280 -0
- package/src/release-config-policy.ts +258 -0
- package/src/typecheck-test-policy.test.ts +797 -0
- package/src/typecheck-test-policy.ts +752 -0
- package/src/version-actions.cts +94 -0
- package/src/workspace-config-policy.test.ts +376 -0
- package/src/workspace-config-policy.ts +390 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/schema",
|
|
3
|
+
"cli": "nx",
|
|
4
|
+
"title": "Configure Bun test tracing",
|
|
5
|
+
"description": "Configure a workspace package for the standard Bun test tracing + dist-test TypeScript setup",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"properties": {
|
|
8
|
+
"project": {
|
|
9
|
+
"type": "string",
|
|
10
|
+
"description": "Nx project name, package name, or project root to configure"
|
|
11
|
+
},
|
|
12
|
+
"opContextModule": {
|
|
13
|
+
"type": "string",
|
|
14
|
+
"description": "Module specifier that exports the package op context"
|
|
15
|
+
},
|
|
16
|
+
"opContextExport": {
|
|
17
|
+
"type": "string",
|
|
18
|
+
"description": "Named export for the package op context",
|
|
19
|
+
"default": "opContext"
|
|
20
|
+
},
|
|
21
|
+
"tracerModule": {
|
|
22
|
+
"type": "string",
|
|
23
|
+
"description": "Module specifier for defineTestTracer",
|
|
24
|
+
"default": "@smoothbricks/lmao/testing/bun"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"required": ["project", "opContextModule"]
|
|
28
|
+
}
|
|
@@ -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 --tui=false --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 --tui=false --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,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
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { addProjectConfiguration, 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(): Tree {
|
|
8
|
+
const tree = createTreeWithEmptyWorkspace();
|
|
9
|
+
tree.write(
|
|
10
|
+
'package.json',
|
|
11
|
+
JSON.stringify(
|
|
12
|
+
{
|
|
13
|
+
name: '@smoothbricks/codebase',
|
|
14
|
+
license: 'MIT',
|
|
15
|
+
repository: { type: 'git', url: 'git+https://github.com/smoothbricks/codebase.git' },
|
|
16
|
+
workspaces: ['packages/*'],
|
|
17
|
+
},
|
|
18
|
+
null,
|
|
19
|
+
2,
|
|
20
|
+
),
|
|
21
|
+
);
|
|
22
|
+
return tree;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function addPackage(tree: Tree, name: string, root: string, extra: Record<string, unknown> = {}): void {
|
|
26
|
+
addProjectConfiguration(tree, name, { root, targets: {} });
|
|
27
|
+
if (tree.exists(`${root}/project.json`)) tree.delete(`${root}/project.json`);
|
|
28
|
+
tree.write(
|
|
29
|
+
`${root}/package.json`,
|
|
30
|
+
JSON.stringify(
|
|
31
|
+
{
|
|
32
|
+
name: `@smoothbricks/${name}`,
|
|
33
|
+
version: '0.0.0',
|
|
34
|
+
private: true,
|
|
35
|
+
nx: { name },
|
|
36
|
+
...extra,
|
|
37
|
+
},
|
|
38
|
+
null,
|
|
39
|
+
2,
|
|
40
|
+
),
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
describe('make-public generator', () => {
|
|
45
|
+
it('promotes private package to public', async () => {
|
|
46
|
+
const tree = setupTree();
|
|
47
|
+
addPackage(tree, 'my-lib', 'packages/my-lib');
|
|
48
|
+
|
|
49
|
+
await generator(tree, { project: 'my-lib' });
|
|
50
|
+
|
|
51
|
+
const pkg = readJson(tree, 'packages/my-lib/package.json');
|
|
52
|
+
expect(pkg.private).toBeUndefined();
|
|
53
|
+
expect(pkg.license).toBe('MIT');
|
|
54
|
+
expect(pkg.publishConfig).toEqual({ access: 'public' });
|
|
55
|
+
expect(pkg.repository).toEqual({
|
|
56
|
+
type: 'git',
|
|
57
|
+
url: 'git+https://github.com/smoothbricks/codebase.git',
|
|
58
|
+
directory: 'packages/my-lib',
|
|
59
|
+
});
|
|
60
|
+
expect(pkg.nx.tags).toContain('npm:public');
|
|
61
|
+
expect(pkg.version).toBe('0.1.0');
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('preserves existing license', async () => {
|
|
65
|
+
const tree = setupTree();
|
|
66
|
+
addPackage(tree, 'my-lib', 'packages/my-lib', { license: 'Apache-2.0' });
|
|
67
|
+
|
|
68
|
+
await generator(tree, { project: 'my-lib' });
|
|
69
|
+
|
|
70
|
+
const pkg = readJson(tree, 'packages/my-lib/package.json');
|
|
71
|
+
expect(pkg.license).toBe('Apache-2.0');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('does not duplicate npm:public tag', async () => {
|
|
75
|
+
const tree = setupTree();
|
|
76
|
+
addPackage(tree, 'my-lib', 'packages/my-lib', { nx: { name: 'my-lib', tags: ['npm:public'] } });
|
|
77
|
+
|
|
78
|
+
await generator(tree, { project: 'my-lib' });
|
|
79
|
+
|
|
80
|
+
const pkg = readJson(tree, 'packages/my-lib/package.json');
|
|
81
|
+
const publicTags = pkg.nx.tags.filter((t: string) => t === 'npm:public');
|
|
82
|
+
expect(publicTags).toHaveLength(1);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('preserves version above 0.0.0', async () => {
|
|
86
|
+
const tree = setupTree();
|
|
87
|
+
addPackage(tree, 'my-lib', 'packages/my-lib', { version: '1.2.3' });
|
|
88
|
+
|
|
89
|
+
await generator(tree, { project: 'my-lib' });
|
|
90
|
+
|
|
91
|
+
const pkg = readJson(tree, 'packages/my-lib/package.json');
|
|
92
|
+
expect(pkg.version).toBe('1.2.3');
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('resolves by project name, package name, and root', async () => {
|
|
96
|
+
const tree = setupTree();
|
|
97
|
+
addPackage(tree, 'my-lib', 'packages/my-lib');
|
|
98
|
+
|
|
99
|
+
// By project name
|
|
100
|
+
await generator(tree, { project: 'my-lib' });
|
|
101
|
+
expect(readJson(tree, 'packages/my-lib/package.json').private).toBeUndefined();
|
|
102
|
+
|
|
103
|
+
// Reset private for next lookup test
|
|
104
|
+
tree.write(
|
|
105
|
+
'packages/my-lib/package.json',
|
|
106
|
+
JSON.stringify(
|
|
107
|
+
{ name: '@smoothbricks/my-lib', version: '0.0.0', private: true, nx: { name: 'my-lib' } },
|
|
108
|
+
null,
|
|
109
|
+
2,
|
|
110
|
+
),
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
// By package name
|
|
114
|
+
await generator(tree, { project: '@smoothbricks/my-lib' });
|
|
115
|
+
expect(readJson(tree, 'packages/my-lib/package.json').private).toBeUndefined();
|
|
116
|
+
|
|
117
|
+
// Reset private for next lookup test
|
|
118
|
+
tree.write(
|
|
119
|
+
'packages/my-lib/package.json',
|
|
120
|
+
JSON.stringify(
|
|
121
|
+
{ name: '@smoothbricks/my-lib', version: '0.0.0', private: true, nx: { name: 'my-lib' } },
|
|
122
|
+
null,
|
|
123
|
+
2,
|
|
124
|
+
),
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
// By root path
|
|
128
|
+
await generator(tree, { project: 'packages/my-lib' });
|
|
129
|
+
expect(readJson(tree, 'packages/my-lib/package.json').private).toBeUndefined();
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('throws for unknown project', async () => {
|
|
133
|
+
const tree = setupTree();
|
|
134
|
+
|
|
135
|
+
expect(generator(tree, { project: 'nonexistent' })).rejects.toThrow('Could not resolve project nonexistent');
|
|
136
|
+
});
|
|
137
|
+
});
|