Skip to content

Commit

Permalink
refactor: make custom config flow much more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Epishev committed Oct 26, 2021
1 parent 87d3f67 commit 7ae0943
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
3 changes: 1 addition & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
* A CLI tool to change the git hooks to commands from config
*/
const {setHooksFromConfig} = require('./simple-git-hooks')
const [configPath] = process.argv.slice(2)

try {
setHooksFromConfig(process.cwd(), configPath)
setHooksFromConfig(process.cwd(), process.argv)
console.log('[INFO] Successfully set all git hooks')
} catch (e) {
console.log('[ERROR], Was not able to set git hooks. Error: ' + e)
Expand Down
21 changes: 18 additions & 3 deletions simple-git-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,11 @@ function checkSimpleGitHooksInDependencies(projectRootPath) {
/**
* Parses the config and sets git hooks
* @param {string} projectRootPath
* @param {string} [configFileName]
* @param {string[]} [argv]
*/
function setHooksFromConfig(projectRootPath=process.cwd(), configFileName='') {
const config = _getConfig(projectRootPath, configFileName)
function setHooksFromConfig(projectRootPath=process.cwd(), argv=process.argv) {
const customConfigPath = _getCustomConfigPath(argv)
const config = _getConfig(projectRootPath, customConfigPath)

if (!config) {
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')
Expand Down Expand Up @@ -223,6 +224,20 @@ function _getPackageJson(projectPath = process.cwd()) {
return { packageJsonContent: JSON.parse(packageJsonDataRaw), packageJsonPath: targetPackageJson }
}

/**
* Takes the first argument from current process argv and returns it
* Returns empty string when argument wasn't passed
* @param {string[]} [argv]
* @returns {string}
*/
function _getCustomConfigPath(argv=[]) {
const cmdIdx = argv.findIndex(val => val === 'simple-git-hooks')

if (cmdIdx === -1) return ''

return argv[cmdIdx + 1] || ''
}

/**
* Gets user-set command either from sources
* First try to get command from .simple-pre-commit.json
Expand Down
2 changes: 1 addition & 1 deletion simple-git-hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ test('creates git hooks and removes unused but preserves specific git hooks', ()
test('creates git hooks and removes unused but preserves specific git hooks', () => {
createGitHooksFolder(projectWithCustomConfigurationFilePath)

spc.setHooksFromConfig(projectWithCustomConfigurationFilePath, 'git-hooks.js')
spc.setHooksFromConfig(projectWithCustomConfigurationFilePath, ['npx', 'simple-git-hooks', './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`}))

Expand Down

0 comments on commit 7ae0943

Please sign in to comment.