Skip to content

Commit b9b0cfb

Browse files
authored
Improve readability of output messages (#25)
* perf: Check env parameters earlier * changge exit code * feat: 🎨 Improve readability of output messages * 0.7.5 * fix: 🐛 add missing options * 0.7.6
1 parent ccdefb7 commit b9b0cfb

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

bin/pre-push.ts

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,22 @@
99
* To enable this hook, rename this file to "pre-commit".
1010
*
1111
*/
12-
import shell from 'shelljs'
12+
import shell, { ShellString } from 'shelljs'
1313
import path from 'path'
1414

1515
const NO_HOOK_VAR = 'NO_HOOK'
1616
const 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+
1828
const argv = process.argv.slice(2)
1929
const 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-
5454
if (refs?.[0]?.localCommit.match(/^0+$/)) {
5555
// delete remote branch
5656
process.exit(0)
5757
}
5858

59+
console.info('[Step-1]', 'Checking last commit...', '\n')
5960
const pkgFile = path.join(process.cwd(), 'package.json')
6061
const 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

6364
if (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 }))
7075
process.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')
7582
const refMaps = refs.map(ref => ref.remoteBranch ? ref.localBranch + ':' + ref.remoteBranch : '')
7683
const cmd = ['git push', remoteName, ...refMaps].join(' ')
77-
shell.exec(cmd).code === 0 || process.exit(1)
84+
checkReturn(shell.exec(cmd, { silent : true }))
7885

7986
console.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+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@chatie/git-scripts",
3-
"version": "0.7.4",
3+
"version": "0.7.6",
44
"description": "Git Hooks Integration for Chatie Projects",
55
"directories": {
66
"doc": "docs",

0 commit comments

Comments
 (0)