Skip to content

Commit c482605

Browse files
Charles LydingZhicheng Wang
Charles Lyding
authored and
Zhicheng Wang
committed
refactor(@angular/cli): use check port utility function in serve command
Close angular#4898
1 parent 1fa936c commit c482605

File tree

2 files changed

+9
-31
lines changed

2 files changed

+9
-31
lines changed

packages/@angular/cli/commands/serve.ts

+5-25
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
import * as denodeify from 'denodeify';
21
import { BuildOptions } from '../models/build-options';
32
import { baseBuildCommandOptions } from './build';
43
import { CliConfig } from '../models/config';
54
import { Version } from '../upgrade/version';
65
import { ServeTaskOptions } from './serve';
6+
import { checkPort } from '../utilities/check-port';
77
import { overrideOptions } from '../utilities/override-options';
88

9-
const SilentError = require('silent-error');
10-
const PortFinder = require('portfinder');
119
const Command = require('../ember-cli/lib/models/command');
12-
const getPort = denodeify<{ host: string, port: number }, number>(PortFinder.getPort);
1310

1411
const config = CliConfig.fromProject() || CliConfig.fromGlobal();
1512
const defaultPort = process.env.PORT || config.get('defaults.serve.port');
1613
const defaultHost = config.get('defaults.serve.host');
17-
PortFinder.basePort = defaultPort;
1814

1915
export interface ServeTaskOptions extends BuildOptions {
2016
port?: number;
@@ -79,34 +75,18 @@ const ServeCommand = Command.extend({
7975
const ServeTask = require('../tasks/serve').default;
8076

8177
Version.assertAngularVersionIs2_3_1OrHigher(this.project.root);
78+
return checkPort(commandOptions.port, commandOptions.host, defaultPort)
79+
.then(port => {
80+
commandOptions.port = port;
8281

83-
return checkExpressPort(commandOptions)
84-
.then((opts: ServeTaskOptions) => {
8582
const serve = new ServeTask({
8683
ui: this.ui,
8784
project: this.project,
8885
});
8986

90-
return serve.run(opts);
87+
return serve.run(commandOptions);
9188
});
9289
}
9390
});
9491

95-
function checkExpressPort(commandOptions: ServeTaskOptions) {
96-
return getPort({ port: commandOptions.port, host: commandOptions.host })
97-
.then((foundPort: number) => {
98-
99-
if (commandOptions.port !== foundPort && commandOptions.port !== 0) {
100-
throw new SilentError(
101-
`Port ${commandOptions.port} is already in use. Use '--port' to specify a different port.`
102-
);
103-
}
104-
105-
// otherwise, our found port is good
106-
commandOptions.port = foundPort;
107-
return commandOptions;
108-
109-
});
110-
}
111-
11292
export default ServeCommand;

packages/@angular/cli/utilities/check-port.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ import * as denodeify from 'denodeify';
22

33
const SilentError = require('silent-error');
44
const PortFinder = require('portfinder');
5-
const getPort = <any>denodeify(PortFinder.getPort);
5+
const getPort = denodeify<{host: string, port: number}, number>(PortFinder.getPort);
66

7-
PortFinder.basePort = 49152;
8-
9-
10-
export function checkPort(port: number, host: string) {
7+
export function checkPort(port: number, host: string, basePort = 49152): Promise<number> {
8+
PortFinder.basePort = basePort;
119
return getPort({ port, host })
12-
.then((foundPort: number) => {
10+
.then(foundPort => {
1311

1412
// If the port isn't available and we weren't looking for any port, throw error.
1513
if (port !== foundPort && port !== 0) {

0 commit comments

Comments
 (0)