@@ -2,7 +2,7 @@ import fs from 'node:fs'
2
2
import path from 'node:path'
3
3
import process from 'node:process'
4
4
import { config } from './config'
5
-
5
+ import type { SetHooksFromConfigOptions } from './types'
6
6
export const VALID_GIT_HOOKS = [
7
7
'applypatch-msg' ,
8
8
'pre-applypatch' ,
@@ -190,25 +190,26 @@ function _getPackageJson(projectPath = process.cwd()) {
190
190
* Parses the config and sets git hooks
191
191
* @param {string } projectRootPath
192
192
*/
193
- export function setHooksFromConfig ( projectRootPath : string = process . cwd ( ) , options ?: { configFile ?: string } = { } ) : void {
193
+ export function setHooksFromConfig ( projectRootPath : string = process . cwd ( ) , options ?: SetHooksFromConfigOptions ) : void {
194
194
if ( ! config || Object . keys ( config ) . length === 0 )
195
195
throw new Error ( '[ERROR] Config was not found! Please add `.git-hooks.config.{ts,js,mjs,cjs,mts,cts,json}` or `git-hooks.config.{ts,js,mjs,cjs,mts,cts,json}` or the `git-hooks` entry in package.json.\r\nCheck README for details' )
196
196
197
+ const configFile = options ?. configFile ? options . configFile : config
197
198
// Only validate hook names that aren't options
198
- const hookKeys = Object . keys ( config ) . filter ( key => key !== 'preserveUnused' && key !== 'verbose' )
199
+ const hookKeys = Object . keys ( configFile ) . filter ( key => key !== 'preserveUnused' && key !== 'verbose' )
199
200
const isValidConfig = hookKeys . every ( key => VALID_GIT_HOOKS . includes ( key as typeof VALID_GIT_HOOKS [ number ] ) )
200
201
201
202
if ( ! isValidConfig )
202
203
throw new Error ( '[ERROR] Config was not in correct format. Please check git hooks or options name' )
203
204
204
- const preserveUnused = Array . isArray ( config . preserveUnused ) ? config . preserveUnused : config . preserveUnused ? VALID_GIT_HOOKS : [ ]
205
+ const preserveUnused = Array . isArray ( configFile . preserveUnused ) ? configFile . preserveUnused : configFile . preserveUnused ? VALID_GIT_HOOKS : [ ]
205
206
206
207
for ( const hook of VALID_GIT_HOOKS ) {
207
- if ( Object . prototype . hasOwnProperty . call ( config , hook ) ) {
208
- if ( ! config [ hook ] )
208
+ if ( Object . prototype . hasOwnProperty . call ( configFile , hook ) ) {
209
+ if ( ! configFile [ hook ] )
209
210
throw new Error ( `[ERROR] Command for ${ hook } is not set` )
210
211
211
- _setHook ( hook , config [ hook ] , projectRootPath )
212
+ _setHook ( hook , configFile [ hook ] , projectRootPath )
212
213
}
213
214
else if ( ! preserveUnused . includes ( hook ) ) {
214
215
_removeHook ( hook , projectRootPath )
0 commit comments