1 | 'use strict'
|
2 |
|
3 | const debug = require('debug')('elint:utils:is-git-hooks')
|
4 | const co = require('co')
|
5 | const find = require('find-process')
|
6 |
|
7 | const huskyCmdReg = /node_modules(\/|\\)husky/
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | function getPPID () {
|
17 | const ppid = process.ppid
|
18 |
|
19 | if (typeof ppid === 'number') {
|
20 | return Promise.resolve(ppid)
|
21 | }
|
22 |
|
23 | return find('pid', process.pid)
|
24 | .then(list => {
|
25 | return list && list[0] && list[0].ppid
|
26 | })
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | function isRunByHusky (ppid) {
|
36 | return find('pid', ppid).then(list => {
|
37 | debug('process list: %o', list)
|
38 |
|
39 | const cmd = list[0] && list[0].cmd
|
40 | const pppid = list[0] && list[0].ppid
|
41 |
|
42 | if (!cmd) {
|
43 | return false
|
44 | }
|
45 |
|
46 | if (huskyCmdReg.test(cmd)) {
|
47 | return true
|
48 | }
|
49 |
|
50 |
|
51 | return isRunByHusky(pppid)
|
52 | })
|
53 | }
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | function isGitHooks () {
|
61 | return co(function * () {
|
62 | const ppid = yield getPPID()
|
63 |
|
64 | debug(`ppid: ${ppid}`)
|
65 |
|
66 | return isRunByHusky(ppid)
|
67 | }).catch( function (err) {
|
68 | debug('error: %o', err)
|
69 | return false
|
70 | })
|
71 | }
|
72 |
|
73 | module.exports = isGitHooks
|