Skip to content

Commit b498549

Browse files
itslennyhansl
authored andcommitted
feat(@angular/cli): allow setting ssl certificate in angular-cli.json (#4730)
Currently users must use the --ssl, -ssl-cert, -ssl-key flags to run the server using an ssl certificate. This update allows users to set those options in default.serve so they can just run `ng serve` without any flags.
1 parent 4b32bb4 commit b498549

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

packages/@angular/cli/commands/serve.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ const Command = require('../ember-cli/lib/models/command');
1111
const config = CliConfig.fromProject() || CliConfig.fromGlobal();
1212
const defaultPort = process.env.PORT || config.get('defaults.serve.port');
1313
const defaultHost = config.get('defaults.serve.host');
14+
const defaultSsl = config.get('defaults.serve.ssl');
15+
const defaultSslKey = config.get('defaults.serve.sslKey');
16+
const defaultSslCert = config.get('defaults.serve.sslCert');
1417

1518
export interface ServeTaskOptions extends BuildOptions {
1619
port?: number;
@@ -37,9 +40,9 @@ export const baseServeCommandOptions: any = overrideOptions(
3740
description: `Listens only on ${defaultHost} by default`
3841
},
3942
{ name: 'proxy-config', type: 'Path', aliases: ['pc'] },
40-
{ name: 'ssl', type: Boolean, default: false },
41-
{ name: 'ssl-key', type: String, default: 'ssl/server.key' },
42-
{ name: 'ssl-cert', type: String, default: 'ssl/server.crt' },
43+
{ name: 'ssl', type: Boolean, default: defaultSsl },
44+
{ name: 'ssl-key', type: String, default: defaultSslKey },
45+
{ name: 'ssl-cert', type: String, default: defaultSslCert },
4346
{
4447
name: 'open',
4548
type: Boolean,

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,24 @@
431431
"description": "The host the application will be served on",
432432
"type": "string",
433433
"default": "localhost"
434+
435+
},
436+
"ssl": {
437+
"description": "Enables ssl for the application",
438+
"type": "boolean",
439+
"default": false
440+
441+
},
442+
"sslKey": {
443+
"description": "The ssl key used by the server",
444+
"type": "string",
445+
"default": "ssl/server.key"
446+
447+
},
448+
"sslCert": {
449+
"description": "The ssl certificate used by the server",
450+
"type": "string",
451+
"default": "ssl/server.crt"
434452
}
435453
}
436454
}
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 = { ssl: true };
11+
}))
12+
.then(() => ngServe())
13+
.then(() => request('https://localhost:4200/'))
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+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { request } from '../../utils/http';
2+
import { assetDir } from '../../utils/assets';
3+
import { killAllProcesses } from '../../utils/process';
4+
import { ngServe } from '../../utils/project';
5+
import { updateJsonFile } from '../../utils/project';
6+
7+
export default function() {
8+
return Promise.resolve()
9+
.then(() => updateJsonFile('.angular-cli.json', configJson => {
10+
const app = configJson.defaults;
11+
app.serve = {
12+
ssl: true,
13+
sslKey: assetDir('ssl/server.key'),
14+
sslCert: assetDir('ssl/server.crt')
15+
};
16+
}))
17+
.then(() => ngServe())
18+
.then(() => request('https://localhost:4200/'))
19+
.then(body => {
20+
if (!body.match(/<app-root>Loading...<\/app-root>/)) {
21+
throw new Error('Response does not match expected value.');
22+
}
23+
})
24+
.then(() => killAllProcesses(), (err) => { killAllProcesses(); throw err; });
25+
26+
}

0 commit comments

Comments
 (0)