Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some minor issues in recognition and loading of custom configs #72

Merged
merged 2 commits into from Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 7 additions & 6 deletions simple-git-hooks.js
Expand Up @@ -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] || ''
}

/**
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 6 additions & 2 deletions simple-git-hooks.test.js
Expand Up @@ -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`}))

Expand Down