/
clone.js
152 lines (135 loc) 路 4.48 KB
/
clone.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* Clones several projects that are known to follow "JavaScript Standard Style" and runs
* the `standard` style checker to verify that it passes without warnings. This helps
* ensure we don't accidentally introduce new style rules that cause previously "good"
* code to start failing with new warnings! (And if we do, then that needs to be a MAJOR
* VERSION BUMP.)
*/
var crossSpawn = require('cross-spawn')
var fs = require('fs')
var minimist = require('minimist')
var mkdirp = require('mkdirp')
var os = require('os')
var parallelLimit = require('run-parallel-limit')
var path = require('path')
var standardPackages = require('standard-packages')
var test = require('tape')
var GIT = 'git'
var STANDARD = path.join(__dirname, '..', 'bin', 'cmd.js')
var TMP = path.join(__dirname, '..', 'tmp')
var PARALLEL_LIMIT = Math.max(os.cpus().length - 1, 1)
var argv = minimist(process.argv.slice(2), {
boolean: [
'disabled',
'offline',
'quick',
'quiet'
]
})
var testPackages = argv.quick
? standardPackages.test.slice(0, 20)
: standardPackages.test
var disabledPackages = []
testPackages = testPackages.filter(function (pkg) {
if (pkg.disable) disabledPackages.push(pkg)
return !pkg.disable
})
if (argv.disabled) {
testPackages = disabledPackages
} else {
test('Disabled Packages', function (t) {
disabledPackages.forEach(function (pkg) {
console.log('DISABLED: ' + pkg.name + ': ' + pkg.disable + ' (' + pkg.repo + ')')
})
t.end()
})
}
test('test github repos that use `standard`', function (t) {
t.plan(testPackages.length)
mkdirp.sync(TMP)
parallelLimit(testPackages.map(function (pkg) {
var name = pkg.name
var url = pkg.repo + '.git'
var folder = path.join(TMP, name)
return function (cb) {
fs.access(path.join(TMP, name), fs.R_OK | fs.W_OK, function (err) {
if (argv.offline) {
if (err) {
t.pass('SKIPPING (offline): ' + name + ' (' + pkg.repo + ')')
return cb(null)
}
runStandard(cb)
} else {
downloadPackage(function (err) {
if (err) return cb(err)
runStandard(cb)
})
}
function downloadPackage (cb) {
if (err) gitClone(cb)
else gitPull(cb)
}
function gitClone (cb) {
var args = [ 'clone', '--depth', 1, url, path.join(TMP, name) ]
spawn(GIT, args, { stdio: 'ignore' }, function (err) {
if (err) err.message += ' (git clone) (' + name + ')'
cb(err)
})
}
function gitPull (cb) {
var args = [ 'pull' ]
spawn(GIT, args, { cwd: folder, stdio: 'ignore' }, function (err) {
if (err) err.message += ' (git pull) (' + name + ')'
cb(err)
})
}
function runStandard (cb) {
var args = [ '--verbose' ]
if (pkg.args) args.push.apply(args, pkg.args)
spawn(STANDARD, args, { cwd: folder }, function (err) {
var str = name + ' (' + pkg.repo + ')'
if (err) {
if (err.message.indexOf('(indent)') || err.message.indexOf('(no-multi-spaces)') || err.message.indexOf('(space-unary-ops)')) {
t.comment('Attempting to fix eslint breaking changes for ' + str)
return runStandardFix(cb)
} else {
t.fail(str)
}
} else {
t.pass(str)
}
cb(null)
})
}
function runStandardFix (cb) {
var args = [ '--fix', '--verbose' ]
if (pkg.args) args.push.apply(args, pkg.args)
spawn(STANDARD, args, { cwd: folder }, function (err) {
var str = name + ' (' + pkg.repo + ') **with --fix'
if (err) { t.fail(str) } else { t.pass(str) }
runGitReset(cb)
})
}
function runGitReset (cb) {
var args = [ 'reset', '--hard' ]
spawn(GIT, args, { cwd: folder }, function (err) {
if (err) err.message += ' (git reset) (' + name + ')'
cb(err)
})
}
})
}
}), PARALLEL_LIMIT, function (err) {
if (err) throw err
})
})
function spawn (command, args, opts, cb) {
if (!opts.stdio) opts.stdio = argv.quiet ? 'ignore' : 'inherit'
var child = crossSpawn(command, args, opts)
child.on('error', cb)
child.on('close', function (code) {
if (code !== 0) return cb(new Error('non-zero exit code: ' + code))
cb(null)
})
return child
}