-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Expand file tree
/
Copy pathserve.ts
More file actions
163 lines (142 loc) · 4.74 KB
/
serve.ts
File metadata and controls
163 lines (142 loc) · 4.74 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import * as denodeify from 'denodeify';
import { BuildOptions } from '../models/build-options';
import { baseBuildCommandOptions } from './build';
import { CliConfig } from '../models/config';
import { Version } from '../upgrade/version';
import { ServeTaskOptions } from './serve';
const SilentError = require('silent-error');
const PortFinder = require('portfinder');
const Command = require('../ember-cli/lib/models/command');
const getPort = <any>denodeify(PortFinder.getPort);
PortFinder.basePort = 49152;
const config = CliConfig.fromProject() || CliConfig.fromGlobal();
const defaultPort = process.env.PORT || config.get('defaults.serve.port');
const defaultHost = config.get('defaults.serve.host');
export interface ServeTaskOptions extends BuildOptions {
watch?: boolean;
port?: number;
host?: string;
proxyConfig?: string;
liveReload?: boolean;
liveReloadHost?: string;
liveReloadPort?: number;
liveReloadBaseUrl?: string;
liveReloadLiveCss?: boolean;
ssl?: boolean;
sslKey?: string;
sslCert?: string;
open?: boolean;
hmr?: boolean;
}
// Expose options unrelated to live-reload to other commands that need to run serve
export const baseServeCommandOptions: any = baseBuildCommandOptions.concat([
{ name: 'port', type: Number, default: defaultPort, aliases: ['p'] },
{
name: 'host',
type: String,
default: defaultHost,
aliases: ['H'],
description: `Listens only on ${defaultHost} by default`
},
{ name: 'proxy-config', type: 'Path', aliases: ['pc'] },
{ name: 'watch', type: Boolean, default: true, aliases: ['w'] },
{ name: 'ssl', type: Boolean, default: false },
{ name: 'ssl-key', type: String, default: 'ssl/server.key' },
{ name: 'ssl-cert', type: String, default: 'ssl/server.crt' },
{
name: 'open',
type: Boolean,
default: false,
aliases: ['o'],
description: 'Opens the url in default browser',
}
]);
const ServeCommand = Command.extend({
name: 'serve',
description: 'Builds and serves your app, rebuilding on file changes.',
aliases: ['server', 's'],
availableOptions: baseServeCommandOptions.concat([
{
name: 'live-reload',
type: Boolean,
aliases: ['lr'],
description: 'Defaults to watch'
},
{
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: 'live-reload-live-css',
type: Boolean,
default: true,
description: 'Whether to live reload CSS (default true)'
},
{
name: 'hmr',
type: Boolean,
default: false,
description: 'Enable hot module replacement',
}
]),
run: function (commandOptions: ServeTaskOptions) {
const ServeTask = require('../tasks/serve').default;
Version.assertAngularVersionIs2_3_1OrHigher(this.project.root);
commandOptions.liveReloadHost = commandOptions.liveReloadHost || commandOptions.host;
commandOptions.liveReload = commandOptions.liveReload || commandOptions.watch;
return checkExpressPort(commandOptions)
.then(() => autoFindLiveReloadPort(commandOptions))
.then((opts: ServeTaskOptions) => {
const serve = new ServeTask({
ui: this.ui,
project: this.project,
});
return serve.run(opts);
});
}
});
function checkExpressPort(commandOptions: ServeTaskOptions) {
return getPort({ port: commandOptions.port, host: commandOptions.host })
.then((foundPort: number) => {
if (commandOptions.port !== foundPort && commandOptions.port !== 0) {
throw new SilentError(
`Port ${commandOptions.port} is already in use. Use '--port' to specify a different port.`
);
}
// otherwise, our found port is good
commandOptions.port = foundPort;
return commandOptions;
});
}
function autoFindLiveReloadPort(commandOptions: ServeTaskOptions) {
return getPort({ port: commandOptions.liveReloadPort, host: commandOptions.liveReloadHost })
.then((foundPort: number) => {
// if live reload port matches express port, try one higher
if (foundPort === commandOptions.port) {
commandOptions.liveReloadPort = foundPort + 1;
return autoFindLiveReloadPort(commandOptions);
}
// port was already open
if (foundPort === commandOptions.liveReloadPort) {
return commandOptions;
}
// use found port as live reload port
commandOptions.liveReloadPort = foundPort;
return commandOptions;
});
}
export default ServeCommand;