UNPKG

@nozomiishii/lefthook-config

Version:
36 lines (35 loc) 1.4 kB
import { existsSync } from "node:fs"; import { readFile, writeFile } from "node:fs/promises"; import path from "node:path"; import { fileURLToPath } from "node:url"; //#region src/init/index.ts /** Scaffold lefthook config into the consumer project. */ /** * bundle後のチャンク位置に依存せず、package.jsonのあるパッケージルートを探す。 * tsdownはinitを `dist/init-<hash>.js` へホイストするため `../../` が固定で使えない。 */ function packageRoot() { let dir = path.dirname(fileURLToPath(import.meta.url)); while (!existsSync(path.join(dir, "package.json"))) { const parent = path.dirname(dir); if (parent === dir) throw new Error("package.json not found"); dir = parent; } return dir; } async function init({ cwd }) { const root = packageRoot(); const selfPkg = JSON.parse(await readFile(path.join(root, "package.json"), "utf8")); const starter = await readFile(path.join(root, "starter.yaml"), "utf8"); const targetPath = path.resolve(cwd, "package.json"); const target = JSON.parse(await readFile(targetPath, "utf8")); target.devDependencies = { ...target.devDependencies, [selfPkg.name]: selfPkg.version, lefthook: selfPkg.peerDependencies.lefthook }; await writeFile(targetPath, `${JSON.stringify(target, null, 2)}\n`); await writeFile(path.resolve(cwd, "lefthook.yaml"), starter); } //#endregion export { init as t };