Skip to content

Commit af22242

Browse files
ayvazjhansl
authored andcommitted
feat(@angular/cli): read proxyConfig from angular-cli.json
easy proxy config by reading default from angular-cli.json (#6240)
1 parent 79b32ec commit af22242

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

docs/documentation/angular-cli.md

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
- *ssl* (`boolean`): Enables ssl for the application. Default is `false`.
8484
- *sslKey* (`string`): The ssl key used by the server. Default is `ssl/server.key`.
8585
- *sslCert* (`string`): The ssl certificate used by the server. Default is `ssl/server.crt`.
86+
- *proxyConfig* (`string`): Proxy configuration file.
8687

8788
- **packageManager** (`string`): Specify which package manager tool to use. Options include `npm`, `cnpm` and `yarn`.
8889

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

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const defaultHost = config.get('defaults.serve.host');
1414
const defaultSsl = config.get('defaults.serve.ssl');
1515
const defaultSslKey = config.get('defaults.serve.sslKey');
1616
const defaultSslCert = config.get('defaults.serve.sslCert');
17+
const defaultProxyConfig = config.get('defaults.serve.proxyConfig');
1718

1819
export interface ServeTaskOptions extends BuildOptions {
1920
port?: number;
@@ -49,6 +50,7 @@ export const baseServeCommandOptions: any = overrideOptions([
4950
{
5051
name: 'proxy-config',
5152
type: 'Path',
53+
default: defaultProxyConfig,
5254
aliases: ['pc'],
5355
description: 'Proxy configuration file.'
5456
},

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

+4
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,10 @@
482482
"description": "The ssl certificate used by the server.",
483483
"type": "string",
484484
"default": "ssl/server.crt"
485+
},
486+
"proxyConfig": {
487+
"description": "Proxy configuration file.",
488+
"type": "string"
485489
}
486490
}
487491
}

tests/e2e/tests/misc/proxy.ts tests/e2e/tests/misc/proxy-config.ts

+28-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {writeFile} from '../../utils/fs';
55
import {request} from '../../utils/http';
66
import {killAllProcesses, ng} from '../../utils/process';
77
import {ngServe} from '../../utils/project';
8-
import {expectToFail} from '../../utils/utils';
9-
8+
import {updateJsonFile} from '../../utils/project';
9+
import {expectToFail} from "../../utils/utils";
1010

1111
export default function() {
1212
// Create an express app that serves as a proxy.
@@ -31,16 +31,39 @@ export default function() {
3131

3232
return Promise.resolve()
3333
.then(() => writeFile(proxyConfigFile, JSON.stringify(proxyConfig, null, 2)))
34-
.then(() => ngServe('--proxy', proxyConfigFile))
34+
.then(() => ngServe('--proxy-config', proxyConfigFile))
35+
.then(() => request('http://localhost:4200/api/test'))
36+
.then(body => {
37+
if (!body.match(/TEST_API_RETURN/)) {
38+
throw new Error('Response does not match expected value.');
39+
}
40+
})
41+
.then(() => killAllProcesses(), (err) => { killAllProcesses(); throw err; })
42+
43+
.then(() => updateJsonFile('.angular-cli.json', configJson => {
44+
const app = configJson.defaults;
45+
app.serve = {
46+
proxyConfig: proxyConfigFile
47+
};
48+
}))
49+
.then(() => ngServe())
3550
.then(() => request('http://localhost:4200/api/test'))
3651
.then(body => {
3752
if (!body.match(/TEST_API_RETURN/)) {
3853
throw new Error('Response does not match expected value.');
3954
}
4055
})
41-
.then(() => server.close(), (err) => { server.close(); throw err; })
4256
.then(() => killAllProcesses(), (err) => { killAllProcesses(); throw err; })
4357

58+
.then(() => server.close(), (err) => { server.close(); throw err; })
59+
4460
// A non-existing proxy file should error.
45-
.then(() => expectToFail(() => ng('serve', '--proxy', 'proxy.non-existent.json')));
61+
.then(() => expectToFail(() => ng('serve', '--proxy-config', 'proxy.non-existent.json')))
62+
.then(() => updateJsonFile('.angular-cli.json', configJson => {
63+
const app = configJson.defaults;
64+
app.serve = {
65+
proxyConfig: 'proxy.non-existent.json'
66+
};
67+
}))
68+
.then(() => expectToFail(() => ng('serve')));
4669
}

0 commit comments

Comments
 (0)