forked from ember-cli/ember-cli
/
serve.js
117 lines (94 loc) · 4.84 KB
/
serve.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'use strict';
var assign = require('lodash/assign');
var Command = require('../models/command');
var Promise = require('../ext/promise');
var SilentError = require('silent-error');
var PortFinder = require('portfinder');
var win = require('../utilities/windows-admin');
var EOL = require('os').EOL;
PortFinder.basePort = 49152;
var getPort = Promise.denodeify(PortFinder.getPort);
var defaultPort = process.env.PORT || 4200;
module.exports = Command.extend({
name: 'serve',
description: 'Builds and serves your app, rebuilding on file changes.',
aliases: ['server', 's'],
availableOptions: [
{ name: 'port', type: Number, default: defaultPort, aliases: ['p'] },
{ name: 'host', type: String, aliases: ['H'], description: 'Listens on all interfaces by default' },
{ name: 'proxy', type: String, aliases: ['pr', 'pxy'] },
{ name: 'insecure-proxy', type: Boolean, default: false, aliases: ['inspr'], description: 'Set false to proxy self-signed SSL certificates' },
{ name: 'watcher', type: String, default: 'events', aliases: ['w'] },
{ name: 'live-reload', type: Boolean, default: true, aliases: ['lr'] },
{ name: 'live-reload-host', type: String, aliases: ['lrh'], description: 'Defaults to host' },
{ name: 'live-reload-base-url', type: String, aliases: ['lrbu'], description: 'Defaults to baseURL' },
{ name: 'live-reload-port', type: Number, aliases: ['lrp'], description: '(Defaults to port number within [49152...65535])' },
{ name: 'environment', type: String, default: 'development', aliases: ['e', { 'dev': 'development' }, { 'prod': 'production' }] },
{ name: 'output-path', type: 'Path', default: 'dist/', aliases: ['op', 'out'] },
{ name: 'ssl', type: Boolean, default: false },
{ name: 'ssl-key', type: String, default: 'ssl/server.key' },
{ name: 'ssl-cert', type: String, default: 'ssl/server.crt' }
],
run: function(commandOptions) {
commandOptions.liveReloadHost = commandOptions.liveReloadHost || commandOptions.host;
return this._checkExpressPort(commandOptions)
.then(this._autoFindLiveReloadPort.bind(this))
.then(function(commandOptions) {
var config = this.project.config(commandOptions.environment);
this.ui.writeDeprecateLine(
'Using the `baseURL` setting is deprecated, use `rootURL` instead.',
!(!config.rootURL && config.baseURL));
this.ui.writeWarnLine(
'The `baseURL` and `rootURL` settings should not be used at the same time.',
!(config.rootURL && config.baseURL));
commandOptions = assign({}, commandOptions, {
rootURL: config.rootURL,
baseURL: config.baseURL || '/'
});
if (commandOptions.proxy) {
if (!commandOptions.proxy.match(/^(http:|https:)/)) {
var message = 'You need to include a protocol with the proxy URL.' + EOL + 'Try --proxy http://' + commandOptions.proxy;
return Promise.reject(new SilentError(message));
}
}
var ServeTask = this.tasks.Serve;
var serve = new ServeTask({
ui: this.ui,
analytics: this.analytics,
project: this.project
});
return win.checkWindowsElevation(this.ui).then(function() {
return serve.run(commandOptions);
});
}.bind(this));
},
_checkExpressPort: function(commandOptions) {
return getPort({ port: commandOptions.port, host: commandOptions.host })
.then(function(foundPort) {
if (commandOptions.port !== foundPort && commandOptions.port !== 0) {
var message = 'Port ' + commandOptions.port + ' is already in use.';
return Promise.reject(new SilentError(message));
}
// otherwise, our found port is good
commandOptions.port = foundPort;
return commandOptions;
}.bind(this));
},
_autoFindLiveReloadPort: function(commandOptions) {
return getPort({ port: commandOptions.liveReloadPort, host: commandOptions.liveReloadHost })
.then(function(foundPort) {
// if live reload port matches express port, try one higher
if (foundPort === commandOptions.port) {
commandOptions.liveReloadPort = foundPort + 1;
return this._autoFindLiveReloadPort(commandOptions);
}
// port was already open
if (foundPort === commandOptions.liveReloadPort) {
return commandOptions;
}
// use found port as live reload port
commandOptions.liveReloadPort = foundPort;
return commandOptions;
}.bind(this));
}
});