From caeef4a6757a811d23bb76bd2c441587178ec6f5 Mon Sep 17 00:00:00 2001 From: Edward Elric Date: Thu, 10 Nov 2022 23:44:39 +0800 Subject: [PATCH] fix(cli): incorrent version without npm folder --- cli/src/update-package.ts | 10 +++++++++- cli/src/utils.ts | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/cli/src/update-package.ts b/cli/src/update-package.ts index 520e715b..7498bdf7 100644 --- a/cli/src/update-package.ts +++ b/cli/src/update-package.ts @@ -1,9 +1,17 @@ -import { writeFileAsync } from './utils' +import { debugFactory } from './debug' +import { writeFileAsync, fileExists } from './utils' + +const debug = debugFactory('update-package') export async function updatePackageJson( path: string, partial: Record, ) { + const exists = await fileExists(path) + if (!exists) { + debug(`File not exists ${path}`) + return + } const old = require(path) await writeFileAsync(path, JSON.stringify({ ...old, ...partial }, null, 2)) } diff --git a/cli/src/utils.ts b/cli/src/utils.ts index cfa5ce4f..ccd00378 100644 --- a/cli/src/utils.ts +++ b/cli/src/utils.ts @@ -1,4 +1,4 @@ -import { readFile, writeFile, copyFile, mkdir, unlink } from 'fs' +import { readFile, writeFile, copyFile, mkdir, unlink, stat } from 'fs' import { promisify } from 'util' export const readFileAsync = promisify(readFile) @@ -6,6 +6,14 @@ export const writeFileAsync = promisify(writeFile) export const unlinkAsync = promisify(unlink) export const copyFileAsync = promisify(copyFile) export const mkdirAsync = promisify(mkdir) +export const statAsync = promisify(stat) + +export async function fileExists(path: string) { + const exists = await statAsync(path) + .then(() => true) + .catch(() => false) + return exists +} export function pick(o: O, ...keys: K[]): Pick { return keys.reduce((acc, key) => {