From 4226561c7ea2ac6f06926970e4d23eb742f3a816 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sat, 5 Sep 2020 01:20:30 +0800 Subject: [PATCH] feat: support --cargo-name config in napi cli --- src/build.ts | 50 +++++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/src/build.ts b/src/build.ts index 4f3e9b2c..2ff8fb38 100644 --- a/src/build.ts +++ b/src/build.ts @@ -28,6 +28,9 @@ export class BuildCommand extends Command { @Command.String('--config,-c') configFileName?: string + @Command.String('--cargo-name') + cargoName?: string + @Command.String({ required: false, }) @@ -36,31 +39,32 @@ export class BuildCommand extends Command { @Command.Path('build') async execute() { const { binaryName } = getNapiConfig(this.configFileName) - let tomlContentString: string - let tomlContent: any - try { - debug('Start read toml') - tomlContentString = await readFileAsync( - join(process.cwd(), 'Cargo.toml'), - 'utf-8', - ) - } catch { - throw new TypeError(`Could not find Cargo.toml in ${process.cwd()}`) - } + let dylibName = this.cargoName + if (!dylibName) { + let tomlContentString: string + let tomlContent: any + try { + debug('Start read toml') + tomlContentString = await readFileAsync( + join(process.cwd(), 'Cargo.toml'), + 'utf-8', + ) + } catch { + throw new TypeError(`Could not find Cargo.toml in ${process.cwd()}`) + } - try { - debug('Start parse toml') - tomlContent = toml.parse(tomlContentString) - } catch { - throw new TypeError('Could not parse the Cargo.toml') - } + try { + debug('Start parse toml') + tomlContent = toml.parse(tomlContentString) + } catch { + throw new TypeError('Could not parse the Cargo.toml') + } - let dylibName - - if (tomlContent.package ?? tomlContent.package.name) { - dylibName = tomlContent.package.name.replace(/-/g, '_') - } else { - throw new TypeError('No package.name field in Cargo.toml') + if (tomlContent.package ?? tomlContent.package.name) { + dylibName = tomlContent.package.name.replace(/-/g, '_') + } else { + throw new TypeError('No package.name field in Cargo.toml') + } } debug(`Dylib name: ${chalk.greenBright(dylibName)}`)