From e90ea9304a2a3180148f7e8f9e5f63eeb374a3cf Mon Sep 17 00:00:00 2001 From: LongYinan Date: Mon, 9 Aug 2021 06:07:34 +0000 Subject: [PATCH] fix(cli): create dist dir if not existed while building --- cli/src/build.ts | 20 ++++++++++++++++---- cli/src/utils.ts | 3 ++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/cli/src/build.ts b/cli/src/build.ts index 6b9219eb..10e299e1 100644 --- a/cli/src/build.ts +++ b/cli/src/build.ts @@ -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( diff --git a/cli/src/utils.ts b/cli/src/utils.ts index 050846d8..e7a8d8a3 100644 --- a/cli/src/utils.ts +++ b/cli/src/utils.ts @@ -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: O, ...keys: K[]): Pick { return keys.reduce((acc, key) => {