-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
90 lines (75 loc) · 2.22 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
84
85
86
87
88
89
90
var shell = require('shelljs');
function noop() {}
function FlowStatusWebpackPlugin(options) {
this.options = options || {};
}
FlowStatusWebpackPlugin.prototype.apply = function(compiler) {
var options = this.options;
var flowArgs = options.flowArgs || '';
var flow = options.binaryPath || 'flow';
var failOnError = options.failOnError || false;
var onSuccess = options.onSuccess || noop;
var onError = options.onError || noop;
var firstRun = true;
var waitingForFlow = false;
function startFlow(cb) {
if (options.restartFlow === false) {
cb();
} else {
shell.exec(flow + ' stop', {silent: true}, function() {
shell.exec(flow + ' start ' + flowArgs, {silent: true}, cb);
});
}
}
function startFlowIfFirstRun(cb) {
if (firstRun) {
firstRun = false;
startFlow(cb);
} else {
cb();
}
}
function flowStatus(successCb, errorCb) {
if (!waitingForFlow) {
waitingForFlow = true;
// this will start a flow server if it was not running
shell.exec(flow + ' status --color always', {silent: true}, function(code, stdout, stderr) {
var hasErrors = code !== 0;
var cb = hasErrors ? errorCb : successCb;
waitingForFlow = false;
cb(stdout, stderr);
});
}
}
var flowError = null;
function checkItWreckIt(cb) {
startFlowIfFirstRun(function () {
flowStatus(function success(stdout) {
onSuccess(stdout);
cb();
}, function error(stdout) {
onError(stdout);
flowError = new Error(stdout);
// Do not pass error to callback to avoid dev server exit
cb();
});
});
}
// Webpack 5 hooks
compiler.hooks.run.tapAsync('FlowStatusWebpackPlugin', (compilation, callback) => {
checkItWreckIt(callback);
});
compiler.hooks.watchRun.tapAsync('FlowStatusWebpackPlugin', (compilation, callback) => {
checkItWreckIt(callback);
});
// Add errors to compilation if needed
compiler.hooks.compilation.tap('FlowStatusWebpackPlugin', (compilation) => {
if (flowError) {
if (failOnError === true) {
compilation.errors.push(flowError);
}
flowError = null;
}
});
};
module.exports = FlowStatusWebpackPlugin;