Merge pull request #378 from napi-rs/build-enhancement

Build enhancement
This commit is contained in:
LongYinan 2020-12-22 15:53:10 +08:00 committed by GitHub
commit 4f30c831a5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -38,6 +38,9 @@ export class BuildCommand extends Command {
@Command.String('--cargo-flags') @Command.String('--cargo-flags')
cargoFlags = '' cargoFlags = ''
@Command.String('--cargo-cwd')
cargoCwd!: string
@Command.String({ @Command.String({
required: false, required: false,
}) })
@ -45,6 +48,9 @@ export class BuildCommand extends Command {
@Command.Path('build') @Command.Path('build')
async execute() { async execute() {
const cwd = this.cargoCwd
? join(process.cwd(), this.cargoCwd)
: process.cwd()
const releaseFlag = this.isRelease ? `--release` : '' const releaseFlag = this.isRelease ? `--release` : ''
const targetFLag = this.targetTripleDir const targetFLag = this.targetTripleDir
? `--target ${this.targetTripleDir}` ? `--target ${this.targetTripleDir}`
@ -71,6 +77,7 @@ export class BuildCommand extends Command {
execSync(cargoCommand, { execSync(cargoCommand, {
env: process.env, env: process.env,
stdio: 'inherit', stdio: 'inherit',
cwd,
}) })
const { binaryName } = getNapiConfig(this.configFileName) const { binaryName } = getNapiConfig(this.configFileName)
let dylibName = this.cargoName let dylibName = this.cargoName
@ -80,11 +87,11 @@ export class BuildCommand extends Command {
try { try {
debug('Start read toml') debug('Start read toml')
tomlContentString = await readFileAsync( tomlContentString = await readFileAsync(
join(process.cwd(), 'Cargo.toml'), join(cwd, 'Cargo.toml'),
'utf-8', 'utf-8',
) )
} catch { } catch {
throw new TypeError(`Could not find Cargo.toml in ${process.cwd()}`) throw new TypeError(`Could not find Cargo.toml in ${cwd}`)
} }
try { try {
@ -99,6 +106,14 @@ export class BuildCommand extends Command {
} else { } else {
throw new TypeError('No package.name field in Cargo.toml') 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)}`) debug(`Dylib name: ${chalk.greenBright(dylibName)}`)
@ -151,7 +166,7 @@ export class BuildCommand extends Command {
distModulePath = `${distModulePath}${platformName}.node` distModulePath = `${distModulePath}${platformName}.node`
} }
const dir = await findUp() const dir = await findUp(cwd)
if (!dir) { if (!dir) {
throw new TypeError('No target dir found') throw new TypeError('No target dir found')