@@ -14,6 +14,7 @@ import { scanForEnvVars, generateEnvExample } from "./scan.js";
1414import { syncEnvFiles } from "./sync.js" ;
1515import { compareMatrix , renderMatrixTable } from "./matrix.js" ;
1616import { watchEnvFiles } from "./watcher.js" ;
17+ import { installPreCommitHook } from "./hooks.js" ;
1718
1819const Colors = {
1920 reset : "\x1b[0m" ,
@@ -294,34 +295,14 @@ function handleMatrix(files: string[], format: OutputFormat, options?: DiffOptio
294295}
295296
296297function handleInstallHook ( ) : number {
297- const gitDir = path . resolve ( ".git" ) ;
298- if ( ! fs . existsSync ( gitDir ) ) {
299- console . error ( Colors . red + "Error: Not a git repository (no .git directory found)" + Colors . reset ) ;
298+ const { success, message } = installPreCommitHook ( ) ;
299+ if ( success ) {
300+ console . log ( Colors . green + message + Colors . reset ) ;
301+ return 0 ;
302+ } else {
303+ console . error ( Colors . red + `Error: ${ message } ` + Colors . reset ) ;
300304 return 1 ;
301305 }
302-
303- const hooksDir = path . join ( gitDir , "hooks" ) ;
304- if ( ! fs . existsSync ( hooksDir ) ) {
305- fs . mkdirSync ( hooksDir , { recursive : true } ) ;
306- }
307-
308- const hookPath = path . join ( hooksDir , "pre-commit" ) ;
309- const hookContent = `#!/bin/sh
310- # env-diff pre-commit hook
311- # Checks that .env files are in sync before committing
312-
313- if [ -f .env.example ] && [ -f .env ]; then
314- npx env-diff .env.example .env --strict
315- if [ $? -ne 0 ]; then
316- echo "env-diff: Environment files are out of sync. Fix errors before committing."
317- exit 1
318- fi
319- fi
320- ` ;
321-
322- fs . writeFileSync ( hookPath , hookContent , { mode : 0o755 } ) ;
323- console . log ( Colors . green + `Pre-commit hook installed at ${ hookPath } ` + Colors . reset ) ;
324- return 0 ;
325306}
326307
327308/**
0 commit comments