Skip to content

Commit 2254e65

Browse files
committed
squash: moving the load and loadPatch functions util
* This moves the function that loads the commit, from either a 'git show' or from a url, into the util module * This is for cleaning up the main cmd.js file which should only really have logic related to the commands
1 parent d5dc539 commit 2254e65

File tree

2 files changed

+47
-51
lines changed

2 files changed

+47
-51
lines changed

bin/cmd.js

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
'use strict'
44

5-
const exec = require('child_process').exec
65
const fs = require('fs')
7-
const http = require('http')
8-
const https = require('https')
9-
const url = require('url')
106
const nopt = require('nopt')
117
const path = require('path')
128
const pretty = require('../lib/format-pretty')
@@ -60,57 +56,12 @@ const args = parsed.argv.remain
6056
if (!args.length)
6157
args.push('HEAD')
6258

63-
// Given a commit hash, load it
64-
// If a URL is passed in, then load the commit remotely
65-
// If not, then do a git show
66-
function load(sha, cb) {
67-
const parsed = url.parse(sha)
68-
if (parsed.protocol) {
69-
return loadPatch(parsed, cb)
70-
}
71-
72-
exec(`git show --quiet --format=medium ${sha}`, (err, stdout, stderr) => {
73-
if (err) return cb(err)
74-
cb(null, stdout.trim())
75-
})
76-
}
77-
78-
// Load the commit from a URL
79-
function loadPatch(uri, cb) {
80-
let h = http
81-
if (~uri.protocol.indexOf('https')) {
82-
h = https
83-
}
84-
uri.headers = {
85-
'user-agent': 'core-validate-commit'
86-
}
87-
h.get(uri, (res) => {
88-
let buf = ''
89-
res.on('data', (chunk) => {
90-
buf += chunk
91-
})
92-
93-
res.on('end', () => {
94-
try {
95-
const out = JSON.parse(buf)
96-
cb(null, out)
97-
} catch (err) {
98-
cb(err)
99-
}
100-
})
101-
}).on('error', cb)
102-
}
103-
10459
// Create a new Validator
10560
const v = new Validator(parsed)
10661

10762
// The --list or -l flag was used
10863
if (parsed.list) {
10964
// Get the list of Rule names
110-
// There is nothing here that says we need to have created that validator first,
111-
// unless at some point we don't count disabled things
112-
// But we should probably just get the rules from the rules in ./lib/rules
113-
// Then this function can move up to the top
11465
const ruleNames = Array.from(v.rules.keys())
11566
// Find the length of the longest Rule names
11667
const max = ruleNames.reduce((m, item) => {
@@ -148,7 +99,7 @@ if (parsed.tap) {
14899
function run() {
149100
if (!args.length) return
150101
const sha = args.shift()
151-
load(sha, (err, data) => {
102+
utils.load(sha, (err, data) => {
152103
if (err) throw err
153104
v.lint(data)
154105
run()
@@ -170,7 +121,7 @@ if (parsed.tap) {
170121
return
171122
}
172123
const sha = args.shift()
173-
load(sha, (err, data) => {
124+
utils.load(sha, (err, data) => {
174125
if (err) throw err
175126
v.lint(data)
176127
})

lib/utils.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
'use strict'
22

3+
const exec = require('child_process').exec
4+
const http = require('http')
5+
const https = require('https')
6+
const url = require('url')
37
const chalk = require('chalk')
48
const CHECK = chalk.green('✔')
59
const X = chalk.red('✖')
@@ -57,3 +61,44 @@ exports.describeSubsystem = function describeSubsystem(subsystems, max = 20) {
5761
}
5862
}
5963
}
64+
65+
// Given a commit hash, load it
66+
// If a URL is passed in, then load the commit remotely
67+
// If not, then do a git show
68+
exports.load = function load(sha, cb) {
69+
const parsed = url.parse(sha)
70+
if (parsed.protocol) {
71+
return exports.loadPatch(parsed, cb)
72+
}
73+
74+
exec(`git show --quiet --format=medium ${sha}`, (err, stdout, stderr) => {
75+
if (err) return cb(err)
76+
cb(null, stdout.trim())
77+
})
78+
}
79+
80+
// Load the commit from a URL
81+
exports.loadPatch = function loadPatch(uri, cb) {
82+
let h = http
83+
if (~uri.protocol.indexOf('https')) {
84+
h = https
85+
}
86+
uri.headers = {
87+
'user-agent': 'core-validate-commit'
88+
}
89+
h.get(uri, (res) => {
90+
let buf = ''
91+
res.on('data', (chunk) => {
92+
buf += chunk
93+
})
94+
95+
res.on('end', () => {
96+
try {
97+
const out = JSON.parse(buf)
98+
cb(null, out)
99+
} catch (err) {
100+
cb(err)
101+
}
102+
})
103+
}).on('error', cb)
104+
}

0 commit comments

Comments
 (0)