-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtesten
executable file
·228 lines (208 loc) · 5.79 KB
/
testen
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env node
'use strict'
/**
* Module dependencies
*/
const fs = require('fs')
const minimist = require('minimist')
const update = require('update-notifier')
const chalk = require('chalk')
const pathExists = require('path-exists')
const figures = require('figures')
const log = require('log-update')
const textTable = require('text-table')
const co = require('co')
const exec = require('then-exec')
const arrayUnique = require('array-unique')
const Hinata = require('hinata')
const travisOrCircle = require('travis-or-circle')
const indento = require('indento')
const series = require('promise.series')
const stringWidth = require('string-width')
const pkg = require('./package')
const presetVersions = require('./lib/preset-versions')
const table = result => textTable(result, {stringLength: stringWidth})
const spin = new Hinata({char: '●', text: ' ', prepend: true, spacing: 1})
update({pkg}).notify()
const cli = minimist(process.argv.slice(2), {
'--': true,
'alias': {
s: 'sequence',
n: 'node',
h: 'help',
v: 'version',
V: 'verbose'
},
'string': ['node']
})
/**
* Print help
*/
if (cli.help) {
console.log([
'',
'Usage:',
'',
' --system: Use current node version',
' -n/--node [version]: Add a node version to test',
' -s, --sequence: Run tests in sequence',
' -- [command]: The test command you expect',
' -V/--verbose: Always output everything',
' ~ example: there are `console.log` in test',
''
].join('\n'))
process.exit()
}
/**
* Print version
*/
if (cli.version) {
console.log(pkg.version)
process.exit()
}
/**
* Local package.json
*/
let localPkg = {}
if (pathExists.sync(process.cwd() + '/package.json')) {
localPkg = require(process.cwd() + '/package.json')
}
/**
* Test script
*/
let testScript = cli['--'].join(' ')
if (!testScript) {
if (localPkg.testen && localPkg.testen.test) {
testScript = localPkg.testen.test
} else {
testScript = 'npm test'
}
}
/**
* Get status form an array of errors
*/
function getStatus(res) {
let status = 0
res.every(r => {
const e = r.error
if (!e) {
return false
}
if (e.code !== undefined && e.code !== 0) {
status = e.code
return false
}
return true
})
return status
}
/**
* Compare two version number
* @param {string} left
* @param {string} right
* @return {number} 1/-1/0
*/
function versionCompare(left, right) {
if (typeof left + typeof right !== 'stringstring') {
throw new TypeError('Expect version number to be string')
}
const a = left.split('.')
const b = right.split('.')
const len = Math.max(a.length, b.length)
for (let i = 0; i < len; i++) {
if ((a[i] && !b[i] && parseInt(a[i]) > 0) || (parseInt(a[i]) > parseInt(b[i]))) {
return 1
} else if ((b[i] && !a[i] && parseInt(b[i]) > 0) || (parseInt(a[i]) < parseInt(b[i]))) {
return -1
}
}
return 0
}
co(function* main () {
/**
* Get node versions from cli or .yml
* If nothing got then read from package.json
* It still nothing, use default node verison or preset versions
* Finally sort and arrayUnique
*/
let nodeVersions = []
if (cli.system) {
const defaultNodeVersion = yield exec('node -v')
nodeVersions = [defaultNodeVersion.stdout.trim().substr(1)]
} else if (cli.node === undefined) {
nodeVersions = travisOrCircle() || []
}
nodeVersions = nodeVersions.concat(cli.node || [])
if ((!nodeVersions || nodeVersions.length === 0) && typeof nodeVersions !== 'number') {
const pkgNodeVersion = localPkg.testen && localPkg.testen.node
if (pkgNodeVersion && pkgNodeVersion.length > 0) {
nodeVersions = pkgNodeVersion
} else {
nodeVersions = presetVersions
}
}
if (!Array.isArray(nodeVersions)) {
nodeVersions = [nodeVersions]
}
nodeVersions = nodeVersions.map(v => String(v))
nodeVersions = arrayUnique(nodeVersions)
nodeVersions.sort((a, b) => versionCompare(a, b))
const spinLength = nodeVersions.length
spin.length = spinLength
spin.start()
/**
* Generate initial result and log as table
*/
const result = nodeVersions.map(v => {
return [' ' + chalk.dim(figures.circle), v, chalk.dim('pending'), '']
})
log('\n' + table(result) + '\n')
/**
* Execute test
*/
let msg = []
const execTest = co.wrap(function* (command, index, version) {
result[index][2] = 'running'
log('\n' + table(result) + '\n')
const startTime = Date.now()
const cmd = yield exec(command)
const endTime = Date.now()
result[index] = [
' ' + (cmd.error ? chalk.red(figures.cross) : chalk.green(figures.tick)),
nodeVersions[index],
cmd.error ? chalk.red('failed') : chalk.green('success'),
chalk.dim(`${endTime - startTime}ms`)
]
if (cmd && (cmd.error || cli.verbose)) {
let output = cmd.stdout.toString().split('\n')
const statusColor = cmd.error ? 'red' : 'green'
output[0] = chalk.bold[statusColor](output[0] || chalk.red(`Node v${version} is not yet installed`))
if (!output[1] && !cmd.error) {
output[1] = 'no output\n'
}
output = output.join('\n')
msg[index] = `${output.trim()}\n${cmd.error ? `${cmd.error.message.trim()}` : ''}`
}
let heading = `${msg.join('\n\n')}\n`
if (heading.trim()) {
heading += '\n'
}
log(heading + table(result) + '\n')
return cmd
})
/**
* Running test for each version of node
*/
const handler = (v, index) => {
return execTest(`. ~/.nvm/nvm.sh && nvm exec ${v} ${testScript}`, index, v)
}
const tasks = cli.sequence ? series(nodeVersions.map((v, index) => {
return () => handler(v, index)
})) : Promise.all(nodeVersions.map(handler))
yield tasks
spin.stop()
}).catch(e => {
log.clear()
console.log(e.stack)
spin.stop()
})