forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserve.run.ts
67 lines (53 loc) · 2.08 KB
/
serve.run.ts
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
import * as denodeify from 'denodeify';
const assign = require('lodash/assign');
const SilentError = require('silent-error');
const PortFinder = require('portfinder');
import ServeTask from '../tasks/serve';
import { Version } from '../upgrade/version';
import { ServeTaskOptions } from './serve';
PortFinder.basePort = 49152;
const getPort = <any>denodeify(PortFinder.getPort);
export default function serveRun(commandOptions: ServeTaskOptions) {
// Check angular version.
Version.assertAngularVersionIs2_3_1OrHigher(this.project.root);
commandOptions.liveReloadHost = commandOptions.liveReloadHost || commandOptions.host;
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;
});
}