Skip to content

Commit 339c8b5

Browse files
committed
feat(serve): Persist serve options in angular-cli.json
1 parent e5ef996 commit 339c8b5

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

packages/angular-cli/commands/serve.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
const PortFinder = require('portfinder');
22
const Command = require('../ember-cli/lib/models/command');
3+
import { CliConfig } from '../models/config';
34

4-
PortFinder.basePort = 49152;
5+
const config = CliConfig.fromProject() || CliConfig.fromGlobal();
56

6-
const defaultPort = process.env.PORT || 4200;
7+
const defaultLiveReloadPort = config.get('defaults.serve.liveReloadPort');
8+
const defaultPort = process.env.PORT || config.get('defaults.serve.port');
9+
const defaultHost = config.get('defaults.serve.host');
10+
11+
PortFinder.basePort = defaultLiveReloadPort;
712

813
export interface ServeTaskOptions {
914
port?: number;
@@ -43,9 +48,9 @@ const ServeCommand = Command.extend({
4348
{
4449
name: 'host',
4550
type: String,
46-
default: 'localhost',
51+
default: defaultHost,
4752
aliases: ['H'],
48-
description: 'Listens only on localhost by default'
53+
description: `Listens only on ${defaultHost} by default`
4954
},
5055
{ name: 'proxy-config', type: 'Path', aliases: ['pc'] },
5156
{ name: 'watcher', type: String, default: 'events', aliases: ['w'] },
@@ -66,7 +71,7 @@ const ServeCommand = Command.extend({
6671
name: 'live-reload-port',
6772
type: Number,
6873
aliases: ['lrp'],
69-
description: '(Defaults to port number within [49152...65535])'
74+
description: `Finds the first open port number within [${defaultLiveReloadPort}...65535]`
7075
},
7176
{
7277
name: 'live-reload-live-css',

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,23 @@
243243
"default": true
244244
}
245245
}
246+
},
247+
"serve": {
248+
"type": "object",
249+
"properties": {
250+
"port": {
251+
"type": "number",
252+
"default": 4200
253+
},
254+
"liveReloadPort": {
255+
"type": "number",
256+
"default": 49152
257+
},
258+
"host": {
259+
"type": "string",
260+
"default": "localhost"
261+
}
262+
}
246263
}
247264
},
248265
"additionalProperties": false

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

Lines changed: 20 additions & 0 deletions
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)