@@ -86,10 +86,10 @@ var createLogger = (debug) => (...args) => {
8686 if ( debug ) console . log ( "[cfb]" , ...args ) ;
8787} ;
8888var createInitialState = ( opts = { } ) => {
89- const argv2 = opts . argv ?? process . argv ;
89+ const argv = opts . argv ?? process . argv ;
9090 const env = opts . env ?? process . env ;
9191 const cwd = opts . cwd ?? process . cwd ( ) ;
92- const [ , , commitMsgPath , source ] = argv2 ;
92+ const [ , , commitMsgPath , source ] = argv ;
9393 if ( ! commitMsgPath ) return null ;
9494 const config = loadConfig ( cwd ) ;
9595 const branch = getCurrentBranch ( ) ;
@@ -254,40 +254,90 @@ var import_fs3 = __toESM(require("fs"), 1);
254254var import_path2 = __toESM ( require ( "path" ) , 1 ) ;
255255var HUSKY_FILE = "prepare-commit-msg" ;
256256var HOOK_LINE = 'cfb "$1" "$2" "$3"' ;
257- function initHusky ( cwd = process . cwd ( ) ) {
257+ var createHuskyState = ( cwd ) => {
258258 const huskyDir = import_path2 . default . join ( cwd , ".husky" ) ;
259259 const hookPath = import_path2 . default . join ( huskyDir , HUSKY_FILE ) ;
260- if ( ! import_fs3 . default . existsSync ( huskyDir ) ) {
261- console . log ( "[cfb] .husky not found. Run `npx husky init` first." ) ;
262- return 0 ;
263- }
264- if ( ! import_fs3 . default . existsSync ( hookPath ) ) {
265- const commandLine = HOOK_LINE ;
266- import_fs3 . default . writeFileSync ( hookPath , commandLine , "utf8" ) ;
267- import_fs3 . default . chmodSync ( hookPath , 493 ) ;
268- console . log ( "[cfb] created .husky/prepare-commit-msg and added cfb hook" ) ;
269- return 0 ;
260+ const huskyExists = import_fs3 . default . existsSync ( huskyDir ) ;
261+ const hookExists = import_fs3 . default . existsSync ( hookPath ) ;
262+ let currentContent ;
263+ let hookPresent ;
264+ if ( hookExists ) {
265+ currentContent = import_fs3 . default . readFileSync ( hookPath , "utf8" ) ;
266+ hookPresent = currentContent . includes ( HOOK_LINE ) ;
270267 }
271- const current = import_fs3 . default . readFileSync ( hookPath , "utf8" ) ;
272- if ( current . includes ( HOOK_LINE ) ) {
273- console . log ( "[cfb] hook already present" ) ;
274- return 0 ;
275- }
276- const updated = current . trimEnd ( ) + `
268+ return {
269+ huskyDir,
270+ hookPath,
271+ huskyExists,
272+ hookExists,
273+ currentContent,
274+ hookPresent
275+ } ;
276+ } ;
277+ var initStrategies = [
278+ ( state ) => ! state . huskyExists ? {
279+ exitCode : 1 ,
280+ message : [
281+ "[cfb] Husky not found. This package requires Husky v9 as a peer dependency." ,
282+ "" ,
283+ " Install and initialize Husky v9:" ,
284+ " npm install --save-dev husky@^9.0.0" ,
285+ " npm exec husky init" ,
286+ "" ,
287+ " Then run: cfb init"
288+ ] . join ( "\n" )
289+ } : null ,
290+ ( state ) => ! state . hookExists ? {
291+ exitCode : 0 ,
292+ message : "[cfb] \u2713 Created .husky/prepare-commit-msg and added cfb hook" ,
293+ action : ( ) => {
294+ import_fs3 . default . writeFileSync ( state . hookPath , HOOK_LINE , "utf8" ) ;
295+ import_fs3 . default . chmodSync ( state . hookPath , 493 ) ;
296+ }
297+ } : null ,
298+ ( state ) => state . hookPresent ? {
299+ exitCode : 0 ,
300+ message : "[cfb] \u2713 Hook already present"
301+ } : null ,
302+ ( state ) => ( {
303+ exitCode : 0 ,
304+ message : "[cfb] \u2713 Appended cfb hook to existing prepare-commit-msg" ,
305+ action : ( ) => {
306+ const updated = state . currentContent . trimEnd ( ) + `
277307
278308${ HOOK_LINE }
279309` ;
280- import_fs3 . default . writeFileSync ( hookPath , updated , "utf8" ) ;
281- console . log ( "[cfb] appended cfb hook to existing prepare-commit-msg" ) ;
282- return 0 ;
310+ import_fs3 . default . writeFileSync ( state . hookPath , updated , "utf8" ) ;
311+ }
312+ } )
313+ ] ;
314+ var applyInitStrategy = ( state ) => {
315+ for ( const strategy of initStrategies ) {
316+ const result = strategy ( state ) ;
317+ if ( result ) return result ;
318+ }
319+ return { exitCode : 1 , message : "[cfb] unexpected error" } ;
320+ } ;
321+ function initHusky ( cwd = process . cwd ( ) ) {
322+ const state = createHuskyState ( cwd ) ;
323+ const result = applyInitStrategy ( state ) ;
324+ result . action ?. ( ) ;
325+ console . log ( result . message ) ;
326+ return result . exitCode ;
283327}
284328
285329// src/cli.ts
286- var argv = process . argv . slice ( 2 ) ;
287- var cmd = argv [ 0 ] ;
288- if ( cmd === "init" ) {
289- process . exit ( initHusky ( process . cwd ( ) ) || 0 ) ;
290- } else {
291- const passthroughArgv = [ process . argv [ 0 ] , process . argv [ 1 ] , ...argv ] ;
292- process . exit ( run ( { argv : passthroughArgv } ) || 0 ) ;
293- }
330+ var createCommandHandlers = ( argv ) => ( {
331+ init : ( ) => initHusky ( process . cwd ( ) ) || 0 ,
332+ default : ( ) => {
333+ const passthroughArgv = [ process . argv [ 0 ] , process . argv [ 1 ] , ...argv ] ;
334+ return run ( { argv : passthroughArgv } ) || 0 ;
335+ }
336+ } ) ;
337+ var executeCommand = ( argv ) => {
338+ const [ cmd ] = argv ;
339+ const handlers = createCommandHandlers ( argv ) ;
340+ const handler = handlers [ cmd ] || handlers . default ;
341+ return handler ( ) ;
342+ } ;
343+ process . exit ( executeCommand ( process . argv . slice ( 2 ) ) ) ;
0 commit comments