Skip to content

Commit 7ae0943

Browse files
author
Konstantin Epishev
committed
refactor: make custom config flow much more flexible
1 parent 87d3f67 commit 7ae0943

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

cli.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
* A CLI tool to change the git hooks to commands from config
66
*/
77
const {setHooksFromConfig} = require('./simple-git-hooks')
8-
const [configPath] = process.argv.slice(2)
98

109
try {
11-
setHooksFromConfig(process.cwd(), configPath)
10+
setHooksFromConfig(process.cwd(), process.argv)
1211
console.log('[INFO] Successfully set all git hooks')
1312
} catch (e) {
1413
console.log('[ERROR], Was not able to set git hooks. Error: ' + e)

simple-git-hooks.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,11 @@ function checkSimpleGitHooksInDependencies(projectRootPath) {
131131
/**
132132
* Parses the config and sets git hooks
133133
* @param {string} projectRootPath
134-
* @param {string} [configFileName]
134+
* @param {string[]} [argv]
135135
*/
136-
function setHooksFromConfig(projectRootPath=process.cwd(), configFileName='') {
137-
const config = _getConfig(projectRootPath, configFileName)
136+
function setHooksFromConfig(projectRootPath=process.cwd(), argv=process.argv) {
137+
const customConfigPath = _getCustomConfigPath(argv)
138+
const config = _getConfig(projectRootPath, customConfigPath)
138139

139140
if (!config) {
140141
throw('[ERROR] Config was not found! Please add `.simple-git-hooks.js` or `simple-git-hooks.js` or `.simple-git-hooks.json` or `simple-git-hooks.json` or `simple-git-hooks` entry in package.json.\r\nCheck README for details')
@@ -223,6 +224,20 @@ function _getPackageJson(projectPath = process.cwd()) {
223224
return { packageJsonContent: JSON.parse(packageJsonDataRaw), packageJsonPath: targetPackageJson }
224225
}
225226

227+
/**
228+
* Takes the first argument from current process argv and returns it
229+
* Returns empty string when argument wasn't passed
230+
* @param {string[]} [argv]
231+
* @returns {string}
232+
*/
233+
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] || ''
239+
}
240+
226241
/**
227242
* Gets user-set command either from sources
228243
* First try to get command from .simple-pre-commit.json

simple-git-hooks.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ test('creates git hooks and removes unused but preserves specific git hooks', ()
265265
test('creates git hooks and removes unused but preserves specific git hooks', () => {
266266
createGitHooksFolder(projectWithCustomConfigurationFilePath)
267267

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

0 commit comments

Comments
 (0)