Skip to content

Commit 5bdcfb6

Browse files
authored
Merge pull request #234 from jasonmit/remove-sighup
Replace SIGHUP event with a custom event
2 parents 866ae49 + 4638aa9 commit 5bdcfb6

5 files changed

Lines changed: 86 additions & 61 deletions

File tree

index.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* jshint node: true */
22
'use strict';
33

4+
var EventEmitter = require('events').EventEmitter;
45
var mergeTrees = require('broccoli-merge-trees');
56

67
var patchEmberApp = require('./lib/ext/patch-ember-app');
@@ -16,15 +17,29 @@ var FastBootBuild = require('./lib/broccoli/fastboot-build');
1617
module.exports = {
1718
name: 'ember-cli-fastboot',
1819

20+
init() {
21+
this._super.init && this._super.init.apply(this, arguments);
22+
23+
this.emitter = new EventEmitter();
24+
},
25+
1926
includedCommands: function() {
2027
return {
21-
'fastboot': require('./lib/commands/fastboot'),
28+
'fastboot': require('./lib/commands/fastboot')(this),
2229

2330
/* fastboot:build is deprecated and will be removed in a future version */
2431
'fastboot:build': require('./lib/commands/fastboot-build')
2532
};
2633
},
2734

35+
on: function() {
36+
this.emitter.on.apply(this.emitter, arguments);
37+
},
38+
39+
emit: function() {
40+
this.emitter.emit.apply(this.emitter, arguments);
41+
},
42+
2843
/**
2944
* Called at the start of the build process to let the addon know it will be
3045
* used. At this point, we can rely on the EMBER_CLI_FASTBOOT environment
@@ -109,7 +124,7 @@ module.exports = {
109124
},
110125

111126
postBuild: function() {
112-
process.emit('SIGHUP');
127+
this.emit('postBuild');
113128
},
114129

115130
};

lib/commands/fastboot.js

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,63 +8,66 @@ const SilentError = require('silent-error');
88
const blockForever = () => (new RSVP.Promise(() => {}));
99
const noop = function() { };
1010

11-
module.exports = {
12-
name: 'fastboot',
13-
description: 'Builds and serves your FastBoot app, rebuilding on file changes.',
11+
module.exports = function(addon) {
12+
return {
13+
name: 'fastboot',
14+
description: 'Builds and serves your FastBoot app, rebuilding on file changes.',
1415

15-
availableOptions: [
16-
{ name: 'build', type: Boolean, default: true },
17-
{ name: 'watch', type: Boolean, default: true, aliases: ['w'] },
18-
{ name: 'environment', type: String, default: 'development', aliases: ['e',{'dev' : 'development'}, {'prod' : 'production'}] },
19-
{ name: 'serve-assets', type: Boolean, default: false },
20-
{ name: 'host', type: String, default: '::' },
21-
{ name: 'port', type: Number, default: 3000 },
22-
{ name: 'output-path', type: String, default: 'dist' },
23-
{ name: 'assets-path', type: String, default: 'dist' }
24-
],
16+
availableOptions: [
17+
{ name: 'build', type: Boolean, default: true },
18+
{ name: 'watch', type: Boolean, default: true, aliases: ['w'] },
19+
{ name: 'environment', type: String, default: 'development', aliases: ['e',{'dev' : 'development'}, {'prod' : 'production'}] },
20+
{ name: 'serve-assets', type: Boolean, default: false },
21+
{ name: 'host', type: String, default: '::' },
22+
{ name: 'port', type: Number, default: 3000 },
23+
{ name: 'output-path', type: String, default: 'dist' },
24+
{ name: 'assets-path', type: String, default: 'dist' }
25+
],
2526

26-
blockForever,
27-
getPort,
28-
ServerTask,
27+
blockForever,
28+
getPort,
29+
ServerTask,
2930

30-
run(options) {
31-
const runBuild = () => this.runBuild(options);
32-
const runServer = () => this.runServer(options);
33-
const blockForever = this.blockForever;
31+
run(options) {
32+
const runBuild = () => this.runBuild(options);
33+
const runServer = () => this.runServer(options);
34+
const blockForever = this.blockForever;
3435

35-
return this.checkPort(options)
36-
.then(runServer) // starts on postBuild SIGHUP
37-
.then(options.build ? runBuild : noop)
38-
.then(blockForever);
39-
},
36+
return this.checkPort(options)
37+
.then(runServer) // starts on postBuild SIGHUP
38+
.then(options.build ? runBuild : noop)
39+
.then(blockForever);
40+
},
4041

41-
runServer(options) {
42-
const ServerTask = this.ServerTask;
43-
const serverTask = new ServerTask({
44-
ui: this.ui,
45-
});
46-
return serverTask.run(options);
47-
},
48-
49-
runBuild(options) {
50-
const BuildTask = options.watch ? this.tasks.BuildWatch : this.tasks.Build;
51-
const buildTask = new BuildTask({
52-
ui: this.ui,
53-
analytics: this.analytics,
54-
project: this.project,
55-
});
56-
buildTask.run(options); // no return, BuildWatch.run blocks forever
57-
},
42+
runServer(options) {
43+
const ServerTask = this.ServerTask;
44+
const serverTask = new ServerTask({
45+
ui: this.ui,
46+
addon: addon
47+
});
48+
return serverTask.run(options);
49+
},
5850

59-
checkPort(options) {
60-
return this.getPort({ port: options.port, host: options.host })
61-
.then((foundPort) => {
62-
if (options.port !== foundPort && options.port !== 0) {
63-
const message = `Port ${options.port} is already in use.`;
64-
return Promise.reject(new SilentError(message));
65-
}
66-
options.port = foundPort;
51+
runBuild(options) {
52+
const BuildTask = options.watch ? this.tasks.BuildWatch : this.tasks.Build;
53+
const buildTask = new BuildTask({
54+
ui: this.ui,
55+
analytics: this.analytics,
56+
project: this.project,
6757
});
68-
},
58+
buildTask.run(options); // no return, BuildWatch.run blocks forever
59+
},
60+
61+
checkPort(options) {
62+
return this.getPort({ port: options.port, host: options.host })
63+
.then((foundPort) => {
64+
if (options.port !== foundPort && options.port !== 0) {
65+
const message = `Port ${options.port} is already in use.`;
66+
return Promise.reject(new SilentError(message));
67+
}
68+
options.port = foundPort;
69+
});
70+
},
6971

72+
}
7073
};

lib/tasks/fastboot-server.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = CoreObject.extend({
2121
run(options) {
2222
debug('run');
2323
const restart = () => this.restart(options);
24-
process.on('SIGHUP', restart);
24+
this.addon.on('postBuild', restart);
2525
},
2626

2727
start(options) {
@@ -120,6 +120,5 @@ module.exports = CoreObject.extend({
120120
delete require.cache[key];
121121
}
122122
});
123-
},
124-
123+
}
125124
});

test/lib-commands-fastboot-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const CoreObject = require('core-object');
44
const camelize = require('ember-cli-string-utils').camelize;
55
const defaults = require('lodash.defaults');
66
const expect = require('chai').expect;
7-
const FastbootCommand = CoreObject.extend(require('../lib/commands/fastboot'));
7+
const FastbootCommand = CoreObject.extend(require('../lib/commands/fastboot')());
88
const RSVP = require('rsvp');
99

1010
function CommandOptions(options) {

test/lib-tasks-fastboot-server-test.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const camelize = require('ember-cli-string-utils').camelize;
55
const defaults = require('lodash.defaults');
66
const EventEmitter = require('events').EventEmitter;
77
const expect = require('chai').expect;
8-
const FastbootCommand = CoreObject.extend(require('../lib/commands/fastboot'));
8+
const FastbootCommand = CoreObject.extend(require('../lib/commands/fastboot')());
99
const FastbootServerTask = require('../lib/tasks/fastboot-server');
1010
const http = require('http');
1111
const RSVP = require('rsvp');
@@ -28,15 +28,23 @@ function MockServer() {
2828
this.address = () => ({ port: 1, host: '0.0.0.0', family: 'IPv4' });
2929
}
3030
MockServer.prototype = Object.create(EventEmitter.prototype);
31+
32+
function MockAddon() {
33+
this.emitter = EventEmitter.apply(this, arguments);
34+
}
35+
36+
MockAddon.prototype = Object.create(EventEmitter.prototype);
37+
3138
const mockUI = { writeLine() {} };
3239

3340
describe('fastboot server task', function() {
3441
let options, task;
35-
42+
let addon = new MockAddon();
3643
beforeEach(function() {
3744
this.sinon = sinon.sandbox.create();
3845
task = new FastbootServerTask({
3946
ui: mockUI,
47+
addon: addon
4048
});
4149
options = new CommandOptions();
4250
});
@@ -46,10 +54,10 @@ describe('fastboot server task', function() {
4654
});
4755

4856
describe('run', function() {
49-
it('calls restart on SIGHUP', function() {
57+
it('calls restart on postBuild', function() {
5058
const restartStub = this.sinon.stub(task, 'restart');
5159
task.run(options);
52-
process.emit('SIGHUP');
60+
addon.emit('postBuild');
5361
expect(restartStub.called).to.be.ok;
5462
});
5563
});

0 commit comments

Comments
 (0)