UNPKG

@napi-rs/cli

Version:
60 lines (52 loc) 1.32 kB
import { readFile, writeFile, unlink, copyFile, mkdir, stat, readdir, access, } from 'node:fs/promises' import pkgJson from '../../package.json' with { type: 'json' } import { debug } from './log.js' export const readFileAsync = readFile export const writeFileAsync = writeFile export const unlinkAsync = unlink export const copyFileAsync = copyFile export const mkdirAsync = mkdir export const statAsync = stat export const readdirAsync = readdir export function fileExists(path: string): Promise<boolean> { return access(path).then( () => true, () => false, ) } export async function dirExistsAsync(path: string) { try { const stats = await statAsync(path) return stats.isDirectory() } catch { return false } } export function pick<O, K extends keyof O>(o: O, ...keys: K[]): Pick<O, K> { return keys.reduce((acc, key) => { acc[key] = o[key] return acc }, {} as O) } export async function updatePackageJson( path: string, partial: Record<string, any>, ) { const exists = await fileExists(path) if (!exists) { debug(`File not exists ${path}`) return } const old = JSON.parse(await readFileAsync(path, 'utf8')) await writeFileAsync(path, JSON.stringify({ ...old, ...partial }, null, 2)) } export const CLI_VERSION = pkgJson.version