Skip to content

Commit

Permalink
Removing signally SIGHUP with a custom event
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmit committed Jul 25, 2016
1 parent 866ae49 commit f24a1f5
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 57 deletions.
19 changes: 17 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* jshint node: true */
'use strict';

var EventEmitter = require('events').EventEmitter;
var mergeTrees = require('broccoli-merge-trees');

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

init() {
this._super.init && this._super.init.apply(this, arguments);

this.emitter = new EventEmitter();
},

includedCommands: function() {
return {
'fastboot': require('./lib/commands/fastboot'),
'fastboot': require('./lib/commands/fastboot')(this),

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

on: function() {
this.emitter.on.apply(this.emitter, arguments);
},

emit: function() {
this.emitter.emit.apply(this.emitter, arguments);
},

/**
* Called at the start of the build process to let the addon know it will be
* used. At this point, we can rely on the EMBER_CLI_FASTBOOT environment
Expand Down Expand Up @@ -109,7 +124,7 @@ module.exports = {
},

postBuild: function() {
process.emit('SIGHUP');
this.emit('postBuild');
},

};
105 changes: 54 additions & 51 deletions lib/commands/fastboot.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,63 +8,66 @@ const SilentError = require('silent-error');
const blockForever = () => (new RSVP.Promise(() => {}));
const noop = function() { };

module.exports = {
name: 'fastboot',
description: 'Builds and serves your FastBoot app, rebuilding on file changes.',
module.exports = function(addon) {
return {
name: 'fastboot',
description: 'Builds and serves your FastBoot app, rebuilding on file changes.',

availableOptions: [
{ name: 'build', type: Boolean, default: true },
{ name: 'watch', type: Boolean, default: true, aliases: ['w'] },
{ name: 'environment', type: String, default: 'development', aliases: ['e',{'dev' : 'development'}, {'prod' : 'production'}] },
{ name: 'serve-assets', type: Boolean, default: false },
{ name: 'host', type: String, default: '::' },
{ name: 'port', type: Number, default: 3000 },
{ name: 'output-path', type: String, default: 'dist' },
{ name: 'assets-path', type: String, default: 'dist' }
],
availableOptions: [
{ name: 'build', type: Boolean, default: true },
{ name: 'watch', type: Boolean, default: true, aliases: ['w'] },
{ name: 'environment', type: String, default: 'development', aliases: ['e',{'dev' : 'development'}, {'prod' : 'production'}] },
{ name: 'serve-assets', type: Boolean, default: false },
{ name: 'host', type: String, default: '::' },
{ name: 'port', type: Number, default: 3000 },
{ name: 'output-path', type: String, default: 'dist' },
{ name: 'assets-path', type: String, default: 'dist' }
],

blockForever,
getPort,
ServerTask,
blockForever,
getPort,
ServerTask,

run(options) {
const runBuild = () => this.runBuild(options);
const runServer = () => this.runServer(options);
const blockForever = this.blockForever;
run(options) {
const runBuild = () => this.runBuild(options);
const runServer = () => this.runServer(options);
const blockForever = this.blockForever;

return this.checkPort(options)
.then(runServer) // starts on postBuild SIGHUP
.then(options.build ? runBuild : noop)
.then(blockForever);
},
return this.checkPort(options)
.then(runServer) // starts on postBuild SIGHUP
.then(options.build ? runBuild : noop)
.then(blockForever);
},

runServer(options) {
const ServerTask = this.ServerTask;
const serverTask = new ServerTask({
ui: this.ui,
});
return serverTask.run(options);
},

runBuild(options) {
const BuildTask = options.watch ? this.tasks.BuildWatch : this.tasks.Build;
const buildTask = new BuildTask({
ui: this.ui,
analytics: this.analytics,
project: this.project,
});
buildTask.run(options); // no return, BuildWatch.run blocks forever
},
runServer(options) {
const ServerTask = this.ServerTask;
const serverTask = new ServerTask({
ui: this.ui,
addon: addon
});
return serverTask.run(options);
},

checkPort(options) {
return this.getPort({ port: options.port, host: options.host })
.then((foundPort) => {
if (options.port !== foundPort && options.port !== 0) {
const message = `Port ${options.port} is already in use.`;
return Promise.reject(new SilentError(message));
}
options.port = foundPort;
runBuild(options) {
const BuildTask = options.watch ? this.tasks.BuildWatch : this.tasks.Build;
const buildTask = new BuildTask({
ui: this.ui,
analytics: this.analytics,
project: this.project,
});
},
buildTask.run(options); // no return, BuildWatch.run blocks forever
},

checkPort(options) {
return this.getPort({ port: options.port, host: options.host })
.then((foundPort) => {
if (options.port !== foundPort && options.port !== 0) {
const message = `Port ${options.port} is already in use.`;
return Promise.reject(new SilentError(message));
}
options.port = foundPort;
});
},

}
};
5 changes: 2 additions & 3 deletions lib/tasks/fastboot-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = CoreObject.extend({
run(options) {
debug('run');
const restart = () => this.restart(options);
process.on('SIGHUP', restart);
this.addon.on('postBuild', restart);
},

start(options) {
Expand Down Expand Up @@ -120,6 +120,5 @@ module.exports = CoreObject.extend({
delete require.cache[key];
}
});
},

}
});
2 changes: 1 addition & 1 deletion test/lib-commands-fastboot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const CoreObject = require('core-object');
const camelize = require('ember-cli-string-utils').camelize;
const defaults = require('lodash.defaults');
const expect = require('chai').expect;
const FastbootCommand = CoreObject.extend(require('../lib/commands/fastboot'));
const FastbootCommand = CoreObject.extend(require('../lib/commands/fastboot')());
const RSVP = require('rsvp');

function CommandOptions(options) {
Expand Down

0 comments on commit f24a1f5

Please sign in to comment.