From 58d4634dacca673a83bf49cfb2dd15c7f8444865 Mon Sep 17 00:00:00 2001 From: forehalo Date: Thu, 7 Jan 2021 23:26:41 +0800 Subject: [PATCH] fix(cli): fix random node process got killed issue --- cli/src/build.ts | 11 ++++++----- cli/src/utils.ts | 4 +++- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/cli/src/build.ts b/cli/src/build.ts index 20cc1c04..ce250279 100644 --- a/cli/src/build.ts +++ b/cli/src/build.ts @@ -8,7 +8,7 @@ import toml from 'toml' import { getNapiConfig } from './consts' import { debugFactory } from './debug' import { getDefaultTargetTriple, parseTriple } from './parse-triple' -import { existsAsync, readFileAsync, writeFileAsync } from './utils' +import { copyFileAsync, existsAsync, readFileAsync, unlinkAsync } from './utils' const debug = debugFactory('build') @@ -173,13 +173,14 @@ export class BuildCommand extends Command { } const sourcePath = join(dir, 'target', targetDir, `${dylibName}${libExt}`) - debug(`Read [${chalk.yellowBright(sourcePath)}] content`) - const dylibContent = await readFileAsync(sourcePath) + if (await existsAsync(distModulePath)) { + debug(`remove old binary [${chalk.yellowBright(sourcePath)}]`) + await unlinkAsync(distModulePath) + } debug(`Write binary content to [${chalk.yellowBright(distModulePath)}]`) - - await writeFileAsync(distModulePath, dylibContent) + await copyFileAsync(sourcePath, distModulePath) } } diff --git a/cli/src/utils.ts b/cli/src/utils.ts index eaa547e9..553f2d20 100644 --- a/cli/src/utils.ts +++ b/cli/src/utils.ts @@ -1,6 +1,8 @@ -import { readFile, writeFile, exists } from 'fs' +import { readFile, writeFile, exists, copyFile, unlink } from 'fs' import { promisify } from 'util' export const readFileAsync = promisify(readFile) export const writeFileAsync = promisify(writeFile) export const existsAsync = promisify(exists) +export const unlinkAsync = promisify(unlink) +export const copyFileAsync = promisify(copyFile)