Skip to content

Commit

Permalink
Add support for APP_ENV (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
iansu authored Nov 28, 2019
1 parent e3c78a6 commit 686d756
Show file tree
Hide file tree
Showing 15 changed files with 1,117 additions and 1,245 deletions.
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build
coverage
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 1.4.0 (November 28, 2019)

- Add support for APP_ENV
- Update dependencies

## 1.3.1 (November 4, 2019)

Fix bug in float parsing regex
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Config Dug

[![Build status](https://github.com/neofinancial/config-dug/workflows/CI/badge.svg)](https://github.com/neofinancial/config-dug/actions)
![TypeScript 3.4.3](https://img.shields.io/badge/TypeScript-3.4.3-brightgreen.svg)
![TypeScript 3.7.2](https://img.shields.io/badge/TypeScript-3.7.2-brightgreen.svg)

![Config Dug](https://github.com/neofinancial/config-dug/blob/master/config-dug.png)

Expand Down Expand Up @@ -33,11 +33,13 @@ module.exports = {
};
```

Environment specific config files are loaded based on the value of the `APP_ENV` or `NODE_ENV` environment variables. If `APP_ENV` is present it will take precedence over `NODE_ENV`.

Settings from these different sources are merged together into a single config object in the following order:

1. `config.default.{ts|js}`
1. `config.${NODE_ENV}.{ts|js}`
1. `config.${NODE_ENV}.local.{ts|js}`
1. `config.${APP_ENV|NODE_ENV}.{ts|js}`
1. `config.${APP_ENV|NODE_ENV}.local.{ts|js}`
1. `config.local.{ts|js}`
1. AWS Secrets Manager
1. Environment variables
Expand Down
28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,23 @@
]
},
"dependencies": {
"aws-param-store": "^2.1.0",
"aws-sdk": "^2.475.0",
"aws-param-store": "^3.1.0",
"aws-sdk": "^2.580.0",
"debug": "^4.1.1"
},
"devDependencies": {
"@types/aws-param-store": "^2.1.0",
"@types/debug": "^4.1.4",
"@types/jest": "^24.0.14",
"@types/node": "^12.0.8",
"eslint": "^6.6.0",
"eslint-config-neo": "^0.4.2",
"husky": "^3.0.9",
"jest": "^24.8.0",
"lint-staged": "^9.4.2",
"prettier": "^1.18.2",
"rimraf": "^2.6.3",
"ts-jest": "^24.0.2",
"typescript": "~3.4.5"
"@types/debug": "^4.1.5",
"@types/jest": "^24.0.23",
"@types/node": "^12.12.14",
"eslint": "^6.7.1",
"eslint-config-neo": "^0.5.1",
"husky": "^3.1.0",
"jest": "^24.9.0",
"lint-staged": "^9.5.0",
"prettier": "^1.19.1",
"rimraf": "^3.0.0",
"ts-jest": "^24.2.0",
"typescript": "~3.7.2"
}
}
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable unicorn/no-nested-ternary */
/* eslint-disable no-console */

import fs from 'fs';
Expand Down Expand Up @@ -100,7 +101,11 @@ const loadEnvironment = (): object => {

const loadConfig = (configPath = ''): ConfigObject => {
const appDirectory = fs.realpathSync(process.cwd());
const environment = process.env.NODE_ENV ? process.env.NODE_ENV : 'development';
const environment = process.env.APP_ENV
? process.env.APP_ENV
: process.env.NODE_ENV
? process.env.NODE_ENV
: 'development';

debug('loading config from', path.resolve(appDirectory, configPath));

Expand All @@ -126,6 +131,7 @@ const loadConfig = (configPath = ''): ConfigObject => {

const init = (): ConfigObject => {
debug('loading default config');

const config = loadConfig();

return config;
Expand Down
4 changes: 0 additions & 4 deletions test/fixtures/javascript/config.development.js

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixtures/javascript/config.development.local.js

This file was deleted.

4 changes: 4 additions & 0 deletions test/fixtures/javascript/config.staging.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
KEY_6: 'staging environment key',
KEY_8: 3
};
3 changes: 3 additions & 0 deletions test/fixtures/javascript/config.staging.local.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
KEY_10: 'staging local key'
};
3 changes: 0 additions & 3 deletions test/fixtures/typescript/config.development.local.ts

This file was deleted.

4 changes: 0 additions & 4 deletions test/fixtures/typescript/config.development.ts

This file was deleted.

3 changes: 3 additions & 0 deletions test/fixtures/typescript/config.staging.local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
KEY_10: 'staging local key'
};
4 changes: 4 additions & 0 deletions test/fixtures/typescript/config.staging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
KEY_6: 'staging environment key',
KEY_8: 3
};
31 changes: 26 additions & 5 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ test('loading JavaScript config works', (): void => {
});
});

test('loading development environment config works', (): void => {
process.env.NODE_ENV = 'development';
test('loading staging environment config with NODE_ENV works', (): void => {
process.env.NODE_ENV = 'staging';

const devConfig = loadConfig('test/fixtures/typescript');

Expand All @@ -41,11 +41,32 @@ test('loading development environment config works', (): void => {
KEY_3: false,
KEY_4: 42,
KEY_5: 4.2,
KEY_6: 'development environment key',
KEY_6: 'staging environment key',
KEY_7: 'local key',
KEY_8: 2,
KEY_8: 3,
KEY_9: 'bar',
KEY_10: 'development local key'
KEY_10: 'staging local key'
});

process.env.NODE_ENV = 'test';
});

test('loading staging environment config with APP_ENV works', (): void => {
process.env.APP_ENV = 'staging';

const devConfig = loadConfig('test/fixtures/typescript');

expect(devConfig).toMatchObject({
KEY_1: 'value 1',
KEY_2: true,
KEY_3: false,
KEY_4: 42,
KEY_5: 4.2,
KEY_6: 'staging environment key',
KEY_7: 'local key',
KEY_8: 3,
KEY_9: 'bar',
KEY_10: 'staging local key'
});

process.env.NODE_ENV = 'test';
Expand Down
Loading

0 comments on commit 686d756

Please sign in to comment.