From f407cef98eb05499ec645c64c9a55df927ba8247 Mon Sep 17 00:00:00 2001 From: Tomer Aberbach Date: Mon, 14 Mar 2022 20:47:00 -0400 Subject: [PATCH 1/2] fix: recognize custom config path for non-npx usage --- simple-git-hooks.js | 9 ++++----- simple-git-hooks.test.js | 7 +++++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/simple-git-hooks.js b/simple-git-hooks.js index 0dbdf8d..9193ce9 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] || '' } /** diff --git a/simple-git-hooks.test.js b/simple-git-hooks.test.js index 88f044e..f168377 100644 --- a/simple-git-hooks.test.js +++ b/simple-git-hooks.test.js @@ -262,10 +262,13 @@ 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'], + ['node', require.resolve(`./cli`)], +])('creates git hooks and removes unused but preserves specific git hooks for command: %s %s ./git-hooks.js', (command, arg1) => { createGitHooksFolder(projectWithCustomConfigurationFilePath) - spc.setHooksFromConfig(projectWithCustomConfigurationFilePath, ['npx', 'simple-git-hooks', './git-hooks.js']) + spc.setHooksFromConfig(projectWithCustomConfigurationFilePath, [command, arg1, './git-hooks.js']) 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`})) From 4b3b62bd0080665e5e990b43afc1a2c2a9382c7a Mon Sep 17 00:00:00 2001 From: Tomer Aberbach Date: Mon, 14 Mar 2022 20:47:41 -0400 Subject: [PATCH 2/2] fix: custom config via absolute path --- simple-git-hooks.js | 4 +++- simple-git-hooks.test.js | 9 +++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/simple-git-hooks.js b/simple-git-hooks.js index 9193ce9..03a5723 100644 --- a/simple-git-hooks.js +++ b/simple-git-hooks.js @@ -310,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 f168377..137891b 100644 --- a/simple-git-hooks.test.js +++ b/simple-git-hooks.test.js @@ -263,12 +263,13 @@ test('creates git hooks and removes unused but preserves specific git hooks', () }) test.each([ - ['npx', 'simple-git-hooks'], - ['node', require.resolve(`./cli`)], -])('creates git hooks and removes unused but preserves specific git hooks for command: %s %s ./git-hooks.js', (command, arg1) => { + ['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, [command, arg1, './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`}))