fix(cli): create dist dir if not existed while building

This commit is contained in:
LongYinan 2021-08-09 06:07:34 +00:00 committed by GitHub
parent 6e7540b2a3
commit e90ea9304a
2 changed files with 18 additions and 5 deletions

View file

@ -8,7 +8,13 @@ import toml from 'toml'
import { getNapiConfig } from './consts'
import { debugFactory } from './debug'
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')
@ -156,12 +162,18 @@ export class BuildCommand extends Command {
debug(`Platform name: ${platformName || chalk.green('[Empty]')}`)
const distFileName = `${binaryName}${platformName}.node`
let distModulePath = join(this.destDir ?? '.', distFileName)
const distModulePath = join(this.destDir ?? '.', distFileName)
const parsedDist = parse(distModulePath)
if (!parsedDist.ext) {
distModulePath = `${distModulePath}${platformName}.node`
if (parsedDist.dir && !(await existsAsync(parsedDist.dir))) {
await mkdirAsync(parsedDist.dir, { recursive: true }).catch((e) => {
console.warn(
chalk.bgYellowBright(
`Create dir [${parsedDist.dir}] failed, reason: ${e.message}`,
),
)
})
}
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'
export const readFileAsync = promisify(readFile)
@ -6,6 +6,7 @@ export const writeFileAsync = promisify(writeFile)
export const existsAsync = promisify(exists)
export const unlinkAsync = promisify(unlink)
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> {
return keys.reduce((acc, key) => {