Skip to content

Commit

Permalink
refactor(@angular/cli): use check port utility function in serve command
Browse files Browse the repository at this point in the history
Close #4898
  • Loading branch information
Charles Lyding authored and filipesilva committed Feb 22, 2017
1 parent 3bba4cb commit d79fd00
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 31 deletions.
30 changes: 5 additions & 25 deletions packages/@angular/cli/commands/serve.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
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';
import { checkPort } from '../utilities/check-port';
import { overrideOptions } from '../utilities/override-options';

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

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

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

Version.assertAngularVersionIs2_3_1OrHigher(this.project.root);
return checkPort(commandOptions.port, commandOptions.host, defaultPort)
.then(port => {
commandOptions.port = port;

return checkExpressPort(commandOptions)
.then((opts: ServeTaskOptions) => {
const serve = new ServeTask({
ui: this.ui,
project: this.project,
});

return serve.run(opts);
return serve.run(commandOptions);
});
}
});

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;

});
}

export default ServeCommand;
10 changes: 4 additions & 6 deletions packages/@angular/cli/utilities/check-port.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import * as denodeify from 'denodeify';

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

PortFinder.basePort = 49152;


export function checkPort(port: number, host: string) {
export function checkPort(port: number, host: string, basePort = 49152): Promise<number> {
PortFinder.basePort = basePort;
return getPort({ port, host })
.then((foundPort: number) => {
.then(foundPort => {

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

0 comments on commit d79fd00

Please sign in to comment.