-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.js
83 lines (70 loc) · 1.59 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'use strict';
var through2 = require('through2');
var PluginError = require('plugin-error');
var exec = require('child_process').exec;
var prependPath = require('./prependPath');
var PLUGIN_NAME = 'gulp-exec';
function doExec(command, opt){
if (!command) {
throw new Error('command is blank');
}
if (!opt) {
opt = {};
}
if (!opt.env) {
opt.env = process.env;
}
prependPath(opt.env);
return through2.obj(function (file, enc, cb){
var cmd = typeof command === 'function' ? command(file) : command;
var that = this;
exec(cmd, opt, function (err, stdout, stderr) {
file.exec = {
err: err,
stdout: stdout.trim(),
stderr: stderr.trim()
};
if (opt.pipeStdout) {
file.exec.contents = file.contents;
file.contents = Buffer.from(stdout, opt.encoding); // FRAGILE: if it wasn't a buffer it is now
}
if (err && !opt.continueOnError) {
that.emit('error', new PluginError(PLUGIN_NAME, err));
}
that.push(file);
cb();
});
});
}
function reporter(opt){
if (!opt) {
opt = {};
}
if (typeof opt.err === 'undefined') {
opt.err = true;
}
if (typeof opt.stderr === 'undefined') {
opt.stderr = true;
}
if (typeof opt.stdout === 'undefined') {
opt.stdout = true;
}
return through2.obj(function (file, enc, cb){
if (file && file.exec) {
var e = file.exec;
if (e.stdout && opt.stdout) {
console.log(e.stdout);
}
if (e.stderr && opt.stderr) {
console.error(e.stderr);
}
if (e.err && opt.err) {
console.error(e.err);
}
}
this.push(file);
cb();
});
}
module.exports = doExec;
module.exports.reporter = reporter;