UNPKG

constatic

Version:

Constatic is a CLI for creating and managing modern TypeScript projects, providing an organized structure and features that streamline development.

39 lines (37 loc) 910 B
// src/cli/fs.ts import constants from "node:constants"; import { access } from "node:fs/promises"; import { join } from "node:path"; import { readPackageJSON, writePackageJSON } from "pkg-types"; class CLIFileSystem { constructor() {} resolvePkgPath(path) { return path.endsWith("package.json") ? path : join(path, "package.json"); } async readPkgJson(path) { const resolved = this.resolvePkgPath(path); const exists = await this.exists(resolved); if (!exists) return null; try { return await readPackageJSON(resolved) ?? null; } catch { return null; } } async writePkgJson(path, pkg) { const resolved = this.resolvePkgPath(path); await writePackageJSON(resolved, pkg); } async exists(path) { try { await access(path, constants.F_OK); return true; } catch { return false; } } } export { CLIFileSystem };