Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(@angular/cli): use check port utility function in serve command #4898

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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