-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-push
42 lines (31 loc) · 1.06 KB
/
pre-push
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python
#
# Python script that implements a Git Hook of type Pre Push
# Author: Alexandre Pardo
# Date: 06/26/2019
#
import json, os, re, sys
from subprocess import check_call
def npm_check_run(task_name, scripts):
has_task = task_name in scripts
if has_task:
command = 'npm run ' + task_name
check_call(command, shell=True)
print '=================================================='
print ' GIT PRE-PUSH HOOK IS GONNA RUN LINT/TEST/BUILD'
print '=================================================='
try:
package_json_file_name = 'package.json'
is_npm_project = os.path.isfile(package_json_file_name)
if not is_npm_project: # Nothing to do
sys.exit(0)
package_json_content = open(package_json_file_name).read()
package_json = json.loads(package_json_content)
scripts = package_json['scripts']
npm_check_run('lint', scripts)
npm_check_run('test', scripts)
npm_check_run('build', scripts)
except Exception as e:
# Oops, either lint, test or build failed.
print 'EXCEPTION THROWN ON PRE-PUSH HOOK', e
sys.exit(1)