Skip to content

Commit a6f04b7

Browse files
authored
Merge pull request #72 from TomerAberbach/master
Fix some minor issues in recognition and loading of custom configs
2 parents 99afedc + 4b3b62b commit a6f04b7

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

simple-git-hooks.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,10 @@ function _getPackageJson(projectPath = process.cwd()) {
231231
* @returns {string}
232232
*/
233233
function _getCustomConfigPath(argv=[]) {
234-
const cmdIdx = argv.findIndex(val => val === 'simple-git-hooks')
235-
236-
if (cmdIdx === -1) return ''
237-
238-
return argv[cmdIdx + 1] || ''
234+
// We'll run as one of the following:
235+
// npx simple-git-hooks ./config.js
236+
// node path/to/simple-git-hooks/cli.js ./config.js
237+
return argv[2] || ''
239238
}
240239

241240
/**
@@ -311,7 +310,9 @@ function _getConfigFromFile(projectRootPath, fileName) {
311310
}
312311

313312
try {
314-
const filePath = path.normalize(projectRootPath + '/' + fileName)
313+
const filePath = path.isAbsolute(fileName)
314+
? fileName
315+
: path.normalize(projectRootPath + '/' + fileName)
315316
if (filePath === __filename) {
316317
return undefined
317318
}

simple-git-hooks.test.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,14 @@ test('creates git hooks and removes unused but preserves specific git hooks', ()
262262
removeGitHooksFolder(projectWithUnusedConfigurationInPackageJsonPath)
263263
})
264264

265-
test('creates git hooks and removes unused but preserves specific git hooks', () => {
265+
test.each([
266+
['npx', 'simple-git-hooks', './git-hooks.js'],
267+
['node', require.resolve(`./cli`), './git-hooks.js'],
268+
['node', require.resolve(`./cli`), require.resolve(`${projectWithCustomConfigurationFilePath}/git-hooks.js`)],
269+
])('creates git hooks and removes unused but preserves specific git hooks for command: %s %s %s', (...args) => {
266270
createGitHooksFolder(projectWithCustomConfigurationFilePath)
267271

268-
spc.setHooksFromConfig(projectWithCustomConfigurationFilePath, ['npx', 'simple-git-hooks', './git-hooks.js'])
272+
spc.setHooksFromConfig(projectWithCustomConfigurationFilePath, args)
269273
const installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithCustomConfigurationFilePath, '.git', 'hooks')))
270274
expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`#!/bin/sh\nexit 1`, 'pre-push':`#!/bin/sh\nexit 1`}))
271275

0 commit comments

Comments
 (0)