This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
forked from yuanqing/gulp-tape
/
index.js
65 lines (54 loc) · 1.66 KB
/
index.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
const childProcess = require('child_process')
const dargs = require('dargs')
const PluginError = require('plugin-error')
const TapParser = require('tap-parser')
const through = require('through2')
const pluginName = 'gulp-tape'
const tapeBinaryFilepath = require.resolve('.bin/tape')
function gulpTape (options) {
options = options || {}
const files = []
function transform (file, encoding, callback) {
if (file.isNull()) {
callback(null, file)
return
}
if (file.isStream()) {
callback(new PluginError(pluginName, 'Streaming is not supported'))
return
}
files.push(file.path)
callback(null, file)
}
function flush (callback) {
const args = [tapeBinaryFilepath]
.concat(files)
.concat(dargs(options, { excludes: ['bail', 'nyc', 'outputStream'] }))
if (options.nyc) {
const nycBinaryFilePath = require.resolve('.bin/nyc')
args.unshift(nycBinaryFilePath)
}
var cmd = args.join(' ').split(' ');
const tapeProcess = childProcess.execFile(cmd[0], cmd.slice(1), function (error) {
if (error) {
callback(new PluginError(pluginName, error))
}
})
tapeProcess.stdout.on('end', callback)
const outputStream = options.outputStream || process.stdout
const tapParser = new TapParser()
if (options.bail) {
tapParser.on('assert', function (assert) {
if (!assert.ok) {
callback(new PluginError(pluginName, 'Test failed'))
}
})
}
tapParser.on('line', function (line) {
outputStream.write(line)
})
tapeProcess.stdout.pipe(tapParser)
}
return through.obj(transform, flush)
}
module.exports = gulpTape