Skip to content

Commit 23b1e9f

Browse files
Merge pull request #23 from explore-node-js/master
sync with main repo
2 parents 01eb64c + d17eef1 commit 23b1e9f

File tree

7 files changed

+107
-33
lines changed

7 files changed

+107
-33
lines changed

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[codecov.io-master-badge]: https://codecov.io/gh/explore-node-js/node.js-parameter-handler/branch/master/graph/badge.svg
44
[codecov.io-master-link]: https://codecov.io/gh/explore-node-js/node.js-parameter-handler
55

6-
| | master
6+
| | master
77
|--- |---
88
| __tests__ |
99
| _< Circle CI >_ | [![build][circle.ci-master-badge]][circle.ci-master-link]
@@ -21,7 +21,7 @@ can be used as config builder, inspired by [@Incenteev/ParameterHandler](https:/
2121

2222
## used technologies
2323
* jest _[for tests only]_
24-
24+
2525
## how to execute tests
2626
`npm test` or, to execute tests with coverage `npm test -- --coverage`
2727

@@ -36,8 +36,18 @@ obverve below sample of _package.json_
3636
"extra": {
3737
"node_parameter_handler": [
3838
{
39-
"source": "tests/fixtures/settings.json.dist", # source
40-
"output": "var/settings.json", # output
39+
"source": "tests/fixtures/settings.json.dist", # source
40+
"output": "var/settings1.json", # output
41+
"envMap": {
42+
"touched": "BASE_URL", # json path to ENV VARIABLE
43+
"test.touched": "PWD",
44+
"test.test.touched": "HOME"
45+
}
46+
},
47+
{
48+
"source": "tests/fixtures/settings.json.dist", # source
49+
"output": "var/settings2.json", # output
50+
"skipUndefined": true, # if variable is not defined do not change
4151
"envMap": {
4252
"touched": "BASE_URL", # json path to ENV VARIABLE
4353
"test.touched": "PWD",

circle.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
machine:
22
node:
3-
version: 6.9
3+
version: 8.11
44

55
dependencies:
6+
pre:
7+
- echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
68
cache_directories:
79
- ~/.npm/
810

@@ -12,3 +14,9 @@ test:
1214
override:
1315
- npm test -- --coverage
1416
- codecov
17+
18+
deployment:
19+
production:
20+
branch: master
21+
commands:
22+
- npm publish

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-parameter-handler",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "build JSON files which can be used as settings",
55
"main": "index.js",
66
"repository": {

src/processor.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = class Processor {
1414
this.files = [];
1515
console.log(chalk.yellow(`>>> PROCESSING FILES`));
1616

17-
this.config.forEach(config => {
17+
this.config.forEach((config) => {
1818
let file = this.processFile(config);
1919

2020
this.files.push(file);
@@ -28,7 +28,7 @@ module.exports = class Processor {
2828
this.files.forEach(file => {
2929
console.log(chalk.green(`>>>>> ${file.getOutputPath()}`));
3030

31-
fs.writeFile(file.getOutputPath(), JSON.stringify(file.getContent(), null, 2), 'UTF-8')
31+
fs.writeFile(file.getOutputPath(), JSON.stringify(file.getContent(), null, 2), 'UTF-8');
3232
});
3333
}
3434

@@ -37,19 +37,19 @@ module.exports = class Processor {
3737
*
3838
* @returns {File}
3939
*/
40-
processFile(config) {
40+
processFile({source, output, envMap, skipUndefined}) {
4141
const file = new File();
4242

43-
const pathSource = this.resolvePath(config.source);
44-
const pathOutput = this.resolvePath(config.output);
43+
const pathSource = this.resolvePath(source);
44+
const pathOutput = this.resolvePath(output);
4545

4646
const packageJsonPath = this.resolvePath(pathSource);
4747
const packageJsonContent = fs.readFileSync(packageJsonPath);
4848

4949
/** @param {{extra: {}}} content */
5050
const packageJson = JSON.parse(packageJsonContent);
51-
const solvedJson = this.resolveOverwritten(config.envMap);
52-
const completedJson = this.constructor.getMergedData(packageJson, solvedJson);
51+
const solvedJson = Processor.resolveOverwritten(envMap, skipUndefined);
52+
const completedJson = deepmerge(packageJson, solvedJson);
5353

5454
file.setSourcePath(pathSource)
5555
.setOutputPath(pathOutput)
@@ -69,24 +69,20 @@ module.exports = class Processor {
6969
return `${this.cwd}/${path}`;
7070
}
7171

72-
resolveOverwritten(envMapping) {
73-
const object = {};
72+
static resolveOverwritten(envMap, skipUndefined) {
73+
const obj = {};
7474

75-
Object.keys(envMapping).forEach(abstractPath => {
76-
const envVariable = envMapping[abstractPath];
77-
const value = this.constructor.getEnvironmentValue(envVariable);
75+
Object.keys(envMap).forEach((abstractPath) => {
76+
const envVariable = envMap[abstractPath];
77+
const value = Processor.getEnvironmentValue(envVariable);
7878

79-
undefined !== value && overwriteFieldValue(abstractPath, value, object);
79+
(undefined !== value || !skipUndefined) && overwriteFieldValue(abstractPath, value, obj);
8080
});
8181

82-
return object;
82+
return obj;
8383
}
8484

85-
static getEnvironmentValue(index) {
86-
return process.env[index] || undefined;
87-
}
88-
89-
static getMergedData(data, overwrittenData) {
90-
return deepmerge(data, overwrittenData);
85+
static getEnvironmentValue(i) {
86+
return process.env[i] || undefined;
9187
}
9288
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
exports[`processor ::process match snapshot 1`] = `
2+
Array [
3+
Object {
4+
"test": Object {
5+
"test": Object {
6+
"touched": undefined,
7+
"untouched": "untouched",
8+
},
9+
"touched": "test",
10+
"untouched": "untouched",
11+
},
12+
"touched": "test",
13+
"untouched": "untouched",
14+
},
15+
Object {
16+
"test": Object {
17+
"test": Object {
18+
"touched": "to be replaced",
19+
"untouched": "untouched",
20+
},
21+
"touched": "to be replaced",
22+
"untouched": "untouched",
23+
},
24+
"touched": "test",
25+
"untouched": "untouched",
26+
},
27+
]
28+
`;

tests/fixtures/package.json

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,21 @@
33
"node_parameter_handler": [
44
{
55
"source": "tests/fixtures/settings.json.dist",
6-
"output": "var/settings.json",
6+
"output": "var/settings1.json",
77
"envMap": {
8-
"touched": "BASE_URL",
9-
"test.touched": "PWD",
10-
"test.test.touched": "HOME"
8+
"touched": "NODE_ENV",
9+
"test.touched": "NODE_ENV",
10+
"test.test.touched": "NODE_ENV_NOT_EXISTS"
11+
}
12+
},
13+
{
14+
"source": "tests/fixtures/settings.json.dist",
15+
"output": "var/settings2.json",
16+
"skipUndefined": true,
17+
"envMap": {
18+
"touched": "NODE_ENV",
19+
"test.touched": "NODE_ENV_NOT_EXISTS",
20+
"test.test.touched": "NODE_ENV_NOT_EXISTS"
1121
}
1222
}
1323
]

tests/processor.test.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,32 @@ describe('processor', () => {
88
describe('::resolvePath', () => {
99
const processor = new Processor(config, cwd);
1010

11-
[ '/', 'test', 'test/', 'test/test', '/test/test' ].forEach(path => {
11+
[ '/', 'test', 'test/', 'test/test', '/test/test' ].forEach((path) => {
1212
it(`should be converted '${path}' to absolute path`, () => {
1313
expect(processor.resolvePath(path)).toMatch(/^\/.*/);
14-
})
14+
});
1515
});
16-
})
16+
});
17+
18+
describe('::getEnvironmentValue', () => {
19+
[
20+
{ env: 'NODE_ENV', expected: 'test' },
21+
{ env: 'NODE_ENV_NOT_EXISTS', expected: undefined },
22+
].forEach(({ env, expected }) => {
23+
it(`should be return '${expected}' when read value of '${env}' env. variable`, () => {
24+
expect(Processor.getEnvironmentValue(env)).toBe(expected);
25+
});
26+
});
27+
});
28+
29+
describe('::process', () => {
30+
const processor = new Processor(config, cwd);
31+
32+
it(`match snapshot`, () => {
33+
processor.process();
34+
const content = processor.files.map((el) => el.content);
35+
36+
expect(content).toMatchSnapshot();
37+
});
38+
});
1739
});

0 commit comments

Comments
 (0)