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 { 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: O, ...keys: K[]): Pick { return keys.reduce((acc, key) => { acc[key] = o[key] return acc }, {} as O) } function isPlainObject(value: unknown): value is Record { return value !== null && typeof value === 'object' && !Array.isArray(value) } function mergePackageJson( old: Record, partial: Record, ): Record { const merged = { ...old } for (const [key, value] of Object.entries(partial)) { if (isPlainObject(merged[key]) && isPlainObject(value)) { merged[key] = mergePackageJson(merged[key], value) } else { merged[key] = value } } return merged } export async function updatePackageJson( path: string, partial: Record, ) { 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(mergePackageJson(old, partial), null, 2), ) } export const CLI_VERSION = pkgJson.version