Skip to content

Commit 7062118

Browse files
committed
feat(serve): Persist serve options in angular-cli.json
1 parent 338e69b commit 7062118

File tree

4 files changed

+62
-9
lines changed

4 files changed

+62
-9
lines changed

packages/angular-cli/commands/serve.ts

Lines changed: 11 additions & 6 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();
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;
@@ -42,9 +47,9 @@ const ServeCommand = Command.extend({
4247
{
4348
name: 'host',
4449
type: String,
45-
default: 'localhost',
50+
default: defaultHost,
4651
aliases: ['H'],
47-
description: 'Listens only on localhost by default'
52+
description: `Listens only on ${defaultHost} by default`
4853
},
4954
{ name: 'proxy-config', type: 'Path', aliases: ['pc'] },
5055
{ name: 'watcher', type: String, default: 'events', aliases: ['w'] },
@@ -65,7 +70,7 @@ const ServeCommand = Command.extend({
6570
name: 'live-reload-port',
6671
type: Number,
6772
aliases: ['lrp'],
68-
description: '(Defaults to port number within [49152...65535])'
73+
description: `Finds the first open port number within [${defaultLiveReloadPort}...65535]`
6974
},
7075
{
7176
name: 'live-reload-live-css',
@@ -103,7 +108,7 @@ const ServeCommand = Command.extend({
103108
},
104109
{ name: 'i18n-file', type: String, default: null },
105110
{ name: 'i18n-format', type: String, default: null },
106-
{ name: 'locale', type: String, default: null }
111+
{ name: 'locale', type: String, default: null }
107112
],
108113

109114
run: function(commandOptions: ServeTaskOptions) {

packages/angular-cli/lib/config/schema.d.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface CliConfig {
1212
apps?: {
1313
root?: string;
1414
outDir?: string;
15-
assets?: string;
15+
assets?: string | string[];
1616
deployUrl?: string;
1717
index?: string;
1818
main?: string;
@@ -23,11 +23,17 @@ export interface CliConfig {
2323
/**
2424
* Global styles to be included in the build.
2525
*/
26-
styles?: string[];
26+
styles?: (string | {
27+
[name: string]: any;
28+
input?: string;
29+
})[];
2730
/**
2831
* Global scripts to be included in the build.
2932
*/
30-
scripts?: string[];
33+
scripts?: (string | {
34+
[name: string]: any;
35+
input?: string;
36+
})[];
3137
/**
3238
* Name and corresponding file for environment config.
3339
*/
@@ -75,5 +81,10 @@ export interface CliConfig {
7581
pipe?: boolean;
7682
service?: boolean;
7783
};
84+
serve?: {
85+
port?: number;
86+
liveReloadPort?: number;
87+
host?: string;
88+
};
7889
};
7990
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,23 @@
227227
"default": true
228228
}
229229
}
230+
},
231+
"serve": {
232+
"type": "object",
233+
"properties": {
234+
"port": {
235+
"type": "number",
236+
"default": 4200
237+
},
238+
"liveReloadPort": {
239+
"type": "number",
240+
"default": 49152
241+
},
242+
"host": {
243+
"type": "string",
244+
"default": "localhost"
245+
}
246+
}
230247
}
231248
},
232249
"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']['serve'];
10+
app['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)