11import fs from 'node:fs'
22import path from 'node:path'
33import process from 'node:process'
4- import { config } from './config'
4+ import { config as defaultConfig } from './config'
55import type { GitHooksConfig , StagedLintConfig , StagedLintTask , SetHooksFromConfigOptions } from './types'
66import { exec } from 'node:child_process'
77import { promisify } from 'node:util'
88import { Logger , italic , bgRed , green , bgYellow } from '@stacksjs/clarity'
99
1010const execAsync = promisify ( exec )
11- const log = new Logger ( 'git-hooks' , {
12- showTags : true
13- } )
11+ let log : Logger | undefined
12+
13+ function getLog ( ) : Logger {
14+ return log ??= new Logger ( 'git-hooks' , {
15+ showTags : true ,
16+ } )
17+ }
1418
1519// Module-level verbose switch. Default: false
1620let VERBOSE = false
@@ -194,12 +198,12 @@ export function areHooksInstalled(projectRootPath: string = process.cwd()): bool
194198 return false
195199 }
196200
197- if ( ! config || Object . keys ( config ) . length === 0 ) {
201+ if ( ! defaultConfig || Object . keys ( defaultConfig ) . length === 0 ) {
198202 return false
199203 }
200204
201205 // Check if at least one configured hook exists and contains our script marker
202- const configuredHooks = Object . keys ( config ) . filter ( key =>
206+ const configuredHooks = Object . keys ( defaultConfig ) . filter ( key =>
203207 VALID_GIT_HOOKS . includes ( key as typeof VALID_GIT_HOOKS [ number ] )
204208 )
205209
@@ -249,16 +253,15 @@ export function getConfigFromPackageJson(projectPath: string): GitHooksConfig |
249253 * Parses the config and sets git hooks
250254 */
251255export function setHooksFromConfig ( projectRootPath : string = process . cwd ( ) , options ?: SetHooksFromConfigOptions ) : void {
252- // Always use the provided configFile if available, otherwise try package.json in projectRoot, then cached config
253- const configFile = options ?. configFile || getConfigFromPackageJson ( projectRootPath ) || ( config && Object . keys ( config ) . length > 0 ? { ...config } : undefined )
256+ const configFile = options ?. configFile || getConfigFromPackageJson ( projectRootPath ) || ( defaultConfig && Object . keys ( defaultConfig ) . length > 0 ? { ...defaultConfig } : undefined )
254257
255258 if ( ! configFile || Object . keys ( configFile ) . length === 0 )
256259 throw new Error ( '[ERROR] Config was not found! Please add `.git-hooks.config.{ts,js,mjs,cjs,json}` or `git-hooks.config.{ts,js,mjs,cjs,json}` or the `git-hooks` entry in package.json.\r\nCheck README for details' )
257260
258261 // Set module verbosity strictly from options (CLI flag). Ignore config.verbose for logs.
259262 VERBOSE = Boolean ( options ?. verbose )
260263 if ( VERBOSE ) {
261- log . config . level = 'debug'
264+ getLog ( ) . config . level = 'debug'
262265 }
263266
264267 _validateStagedLintConfig ( configFile )
@@ -277,7 +280,7 @@ export function setHooksFromConfig(projectRootPath: string = process.cwd(), opti
277280 // For programmatic usage, fall back to config file setting
278281 const verbose = options ?. verbose !== undefined ? options . verbose : ( configFile . verbose ?? false )
279282 if ( verbose ) {
280- log . debug ( `Hook Keys: ${ logKeys } ` )
283+ getLog ( ) . debug ( `Hook Keys: ${ logKeys } ` )
281284 }
282285 for ( const hook of VALID_GIT_HOOKS ) {
283286 if ( Object . prototype . hasOwnProperty . call ( configFile , hook ) ) {
@@ -347,7 +350,7 @@ else {
347350
348351 const addOrModify = fs . existsSync ( hookPath ) ? 'Modify' : 'Add'
349352 if ( verbose ) {
350- log . debug ( `${ addOrModify } ${ italic ( hook ) } hook` )
353+ getLog ( ) . debug ( `${ addOrModify } ${ italic ( hook ) } hook` )
351354 }
352355
353356 fs . writeFileSync ( hookPath , hookCommand , { mode : 0o755 } )
@@ -369,12 +372,12 @@ function _removeHook(hook: string, projectRoot = process.cwd(), verbose = false)
369372 const hookPath = path . normalize ( `${ gitRoot } /hooks/${ hook } ` )
370373
371374 if ( fs . existsSync ( hookPath ) ) {
372- if ( VERBOSE ) log . debug ( `Hook ${ hook } is not set, removing!` )
375+ if ( VERBOSE ) getLog ( ) . debug ( `Hook ${ hook } is not set, removing!` )
373376 fs . unlinkSync ( hookPath )
374377 }
375378
376379 if ( verbose )
377- log . success ( `Successfully removed the ${ hook } hook` )
380+ getLog ( ) . success ( `Successfully removed the ${ hook } hook` )
378381}
379382
380383/**
0 commit comments