99 * To enable this hook, rename this file to "pre-commit".
1010 *
1111 */
12- import shell from 'shelljs'
12+ import shell , { ShellString } from 'shelljs'
1313import path from 'path'
1414
1515const NO_HOOK_VAR = 'NO_HOOK'
1616const INNER_PRE_HOOK = 'CHATIE_INNER_PRE_HOOK'
1717
18+ if ( process . env [ NO_HOOK_VAR ] ) {
19+ // user set NO_HOOK=1 to prevent this hook works
20+ process . exit ( 0 )
21+ }
22+
23+ if ( process . env [ INNER_PRE_HOOK ] ) {
24+ // http://stackoverflow.com/a/21334985/1123955
25+ process . exit ( 0 )
26+ }
27+
1828const argv = process . argv . slice ( 2 )
1929const remoteName = argv [ 0 ] || ''
2030// const remoteUrl = argv[1] || ''
@@ -41,40 +51,37 @@ for (let i = 2; i + 4 <= argv.length;) {
4151 refs . push ( ref )
4252}
4353
44- if ( process . env [ NO_HOOK_VAR ] ) {
45- // user set NO_HOOK=1 to prevent this hook works
46- process . exit ( 0 )
47- }
48-
49- if ( process . env [ INNER_PRE_HOOK ] ) {
50- // http://stackoverflow.com/a/21334985/1123955
51- process . exit ( 0 )
52- }
53-
5454if ( refs ?. [ 0 ] ?. localCommit . match ( / ^ 0 + $ / ) ) {
5555 // delete remote branch
5656 process . exit ( 0 )
5757}
5858
59+ console . info ( '[Step-1]' , 'Checking last commit...' , '\n' )
5960const pkgFile = path . join ( process . cwd ( ) , 'package.json' )
6061const packageVersion = require ( pkgFile ) . version
61- const lastCommitMsg = shell . exec ( 'git log --pretty=format:"%s" HEAD^0 -1' , { silent : true } ) . stdout
62+ const lastCommitMsg = checkReturn ( shell . exec ( 'git log --pretty=format:"%s" HEAD^0 -1' , { silent : true } ) ) . stdout
6263
6364if ( packageVersion === lastCommitMsg ) {
65+ console . info ( '[Step-1]' , 'No need to bump the package version.' , '\n' )
6466 process . exit ( 0 )
6567}
6668
67- shell . exec ( 'npm run lint' ) . code === 0 || process . exit ( 1 )
68- shell . rm ( '-f' , 'package-lock.json' )
69- shell . exec ( 'npm version patch --no-package-lock' ) . code === 0 || process . exit ( 1 )
69+ console . info ( '[Step-2]' , 'Checking lint...' , '\n' )
70+ checkReturn ( shell . exec ( 'npm run lint' , { silent : true } ) )
71+
72+ console . info ( '[Step-3]' , 'Bump the package version...' , '\n' )
73+ checkReturn ( shell . rm ( '-f' , 'package-lock.json' ) )
74+ checkReturn ( shell . exec ( 'npm version patch --no-package-lock' , { silent : true } ) )
7075process . env [ INNER_PRE_HOOK ] = '1'
7176
72- const version = shell . exec ( 'git log --pretty=format:"%s" HEAD^0 -1' , { silent : true } ) . stdout
73- shell . exec ( `git tag -d v${ version } ` ) . code === 0 || process . exit ( 1 )
77+ console . info ( '[Step-4]' , 'Remove git tag...' , '\n' )
78+ const version = checkReturn ( shell . exec ( 'git log --pretty=format:"%s" HEAD^0 -1' , { silent : true } ) ) . stdout
79+ checkReturn ( shell . exec ( `git tag -d v${ version } ` , { silent : true } ) )
7480
81+ console . info ( '[Step-5]' , 'Push...' , '\n' )
7582const refMaps = refs . map ( ref => ref . remoteBranch ? ref . localBranch + ':' + ref . remoteBranch : '' )
7683const cmd = [ 'git push' , remoteName , ...refMaps ] . join ( ' ' )
77- shell . exec ( cmd ) . code === 0 || process . exit ( 1 )
84+ checkReturn ( shell . exec ( cmd , { silent : true } ) )
7885
7986console . info ( String . raw `
8087____ _ _ ____ _
@@ -101,4 +108,13 @@ console.info(`
101108
102109` )
103110
104- process . exit ( 1 )
111+ process . exit ( 42 )
112+
113+ function checkReturn ( ret : ShellString ) {
114+ if ( ret . code !== 0 ) {
115+ const line = '------------------------------------------'
116+ console . error ( `Error:\n${ line } \n\n${ ret . stderr } \n\n${ line } \n` )
117+ process . exit ( 1 )
118+ }
119+ return ret
120+ }
0 commit comments