Socket
Socket
Sign inDemoInstall

husky

Package Overview
Dependencies
Maintainers
1
Versions
214
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

husky - npm Package Compare versions

Comparing version 0.12.0-1 to 0.12.0-2

2

package.json
{
"name": "husky",
"version": "0.12.0-1",
"version": "0.12.0-2",
"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 @@ "main": "index.js",

@@ -21,17 +21,20 @@ var fs = require('fs')

var dir = findParentDir.sync(dirname, '.git')
var gitDir = path.join(dir, '.git')
var stats = fs.lstatSync(gitDir)
if (stats.isFile()) {
// Expect following format
// git: pathToGit
gitDir = fs
.readFileSync(gitDir, 'utf-8')
.split(':')[1]
.trim()
if (dir) {
var gitDir = path.join(dir, '.git')
var stats = fs.lstatSync(gitDir)
return path.join(dir, gitDir, 'hooks')
if (stats.isFile()) {
// Expect following format
// git: pathToGit
gitDir = fs
.readFileSync(gitDir, 'utf-8')
.split(':')[1]
.trim()
return path.join(dir, gitDir, 'hooks')
}
return path.join(gitDir, 'hooks')
}
return path.join(gitDir, 'hooks')
}

@@ -133,5 +136,5 @@

// Get project directory based on hooks directory
// For example for /some/project/.git/hooks should be /some/project
var projectDir = findParentDir.sync(hooksDir, '.git')
// Get project directory
// When used in submodule, the project dir is the first .git that is found
var projectDir = findParentDir.sync(huskyDir, '.git')

@@ -169,7 +172,12 @@ // In order to support projects with package.json in a different directory

var hooksDir = findHooksDir(huskyDir)
hooks.forEach(function (hookName) {
npmScriptName = hookName.replace(/-/g, '')
createHook(huskyDir, hooksDir, hookName, npmScriptName)
})
console.log('done\n')
if (hooksDir) {
hooks.forEach(function (hookName) {
npmScriptName = hookName.replace(/-/g, '')
createHook(huskyDir, hooksDir, hookName, npmScriptName)
})
console.log('done\n')
} else {
console.log('Can\'t find .git directory, skipping Git hooks installation')
}
} catch (e) {

@@ -183,2 +191,3 @@ console.error(e)

var hooksDir = findHooksDir(huskyDir)
hooks.forEach(function (hookName) {

@@ -185,0 +194,0 @@ removeHook(hooksDir, hookName)

@@ -7,2 +7,4 @@ var fs = require('fs')

var gitDir = '/.git'
function readHook(hookPath) {

@@ -16,23 +18,3 @@ return fs.readFileSync(path.join(gitDir, hookPath), 'utf-8')

var layout = {}
var gitDir = '/project/.git'
// Set different paths where husky can be installed
var projectDir = '/project/node_modules/husky'
var subProjectDir = '/project/some/path/node_modules/husky'
var subModuleDir = '/project/subproject/node_modules/husky'
layout[gitDir] = {}
layout[path.join(gitDir, 'modules/subproject/hooks')] = {}
layout[projectDir] = {}
layout[subProjectDir] = {}
layout[path.join(subModuleDir, '../..')] = {
'.git': 'git: ../.git/modules/subproject'
}
describe('husky', function () {
beforeEach(function () {
mock(layout)
})
afterEach(function() {

@@ -42,59 +24,94 @@ mock.restore()

describe('install', function () {
it('should support basic layout', function () {
husky.installFrom(projectDir)
var hook = readHook('hooks/pre-commit')
it('should support basic layout', function () {
mock({
'/.git/hooks': {},
'/node_modules/husky': {}
})
husky.installFrom('/node_modules/husky')
var hook = readHook('hooks/pre-commit')
expect(hook).toInclude('# husky')
expect(hook).toInclude('cd .')
expect(hook).toInclude('npm run precommit')
expect(hook).toInclude('# husky')
expect(hook).toInclude('cd .')
expect(hook).toInclude('npm run precommit')
husky.uninstallFrom('/node_modules/husky')
expect(exists('hooks/pre-push')).toBeFalsy()
})
it('should support project installed in sub directory', function () {
mock({
'/.git/hooks': {},
'/A/B/node_modules/husky': {}
})
it('should support project installed in sub directory', function () {
husky.installFrom(subProjectDir)
var hook = readHook('hooks/pre-commit')
husky.installFrom('/A/B/node_modules/husky')
var hook = readHook('hooks/pre-commit')
expect(hook).toInclude('cd some/path')
expect(hook).toInclude('cd A/B')
husky.uninstallFrom('/A/B/node_modules/husky')
expect(exists('hooks/pre-push')).toBeFalsy()
})
it('should support git submodule', function () {
mock({
'/.git/modules/A/B': {},
'/A/B/.git': 'git: ../../.git/modules/A/B',
'/A/B/node_modules/husky': {}
})
it('should support git submodule', function () {
husky.installFrom(subModuleDir)
var hook = readHook('modules/subproject/hooks/pre-commit')
husky.installFrom('/A/B/node_modules/husky')
var hook = readHook('modules/A/B/hooks/pre-commit')
expect(hook).toInclude('cd subproject')
expect(hook).toInclude('cd .')
husky.uninstallFrom('/A/B/node_modules/husky')
expect(exists('hooks/pre-push')).toBeFalsy()
})
it('should support git submodule and sub directory', function () {
mock({
'/.git/modules/A/B': {},
'/A/B/.git': 'git: ../../.git/modules/A/B',
'/A/B/C/node_modules/husky': {}
})
it('should not overwrite user hooks', function () {
// Create a pre-push hook
var hooksDir = path.join(gitDir, 'hooks')
fs.mkdirSync(hooksDir)
fs.writeFileSync(path.join(hooksDir, 'pre-push'), 'foo')
husky.installFrom('/A/B/C/node_modules/husky')
var hook = readHook('modules/A/B/hooks/pre-commit')
// Verify that it's not overwritten
husky.installFrom(projectDir)
var hook = readHook('hooks/pre-push')
expect(hook).toBe('foo')
})
expect(hook).toInclude('cd C')
husky.uninstallFrom('/A/B/app/node_modules/husky')
expect(exists('hooks/pre-push')).toBeFalsy()
})
describe('uninstall', function () {
it('should support basic layout', function () {
husky.uninstallFrom(projectDir)
expect(exists('hooks/pre-push')).toBeFalsy()
it('should not modify user hooks', function () {
mock({
'/.git/hooks': {},
'/.git/hooks/pre-push': 'foo',
'/node_modules/husky': {}
})
it('should support project installed in sub directory', function () {
husky.uninstallFrom(subProjectDir)
expect(exists('hooks/pre-push')).toBeFalsy()
// Verify that it's not overwritten
husky.installFrom('/node_modules/husky')
var hook = readHook('hooks/pre-push')
expect(hook).toBe('foo')
husky.uninstallFrom('/node_modules/husky')
expect(exists('hooks/pre-push')).toBeTruthy()
})
it('should not crash if there\'s no .git directory', function () {
mock({
'/node_modules/husky': {}
})
it('should not remove user hooks', function () {
var hooksDir = path.join(gitDir, 'hooks')
fs.mkdirSync(hooksDir)
fs.writeFileSync(path.join(hooksDir, 'pre-push'), 'foo')
expect(() => husky.installFrom('/node_modules/husky'))
.toNotThrow()
husky.uninstallFrom(projectDir)
expect(exists('hooks/pre-push')).toBeTruthy()
})
expect(() => husky.uninstallFrom('/node_modules/husky'))
.toNotThrow()
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc