Merge pull request #860 from napi-rs/pipe-command

feat(napi): add pipe flag to pipe the generated files into custom command
This commit is contained in:
LongYinan 2021-11-16 14:11:06 +08:00 committed by GitHub
commit 0743ced07a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -80,6 +80,12 @@ export class BuildCommand extends Command {
)} file`, )} file`,
}) })
pipe?: string = Option.String('--pipe', {
description: `Pipe [${chalk.green(
'.js/.ts',
)}] files to this command, eg ${chalk.green('prettier -w')}`,
})
destDir = Option.String({ destDir = Option.String({
required: false, required: false,
}) })
@ -234,19 +240,46 @@ export class BuildCommand extends Command {
debug(`Write binary content to [${chalk.yellowBright(distModulePath)}]`) debug(`Write binary content to [${chalk.yellowBright(distModulePath)}]`)
await copyFileAsync(sourcePath, distModulePath) await copyFileAsync(sourcePath, distModulePath)
const dtsFilePath = join(
process.cwd(),
this.destDir ?? '.',
this.dts ?? 'index.d.ts',
)
const idents = await processIntermediateTypeFile( const idents = await processIntermediateTypeFile(
intermediateTypeFile, intermediateTypeFile,
join(this.destDir ?? '.', this.dts ?? 'index.d.ts'), dtsFilePath,
) )
await writeJsBinding( if (this.pipe) {
binaryName, const pipeCommand = `${this.pipe} ${dtsFilePath}`
packageName, console.info(`Run ${chalk.green(pipeCommand)}`)
try {
execSync(pipeCommand, { stdio: 'inherit', env: process.env })
} catch (e) {
console.warn(
chalk.bgYellowBright('Pipe the dts file to command failed'),
e,
)
}
}
const jsBindingFilePath =
this.jsBinding && this.jsBinding !== 'false' this.jsBinding && this.jsBinding !== 'false'
? join(process.cwd(), this.jsBinding) ? join(process.cwd(), this.jsBinding)
: null, : null
idents, await writeJsBinding(binaryName, packageName, jsBindingFilePath, idents)
if (this.pipe && jsBindingFilePath) {
const pipeCommand = `${this.pipe} ${jsBindingFilePath}`
console.info(`Run ${chalk.green(pipeCommand)}`)
try {
execSync(pipeCommand, { stdio: 'inherit', env: process.env })
} catch (e) {
console.warn(
chalk.bgYellowBright('Pipe the js binding file to command failed'),
e,
) )
} }
}
}
} }
async function findUp(dir = process.cwd()): Promise<string | null> { async function findUp(dir = process.cwd()): Promise<string | null> {