Skip to content

Commit

Permalink
feat(@angular/cli): read proxyConfig from angular-cli.json
Browse files Browse the repository at this point in the history
easy proxy config by reading default from angular-cli.json (#6240)
  • Loading branch information
ayvazj authored and hansl committed Jul 12, 2017
1 parent 79b32ec commit af22242
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/documentation/angular-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
- *ssl* (`boolean`): Enables ssl for the application. Default is `false`.
- *sslKey* (`string`): The ssl key used by the server. Default is `ssl/server.key`.
- *sslCert* (`string`): The ssl certificate used by the server. Default is `ssl/server.crt`.
- *proxyConfig* (`string`): Proxy configuration file.

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

Expand Down
2 changes: 2 additions & 0 deletions packages/@angular/cli/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const defaultHost = config.get('defaults.serve.host');
const defaultSsl = config.get('defaults.serve.ssl');
const defaultSslKey = config.get('defaults.serve.sslKey');
const defaultSslCert = config.get('defaults.serve.sslCert');
const defaultProxyConfig = config.get('defaults.serve.proxyConfig');

export interface ServeTaskOptions extends BuildOptions {
port?: number;
Expand Down Expand Up @@ -49,6 +50,7 @@ export const baseServeCommandOptions: any = overrideOptions([
{
name: 'proxy-config',
type: 'Path',
default: defaultProxyConfig,
aliases: ['pc'],
description: 'Proxy configuration file.'
},
Expand Down
4 changes: 4 additions & 0 deletions packages/@angular/cli/lib/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,10 @@
"description": "The ssl certificate used by the server.",
"type": "string",
"default": "ssl/server.crt"
},
"proxyConfig": {
"description": "Proxy configuration file.",
"type": "string"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {writeFile} from '../../utils/fs';
import {request} from '../../utils/http';
import {killAllProcesses, ng} from '../../utils/process';
import {ngServe} from '../../utils/project';
import {expectToFail} from '../../utils/utils';

import {updateJsonFile} from '../../utils/project';
import {expectToFail} from "../../utils/utils";

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

return Promise.resolve()
.then(() => writeFile(proxyConfigFile, JSON.stringify(proxyConfig, null, 2)))
.then(() => ngServe('--proxy', proxyConfigFile))
.then(() => ngServe('--proxy-config', proxyConfigFile))
.then(() => request('http://localhost:4200/api/test'))
.then(body => {
if (!body.match(/TEST_API_RETURN/)) {
throw new Error('Response does not match expected value.');
}
})
.then(() => killAllProcesses(), (err) => { killAllProcesses(); throw err; })

.then(() => updateJsonFile('.angular-cli.json', configJson => {
const app = configJson.defaults;
app.serve = {
proxyConfig: proxyConfigFile
};
}))
.then(() => ngServe())
.then(() => request('http://localhost:4200/api/test'))
.then(body => {
if (!body.match(/TEST_API_RETURN/)) {
throw new Error('Response does not match expected value.');
}
})
.then(() => server.close(), (err) => { server.close(); throw err; })
.then(() => killAllProcesses(), (err) => { killAllProcesses(); throw err; })

.then(() => server.close(), (err) => { server.close(); throw err; })

// A non-existing proxy file should error.
.then(() => expectToFail(() => ng('serve', '--proxy', 'proxy.non-existent.json')));
.then(() => expectToFail(() => ng('serve', '--proxy-config', 'proxy.non-existent.json')))
.then(() => updateJsonFile('.angular-cli.json', configJson => {
const app = configJson.defaults;
app.serve = {
proxyConfig: 'proxy.non-existent.json'
};
}))
.then(() => expectToFail(() => ng('serve')));
}

0 comments on commit af22242

Please sign in to comment.