From 5f2bea0b97068580b91ca0b289af93acd8ead629 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Tue, 22 Dec 2020 15:02:57 +0800 Subject: [PATCH 1/2] feat(cli): support --cargo-cwd flag --- cli/src/build.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/cli/src/build.ts b/cli/src/build.ts index 8dec9f2d..96f6553b 100644 --- a/cli/src/build.ts +++ b/cli/src/build.ts @@ -38,6 +38,9 @@ export class BuildCommand extends Command { @Command.String('--cargo-flags') cargoFlags = '' + @Command.String('--cargo-cwd') + cargoCwd!: string + @Command.String({ required: false, }) @@ -45,6 +48,9 @@ export class BuildCommand extends Command { @Command.Path('build') async execute() { + const cwd = this.cargoCwd + ? join(process.cwd(), this.cargoCwd) + : process.cwd() const releaseFlag = this.isRelease ? `--release` : '' const targetFLag = this.targetTripleDir ? `--target ${this.targetTripleDir}` @@ -71,6 +77,7 @@ export class BuildCommand extends Command { execSync(cargoCommand, { env: process.env, stdio: 'inherit', + cwd, }) const { binaryName } = getNapiConfig(this.configFileName) let dylibName = this.cargoName @@ -80,11 +87,11 @@ export class BuildCommand extends Command { try { debug('Start read toml') tomlContentString = await readFileAsync( - join(process.cwd(), 'Cargo.toml'), + join(cwd, 'Cargo.toml'), 'utf-8', ) } catch { - throw new TypeError(`Could not find Cargo.toml in ${process.cwd()}`) + throw new TypeError(`Could not find Cargo.toml in ${cwd}`) } try { @@ -151,7 +158,7 @@ export class BuildCommand extends Command { distModulePath = `${distModulePath}${platformName}.node` } - const dir = await findUp() + const dir = await findUp(cwd) if (!dir) { throw new TypeError('No target dir found') From 754462238a1b85ce4c2147f29930058a0c916c9f Mon Sep 17 00:00:00 2001 From: LongYinan Date: Tue, 22 Dec 2020 15:14:29 +0800 Subject: [PATCH 2/2] feat(cli): check 'cdylib' in crate-type --- cli/src/build.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cli/src/build.ts b/cli/src/build.ts index 96f6553b..7f5ff343 100644 --- a/cli/src/build.ts +++ b/cli/src/build.ts @@ -106,6 +106,14 @@ export class BuildCommand extends Command { } else { throw new TypeError('No package.name field in Cargo.toml') } + + if (!tomlContent.lib?.['crate-type']?.includes?.('cdylib')) { + throw new TypeError( + `Missing ${chalk.green('create-type = ["cdylib"]')} in ${chalk.green( + '[lib]', + )}`, + ) + } } debug(`Dylib name: ${chalk.greenBright(dylibName)}`)