Skip to content

Commit 537815c

Browse files
committed
refactor: build and serve commands
1 parent 7bc20ff commit 537815c

File tree

11 files changed

+507
-4
lines changed

11 files changed

+507
-4
lines changed

addon/ng2/commands/build.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import * as Command from 'ember-cli/lib/models/command';
2+
import * as win from 'ember-cli/lib/utilities/windows-admin';
3+
import * as Build from '../tasks/build';
4+
import * as BuildWatch from '../tasks/build-watch';
5+
6+
module.exports = Command.extend({
7+
name: 'build',
8+
description: 'Builds your app and places it into the output path (dist/ by default).',
9+
aliases: ['b'],
10+
11+
availableOptions: [
12+
{ name: 'environment', type: String, default: 'development', aliases: ['e', { 'dev': 'development' }, { 'prod': 'production' }] },
13+
{ name: 'output-path', type: 'Path', default: 'dist/', aliases: ['o'] },
14+
{ name: 'watch', type: Boolean, default: false, aliases: ['w'] },
15+
{ name: 'watcher', type: String },
16+
{ name: 'suppress-sizes', type: Boolean, default: false }
17+
],
18+
19+
run: function(commandOptions) {
20+
const BuildTask = this.taskFor(commandOptions);
21+
const buildTask = new BuildTask({
22+
ui: this.ui,
23+
analytics: this.analytics,
24+
project: this.project
25+
});
26+
27+
return win.checkWindowsElevation(this.ui).then(function () {
28+
return buildTask.run(commandOptions);
29+
});
30+
},
31+
32+
taskFor: function(options) {
33+
if (options.watch) {
34+
return BuildWatch;
35+
} else {
36+
return Build;
37+
}
38+
}
39+
});
40+
41+
module.exports.overrideCore = true;

addon/ng2/commands/serve.ts

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import * as assign from 'lodash/assign';
2+
import * as Command from 'ember-cli/lib/models/command';
3+
import * as Serve from '../tasks/serve';
4+
import * as Promise from 'ember-cli/lib/ext/promise');
5+
import * as SilentError from 'silent-error';
6+
import * as PortFinder from 'portfinder';
7+
import * as win from 'ember-cli/lib/utilities/windows-admin';
8+
import * as EOL from 'os'.EOL;
9+
10+
PortFinder.basePort = 49152;
11+
12+
let getPort = Promise.denodeify(PortFinder.getPort);
13+
let defaultPort = process.env.PORT || 4200;
14+
15+
const ServeCommand = Command.extend({
16+
name: 'serve',
17+
description: 'Builds and serves your Angular2 app, rebuilding on file changes.',
18+
aliases: ['server', 's'],
19+
20+
availableOptions: [
21+
{ name: 'port', type: Number, default: defaultPort, aliases: ['p'] },
22+
{ name: 'host', type: String, aliases: ['H'], description: 'Listens on all interfaces by default' },
23+
{ name: 'proxy', type: String, aliases: ['pr', 'pxy'] },
24+
{ name: 'insecure-proxy', type: Boolean, default: false, aliases: ['inspr'], description: 'Set false to proxy self-signed SSL certificates' },
25+
{ name: 'watcher', type: String, default: 'events', aliases: ['w'] },
26+
{ name: 'live-reload', type: Boolean, default: true, aliases: ['lr'] },
27+
{ name: 'live-reload-host', type: String, aliases: ['lrh'], description: 'Defaults to host' },
28+
{ name: 'live-reload-base-url', type: String, aliases: ['lrbu'], description: 'Defaults to baseURL' },
29+
{ name: 'live-reload-port', type: Number, aliases: ['lrp'], description: '(Defaults to port number within [49152...65535])' },
30+
{ name: 'environment', type: String, default: 'development', aliases: ['e', { 'dev': 'development' }, { 'prod': 'production' }] },
31+
{ name: 'output-path', type: 'Path', default: 'dist/', aliases: ['op', 'out'] },
32+
{ name: 'ssl', type: Boolean, default: false },
33+
{ name: 'ssl-key', type: String, default: 'ssl/server.key' },
34+
{ name: 'ssl-cert', type: String, default: 'ssl/server.crt' }
35+
],
36+
37+
run: function(commandOptions) {
38+
commandOptions.liveReloadHost = commandOptions.liveReloadHost || commandOptions.host;
39+
40+
return this._checkExpressPort(commandOptions)
41+
.then(this._autoFindLiveReloadPort.bind(this))
42+
.then(function(commandOptions) {
43+
commandOptions = assign({}, commandOptions, {
44+
baseURL: this.project.config(commandOptions.environment).baseURL || '/'
45+
});
46+
47+
if (commandOptions.proxy) {
48+
if (!commandOptions.proxy.match(/^(http:|https:)/)) {
49+
var message = 'You need to include a protocol with the proxy URL.' + EOL + 'Try --proxy http://' + commandOptions.proxy;
50+
51+
return Promise.reject(new SilentError(message));
52+
}
53+
}
54+
55+
var ServeTask = Serve;
56+
var serve = new ServeTask({
57+
ui: this.ui,
58+
analytics: this.analytics,
59+
project: this.project
60+
});
61+
62+
return win.checkWindowsElevation(this.ui).then(function() {
63+
return serve.run(commandOptions);
64+
});
65+
}.bind(this));
66+
},
67+
68+
_checkExpressPort: function(commandOptions) {
69+
return getPort({ port: commandOptions.port, host: commandOptions.host })
70+
.then(function(foundPort) {
71+
72+
if (commandOptions.port !== foundPort && commandOptions.port !== 0) {
73+
var message = 'Port ' + commandOptions.port + ' is already in use.';
74+
return Promise.reject(new SilentError(message));
75+
}
76+
77+
commandOptions.port = foundPort;
78+
return commandOptions;
79+
80+
}.bind(this));
81+
},
82+
83+
_autoFindLiveReloadPort: function(commandOptions) {
84+
return getPort({ port: commandOptions.liveReloadPort, host: commandOptions.liveReloadHost })
85+
.then(function(foundPort) {
86+
87+
if (foundPort === commandOptions.port) {
88+
commandOptions.liveReloadPort = foundPort + 1;
89+
return this._autoFindLiveReloadPort(commandOptions);
90+
}
91+
92+
if (foundPort === commandOptions.liveReloadPort) {
93+
return commandOptions;
94+
}
95+
96+
commandOptions.liveReloadPort = foundPort;
97+
return commandOptions;
98+
99+
}.bind(this));
100+
}
101+
102+
});
103+
104+
module.exports = ServeCommand;
105+
module.exports.overrideCore = true;

addon/ng2/commands/test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
var Promise = require('ember-cli/lib/ext/promise');
44
var TestCommand = require('ember-cli/lib/commands/test');
55
var win = require('ember-cli/lib/utilities/windows-admin');
6-
var BuildTask = require('ember-cli/lib/tasks/build');
7-
var BuildWatchTask = require('ember-cli/lib/tasks/build-watch');
6+
var BuildTask = require('../tasks/build');
7+
var BuildWatchTask = require('../tasks/build-watch');
88
var TestTask = require('../tasks/test');
99

1010

addon/ng2/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ module.exports = {
1414
'format': require('./commands/format'),
1515
'version': require('./commands/version'),
1616
'completion': require('./commands/completion'),
17-
'doc': require('./commands/doc')
17+
'doc': require('./commands/doc'),
18+
'serve': require('./commands/serve'),
19+
'build': require('./commands/build')
1820
};
1921
}
2022
};

addon/ng2/tasks/build-watch.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as chalk from 'chalk';
2+
import * as Task from 'ember-cli/lib/models/task';
3+
import * as Watcher from 'ember-cli/lib/models/watcher';
4+
import * as Builder from './builder';
5+
import * as Promise from 'ember-cli/lib/ext/promise';
6+
7+
module.exports = Task.extend({
8+
run: function(options) {
9+
this.ui.startProgress(
10+
chalk.green('Building'), chalk.green('.')
11+
);
12+
13+
return new Watcher({
14+
ui: this.ui,
15+
builder: new Builder({
16+
ui: this.ui,
17+
outputPath: options.outputPath,
18+
environment: options.environment,
19+
project: this.project
20+
}),
21+
analytics: this.analytics,
22+
options: options
23+
}).then(function() {
24+
return new Promise(function () {});
25+
});
26+
}
27+
});

addon/ng2/tasks/build.ts

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import * as chalk from 'chalk';
2+
import * as Task from 'ember-cli/lib/models/task';
3+
import * as Builder from './builder';
4+
5+
module.exports = Task.extend({
6+
run: function(options) {
7+
const ui = this.ui;
8+
const analytics = this.analytics;
9+
10+
ui.startProgress(chalk.green('Building'), chalk.green('.'));
11+
12+
const builder = new Builder({
13+
ui: ui,
14+
outputPath: options.outputPath,
15+
environment: options.environment,
16+
project: this.project
17+
});
18+
19+
let totalTime;
20+
21+
return builder.build()
22+
.then(function(results) {
23+
totalTime = parseInt(results.totalTime / 1e6, 10);
24+
25+
analytics.track({
26+
name: 'angular build',
27+
message: totalTime + 'ms'
28+
});
29+
30+
analytics.trackTiming({
31+
category: 'rebuild',
32+
variable: 'build time',
33+
label: 'broccoli build time',
34+
value: totalTime
35+
});
36+
})
37+
.finally(function() {
38+
ui.stopProgress();
39+
return builder.cleanup();
40+
})
41+
.then(function() {
42+
ui.writeLine(chalk.green(`Built Angular2 project successfully in ${totalTime}ms. Stored in "` +
43+
options.outputPath + '".'));
44+
})
45+
.catch(function(err) {
46+
ui.writeLine(chalk.red('Build failed.'));
47+
48+
throw err;
49+
});
50+
}
51+
});

0 commit comments

Comments
 (0)