forked from FredKSchott/snowpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
47 lines (44 loc) · 1.42 KB
/
plugin.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
const execa = require('execa');
const npmRunPath = require('npm-run-path');
const cwd = process.cwd();
function runScriptPlugin(_, {name, cmd, watch, output}) {
const [cmdProgram] = cmd.split(' ');
const watchCmd = watch && watch.replace('$1', cmd);
return {
name: name || cmdProgram,
async run({isDev, log}) {
const workerPromise = execa.command(isDev ? watchCmd || cmd : cmd, {
env: npmRunPath.env(),
extendEnv: true,
shell: true,
windowsHide: false,
cwd,
});
const {stdout, stderr} = workerPromise;
function dataListener(chunk) {
let stdOutput = chunk.toString();
if (output === 'stream') {
log('CONSOLE_INFO', {msg: stdOutput});
return;
}
if (stdOutput.includes('\u001bc') || stdOutput.includes('\x1Bc')) {
log('WORKER_RESET', {});
stdOutput = stdOutput.replace(/\x1Bc/, '').replace(/\u001bc/, '');
}
if (cmdProgram === 'tsc') {
const errorMatch = stdOutput.match(/Found (\d+) error/);
if (errorMatch) {
if (errorMatch[1] === '0') {
stdOutput = stdOutput.trim();
}
}
}
log('WORKER_MSG', {level: 'log', msg: stdOutput});
}
stdout && stdout.on('data', dataListener);
stderr && stderr.on('data', dataListener);
return workerPromise;
},
};
}
module.exports = runScriptPlugin;