diff --git a/packages/angular-cli/commands/serve.ts b/packages/angular-cli/commands/serve.ts index 0822b41ce1df..e7ba4b7c94ab 100644 --- a/packages/angular-cli/commands/serve.ts +++ b/packages/angular-cli/commands/serve.ts @@ -1,12 +1,14 @@ import { BuildOptions } from '../models/webpack-config'; import { BaseBuildCommandOptions } from './build'; - +import { CliConfig } from '../models/config'; const PortFinder = require('portfinder'); const Command = require('../ember-cli/lib/models/command'); +const config = CliConfig.fromProject() || CliConfig.fromGlobal(); PortFinder.basePort = 49152; -const defaultPort = process.env.PORT || 4200; +const defaultPort = process.env.PORT || config.get('defaults.serve.port'); +const defaultHost = config.get('defaults.serve.host'); export interface ServeTaskOptions extends BuildOptions { port?: number; @@ -34,9 +36,9 @@ const ServeCommand = Command.extend({ { name: 'host', type: String, - default: 'localhost', + default: defaultHost, aliases: ['H'], - description: 'Listens only on localhost by default' + description: `Listens only on ${defaultHost} by default` }, { name: 'proxy-config', type: 'Path', aliases: ['pc'] }, { name: 'live-reload', type: Boolean, default: true, aliases: ['lr'] }, diff --git a/packages/angular-cli/lib/config/schema.json b/packages/angular-cli/lib/config/schema.json index 67b0473f88e5..905b198635d2 100644 --- a/packages/angular-cli/lib/config/schema.json +++ b/packages/angular-cli/lib/config/schema.json @@ -247,6 +247,19 @@ "default": true } } + }, + "serve": { + "type": "object", + "properties": { + "port": { + "type": "number", + "default": 4200 + }, + "host": { + "type": "string", + "default": "localhost" + } + } } }, "additionalProperties": false diff --git a/tests/e2e/tests/misc/default-port.ts b/tests/e2e/tests/misc/default-port.ts new file mode 100644 index 000000000000..d3de7d535828 --- /dev/null +++ b/tests/e2e/tests/misc/default-port.ts @@ -0,0 +1,20 @@ +import { request } from '../../utils/http'; +import { killAllProcesses } from '../../utils/process'; +import { ngServe } from '../../utils/project'; +import { updateJsonFile } from '../../utils/project'; + +export default function() { + return Promise.resolve() + .then(() => updateJsonFile('angular-cli.json', configJson => { + const app = configJson.defaults; + app.serve = { port: 4201 }; + })) + .then(() => ngServe()) + .then(() => request('http://localhost:4201/')) + .then(body => { + if (!body.match(/Loading...<\/app-root>/)) { + throw new Error('Response does not match expected value.'); + } + }) + .then(() => killAllProcesses(), (err) => { killAllProcesses(); throw err; }); +}