Comparing version 0.15.0-beta.3 to 0.15.0-beta.4
12
husky.js
const fs = require('fs') | ||
const execa = require('execa') | ||
@@ -8,6 +9,15 @@ // Prevent error when cloning husky project | ||
// Find Git dir | ||
const { status, stdout, stderr } = execa.sync('git', ['rev-parse --git-dir']) | ||
const gitDir = stdout | ||
if (status !== 0) { | ||
console.log(stderr) | ||
process.exit(1) | ||
} | ||
// Run installer | ||
require('./lib/install')[action](__dirname) | ||
require('./lib/install')[action](__dirname, gitDir) | ||
} else { | ||
console.error("Error can't find lib directory, run `npm run build`") | ||
} |
@@ -5,14 +5,5 @@ "use strict"; | ||
exports.huskyIdentifier = '# husky'; | ||
// Script template | ||
const hookScript = `#!/bin/sh | ||
exports.default = `#!/bin/sh | ||
${exports.huskyIdentifier} | ||
hookname=\`basename "$0"\` | ||
[ -f package.json ] && cat package.json | grep -q "\\"$hookname\\"[[:space:]]*:" | ||
if [[ $? -eq 0 ]]; then | ||
./node_modules/.bin/run-node ./node_modules/husky/lib/run $hookname | ||
exit $? | ||
fi | ||
./node_modules/husky/scripts/run.sh | ||
`; | ||
exports.default = hookScript; |
@@ -7,2 +7,3 @@ "use strict"; | ||
const pkgDir = require("pkg-dir"); | ||
const read_pkg_1 = require("read-pkg"); | ||
const hookScript_1 = require("./hookScript"); | ||
@@ -78,5 +79,2 @@ const is_1 = require("./is"); | ||
} | ||
class HuskyError extends Error { | ||
} | ||
exports.HuskyError = HuskyError; | ||
function getHooks(userDir) { | ||
@@ -86,76 +84,41 @@ const gitHooksDir = path.join(userDir, '.git/hooks'); | ||
} | ||
function getGitDir(huskyModuleDir) { | ||
const nodeModulesDir = path.join(huskyModuleDir, '..'); | ||
// Search user's package.json starting from node_modules directory | ||
// Should support pnpm directory hierarchy | ||
const userPackageDir = pkgDir.sync(nodeModulesDir); | ||
return path.join(userPackageDir, '.git'); | ||
function getConf(huskyDir) { | ||
const pkg = read_pkg_1.default.sync(huskyDir); | ||
const defaults = { | ||
skipCI: true | ||
}; | ||
return Object.assign({ defaults }, pkg.husky); | ||
} | ||
function getUserDir(rootDir) { | ||
return pkgDir.sync(path.join(rootDir, '..')); | ||
} | ||
function checkCI() { | ||
if (is_ci_1.default) { | ||
throw new HuskyError('CI detected, skipping Git hooks installation"'); | ||
function install(gitDir, huskyDir) { | ||
console.log('husky > setting up git hooks'); | ||
const userDir = pkgDir.sync(path.join(huskyDir, '..')); | ||
const conf = getConf(userDir); | ||
if (is_ci_1.default && conf.skipCI) { | ||
console.log('CI detected, skipping Git hooks installation"'); | ||
return; | ||
} | ||
} | ||
function checkUserDir(userDir) { | ||
if (!fs.existsSync(userDir)) { | ||
throw new HuskyError("Error: Can't find package.json"); | ||
if (userDir === null) { | ||
console.log("Can't find package.json, skipping Git hooks installation"); | ||
return; | ||
} | ||
} | ||
function checkGitDir(userDir) { | ||
if (!fs.existsSync(path.join(userDir, '.git/hooks'))) { | ||
throw new HuskyError(`Error: Can't find .git directory in ${userDir}`); | ||
if (userDir !== gitDir) { | ||
console.log(`Expecting package.json to be at the same level than .git, skipping Git hooks installation`); | ||
return; | ||
} | ||
} | ||
function checkGitHooksDir(userDir) { | ||
if (!fs.existsSync(path.join(userDir, '.git/hooks'))) { | ||
throw new HuskyError(`Error: Can't find .git/hooks directory in ${userDir}`); | ||
} | ||
} | ||
function install(rootDir) { | ||
try { | ||
console.log('husky > setting up git hooks'); | ||
const userDir = getUserDir(rootDir); | ||
// Checks | ||
checkCI(); | ||
checkUserDir(userDir); | ||
checkGitDir(userDir); | ||
checkGitHooksDir(userDir); | ||
// Create hooks | ||
const hooks = getHooks(userDir); | ||
createHooks(hooks); | ||
} | ||
catch (e) { | ||
if (e instanceof HuskyError) { | ||
console.log(e.message); | ||
} | ||
else { | ||
throw e; | ||
} | ||
} | ||
// Create hooks | ||
const hooks = getHooks(gitDir); | ||
createHooks(hooks); | ||
console.log(`husky > done`); | ||
} | ||
exports.install = install; | ||
function uninstall(rootDir) { | ||
try { | ||
console.log('husky > uninstalling git hooks'); | ||
const userDir = getUserDir(rootDir); | ||
// Checks | ||
checkUserDir(userDir); | ||
checkGitDir(userDir); | ||
checkGitHooksDir(userDir); | ||
function uninstall(gitDir, huskyDir) { | ||
console.log('husky > uninstalling git hooks'); | ||
const userDir = pkgDir.sync(path.join(huskyDir, '..')); | ||
if (userDir === gitDir) { | ||
// Remove hooks | ||
const hooks = getHooks(userDir); | ||
const hooks = getHooks(gitDir); | ||
removeHooks(hooks); | ||
} | ||
catch (e) { | ||
// Ignore husky errors | ||
if (!(e instanceof HuskyError)) { | ||
console.log(e); | ||
} | ||
} | ||
console.log('husky > done'); | ||
} | ||
exports.uninstall = uninstall; |
@@ -5,4 +5,3 @@ "use strict"; | ||
const readPkg = require("read-pkg"); | ||
function run(argv, { cwd = process.cwd() } = {}) { | ||
const hookName = argv[2] || ''; | ||
function run([, , hookName = ''], { cwd = process.cwd() } = {}) { | ||
const pkg = readPkg.sync(cwd); | ||
@@ -9,0 +8,0 @@ const command = pkg && pkg.husky && pkg.husky.hooks && pkg.husky.hooks[hookName]; |
{ | ||
"name": "husky", | ||
"version": "0.15.0-beta.3", | ||
"version": "0.15.0-beta.4", | ||
"description": "Prevents bad commit or push (git hooks, pre-commit/precommit, pre-push/prepush, post-merge/postmerge and all that stuff...)", | ||
@@ -5,0 +5,0 @@ "scripts": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13
16392
192