Skip to content

Commit

Permalink
Update hook script and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
typicode committed Jan 11, 2017
1 parent 505bd2e commit 6de2176
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 49 deletions.
8 changes: 3 additions & 5 deletions bin/install.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
// Run when package is installed
var fs = require('fs')
var path = require('path')
var isCI = require('is-ci')
var husky = require('../src/')
var hooks = require('../src/hooks.json')

if (isCI) {
console.log('\033[4;36m%s\033[0m', 'husky')
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('\\033[4;36m%s\\033[0m', 'husky')
console.log('setting up hooks')

var huskyDir = path.join(__dirname, '..')
Expand Down
5 changes: 2 additions & 3 deletions bin/uninstall.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// Run when package is uninstalled
var path = require('path')
var husky = require('../src/')
var hooks = require('../src/hooks.json')

console.log('\033[4;36m%s\033[0m', 'husky')
console.log('\\033[4;36m%s\\033[0m', 'husky')
console.log('uninstalling')

var huskyDir = path.join(__dirname, '..')
husky.uninstallFrom(huskyDir)
husky.uninstallFrom(huskyDir)
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"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",
"uninstall": "node ./bin/uninstall.js"
},
Expand Down Expand Up @@ -36,11 +37,17 @@
"expect": "^1.20.2",
"mocha": "^3.2.0",
"mock-fs": "^3.12.1",
"rimraf": "^2.2.8"
"rimraf": "^2.2.8",
"standard": "^8.6.0"
},
"dependencies": {
"find-parent-dir": "^0.3.0",
"is-ci": "^1.0.9",
"normalize-path": "^1.0.0"
},
"standard": {
"env": {
"mocha": true
}
}
}
73 changes: 40 additions & 33 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
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))
}

function isHusky (filename) {
var data = fs.readFileSync(filename, 'utf-8')
return data.indexOf('# husky') !== -1
return data.indexOf('#husky') !== -1
}

function findHooksDir (dirname) {
Expand Down Expand Up @@ -45,17 +44,32 @@ function getHookScript (hookName, relativePath, cmd) {
// Hook script
var arr = [
'#!/bin/sh',
'# husky ' + pkg.version,
'#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*:\'',
'}',
''
]

arr = arr.concat([
'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',
''
])

Expand All @@ -77,28 +91,13 @@ function getHookScript (hookName, relativePath, cmd) {

if (process.platform === 'darwin') {
arr = arr.concat([
'if ! [ command -v npm >/dev/null 2>&1 ];',
'then',
' BREW_NVM_DIR="/usr/local/opt/nvm"',
' [ -s "$BREW_NVM_DIR/nvm.sh" ] && . "$BREW_NVM_DIR/nvm.sh"',
' command -v nvm >/dev/null 2>&1 && [ -f .nvmrc ] && nvm use',
'fi',
'exists npm || load_nvm BREW_NVM_DIR /usr/local/opt/nvm',
''
])
}

arr = arr.concat([
// Test if npm is not already in path
// If npm isn't in path, try to load it using nvm
'if ! [ command -v npm >/dev/null 2>&1 ];',
'then',
// 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',
''
])
} else {
Expand All @@ -112,21 +111,29 @@ function getHookScript (hookName, relativePath, cmd) {
}

// 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',
' echo',
' echo "husky - ' + hookName + ' hook failed (add --no-verify to bypass)"',
'npm run -s ' + cmd + ' || {',
' echo',
' echo "husky > ' + hookName + ' hook failed (add --no-verify to bypass)"',
' echo "husky > To debug, use \'npm run precommit\'',
' exit 1',
'fi',
'}',
''
])

Expand Down Expand Up @@ -177,7 +184,7 @@ function installFrom (huskyDir) {

if (hooksDir) {
hooks.forEach(function (hookName) {
npmScriptName = hookName.replace(/-/g, '')
var npmScriptName = hookName.replace(/-/g, '')
createHook(huskyDir, hooksDir, hookName, npmScriptName)
})
console.log('done\n')
Expand Down
11 changes: 5 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ var husky = require('../src')

var gitDir = '/.git'

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))
}

describe('husky', function () {
afterEach(function() {
afterEach(function () {
mock.restore()
})

Expand All @@ -24,11 +24,11 @@ describe('husky', function () {
'/.git/hooks': {},
'/node_modules/husky': {}
})

husky.installFrom('/node_modules/husky')
var hook = readHook('hooks/pre-commit')

expect(hook).toInclude('# husky')
expect(hook).toInclude('#husky')
expect(hook).toInclude('cd .')
expect(hook).toInclude('npm run precommit')

Expand Down Expand Up @@ -83,7 +83,6 @@ describe('husky', function () {
expect(exists('hooks/pre-push')).toBeFalsy()
})


it('should not modify user hooks', function () {
mock({
'/.git/hooks': {},
Expand Down

0 comments on commit 6de2176

Please sign in to comment.