Skip to content

Commit da255b0

Browse files
delastevehansl
authored andcommitted
feat(serve): Persist serve options in angular-cli.json (angular#3908)
Closes angular#1156
1 parent 5852783 commit da255b0

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

packages/angular-cli/commands/serve.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { BuildOptions } from '../models/webpack-config';
22
import { BaseBuildCommandOptions } from './build';
3-
3+
import { CliConfig } from '../models/config';
44
const PortFinder = require('portfinder');
55
const Command = require('../ember-cli/lib/models/command');
6+
const config = CliConfig.fromProject() || CliConfig.fromGlobal();
67

78
PortFinder.basePort = 49152;
89

9-
const defaultPort = process.env.PORT || 4200;
10+
const defaultPort = process.env.PORT || config.get('defaults.serve.port');
11+
const defaultHost = config.get('defaults.serve.host');
1012

1113
export interface ServeTaskOptions extends BuildOptions {
1214
port?: number;
@@ -34,9 +36,9 @@ const ServeCommand = Command.extend({
3436
{
3537
name: 'host',
3638
type: String,
37-
default: 'localhost',
39+
default: defaultHost,
3840
aliases: ['H'],
39-
description: 'Listens only on localhost by default'
41+
description: `Listens only on ${defaultHost} by default`
4042
},
4143
{ name: 'proxy-config', type: 'Path', aliases: ['pc'] },
4244
{ name: 'live-reload', type: Boolean, default: true, aliases: ['lr'] },

packages/angular-cli/lib/config/schema.json

+16
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,22 @@
273273
"default": true
274274
}
275275
}
276+
},
277+
"serve": {
278+
"description": "Properties to be passed to the serve command",
279+
"type": "object",
280+
"properties": {
281+
"port": {
282+
"description": "The port the application will be served on",
283+
"type": "number",
284+
"default": 4200
285+
},
286+
"host": {
287+
"description": "The host the application will be served on",
288+
"type": "string",
289+
"default": "localhost"
290+
}
291+
}
276292
}
277293
},
278294
"additionalProperties": false

tests/e2e/tests/misc/default-port.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { request } from '../../utils/http';
2+
import { killAllProcesses } from '../../utils/process';
3+
import { ngServe } from '../../utils/project';
4+
import { updateJsonFile } from '../../utils/project';
5+
6+
export default function() {
7+
return Promise.resolve()
8+
.then(() => updateJsonFile('angular-cli.json', configJson => {
9+
const app = configJson.defaults;
10+
app.serve = { port: 4201 };
11+
}))
12+
.then(() => ngServe())
13+
.then(() => request('http://localhost:4201/'))
14+
.then(body => {
15+
if (!body.match(/<app-root>Loading...<\/app-root>/)) {
16+
throw new Error('Response does not match expected value.');
17+
}
18+
})
19+
.then(() => killAllProcesses(), (err) => { killAllProcesses(); throw err; });
20+
}

0 commit comments

Comments
 (0)