-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
41 changed files
with
854 additions
and
331 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
# compiled output | ||
/dist | ||
/tmp | ||
tmp | ||
/out-tsc | ||
|
||
# dependencies | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { execSync } from 'child_process'; | ||
import { createTestProject, runNxCommand } from './utils'; | ||
import { rmSync } from 'fs'; | ||
import { listFiles } from '@nx/plugin/testing'; | ||
|
||
describe('napi', () => { | ||
let projectDirectory: string; | ||
beforeAll(() => { | ||
projectDirectory = createTestProject(); | ||
|
||
// The plugin has been built and published to a local registry in the jest globalSetup | ||
// Install the plugin built with the latest source code into the test repo | ||
execSync(`npm install @monodon/rust@e2e`, { | ||
cwd: projectDirectory, | ||
stdio: 'inherit', | ||
env: process.env, | ||
}); | ||
}); | ||
|
||
afterAll(() => { | ||
// Cleanup the test project | ||
rmSync(projectDirectory, { | ||
recursive: true, | ||
force: true, | ||
}); | ||
}); | ||
|
||
it('should create a napi project', () => { | ||
runNxCommand( | ||
`generate @monodon/rust:lib napi-proj --napi`, | ||
projectDirectory | ||
); | ||
|
||
expect(listFiles('napi_proj/npm').length).toBeGreaterThan(0); | ||
|
||
expect(() => | ||
runNxCommand(`build napi_proj`, projectDirectory) | ||
).not.toThrow(); | ||
|
||
const files = listFiles('napi_proj'); | ||
expect(files).toContain('.node'); | ||
|
||
expect(() => | ||
runNxCommand( | ||
`build napi_proj -- --target wasm32-wasip1-threads`, | ||
projectDirectory | ||
) | ||
).not.toThrow(); | ||
const files2 = listFiles('napi_proj'); | ||
expect(files2).toContain('wasm32-wasi.node'); | ||
expect(files2).toContain('wasi-worker.mjs'); | ||
expect(files2).toContain('wasi-worker-browser.mjs'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { dirname, join } from 'path'; | ||
import { mkdirSync, rmSync } from 'fs'; | ||
import { execSync } from 'child_process'; | ||
import { tmpProjPath } from '@nx/plugin/testing'; | ||
|
||
/** | ||
* Creates a test project with create-nx-workspace and installs the plugin | ||
* @returns The directory where the test project was created | ||
*/ | ||
export function createTestProject() { | ||
const projectName = 'test-project'; | ||
const projectDirectory = tmpProjPath(projectName); | ||
|
||
// Ensure projectDirectory is empty | ||
rmSync(projectDirectory, { | ||
recursive: true, | ||
force: true, | ||
}); | ||
mkdirSync(dirname(projectDirectory), { | ||
recursive: true, | ||
}); | ||
|
||
execSync( | ||
`npx --yes create-nx-workspace@latest ${projectName} --preset apps --nxCloud=skip --no-interactive`, | ||
{ | ||
cwd: dirname(projectDirectory), | ||
stdio: 'inherit', | ||
env: process.env, | ||
} | ||
); | ||
console.log(`Created test project in "${projectDirectory}"`); | ||
|
||
return projectDirectory; | ||
} | ||
|
||
export function runNxCommand(command: string, projectDir: string) { | ||
execSync(`npx nx ${command}`, { cwd: projectDir, stdio: 'inherit' }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"generators": { | ||
"updating-napi": { | ||
"version": "2.0.0", | ||
"description": "Migration for v2.0.0", | ||
"implementation": "./src/migrations/updating-napi" | ||
} | ||
}, | ||
"packageJsonUpdates": { | ||
"2.0.0": { | ||
"version": "2.0.0-beta.1", | ||
"requires": { | ||
"@napi-rs/cli": "<3.0.0" | ||
}, | ||
"packages": { | ||
"@napi-rs/cli": { | ||
"version": "3.0.0-alpha.55", | ||
"alwaysAddToPackageJson": false | ||
}, | ||
"@napi-rs/wasm-runtime": { | ||
"version": "^0.2.4", | ||
"alwaysAddToPackageJson": true | ||
}, | ||
"emnapi": { | ||
"version": "^1.1.0", | ||
"alwaysAddToPackageJson": true | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,53 @@ | ||
import { ExecutorContext, getPackageManagerCommand } from '@nx/devkit'; | ||
import { NapiExecutorSchema } from './schema'; | ||
import { runProcess } from '../../utils/run-process'; | ||
import { ExecutorContext, joinPathFragments, workspaceRoot } from '@nx/devkit'; | ||
import { NapiExecutorSchema } from './schema.js'; | ||
import { join } from 'path'; | ||
import { fileExists } from 'nx/src/utils/fileutils'; | ||
import { fileExists } from 'nx/src/utils/fileutils.js'; | ||
import { cargoMetadata } from '../../utils/cargo'; | ||
|
||
export default async function runExecutor( | ||
options: NapiExecutorSchema, | ||
context: ExecutorContext | ||
) { | ||
const { exec } = getPackageManagerCommand(); | ||
const command = `${exec} napi build`; | ||
const args: string[] = []; | ||
if (options.release) { | ||
args.push('--release'); | ||
const { NapiCli } = await import('@napi-rs/cli'); | ||
const projectRoot = | ||
context.projectGraph?.nodes[context.projectName ?? ''].data.root; | ||
const packageJson = join(projectRoot ?? '.', 'package.json'); | ||
if (!fileExists(packageJson)) { | ||
throw new Error(`Could not find package.json at ${packageJson}`); | ||
} | ||
|
||
if (options.target) { | ||
args.push('--target'); | ||
args.push(options.target); | ||
} | ||
const napi = new NapiCli(); | ||
|
||
args.push('--platform'); | ||
const buildOptions: Parameters<typeof napi.build>[0] = {}; | ||
|
||
const projectRoot = | ||
context.projectGraph?.nodes[context.projectName ?? ''].data.root; | ||
const projectJson = join(projectRoot ?? '.', 'package.json'); | ||
if (!fileExists(projectJson)) { | ||
throw new Error(`Could not find package.json at ${projectJson}`); | ||
buildOptions.platform = true; | ||
buildOptions.jsBinding = options.jsFile; | ||
buildOptions.outputDir = options.dist; | ||
buildOptions.manifestPath = join(projectRoot ?? '.', 'Cargo.toml'); | ||
buildOptions.packageJsonPath = packageJson; | ||
if (options.release) { | ||
buildOptions.release = true; | ||
} | ||
|
||
args.push('-c'); | ||
args.push(projectJson); | ||
|
||
if (typeof projectRoot == 'string') { | ||
args.push('--cargo-cwd'); | ||
args.push(projectRoot); | ||
if (options.target) { | ||
buildOptions.target = options.target; | ||
} | ||
|
||
args.push('--js'); | ||
args.push(options.jsFile); | ||
if (options.zig) { | ||
buildOptions.crossCompile = true; | ||
} | ||
|
||
args.push(options.dist); | ||
const metadata = cargoMetadata(); | ||
buildOptions.targetDir = | ||
metadata?.target_directory ?? | ||
joinPathFragments(workspaceRoot, 'dist', 'cargo'); | ||
|
||
if (options.zig) { | ||
args.push('--zig'); | ||
if (process.env.VERCEL) { | ||
// Vercel doesnt have support for cargo atm, so auto success builds | ||
return { success: true }; | ||
} | ||
|
||
return runProcess(command, ...args); | ||
const { task } = await napi.build(buildOptions); | ||
const output = await task; | ||
return { success: true, terminalOutput: output }; | ||
} |
3 changes: 0 additions & 3 deletions
3
packages/rust/src/generators/add-napi/files/npm/darwin-arm64/README.md__template__
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
packages/rust/src/generators/add-napi/files/npm/darwin-arm64/package.json__template__
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
packages/rust/src/generators/add-napi/files/npm/darwin-x64/README.md__template__
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
packages/rust/src/generators/add-napi/files/npm/darwin-x64/package.json__template__
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
packages/rust/src/generators/add-napi/files/npm/freebsd-x64/README.md__template__
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.