Skip to content

Commit e1211e5

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

File tree

5 files changed

+129
-77
lines changed

5 files changed

+129
-77
lines changed

packages/angular-cli/blueprints/ng2/files/angular-cli.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
"module": false,
5555
"pipe": true,
5656
"service": true
57+
},
58+
"serve": {
59+
"port": 4200,
60+
"liveReloadPort": 49152,
61+
"host": "localhost"
5762
}
5863
}
5964
}

packages/angular-cli/commands/serve.ts

Lines changed: 14 additions & 9 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') || 49152;
8+
const defaultPort = process.env.PORT || config.get('defaults.serve.port');
9+
const defaultHost = config.get('defaults.serve.host') || 'localhost';
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,12 +108,12 @@ 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

109-
run: function(commandOptions: ServeTaskOptions) {
110-
return require('./serve.run').default.call(this, commandOptions);
111-
}
114+
run: function(commandOptions: ServeTaskOptions) {
115+
return require('./serve.run').default.call(this, commandOptions);
116+
}
112117
});
113118

114119
export default ServeCommand;
Lines changed: 73 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,84 @@
11
export interface CliConfig {
2+
/**
3+
* The global configuration of the project.
4+
*/
5+
project?: {
6+
version?: string;
7+
name?: string;
8+
};
9+
/**
10+
* Properties of the different applications in this project.
11+
*/
12+
apps?: {
13+
root?: string;
14+
outDir?: string;
15+
assets?: string;
16+
deployUrl?: string;
17+
index?: string;
18+
main?: string;
19+
test?: string;
20+
tsconfig?: string;
21+
prefix?: string;
22+
mobile?: boolean;
223
/**
3-
* The global configuration of the project.
24+
* Global styles to be included in the build.
425
*/
5-
project?: {
6-
version?: string;
7-
name?: string;
8-
};
9-
/**
10-
* Properties of the different applications in this project.
11-
*/
12-
apps?: {
13-
root?: string;
14-
outDir?: string;
15-
assets?: string;
16-
deployUrl?: string;
17-
index?: string;
18-
main?: string;
19-
test?: string;
20-
tsconfig?: string;
21-
prefix?: string;
22-
mobile?: boolean;
23-
/**
24-
* Global styles to be included in the build.
25-
*/
26-
styles?: string[];
27-
/**
28-
* Global scripts to be included in the build.
29-
*/
30-
scripts?: string[];
31-
/**
32-
* Name and corresponding file for environment config.
33-
*/
34-
environments?: {
35-
[name: string]: any;
36-
};
37-
}[];
26+
styles?: string[];
3827
/**
39-
* Configuration reserved for installed third party addons.
28+
* Global scripts to be included in the build.
4029
*/
41-
addons?: {
42-
[name: string]: any;
43-
}[];
30+
scripts?: string[];
4431
/**
45-
* Configuration reserved for installed third party packages.
32+
* Name and corresponding file for environment config.
4633
*/
47-
packages?: {
48-
[name: string]: any;
49-
}[];
50-
e2e?: {
51-
protractor?: {
52-
config?: string;
53-
};
34+
environments?: {
35+
[name: string]: any;
36+
};
37+
}[];
38+
/**
39+
* Configuration reserved for installed third party addons.
40+
*/
41+
addons?: {
42+
[name: string]: any;
43+
}[];
44+
/**
45+
* Configuration reserved for installed third party packages.
46+
*/
47+
packages?: {
48+
[name: string]: any;
49+
}[];
50+
e2e?: {
51+
protractor?: {
52+
config?: string;
53+
};
54+
};
55+
test?: {
56+
karma?: {
57+
config?: string;
5458
};
55-
test?: {
56-
karma?: {
57-
config?: string;
58-
};
59+
};
60+
defaults?: {
61+
styleExt?: string;
62+
prefixInterfaces?: boolean;
63+
poll?: number;
64+
viewEncapsulation?: string;
65+
changeDetection?: string;
66+
inline?: {
67+
style?: boolean;
68+
template?: boolean;
5969
};
60-
defaults?: {
61-
styleExt?: string;
62-
prefixInterfaces?: boolean;
63-
poll?: number;
64-
viewEncapsulation?: string;
65-
changeDetection?: string;
66-
inline?: {
67-
style?: boolean;
68-
template?: boolean;
69-
};
70-
spec?: {
71-
class?: boolean;
72-
component?: boolean;
73-
directive?: boolean;
74-
module?: boolean;
75-
pipe?: boolean;
76-
service?: boolean;
77-
};
70+
spec?: {
71+
class?: boolean;
72+
component?: boolean;
73+
directive?: boolean;
74+
module?: boolean;
75+
pipe?: boolean;
76+
service?: boolean;
7877
};
78+
serve?: {
79+
port: number;
80+
liveReloadPort: number;
81+
host: string;
82+
}
83+
};
7984
}

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
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)