Skip to content

Commit f407cef

Browse files
committed
fix: recognize custom config path for non-npx usage
1 parent 99afedc commit f407cef

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

simple-git-hooks.js

Lines changed: 4 additions & 5 deletions
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
/**

simple-git-hooks.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,13 @@ 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'],
267+
['node', require.resolve(`./cli`)],
268+
])('creates git hooks and removes unused but preserves specific git hooks for command: %s %s ./git-hooks.js', (command, arg1) => {
266269
createGitHooksFolder(projectWithCustomConfigurationFilePath)
267270

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

0 commit comments

Comments
 (0)