-
Notifications
You must be signed in to change notification settings - Fork 9
/
plugin.js
75 lines (63 loc) · 1.86 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
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
var flowStatus = require('./lib/flowStatus');
function FlowtypePlugin(options) {
this._options = options || {};
if (!this._options.cwd) {
this._options.cwd = process.cwd();
}
this._resources = [];
this._isFlowRunning = false;
this._flowStatus = null;
}
FlowtypePlugin.prototype.getFlowStatus = function () {
if (this._isFlowRunning) {
return true;
}
this._isFlowRunning = true;
flowStatus(this._options.cwd, function (status) {
this._flowStatus = status;
this._isFlowRunning = false;
this._notifyResources();
}.bind(this));
};
FlowtypePlugin.prototype.addResource = function (resourcePath, callback) {
var resource = {path: resourcePath, callback: callback};
if (this._isFlowRunning) {
this._resources.push(resource);
} else {
this._notifyResourceError(resource);
}
};
FlowtypePlugin.prototype.clearResources = function () {
this._resources = [];
};
FlowtypePlugin.prototype._notifyResources = function () {
for (var i = 0; i < this._resources.length; i++) {
var resource = this._resources[i];
this._notifyResourceError(resource);
}
this.clearResources();
};
FlowtypePlugin.prototype._notifyResourceError = function(resource) {
if (resource.callback) {
var errors = [];
if (this._flowStatus && !this._flowStatus.passed) {
errors = this._flowStatus.errors
}
resource.callback(errors, this._options);
}
};
FlowtypePlugin.prototype.apply = function (compiler) {
var plugin = this;
compiler.plugin('compile', function () {
plugin.clearResources();
plugin.getFlowStatus();
});
compiler.plugin('compilation', function (compilation) {
compilation.plugin('normal-module-loader', function (context, module) {
context.flowtypeCheck = function(resourcePath, callback) {
plugin.addResource(resourcePath, callback);
};
});
});
};
module.exports = FlowtypePlugin;