Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[codecov.io-master-badge]: https://codecov.io/gh/explore-node-js/node.js-parameter-handler/branch/master/graph/badge.svg
[codecov.io-master-link]: https://codecov.io/gh/explore-node-js/node.js-parameter-handler

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

## used technologies
* jest _[for tests only]_

## how to execute tests
`npm test` or, to execute tests with coverage `npm test -- --coverage`

Expand All @@ -36,8 +36,18 @@ obverve below sample of _package.json_
"extra": {
"node_parameter_handler": [
{
"source": "tests/fixtures/settings.json.dist", # source
"output": "var/settings.json", # output
"source": "tests/fixtures/settings.json.dist", # source
"output": "var/settings1.json", # output
"envMap": {
"touched": "BASE_URL", # json path to ENV VARIABLE
"test.touched": "PWD",
"test.test.touched": "HOME"
}
},
{
"source": "tests/fixtures/settings.json.dist", # source
"output": "var/settings2.json", # output
"skipUndefined": true, # if variable is not defined do not change
"envMap": {
"touched": "BASE_URL", # json path to ENV VARIABLE
"test.touched": "PWD",
Expand Down
36 changes: 16 additions & 20 deletions src/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = class Processor {
this.files = [];
console.log(chalk.yellow(`>>> PROCESSING FILES`));

this.config.forEach(config => {
this.config.forEach((config) => {
let file = this.processFile(config);

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

fs.writeFile(file.getOutputPath(), JSON.stringify(file.getContent(), null, 2), 'UTF-8')
fs.writeFile(file.getOutputPath(), JSON.stringify(file.getContent(), null, 2), 'UTF-8');
});
}

Expand All @@ -37,19 +37,19 @@ module.exports = class Processor {
*
* @returns {File}
*/
processFile(config) {
processFile({source, output, envMap, skipUndefined}) {
const file = new File();

const pathSource = this.resolvePath(config.source);
const pathOutput = this.resolvePath(config.output);
const pathSource = this.resolvePath(source);
const pathOutput = this.resolvePath(output);

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

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

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

resolveOverwritten(envMapping) {
const object = {};
static resolveOverwritten(envMap, skipUndefined) {
const obj = {};

Object.keys(envMapping).forEach(abstractPath => {
const envVariable = envMapping[abstractPath];
const value = this.constructor.getEnvironmentValue(envVariable);
Object.keys(envMap).forEach((abstractPath) => {
const envVariable = envMap[abstractPath];
const value = Processor.getEnvironmentValue(envVariable);

undefined !== value && overwriteFieldValue(abstractPath, value, object);
(undefined !== value || !skipUndefined) && overwriteFieldValue(abstractPath, value, obj);
});

return object;
return obj;
}

static getEnvironmentValue(index) {
return process.env[index] || undefined;
}

static getMergedData(data, overwrittenData) {
return deepmerge(data, overwrittenData);
static getEnvironmentValue(i) {
return process.env[i] || undefined;
}
};
28 changes: 28 additions & 0 deletions tests/__snapshots__/processor.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
exports[`processor ::process match snapshot 1`] = `
Array [
Object {
"test": Object {
"test": Object {
"touched": undefined,
"untouched": "untouched",
},
"touched": "test",
"untouched": "untouched",
},
"touched": "test",
"untouched": "untouched",
},
Object {
"test": Object {
"test": Object {
"touched": "to be replaced",
"untouched": "untouched",
},
"touched": "to be replaced",
"untouched": "untouched",
},
"touched": "test",
"untouched": "untouched",
},
]
`;
18 changes: 14 additions & 4 deletions tests/fixtures/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@
"node_parameter_handler": [
{
"source": "tests/fixtures/settings.json.dist",
"output": "var/settings.json",
"output": "var/settings1.json",
"envMap": {
"touched": "BASE_URL",
"test.touched": "PWD",
"test.test.touched": "HOME"
"touched": "NODE_ENV",
"test.touched": "NODE_ENV",
"test.test.touched": "NODE_ENV_NOT_EXISTS"
}
},
{
"source": "tests/fixtures/settings.json.dist",
"output": "var/settings2.json",
"skipUndefined": true,
"envMap": {
"touched": "NODE_ENV",
"test.touched": "NODE_ENV_NOT_EXISTS",
"test.test.touched": "NODE_ENV_NOT_EXISTS"
}
}
]
Expand Down
28 changes: 25 additions & 3 deletions tests/processor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,32 @@ describe('processor', () => {
describe('::resolvePath', () => {
const processor = new Processor(config, cwd);

[ '/', 'test', 'test/', 'test/test', '/test/test' ].forEach(path => {
[ '/', 'test', 'test/', 'test/test', '/test/test' ].forEach((path) => {
it(`should be converted '${path}' to absolute path`, () => {
expect(processor.resolvePath(path)).toMatch(/^\/.*/);
})
});
});
})
});

describe('::getEnvironmentValue', () => {
[
{ env: 'NODE_ENV', expected: 'test' },
{ env: 'NODE_ENV_NOT_EXISTS', expected: undefined },
].forEach(({ env, expected }) => {
it(`should be return '${expected}' when read value of '${env}' env. variable`, () => {
expect(Processor.getEnvironmentValue(env)).toBe(expected);
});
});
});

describe('::process', () => {
const processor = new Processor(config, cwd);

it(`match snapshot`, () => {
processor.process();
const content = processor.files.map((el) => el.content);

expect(content).toMatchSnapshot();
});
});
});