UNPKG

@neodx/vfs

Version:

Simple virtual file system - working dir context, lazy changes, different modes, integrations and moreover

56 lines (53 loc) 1.8 kB
import { addPackageJsonDependencies, removePackageJsonDependencies } from '@neodx/pkg-misc'; import { hasOwn, fromKeys, toArray, identity } from '@neodx/std'; import { P, match } from 'ts-pattern'; import { c as createVfsPlugin } from '../_internal/create-vfs-plugin-BzqnUd8c.mjs'; import { createJsonFileApi } from './json.mjs'; function packageJson() { return createVfsPlugin('packageJson', vfs => { vfs.packageJson = (path = 'package.json') => createVfsPackageJsonFileApi(vfs, path); return vfs; }); } function createVfsPackageJsonFileApi(vfs, path) { const file = createJsonFileApi(vfs, path); const sync = async content => { if (content) await file.write(content); return Boolean(content); }; return { ...file, async hasDependency(name) { const pkg = await file.read(); return allDeps.some(type => hasOwn(pkg[type] ?? {}, name)); }, async addDependencies(deps) { return sync( addPackageJsonDependencies( await file.read(), match(deps) .with(stringOrArray, name => ({ dependencies: fromKeys(toArray(name), () => '*') })) .with(P._, identity) .exhaustive() ) ); }, async removeDependencies(deps) { return sync( removePackageJsonDependencies( await file.read(), match(deps) .with(stringOrArray, name => fromKeys(allDeps, () => toArray(name))) .with(P._, identity) .exhaustive() ) ); } }; } const stringOrArray = P.string.or(P.array(P.string)); const allDeps = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies']; export { createVfsPackageJsonFileApi, packageJson }; //# sourceMappingURL=package-json.mjs.map