UNPKG

gtw

Version:

git

89 lines (70 loc) 1.9 kB
const fs = require("fs-extra"); const path = require("path"); const colors = require("colors"); const cwd = process.cwd(); const userPkg = getUserPkg(); function getUserPkg() { return fs.existsSync(path.resolve(cwd, "package.json")) ? require(path.resolve(cwd, "package.json")) : {}; } const huskyVersion = getHuskyVersion(); function install(hook, { write = false }) { const src = path.resolve(__dirname, `../src/hooks/${hook}.js`); if (!write) { if (fs.existsSync(src)) { const dest = path.resolve(cwd, `.gtw/${hook}.js`); if (!fs.existsSync(dest)) { fs.copySync(src, dest) } else { console.log(colors.yellow(`Hook ${hook} already exists`)); } } } else { writeHook(hook); } process.exit(1); } function remove(hook) { let hookPath = path.resolve(cwd, `.gtw/${hook}.js`); if (!fs.existsSync(hookPath)) { if (huskyVersion) { if (huskyVersion >= 6) hookPath = path.resolve(cwd, `.husky/${hook}`); } else { hookPath = path.resolve(cwd, `.git/${hook}`); } } fs.removeSync(hookPath); process.exit(1); } function writeHook(hook) { if (huskyVersion) { let _path; if (huskyVersion >= 6) { _path = path.resolve(cwd, ".husky/post-checkout"); } else { _path = path.resolve(cwd, ".git/hooks/post-checkout"); } handleWrite(hook, _path); } } function handleWrite(hook, path) { if (fs.existsSync(path)) { fs.appendFileSync(path, `node node_modules/gtw/src/hooks/${hook}.js`); } else { throw new Error(`${path} not found`); } } function getHuskyVersion() { if (!userPkg) throw new Error("Package does not exist"); if (userPkg.devDependencies) { const husky = userPkg.devDependencies.husky; return husky ? husky.replace(/~|\^|=/, "").match(/^(.*?)\.(.*?)$/)[1] : null; } } module.exports = { install, remove, };