husky
Advanced tools
Comparing version 0.12.0 to 0.13.0-0
// Run when package is installed | ||
var fs = require('fs') | ||
var path = require('path') | ||
var chalk = require('chalk') | ||
var isCI = require('is-ci') | ||
var husky = require('../src/') | ||
var hooks = require('../src/hooks.json') | ||
console.log(chalk.cyan.underline('husky')) | ||
if (isCI) { | ||
console.log('\033[4;36m%s\033[0m', 'husky') | ||
console.log('CI detected, skipping Git hooks installation') | ||
return | ||
process.exit(0) | ||
} | ||
console.log('\033[4;36m%s\033[0m', 'husky') | ||
console.log('setting up hooks') | ||
@@ -16,0 +15,0 @@ |
// Run when package is uninstalled | ||
var path = require('path') | ||
var chalk = require('chalk') | ||
var husky = require('../src/') | ||
var hooks = require('../src/hooks.json') | ||
console.log('\033[4;36m%s\033[0m', 'husky') | ||
console.log(chalk.cyan.underline('husky')) | ||
console.log('uninstalling') | ||
var huskyDir = path.join(__dirname, '..') | ||
husky.uninstallFrom(huskyDir) | ||
husky.uninstallFrom(huskyDir) |
{ | ||
"name": "husky", | ||
"version": "0.12.0", | ||
"version": "0.13.0-0", | ||
"description": "Prevents bad commit or push (git hooks, pre-commit/precommit, pre-push/prepush, post-merge/postmerge and all that stuff...)", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha", | ||
"test": "mocha && standard", | ||
"precommit": "npm test", | ||
"install": "node ./bin/install.js", | ||
@@ -39,9 +40,16 @@ "uninstall": "node ./bin/uninstall.js" | ||
"mock-fs": "^3.12.1", | ||
"rimraf": "^2.2.8" | ||
"rimraf": "^2.2.8", | ||
"standard": "^8.6.0" | ||
}, | ||
"dependencies": { | ||
"chalk": "^1.1.3", | ||
"find-parent-dir": "^0.3.0", | ||
"is-ci": "^1.0.9", | ||
"normalize-path": "^1.0.0" | ||
}, | ||
"standard": { | ||
"env": { | ||
"mocha": true | ||
} | ||
} | ||
} |
var fs = require('fs') | ||
var path = require('path') | ||
var exec = require('child_process').exec | ||
var normalize = require('normalize-path') | ||
var findParentDir = require('find-parent-dir') | ||
var hooks = require('./hooks.json') | ||
var pkg = require('../package.json') | ||
function write (filename, data) { | ||
fs.writeFileSync(filename, data) | ||
fs.chmodSync(filename, 0755) | ||
fs.chmodSync(filename, parseInt('0755', 8)) | ||
} | ||
@@ -16,3 +15,3 @@ | ||
var data = fs.readFileSync(filename, 'utf-8') | ||
return data.indexOf('# husky') !== -1 | ||
return data.indexOf('#husky') !== -1 | ||
} | ||
@@ -49,3 +48,20 @@ | ||
'#!/bin/sh', | ||
'# husky' | ||
'#husky ' + pkg.version, | ||
'', | ||
'command_exists () {', | ||
' command -v "$1" >/dev/null 2>&1', | ||
'}', | ||
'', | ||
'load_nvm () {', | ||
' export $1=$2', | ||
' [ -s "$1/nvm.sh" ] && . $1/nvm.sh', | ||
' exists nvm && [ -f .nvmrc ] && nvm use', | ||
'}', | ||
'', | ||
'has_hook_script () {', | ||
' [ -f package.json ] && cat package.json | grep -q \'"$1"\\s*:\'', | ||
'}', | ||
'' | ||
] | ||
@@ -55,8 +71,7 @@ | ||
'cd ' + normalizedPath, | ||
'', | ||
// Fix for issue #16 #24 | ||
// Test if script is defined in package.json | ||
'[ -f package.json ] && cat package.json | grep -q \'"' + cmd + '"\\s*:\'', | ||
// package.json or script can't be found exit | ||
'[ $? -ne 0 ] && exit 0', | ||
// If script is not defined in package.json then exit | ||
'has_hook_script ' + cmd + ' || exit 0', | ||
'' | ||
]) | ||
@@ -70,9 +85,2 @@ | ||
if (process.platform === 'darwin') { | ||
// If nvm was installed using homebrew, | ||
// nvm script will be found in /usr/local/opt/nvm | ||
arr = arr.concat([ | ||
'BREW_NVM_DIR="/usr/local/opt/nvm"', | ||
'[ -s "$BREW_NVM_DIR/nvm.sh" ] && . "$BREW_NVM_DIR/nvm.sh"' | ||
]) | ||
// Add | ||
@@ -88,14 +96,12 @@ // Brew standard installation path /use/local/bin | ||
if (process.platform === 'darwin') { | ||
arr = arr.concat([ | ||
'exists npm || load_nvm BREW_NVM_DIR /usr/local/opt/nvm', | ||
'' | ||
]) | ||
} | ||
arr = arr.concat([ | ||
// Test if npm is not already in path | ||
'if ! [ command -v npm >/dev/null 2>&1 ];', | ||
'then', | ||
// If npm isn't in path, try to load it using nvm | ||
// If nvm was installed using install script, nvm script will be found in $NVM_DIR | ||
' export NVM_DIR="' + home + '/.nvm"', | ||
// This will load default Node version or version specified by .nvmrc | ||
' [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"', | ||
// Test if nvm is in PATH and load version specified by .nvmrc | ||
' command -v nvm >/dev/null 2>&1 && [ -f .nvmrc ] && nvm use', | ||
'fi' | ||
'exists npm || load_nvm NVM_DIR ' + home + '/.nvm', | ||
'' | ||
]) | ||
@@ -113,17 +119,25 @@ } else { | ||
// Can't find npm message | ||
var npmNotFound = 'husky - can\'t find npm in PATH. Skipping ' + cmd + ' script in package.json' | ||
var npmNotFound = '> husky - Can\'t find npm in PATH. Skipping ' + cmd + ' script in package.json' | ||
arr = arr.concat([ | ||
// Test if npm is in PATH | ||
'command -v npm >/dev/null 2>&1 || { echo >&2 "' + npmNotFound + '"; exit 0; }', | ||
'exists npm || {', | ||
' echo >&2 "' + npmNotFound + '"', | ||
' exit 0', | ||
'}', | ||
'', | ||
// Run script | ||
'echo', | ||
'echo "husky > npm run -s ' + cmd, | ||
'echo', | ||
'', | ||
'export GIT_PARAMS="$*"', | ||
'npm run ' + cmd, | ||
'if [ $? -ne 0 ]; then', | ||
'npm run -s ' + cmd + ' || {', | ||
' echo', | ||
' echo "husky - ' + hookName + ' hook failed (add --no-verify to bypass)"', | ||
' echo', | ||
' echo "> husky - ' + hookName + ' hook failed (add --no-verify to bypass)"', | ||
' echo "> husky - To debug, use \'npm run precommit\'', | ||
' exit 1', | ||
'fi', | ||
'}', | ||
'' | ||
@@ -175,2 +189,10 @@ ]) | ||
try { | ||
var isInSubNodeModule = (huskyDir.match(/node_modules/g) || []).length > 1 | ||
if (isInSubNodeModule) { | ||
return console.log( | ||
'Trying to install from sub \'node_module\' directory,', | ||
'skipping Git hooks installation' | ||
) | ||
} | ||
var hooksDir = findHooksDir(huskyDir) | ||
@@ -180,3 +202,3 @@ | ||
hooks.forEach(function (hookName) { | ||
npmScriptName = hookName.replace(/-/g, '') | ||
var npmScriptName = hookName.replace(/-/g, '') | ||
createHook(huskyDir, hooksDir, hookName, npmScriptName) | ||
@@ -209,2 +231,2 @@ }) | ||
uninstallFrom: uninstallFrom | ||
} | ||
} |
@@ -9,7 +9,7 @@ var fs = require('fs') | ||
function readHook(hookPath) { | ||
function readHook (hookPath) { | ||
return fs.readFileSync(path.join(gitDir, hookPath), 'utf-8') | ||
} | ||
function exists(hookPath) { | ||
function exists (hookPath) { | ||
return fs.existsSync(path.join(gitDir, hookPath)) | ||
@@ -19,3 +19,3 @@ } | ||
describe('husky', function () { | ||
afterEach(function() { | ||
afterEach(function () { | ||
mock.restore() | ||
@@ -29,7 +29,7 @@ }) | ||
}) | ||
husky.installFrom('/node_modules/husky') | ||
var hook = readHook('hooks/pre-commit') | ||
expect(hook).toInclude('# husky') | ||
expect(hook).toInclude('#husky') | ||
expect(hook).toInclude('cd .') | ||
@@ -89,3 +89,2 @@ expect(hook).toInclude('npm run precommit') | ||
it('should not modify user hooks', function () { | ||
@@ -107,2 +106,12 @@ mock({ | ||
it('should not install from /node_modules/A/node_modules', function () { | ||
mock({ | ||
'/.git/hooks': {}, | ||
'/node_modules/A/node_modules/husky': {} | ||
}) | ||
husky.installFrom('/node_modules/A/node_modules/husky') | ||
expect(exists('hooks/pre-push')).toBeFalsy() | ||
}) | ||
it('should not crash if there\'s no .git directory', function () { | ||
@@ -109,0 +118,0 @@ mock({ |
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
15468
12
323
3
0
4
5
+ Addedchalk@^1.1.3
+ Addedansi-regex@2.1.1(transitive)
+ Addedansi-styles@2.2.1(transitive)
+ Addedchalk@1.1.3(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedhas-ansi@2.0.0(transitive)
+ Addedstrip-ansi@3.0.1(transitive)
+ Addedsupports-color@2.0.0(transitive)