forked from nodejs/core-validate-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd.js
More file actions
executable file
·93 lines (77 loc) · 1.75 KB
/
Copy pathcmd.js
File metadata and controls
executable file
·93 lines (77 loc) · 1.75 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env node
'use strict'
const exec = require('child_process').exec
const http = require('http')
const https = require('https')
const url = require('url')
const nopt = require('nopt')
const pretty = require('../lib/format-pretty')
const Validator = require('../lib')
const knownOpts = { help: Boolean
, version: Boolean
, 'validate-metadata': Boolean
}
const shortHand = { h: ['--help']
, v: ['--version']
, V: ['--validate-metadata']
}
const parsed = nopt(knownOpts, shortHand)
const usage = require('help')()
if (parsed.help) {
return usage()
}
if (parsed.version) {
console.log('core-validate-commit', 'v' + require('../package').version)
return
}
const args = parsed.argv.remain
function load(sha, cb) {
const parsed = url.parse(sha)
if (parsed.protocol) {
return loadPatch(parsed, cb)
}
exec(`git show --quiet ${sha}`, (err, stdout, stderr) => {
if (err) return cb(err)
cb(null, stdout.trim())
})
}
function loadPatch(uri, cb) {
var h = http
if (~uri.protocol.indexOf('https')) {
h = https
}
uri.headers = {
'user-agent': 'core-validate-commit'
}
h.get(uri, (res) => {
var buf = ''
res.on('data', (chunk) => {
buf += chunk
})
res.on('end', () => {
try {
const out = JSON.parse(buf)
cb(null, out)
} catch (err) {
cb(err)
}
})
}).on('error', cb)
}
const v = new Validator(parsed)
v.on('commit', (c) => {
pretty(c.commit, c.messages, v)
run()
})
function run() {
if (!args.length) {
process.exitCode = v.errors
return
}
const sha = args.shift()
load(sha, (err, data) => {
if (err) throw err
v.lint(data)
})
}
run()