-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathindex.js
45 lines (34 loc) · 1.04 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
'use strict';
const format = require('path').format;
const execa = require('execa').shell;
const NAME = '@taskr/shell';
module.exports = function (task, utils) {
const setError = str => task.emit('plugin_error', { error:str, plugin:NAME });
task.plugin('shell', { every:false }, function * (files, cmd, opts) {
opts = opts || {};
if (typeof cmd !== 'string') {
opts = cmd;
cmd = opts.cmd;
}
const args = opts.args || [];
if (!cmd) {
return setError('No command received!');
}
const isGlob = opts.glob || false;
// output header
const head = `${NAME}:${isGlob ? '\n\t' : ' '}`;
const tail = isGlob ? '\n\t' : '\n';
const runWith = str => {
// use file or glob
const c = cmd.replace(/\$(file|glob)/gi, str);
// pass all args to execa
return execa.apply(this, [c, args, opts]).then(res => {
utils.log(head + res.stdout.replace(/\n/g, tail));
}).catch(err => {
setError(err.message);
});
};
const src = isGlob ? this._.globs : files.map(format);
return yield Promise.all(src.map(runWith));
});
};