2024-01-25 09:49:33 +08:00
import c from 'child_process'
2024-07-18 20:25:45 +08:00
import f , { readdir , writeFileSync as w } from 'fs'
2024-01-25 09:49:33 +08:00
import p from 'path'
2024-07-18 20:25:45 +08:00
let l = [ 'pre-commit' , 'prepare-commit-msg' , 'commit-msg' , 'post-commit' , 'applypatch-msg' , 'pre-applypatch' , 'post-applypatch' , 'pre-rebase' , 'post-rewrite' , 'post-checkout' , 'post-merge' , 'pre-push' , 'pre-auto-gc' ] ,
2024-07-25 20:53:25 +08:00
msg = ` echo "husky - DEPRECATED \n \n Please remove the following lines from your hook scripts: \n \n #!/usr/bin/env sh \n . \\ " \\ $ (dirname -- \\ " \\ $ 0 \\ ")/_/husky.sh \\ " \n \n They WILL FAIL in v10.0.0 \n " `
2024-01-25 09:49:33 +08:00
export default ( d = '.husky' ) => {
if ( process . env . HUSKY === '0' ) return 'HUSKY=0 skip install'
if ( d . includes ( '..' ) ) return '.. not allowed'
if ( ! f . existsSync ( '.git' ) ) return ` .git can't be found `
let _ = ( x = '' ) => p . join ( d , '_' , x )
2024-01-25 21:32:57 +08:00
let { status : s , stderr : e } = c . spawnSync ( 'git' , [ 'config' , 'core.hooksPath' , ` ${ d } /_ ` ] )
2024-01-25 09:49:33 +08:00
if ( s == null ) return 'git command not found'
if ( s ) return '' + e
2024-07-18 20:25:45 +08:00
f . rmSync ( _ ( 'husky.sh' ) , { force : true } )
2024-01-25 09:49:33 +08:00
f . mkdirSync ( _ ( ) , { recursive : true } )
w ( _ ( '.gitignore' ) , '*' )
2024-02-01 22:39:13 +08:00
f . copyFileSync ( new URL ( 'husky' , import . meta . url ) , _ ( 'h' ) )
2024-06-12 19:20:19 +08:00
l . forEach ( h => w ( _ ( h ) , ` #!/usr/bin/env sh \n . " \$ (dirname " \$ 0")/h" ` , { mode : 0o755 } ) )
2024-07-25 20:53:25 +08:00
w ( _ ( 'husky.sh' ) , msg )
2024-01-25 09:49:33 +08:00
return ''
2024-01-26 07:41:29 +08:00
}