diff --git a/simple-git-hooks.js b/simple-git-hooks.js index 0dbdf8d..03a5723 100644 --- a/simple-git-hooks.js +++ b/simple-git-hooks.js @@ -231,11 +231,10 @@ function _getPackageJson(projectPath = process.cwd()) { * @returns {string} */ function _getCustomConfigPath(argv=[]) { - const cmdIdx = argv.findIndex(val => val === 'simple-git-hooks') - - if (cmdIdx === -1) return '' - - return argv[cmdIdx + 1] || '' + // We'll run as one of the following: + // npx simple-git-hooks ./config.js + // node path/to/simple-git-hooks/cli.js ./config.js + return argv[2] || '' } /** @@ -311,7 +310,9 @@ function _getConfigFromFile(projectRootPath, fileName) { } try { - const filePath = path.normalize(projectRootPath + '/' + fileName) + const filePath = path.isAbsolute(fileName) + ? fileName + : path.normalize(projectRootPath + '/' + fileName) if (filePath === __filename) { return undefined } diff --git a/simple-git-hooks.test.js b/simple-git-hooks.test.js index 88f044e..137891b 100644 --- a/simple-git-hooks.test.js +++ b/simple-git-hooks.test.js @@ -262,10 +262,14 @@ test('creates git hooks and removes unused but preserves specific git hooks', () removeGitHooksFolder(projectWithUnusedConfigurationInPackageJsonPath) }) -test('creates git hooks and removes unused but preserves specific git hooks', () => { +test.each([ + ['npx', 'simple-git-hooks', './git-hooks.js'], + ['node', require.resolve(`./cli`), './git-hooks.js'], + ['node', require.resolve(`./cli`), require.resolve(`${projectWithCustomConfigurationFilePath}/git-hooks.js`)], +])('creates git hooks and removes unused but preserves specific git hooks for command: %s %s %s', (...args) => { createGitHooksFolder(projectWithCustomConfigurationFilePath) - spc.setHooksFromConfig(projectWithCustomConfigurationFilePath, ['npx', 'simple-git-hooks', './git-hooks.js']) + spc.setHooksFromConfig(projectWithCustomConfigurationFilePath, args) const installedHooks = getInstalledGitHooks(path.normalize(path.join(projectWithCustomConfigurationFilePath, '.git', 'hooks'))) expect(JSON.stringify(installedHooks)).toBe(JSON.stringify({'pre-commit':`#!/bin/sh\nexit 1`, 'pre-push':`#!/bin/sh\nexit 1`}))