@smoothbricks/nx-plugin 0.3.8 → 0.3.10
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 +7 -0
- package/dist/cross-check-policy.d.ts +65 -0
- package/dist/cross-check-policy.d.ts.map +1 -0
- package/dist/cross-check-policy.js +64 -0
- package/dist/executors/napi-cross-toolchain/executor.d.ts +42 -0
- package/dist/executors/napi-cross-toolchain/executor.d.ts.map +1 -0
- package/dist/executors/napi-cross-toolchain/executor.js +118 -0
- package/dist/executors/napi-cross-toolchain/schema.d.ts +4 -0
- package/dist/executors/napi-cross-toolchain/schema.d.ts.map +1 -0
- package/dist/executors/napi-cross-toolchain/schema.js +1 -0
- package/dist/generators/create-package/generator.d.ts.map +1 -1
- package/dist/generators/create-package/generator.js +77 -5
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +122 -12
- package/dist/typecheck-test-policy.d.ts.map +1 -1
- package/dist/typecheck-test-policy.js +22 -2
- package/dist/workspace-config-policy.js +14 -0
- package/executors.json +5 -0
- package/generators.json +1 -1
- package/package.json +7 -46
- package/src/cross-check-policy.test.ts +81 -0
- package/src/cross-check-policy.ts +70 -0
- package/src/executors/napi-cross-toolchain/executor.test.ts +112 -0
- package/src/executors/napi-cross-toolchain/executor.ts +157 -0
- package/src/executors/napi-cross-toolchain/schema.json +15 -0
- package/src/executors/napi-cross-toolchain/schema.ts +3 -0
- package/src/generators/create-package/generator.test.ts +49 -0
- package/src/generators/create-package/generator.ts +88 -6
- package/src/generators/create-package/schema.d.ts +2 -1
- package/src/generators/create-package/schema.json +7 -2
- package/src/index.test.ts +92 -14
- package/src/index.ts +136 -12
- package/src/typecheck-test-policy.ts +24 -2
- package/src/workspace-config-policy.ts +14 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { afterAll, beforeAll, describe, expect, it } from 'bun:test';
|
|
2
|
+
import { existsSync, mkdirSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
|
|
6
|
+
import { prewarmCrossToolchain } from './executor.js';
|
|
7
|
+
|
|
8
|
+
// The pins that make extraction possible live on the consumer, so the real
|
|
9
|
+
// extraction runs against that project: `cowshed` declares the
|
|
10
|
+
// `@napi-rs/cross-toolchain-*-target-*` optionalDependencies its cross builds
|
|
11
|
+
// need. aarch64 is the one triple pinned for both an arm64 and an x64 host, so
|
|
12
|
+
// this covers developer machines and CI with the same archive.
|
|
13
|
+
const consumerProjectRoot = join(import.meta.dir, '../../../../cowshed');
|
|
14
|
+
const triple = 'aarch64-unknown-linux-gnu';
|
|
15
|
+
const temporaryDirs: string[] = [];
|
|
16
|
+
|
|
17
|
+
function temporaryDir(prefix: string): string {
|
|
18
|
+
const dir = mkdtempSync(join(tmpdir(), prefix));
|
|
19
|
+
temporaryDirs.push(dir);
|
|
20
|
+
return dir;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let firstHome = '';
|
|
24
|
+
let firstOutcome = '';
|
|
25
|
+
let version = '';
|
|
26
|
+
|
|
27
|
+
beforeAll(() => {
|
|
28
|
+
firstHome = temporaryDir('napi-cross-toolchain-');
|
|
29
|
+
firstOutcome = prewarmCrossToolchain({
|
|
30
|
+
projectRoot: consumerProjectRoot,
|
|
31
|
+
triple,
|
|
32
|
+
platform: 'linux',
|
|
33
|
+
home: firstHome,
|
|
34
|
+
});
|
|
35
|
+
// The directory napi probes is keyed by the resolved @napi-rs/cross-toolchain
|
|
36
|
+
// version, so read it back instead of restating the pinned version here.
|
|
37
|
+
const versions = readdirSync(join(firstHome, '.napi-rs', 'cross-toolchain'));
|
|
38
|
+
expect(versions).toHaveLength(1);
|
|
39
|
+
version = versions[0] ?? '';
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
afterAll(() => {
|
|
43
|
+
for (const dir of temporaryDirs) {
|
|
44
|
+
rmSync(dir, { force: true, recursive: true });
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('napi-cross-toolchain executor', () => {
|
|
49
|
+
it('extracts the pinned toolchain once and then reports it ready', () => {
|
|
50
|
+
expect(firstOutcome).toBe('extracted');
|
|
51
|
+
|
|
52
|
+
// napi derives the compiler, sysroot and PATH entries from this layout and
|
|
53
|
+
// skips extraction on the package.json marker alone: a marker without the
|
|
54
|
+
// toolchain turns every later build into a missing-linker failure.
|
|
55
|
+
const toolchain = join(firstHome, '.napi-rs', 'cross-toolchain', version, triple);
|
|
56
|
+
expect(existsSync(join(toolchain, 'package.json'))).toBe(true);
|
|
57
|
+
expect(existsSync(join(toolchain, 'bin', `${triple}-gcc`))).toBe(true);
|
|
58
|
+
expect(existsSync(join(toolchain, triple, 'sysroot'))).toBe(true);
|
|
59
|
+
|
|
60
|
+
expect(
|
|
61
|
+
prewarmCrossToolchain({ projectRoot: consumerProjectRoot, triple, platform: 'linux', home: firstHome }),
|
|
62
|
+
).toBe('ready');
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('lands the toolchain in the empty directory napi creates before probing', () => {
|
|
66
|
+
const home = temporaryDir('napi-cross-toolchain-');
|
|
67
|
+
const destination = join(home, '.napi-rs', 'cross-toolchain', version, triple);
|
|
68
|
+
mkdirSync(destination, { recursive: true });
|
|
69
|
+
|
|
70
|
+
expect(prewarmCrossToolchain({ projectRoot: consumerProjectRoot, triple, platform: 'linux', home })).toBe(
|
|
71
|
+
'extracted',
|
|
72
|
+
);
|
|
73
|
+
expect(existsSync(join(destination, 'package.json'))).toBe(true);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('leaves hosts that cannot run the toolchain to the build itself', () => {
|
|
77
|
+
expect(
|
|
78
|
+
prewarmCrossToolchain({
|
|
79
|
+
projectRoot: consumerProjectRoot,
|
|
80
|
+
triple,
|
|
81
|
+
platform: 'darwin',
|
|
82
|
+
home: temporaryDir('napi-cross-toolchain-'),
|
|
83
|
+
}),
|
|
84
|
+
).toBe('unsupported-host');
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('rejects triples that have no cross toolchain package', () => {
|
|
88
|
+
expect(() =>
|
|
89
|
+
prewarmCrossToolchain({
|
|
90
|
+
projectRoot: consumerProjectRoot,
|
|
91
|
+
triple: 'armv7-unknown-linux-gnueabihf',
|
|
92
|
+
platform: 'linux',
|
|
93
|
+
home: temporaryDir('napi-cross-toolchain-'),
|
|
94
|
+
}),
|
|
95
|
+
).toThrow('Unsupported --use-napi-cross triple armv7-unknown-linux-gnueabihf');
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('names the optional dependency a project is missing', () => {
|
|
99
|
+
const projectRoot = temporaryDir('napi-cross-consumer-');
|
|
100
|
+
writeFileSync(join(projectRoot, 'package.json'), '{"name":"unpinned"}\n');
|
|
101
|
+
|
|
102
|
+
expect(() =>
|
|
103
|
+
prewarmCrossToolchain({
|
|
104
|
+
projectRoot,
|
|
105
|
+
triple,
|
|
106
|
+
platform: 'linux',
|
|
107
|
+
hostArch: 'x64',
|
|
108
|
+
home: temporaryDir('napi-cross-toolchain-'),
|
|
109
|
+
}),
|
|
110
|
+
).toThrow('@napi-rs/cross-toolchain-x64-target-aarch64 is not installed');
|
|
111
|
+
});
|
|
112
|
+
});
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync } from 'node:fs';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import { homedir } from 'node:os';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
|
|
7
|
+
import type { NapiCrossToolchainOptions } from './schema.js';
|
|
8
|
+
|
|
9
|
+
interface NapiCrossToolchainContext {
|
|
10
|
+
root: string;
|
|
11
|
+
projectName?: string;
|
|
12
|
+
projectsConfigurations?: {
|
|
13
|
+
projects: Record<string, { root?: string }>;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Package-name arch segment of `@napi-rs/cross-toolchain-<host>-target-<arch>`. */
|
|
18
|
+
const TARGET_PACKAGE_ARCH: Readonly<Record<string, string>> = {
|
|
19
|
+
'aarch64-unknown-linux-gnu': 'aarch64',
|
|
20
|
+
'x86_64-unknown-linux-gnu': 'x86_64',
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const HOST_PACKAGE_ARCH: Readonly<Record<string, string>> = {
|
|
24
|
+
arm64: 'arm64',
|
|
25
|
+
x64: 'x64',
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type PrewarmOutcome = 'ready' | 'extracted' | 'unsupported-host';
|
|
29
|
+
|
|
30
|
+
export default async function napiCrossToolchainExecutor(
|
|
31
|
+
options: NapiCrossToolchainOptions,
|
|
32
|
+
context: NapiCrossToolchainContext,
|
|
33
|
+
): Promise<{ success: boolean }> {
|
|
34
|
+
const projectName = context.projectName;
|
|
35
|
+
if (!projectName) {
|
|
36
|
+
throw new Error('@smoothbricks/nx-plugin:napi-cross-toolchain requires a project context.');
|
|
37
|
+
}
|
|
38
|
+
const project = context.projectsConfigurations?.projects[projectName];
|
|
39
|
+
if (!project) {
|
|
40
|
+
throw new Error(`Project ${projectName} was not found in the Nx project graph.`);
|
|
41
|
+
}
|
|
42
|
+
const projectRoot = path.resolve(context.root, project.root ?? '.');
|
|
43
|
+
const triple = options.triple;
|
|
44
|
+
|
|
45
|
+
console.log(`${triple}: ${prewarmCrossToolchain({ projectRoot, triple })}`);
|
|
46
|
+
|
|
47
|
+
return { success: true };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Extract the pinned cross toolchain into the directory
|
|
52
|
+
* `napi build --use-napi-cross` probes, so its own download never runs.
|
|
53
|
+
*
|
|
54
|
+
* WHY this exists: `@napi-rs/cross-toolchain@1.0.3`'s `download()` uses its own
|
|
55
|
+
* package directory as scratch space — `npm pack` there, unpack, then a
|
|
56
|
+
* non-`force` `rmSync` of the tarball. Under `linker = "isolated"` with
|
|
57
|
+
* `globalStore = true` that directory is the shared Bun store (in CI a
|
|
58
|
+
* host-wide mutable bind mount), so two concurrent `--use-napi-cross` builds of
|
|
59
|
+
* one triple pack the same filename into the same directory and the loser dies
|
|
60
|
+
* with `ENOENT ... lstat '<tarball>'`. Nx runs `cli-x64-linux` and
|
|
61
|
+
* `napi-x64-linux` in one wave, which is exactly that race.
|
|
62
|
+
*
|
|
63
|
+
* Extraction stages into a private sibling directory and lands with `rename`,
|
|
64
|
+
* so racing callers — and repeated runs after a crash — cannot corrupt the
|
|
65
|
+
* result: `rename` replaces an empty directory (napi itself creates one before
|
|
66
|
+
* probing) and fails with ENOTEMPTY against a populated one, which means
|
|
67
|
+
* another caller already won.
|
|
68
|
+
*/
|
|
69
|
+
export function prewarmCrossToolchain(options: {
|
|
70
|
+
projectRoot: string;
|
|
71
|
+
triple: string;
|
|
72
|
+
platform?: string;
|
|
73
|
+
hostArch?: string;
|
|
74
|
+
home?: string;
|
|
75
|
+
}): PrewarmOutcome {
|
|
76
|
+
const { projectRoot, triple } = options;
|
|
77
|
+
const targetArch = TARGET_PACKAGE_ARCH[triple];
|
|
78
|
+
if (!targetArch) {
|
|
79
|
+
throw new Error(
|
|
80
|
+
`Unsupported --use-napi-cross triple ${triple}. Supported: ${Object.keys(TARGET_PACKAGE_ARCH).join(', ')}.`,
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
const hostArch = HOST_PACKAGE_ARCH[options.hostArch ?? process.arch];
|
|
84
|
+
// napi-rs ships these toolchains for Linux x64 and Linux arm64 hosts only,
|
|
85
|
+
// and refuses `--use-napi-cross` anywhere else. Extracting on a host that can
|
|
86
|
+
// never use the result would only demand pins nothing needs, so leave the
|
|
87
|
+
// unsupported-host diagnostic to the build itself.
|
|
88
|
+
if (!hostArch || (options.platform ?? process.platform) !== 'linux') {
|
|
89
|
+
return 'unsupported-host';
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const requireFromProject = createRequire(path.join(projectRoot, 'package.json'));
|
|
93
|
+
const targetPackage = `@napi-rs/cross-toolchain-${hostArch}-target-${targetArch}`;
|
|
94
|
+
let targetModule: unknown;
|
|
95
|
+
try {
|
|
96
|
+
targetModule = requireFromProject(targetPackage);
|
|
97
|
+
} catch (error) {
|
|
98
|
+
throw new Error(
|
|
99
|
+
`${targetPackage} is not installed for ${projectRoot}. Add it to optionalDependencies so ${triple} ` +
|
|
100
|
+
'builds extract the pinned toolchain instead of downloading into the shared Bun store.',
|
|
101
|
+
{ cause: error },
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
if (
|
|
105
|
+
typeof targetModule !== 'object' ||
|
|
106
|
+
targetModule === null ||
|
|
107
|
+
!('toolchainPath' in targetModule) ||
|
|
108
|
+
typeof targetModule.toolchainPath !== 'string'
|
|
109
|
+
) {
|
|
110
|
+
throw new Error(`${targetPackage} no longer exports a toolchainPath string.`);
|
|
111
|
+
}
|
|
112
|
+
const tarball = targetModule.toolchainPath;
|
|
113
|
+
|
|
114
|
+
const home = options.home ?? homedir();
|
|
115
|
+
const destination = path.join(home, '.napi-rs', 'cross-toolchain', crossToolchainVersion(requireFromProject), triple);
|
|
116
|
+
const marker = path.join(destination, 'package.json');
|
|
117
|
+
if (existsSync(marker)) {
|
|
118
|
+
return 'ready';
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
mkdirSync(path.dirname(destination), { recursive: true });
|
|
122
|
+
const staging = mkdtempSync(`${destination}.staging-`);
|
|
123
|
+
try {
|
|
124
|
+
execFileSync('tar', ['-xJf', tarball, '-C', staging], { stdio: 'inherit' });
|
|
125
|
+
if (!existsSync(path.join(staging, 'package.json'))) {
|
|
126
|
+
throw new Error(
|
|
127
|
+
`${tarball} did not contain the package.json marker napi probes; the toolchain archive layout changed.`,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
renameSync(staging, destination);
|
|
131
|
+
} catch (error) {
|
|
132
|
+
rmSync(staging, { force: true, recursive: true });
|
|
133
|
+
const code = error instanceof Error && 'code' in error ? error.code : undefined;
|
|
134
|
+
if ((code === 'ENOTEMPTY' || code === 'EEXIST') && existsSync(marker)) {
|
|
135
|
+
return 'ready';
|
|
136
|
+
}
|
|
137
|
+
throw error;
|
|
138
|
+
}
|
|
139
|
+
return 'extracted';
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* `napi` derives the extraction directory from `@napi-rs/cross-toolchain`'s own
|
|
144
|
+
* version, so read it from the package the CLI resolves — not from the
|
|
145
|
+
* per-target package, whose version only happens to match today.
|
|
146
|
+
*/
|
|
147
|
+
function crossToolchainVersion(requireFromProject: NodeJS.Require): string {
|
|
148
|
+
const cliRequire = createRequire(requireFromProject.resolve('@napi-rs/cli/package.json'));
|
|
149
|
+
const indexPath = cliRequire.resolve('@napi-rs/cross-toolchain');
|
|
150
|
+
const manifest: unknown = JSON.parse(readFileSync(path.join(path.dirname(indexPath), 'package.json'), 'utf-8'));
|
|
151
|
+
const version =
|
|
152
|
+
typeof manifest === 'object' && manifest !== null && 'version' in manifest ? manifest.version : undefined;
|
|
153
|
+
if (typeof version !== 'string' || version.length === 0) {
|
|
154
|
+
throw new Error(`@napi-rs/cross-toolchain resolved at ${indexPath} has no version.`);
|
|
155
|
+
}
|
|
156
|
+
return version;
|
|
157
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/schema",
|
|
3
|
+
"version": 2,
|
|
4
|
+
"title": "N-API Cross Toolchain Executor",
|
|
5
|
+
"description": "Extract the pinned @napi-rs cross toolchain into ~/.napi-rs so `napi build --use-napi-cross` never downloads it.",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"properties": {
|
|
8
|
+
"triple": {
|
|
9
|
+
"type": "string",
|
|
10
|
+
"description": "Rust target triple to prepare, e.g. x86_64-unknown-linux-gnu."
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"required": ["triple"],
|
|
14
|
+
"additionalProperties": false
|
|
15
|
+
}
|
|
@@ -13,6 +13,7 @@ function setupTree(rootName = '@smoothbricks/codebase'): Tree {
|
|
|
13
13
|
name: rootName,
|
|
14
14
|
version: '0.0.0',
|
|
15
15
|
private: true,
|
|
16
|
+
license: 'MIT',
|
|
16
17
|
repository: { type: 'git', url: 'git+https://github.com/smoothbricks/codebase.git' },
|
|
17
18
|
workspaces: ['packages/*'],
|
|
18
19
|
},
|
|
@@ -124,6 +125,54 @@ describe('create-package generator', () => {
|
|
|
124
125
|
});
|
|
125
126
|
});
|
|
126
127
|
|
|
128
|
+
describe('rust-crate variant', () => {
|
|
129
|
+
it('creates a Cargo workspace whose generic targets are inferred by the plugin', async () => {
|
|
130
|
+
const tree = setupTree();
|
|
131
|
+
await generator(tree, { name: 'ferris', variant: 'rust-crate' });
|
|
132
|
+
|
|
133
|
+
const pkg = readJson(tree, 'packages/ferris/package.json');
|
|
134
|
+
expect(pkg).toEqual({
|
|
135
|
+
name: '@smoothbricks/ferris',
|
|
136
|
+
version: '0.0.0',
|
|
137
|
+
private: true,
|
|
138
|
+
scripts: { test: 'nx run ferris:test --outputStyle=stream' },
|
|
139
|
+
nx: { name: 'ferris' },
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
const workspace = tree.read('packages/ferris/Cargo.toml', 'utf-8');
|
|
143
|
+
expect(workspace).toContain('[workspace]');
|
|
144
|
+
expect(workspace).toContain('members = ["crates/ferris"]');
|
|
145
|
+
expect(workspace).toContain('edition = "2024"');
|
|
146
|
+
expect(workspace).toContain('license = "MIT"');
|
|
147
|
+
expect(workspace).toContain('repository = "git+https://github.com/smoothbricks/codebase.git"');
|
|
148
|
+
|
|
149
|
+
const crate = tree.read('packages/ferris/crates/ferris/Cargo.toml', 'utf-8');
|
|
150
|
+
expect(crate).toContain('[package]');
|
|
151
|
+
expect(crate).toContain('name = "ferris"');
|
|
152
|
+
expect(crate).toContain('version.workspace = true');
|
|
153
|
+
expect(tree.read('packages/ferris/crates/ferris/src/lib.rs', 'utf-8')).toBe('#![forbid(unsafe_code)]\n');
|
|
154
|
+
expect(tree.exists('packages/ferris/tsconfig.json')).toBe(false);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('declares tool-native wasm metadata for cross-platform output inference', async () => {
|
|
158
|
+
const tree = setupTree();
|
|
159
|
+
await generator(tree, { name: 'ferris', variant: 'rust-crate', wasm: true });
|
|
160
|
+
|
|
161
|
+
const crate = tree.read('packages/ferris/crates/ferris/Cargo.toml', 'utf-8');
|
|
162
|
+
expect(crate).toContain('crate-type = ["cdylib", "rlib"]');
|
|
163
|
+
expect(crate).toContain('[package.metadata.smoothbricks.wasm-bindgen]');
|
|
164
|
+
expect(crate).toContain('targets = ["web", "nodejs"]');
|
|
165
|
+
expect(crate).toContain('out-dir = "dist-wasm"');
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it('refuses npm publication flags on Cargo packages', async () => {
|
|
169
|
+
const tree = setupTree();
|
|
170
|
+
await expect(generator(tree, { name: 'ferris', variant: 'rust-crate', public: true })).rejects.toThrow(
|
|
171
|
+
'rust-crate does not use npm publication metadata',
|
|
172
|
+
);
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
127
176
|
it('rejects existing package', async () => {
|
|
128
177
|
const tree = setupTree();
|
|
129
178
|
tree.write('packages/existing/package.json', '{}');
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
// Scaffold
|
|
1
|
+
// Scaffold TypeScript libraries and Rust workspaces through one convention-owned generator.
|
|
2
2
|
// CLI wiring: packages/cli/src/generate/index.ts (variant registry)
|
|
3
3
|
import { readJson, type Tree, writeJson } from 'nx/src/devkit-exports.js';
|
|
4
4
|
|
|
5
5
|
import type { CreatePackageGeneratorSchema } from './schema.js';
|
|
6
6
|
|
|
7
7
|
export default async function generator(tree: Tree, schema: CreatePackageGeneratorSchema): Promise<void> {
|
|
8
|
-
const rootPkg = readJson<{ name?: string; repository?: unknown }>(tree, 'package.json');
|
|
8
|
+
const rootPkg = readJson<{ name?: string; license?: string; repository?: unknown }>(tree, 'package.json');
|
|
9
9
|
const scope = extractScope(rootPkg.name);
|
|
10
10
|
const packageName = scope ? `${scope}/${schema.name}` : schema.name;
|
|
11
11
|
const projectRoot = `packages/${schema.name}`;
|
|
@@ -13,8 +13,15 @@ export default async function generator(tree: Tree, schema: CreatePackageGenerat
|
|
|
13
13
|
if (tree.exists(`${projectRoot}/package.json`)) {
|
|
14
14
|
throw new Error(`Package already exists at ${projectRoot}`);
|
|
15
15
|
}
|
|
16
|
+
if (schema.variant === 'ts-lib' && schema.wasm) {
|
|
17
|
+
throw new Error('wasm output metadata is only available for rust-crate');
|
|
18
|
+
}
|
|
16
19
|
|
|
17
|
-
|
|
20
|
+
if (schema.variant === 'rust-crate') {
|
|
21
|
+
writeRustFiles(tree, schema, packageName, projectRoot, rootPkg);
|
|
22
|
+
} else {
|
|
23
|
+
writeTypeScriptFiles(tree, schema, packageName, projectRoot, rootPkg);
|
|
24
|
+
}
|
|
18
25
|
}
|
|
19
26
|
|
|
20
27
|
function extractScope(name: string | undefined): string | null {
|
|
@@ -23,14 +30,14 @@ function extractScope(name: string | undefined): string | null {
|
|
|
23
30
|
return match?.[1] ?? null;
|
|
24
31
|
}
|
|
25
32
|
|
|
26
|
-
function
|
|
33
|
+
function writeTypeScriptFiles(
|
|
27
34
|
tree: Tree,
|
|
28
35
|
schema: CreatePackageGeneratorSchema,
|
|
29
36
|
packageName: string,
|
|
30
37
|
projectRoot: string,
|
|
31
38
|
rootPkg: { name?: string; repository?: unknown },
|
|
32
39
|
): void {
|
|
33
|
-
const packageJson =
|
|
40
|
+
const packageJson = buildTypeScriptPackageJson(schema, packageName, projectRoot, rootPkg);
|
|
34
41
|
writeJson(tree, `${projectRoot}/package.json`, packageJson);
|
|
35
42
|
|
|
36
43
|
writeJson(tree, `${projectRoot}/tsconfig.json`, {
|
|
@@ -82,7 +89,7 @@ function writeCommonFiles(
|
|
|
82
89
|
tree.write(`${projectRoot}/src/index.ts`, '');
|
|
83
90
|
}
|
|
84
91
|
|
|
85
|
-
function
|
|
92
|
+
function buildTypeScriptPackageJson(
|
|
86
93
|
schema: CreatePackageGeneratorSchema,
|
|
87
94
|
packageName: string,
|
|
88
95
|
projectRoot: string,
|
|
@@ -154,6 +161,81 @@ function buildPackageJson(
|
|
|
154
161
|
|
|
155
162
|
return packageJson;
|
|
156
163
|
}
|
|
164
|
+
|
|
165
|
+
function writeRustFiles(
|
|
166
|
+
tree: Tree,
|
|
167
|
+
schema: CreatePackageGeneratorSchema,
|
|
168
|
+
packageName: string,
|
|
169
|
+
projectRoot: string,
|
|
170
|
+
rootPkg: { license?: string; repository?: unknown },
|
|
171
|
+
): void {
|
|
172
|
+
if (schema.public) {
|
|
173
|
+
throw new Error('rust-crate does not use npm publication metadata; publish Rust crates through Cargo policy');
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const crateName = schema.name;
|
|
177
|
+
const crateRoot = `${projectRoot}/crates/${crateName}`;
|
|
178
|
+
writeJson(tree, `${projectRoot}/package.json`, {
|
|
179
|
+
name: packageName,
|
|
180
|
+
version: '0.0.0',
|
|
181
|
+
private: true,
|
|
182
|
+
scripts: {
|
|
183
|
+
test: `nx run ${schema.name}:test --outputStyle=stream`,
|
|
184
|
+
},
|
|
185
|
+
nx: {
|
|
186
|
+
name: schema.name,
|
|
187
|
+
},
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
const repository = repositoryUrl(rootPkg.repository);
|
|
191
|
+
const workspacePackage = [
|
|
192
|
+
'[workspace]',
|
|
193
|
+
'resolver = "3"',
|
|
194
|
+
`members = ["crates/${crateName}"]`,
|
|
195
|
+
'',
|
|
196
|
+
'[workspace.package]',
|
|
197
|
+
'version = "0.0.0"',
|
|
198
|
+
'edition = "2024"',
|
|
199
|
+
`license = ${JSON.stringify(rootPkg.license ?? 'UNLICENSED')}`,
|
|
200
|
+
'publish = false',
|
|
201
|
+
...(repository === null ? [] : [`repository = ${JSON.stringify(repository)}`]),
|
|
202
|
+
'',
|
|
203
|
+
];
|
|
204
|
+
tree.write(`${projectRoot}/Cargo.toml`, workspacePackage.join('\n'));
|
|
205
|
+
|
|
206
|
+
const crateManifest = [
|
|
207
|
+
'[package]',
|
|
208
|
+
`name = ${JSON.stringify(crateName)}`,
|
|
209
|
+
'version.workspace = true',
|
|
210
|
+
'edition.workspace = true',
|
|
211
|
+
'license.workspace = true',
|
|
212
|
+
'publish.workspace = true',
|
|
213
|
+
...(schema.wasm
|
|
214
|
+
? [
|
|
215
|
+
'',
|
|
216
|
+
'[lib]',
|
|
217
|
+
'crate-type = ["cdylib", "rlib"]',
|
|
218
|
+
'',
|
|
219
|
+
'[package.metadata.smoothbricks.wasm-bindgen]',
|
|
220
|
+
'targets = ["web", "nodejs"]',
|
|
221
|
+
'out-dir = "dist-wasm"',
|
|
222
|
+
]
|
|
223
|
+
: []),
|
|
224
|
+
'',
|
|
225
|
+
];
|
|
226
|
+
tree.write(`${crateRoot}/Cargo.toml`, crateManifest.join('\n'));
|
|
227
|
+
tree.write(`${crateRoot}/src/lib.rs`, '#![forbid(unsafe_code)]\n');
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function repositoryUrl(repository: unknown): string | null {
|
|
231
|
+
if (typeof repository === 'string') {
|
|
232
|
+
return repository;
|
|
233
|
+
}
|
|
234
|
+
if (!isRecord(repository)) {
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
return typeof repository.url === 'string' ? repository.url : null;
|
|
238
|
+
}
|
|
157
239
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
158
240
|
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
159
241
|
}
|
|
@@ -11,12 +11,17 @@
|
|
|
11
11
|
"variant": {
|
|
12
12
|
"type": "string",
|
|
13
13
|
"description": "Package variant to scaffold.",
|
|
14
|
-
"enum": ["ts-lib"],
|
|
14
|
+
"enum": ["ts-lib", "rust-crate"],
|
|
15
15
|
"default": "ts-lib"
|
|
16
16
|
},
|
|
17
17
|
"public": {
|
|
18
18
|
"type": "boolean",
|
|
19
|
-
"description": "Configure for npm publication (license, publishConfig, npm:public tag).",
|
|
19
|
+
"description": "Configure a TypeScript package for npm publication (license, publishConfig, npm:public tag).",
|
|
20
|
+
"default": false
|
|
21
|
+
},
|
|
22
|
+
"wasm": {
|
|
23
|
+
"type": "boolean",
|
|
24
|
+
"description": "For rust-crate, scaffold wasm-bindgen metadata for web and Node.js output families.",
|
|
20
25
|
"default": false
|
|
21
26
|
}
|
|
22
27
|
},
|