Merge pull request #687 from napi-rs/create-dist-dir-if-not-existed

fix(cli): create dist dir if not existed while building
This commit is contained in:
LongYinan 2021-08-09 22:00:47 +08:00 committed by GitHub
commit 14bc9fce4a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 5 deletions

View file

@ -8,7 +8,13 @@ import toml from 'toml'
import { getNapiConfig } from './consts' import { getNapiConfig } from './consts'
import { debugFactory } from './debug' import { debugFactory } from './debug'
import { getDefaultTargetTriple, parseTriple } from './parse-triple' import { getDefaultTargetTriple, parseTriple } from './parse-triple'
import { copyFileAsync, existsAsync, readFileAsync, unlinkAsync } from './utils' import {
copyFileAsync,
existsAsync,
mkdirAsync,
readFileAsync,
unlinkAsync,
} from './utils'
const debug = debugFactory('build') const debug = debugFactory('build')
@ -156,12 +162,18 @@ export class BuildCommand extends Command {
debug(`Platform name: ${platformName || chalk.green('[Empty]')}`) debug(`Platform name: ${platformName || chalk.green('[Empty]')}`)
const distFileName = `${binaryName}${platformName}.node` const distFileName = `${binaryName}${platformName}.node`
let distModulePath = join(this.destDir ?? '.', distFileName) const distModulePath = join(this.destDir ?? '.', distFileName)
const parsedDist = parse(distModulePath) const parsedDist = parse(distModulePath)
if (!parsedDist.ext) { if (parsedDist.dir && !(await existsAsync(parsedDist.dir))) {
distModulePath = `${distModulePath}${platformName}.node` await mkdirAsync(parsedDist.dir, { recursive: true }).catch((e) => {
console.warn(
chalk.bgYellowBright(
`Create dir [${parsedDist.dir}] failed, reason: ${e.message}`,
),
)
})
} }
const sourcePath = join( const sourcePath = join(

View file

@ -1,4 +1,4 @@
import { readFile, writeFile, exists, copyFile, unlink } from 'fs' import { readFile, writeFile, exists, copyFile, mkdir, unlink } from 'fs'
import { promisify } from 'util' import { promisify } from 'util'
export const readFileAsync = promisify(readFile) export const readFileAsync = promisify(readFile)
@ -6,6 +6,7 @@ export const writeFileAsync = promisify(writeFile)
export const existsAsync = promisify(exists) export const existsAsync = promisify(exists)
export const unlinkAsync = promisify(unlink) export const unlinkAsync = promisify(unlink)
export const copyFileAsync = promisify(copyFile) export const copyFileAsync = promisify(copyFile)
export const mkdirAsync = promisify(mkdir)
export function pick<O, K extends keyof O>(o: O, ...keys: K[]): Pick<O, K> { export function pick<O, K extends keyof O>(o: O, ...keys: K[]): Pick<O, K> {
return keys.reduce((acc, key) => { return keys.reduce((acc, key) => {