@@ -2,7 +2,7 @@ import fs from 'node:fs'
22import path from 'node:path'
33import process from 'node:process'
44import { config } from './config'
5-
5+ import type { SetHooksFromConfigOptions } from './types'
66export const VALID_GIT_HOOKS = [
77 'applypatch-msg' ,
88 'pre-applypatch' ,
@@ -190,25 +190,26 @@ function _getPackageJson(projectPath = process.cwd()) {
190190 * Parses the config and sets git hooks
191191 * @param {string } projectRootPath
192192 */
193- export function setHooksFromConfig ( projectRootPath : string = process . cwd ( ) , options ?: { configFile ?: string } = { } ) : void {
193+ export function setHooksFromConfig ( projectRootPath : string = process . cwd ( ) , options ?: SetHooksFromConfigOptions ) : void {
194194 if ( ! config || Object . keys ( config ) . length === 0 )
195195 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' )
196196
197+ const configFile = options ?. configFile ? options . configFile : config
197198 // 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' )
199200 const isValidConfig = hookKeys . every ( key => VALID_GIT_HOOKS . includes ( key as typeof VALID_GIT_HOOKS [ number ] ) )
200201
201202 if ( ! isValidConfig )
202203 throw new Error ( '[ERROR] Config was not in correct format. Please check git hooks or options name' )
203204
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 : [ ]
205206
206207 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 ] )
209210 throw new Error ( `[ERROR] Command for ${ hook } is not set` )
210211
211- _setHook ( hook , config [ hook ] , projectRootPath )
212+ _setHook ( hook , configFile [ hook ] , projectRootPath )
212213 }
213214 else if ( ! preserveUnused . includes ( hook ) ) {
214215 _removeHook ( hook , projectRootPath )
0 commit comments