Skip to content

Commit 5f4568b

Browse files
authored
Merge pull request #2 from Realytics/dev
0.1.1
2 parents 7601f50 + 0538eac commit 5f4568b

File tree

4 files changed

+88
-36
lines changed

4 files changed

+88
-36
lines changed

lib/cluster.js

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,47 @@
1-
var cluster = require('cluster');
21
var process = require('process');
2+
var childProcess = require('child_process');
33
var path = require('path');
44

55
var WorkResult = require('./WorkResult');
66
var NormalizedMessage = require('./NormalizedMessage');
77

8-
// setup master
9-
cluster.setupMaster({
10-
exec: path.join(__dirname, 'service.js'),
11-
args: ['--max-old-space-size=' + process.env.MEMORY_LIMIT]
12-
});
13-
148
// fork workers...
159
var division = parseInt(process.env.WORK_DIVISION);
10+
var workers = [];
1611

17-
for (var i = 0; i < division; i++) {
18-
cluster.fork({ WORK_NUMBER: i });
12+
for (var number = 0; number < division; number++) {
13+
workers.push(
14+
childProcess.fork(
15+
path.resolve(__dirname, './service.js'),
16+
[],
17+
{
18+
execArgv: ['--max-old-space-size=' + process.env.MEMORY_LIMIT],
19+
env: Object.assign({}, process.env, { WORKER_NUMBER: number }),
20+
stdio: ['inherit', 'inherit', 'inherit', 'ipc']
21+
}
22+
)
23+
);
1924
}
2025

21-
var workerIds = Object.keys(cluster.workers);
22-
var result = new WorkResult(workerIds);
26+
var pids = workers.map(function (worker) { return worker.pid; });
27+
var result = new WorkResult(pids);
2328

2429
process.on('message', function (message) {
2530
// broadcast message to all workers
26-
workerIds.forEach(function (workerId) {
27-
cluster.workers[workerId].send(message);
31+
workers.forEach(function (worker) {
32+
worker.send(message);
2833
});
2934

3035
// clear previous result set
3136
result.clear();
3237
});
3338

3439
// listen to all workers
35-
workerIds.forEach(function (workerId) {
36-
cluster.workers[workerId].on('message', function (message) {
40+
workers.forEach(function (worker) {
41+
worker.on('message', function (message) {
3742
// set result from worker
3843
result.set(
39-
workerId,
44+
worker.pid,
4045
{
4146
diagnostics: message.diagnostics.map(NormalizedMessage.createFromJSON),
4247
lints: message.lints.map(NormalizedMessage.createFromJSON)
@@ -58,7 +63,26 @@ workerIds.forEach(function (workerId) {
5863
merged.diagnostics = NormalizedMessage.deduplicate(merged.diagnostics);
5964
merged.lints = NormalizedMessage.deduplicate(merged.lints);
6065

61-
process.send(merged);
66+
try {
67+
process.send(merged);
68+
} catch (e) {
69+
// channel closed...
70+
process.exit();
71+
}
6272
}
6373
});
6474
});
75+
76+
process.on('SIGINT', function () {
77+
process.exit();
78+
});
79+
80+
process.on('exit', function () {
81+
workers.forEach(function (worker) {
82+
try {
83+
worker.kill();
84+
} catch (e) {
85+
// do nothing...
86+
}
87+
});
88+
});

lib/index.js

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var NormalizedMessage = require('./NormalizedMessage');
1616
* Options description in README.md
1717
*/
1818
function ForkTsCheckerWebpackPlugin (options) {
19+
this.options = Object.assign({}, options);
1920
this.tsconfig = options.tsconfig || './tsconfig.json';
2021
this.tslint = options.tslint === false ? false : options.tslint || './tslint.json';
2122
this.watch = isString(options.watch) ? [options.watch] : options.watch || [];
@@ -130,15 +131,11 @@ ForkTsCheckerWebpackPlugin.prototype.pluginStart = function () {
130131

131132
ForkTsCheckerWebpackPlugin.prototype.pluginStop = function () {
132133
this.compiler.plugin('done', function () {
133-
if (!this.isWatching && this.service) {
134-
try {
135-
this.service.kill();
136-
} catch (e) {
137-
if (this.logger && !this.silent) {
138-
this.logger.error(e);
139-
}
140-
}
141-
}
134+
this.killService();
135+
}.bind(this));
136+
137+
process.on('exit', function () {
138+
this.killService(true);
142139
}.bind(this));
143140
};
144141

@@ -230,8 +227,13 @@ ForkTsCheckerWebpackPlugin.prototype.spawnService = function () {
230227
'Using ' + this.colors.bold(this.workersNumber === 1 ? '1 worker' : this.workersNumber + ' workers') +
231228
' with ' + this.colors.bold(this.memoryLimit + 'MB') + ' memory limit'
232229
);
233-
var lines = [message, performance, this.colors.grey(this.tsconfigPath)];
234-
if (this.tslint) {
230+
var lines = [message, performance];
231+
if (!this.options.tsconfig) {
232+
// auto-detect tsconfig path - print to the user to be sure that it's proper file
233+
lines.push(this.colors.grey(this.tsconfigPath));
234+
}
235+
if (this.tslint && !this.options.tslint) {
236+
// auto-detect tslint path - print to the user to be sure that it's proper file
235237
lines.push(this.colors.grey(this.tslint));
236238
}
237239

@@ -251,6 +253,23 @@ ForkTsCheckerWebpackPlugin.prototype.spawnService = function () {
251253
this.service.on('exit', this.handleServiceExit.bind(this));
252254
};
253255

256+
ForkTsCheckerWebpackPlugin.prototype.killService = function (force) {
257+
if ((force || !this.isWatching) && this.service) {
258+
try {
259+
if (this.cancellationToken) {
260+
this.cancellationToken.requestCancellation();
261+
}
262+
263+
this.service.kill();
264+
this.service = undefined;
265+
} catch (e) {
266+
if (this.logger && !this.silent) {
267+
this.logger.error(e);
268+
}
269+
}
270+
}
271+
};
272+
254273
ForkTsCheckerWebpackPlugin.prototype.handleServiceMessage = function (message) {
255274
if (this.cancellationToken) {
256275
this.cancellationToken.cleanupCancellation();

lib/service.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ var checker = new IncrementalChecker(
1111
parseInt(process.env.WORK_DIVISION, 10)
1212
);
1313

14-
var diagnostics = [];
15-
var lints = [];
16-
1714
function run (cancellationToken) {
15+
var diagnostics = [];
16+
var lints = [];
17+
1818
checker.nextIteration();
1919

2020
try {
@@ -31,13 +31,22 @@ function run (cancellationToken) {
3131
}
3232

3333
if (!cancellationToken.isCancellationRequested()) {
34-
process.send({
35-
diagnostics: diagnostics,
36-
lints: lints
37-
});
34+
try {
35+
process.send({
36+
diagnostics: diagnostics,
37+
lints: lints
38+
});
39+
} catch (e) {
40+
// channel closed...
41+
process.exit();
42+
}
3843
}
3944
}
4045

4146
process.on('message', function (message) {
4247
run(CancellationToken.createFromJSON(message));
4348
});
49+
50+
process.on('SIGINT', function () {
51+
process.exit();
52+
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fork-ts-checker-webpack-plugin",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "Runs typescript type checker and linter on separate process.",
55
"main": "lib/index.js",
66
"scripts": {

0 commit comments

Comments
 (0)