Skip to content

Commit 9a6a1c1

Browse files
Merge pull request #2 from eugene-matvejev/v1
init commit. proof of concept
2 parents e788a07 + d82f51a commit 9a6a1c1

File tree

7 files changed

+175
-0
lines changed

7 files changed

+175
-0
lines changed

index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
let fs = require('fs'),
4+
packageJsonPath = `${process.cwd()}/package.json`,
5+
packageJsonContent = fs.readFileSync(packageJsonPath),
6+
/** @param {{extra: {node_parameter_handler: []}}} content */
7+
packageJson = JSON.parse(packageJsonContent),
8+
Processor = require('./src/processor'),
9+
processor = new Processor(packageJson.extra.node_parameter_handler, process.cwd());
10+
11+
processor.process();
12+
processor.write();

package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "node.js-parameter-handler",
3+
"version": "0.1.0",
4+
"description": "build .json files which can be used as settings",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "in progress"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/explore-node-js/node.js-parameter-handler.git"
12+
},
13+
"keywords": [
14+
"config handler, parameter handler"
15+
],
16+
"author": "Eugene Matvejev <eugene.matvejev@gmail.com>",
17+
"license": "MIT",
18+
"bugs": {
19+
"url": "https://github.com/explore-node-js/node.js-parameter-handler/issues"
20+
},
21+
"homepage": "https://github.com/explore-node-js/node.js-parameter-handler#readme",
22+
"dependencies": {
23+
"deepmerge": "^1.3.1",
24+
"jsonfile": "^2.4.0"
25+
}
26+
}

src/file.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module.exports = class File {
2+
}

src/processor.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
let fs = require('fs'),
2+
deepmerge = require('deepmerge'),
3+
File = require('./file');
4+
5+
module.exports = class Processor {
6+
constructor(config, cwd) {
7+
this.config = config;
8+
this.cwd = cwd;
9+
}
10+
11+
process() {
12+
this.files = [];
13+
14+
this.config.forEach(config => {
15+
let file = this.processFile(config);
16+
17+
this.files.push(file);
18+
})
19+
}
20+
21+
write() {
22+
this.files.forEach(file => fs.writeFile(file.output, JSON.stringify(file.content, null, 2), 'UTF-8'));
23+
}
24+
25+
/**
26+
* @param {{envMap: {}, output: string, source: string}} config
27+
*
28+
* @returns {*}
29+
*/
30+
processFile(config) {
31+
let file = new File();
32+
33+
let pathSource = this.resolvePath(config.source),
34+
pathOutput = this.resolvePath(config.output);
35+
36+
let packageJsonPath = this.resolvePath(pathSource),
37+
packageJsonContent = fs.readFileSync(packageJsonPath);
38+
39+
/** @param {{extra: {}}} content */
40+
let packageJson = JSON.parse(packageJsonContent),
41+
solvedJson = this.resolveOverwritten(config.envMap),
42+
completedJson = this.constructor.getMergedData(packageJson, solvedJson);
43+
44+
file.source = pathSource;
45+
file.output = pathOutput;
46+
file.content = completedJson;
47+
48+
return file;
49+
}
50+
51+
/**
52+
* @param {string} path
53+
*/
54+
resolvePath(path) {
55+
if ('/' === path.charAt(0)) {
56+
return path;
57+
}
58+
59+
return `${this.cwd}/${path}`;
60+
}
61+
62+
resolveOverwritten(envMapping) {
63+
let object = {};
64+
65+
for (let abstractPath of Object.keys(envMapping)) {
66+
let envVariable = envMapping[abstractPath],
67+
value = this.constructor.getEnvironmentValue(envVariable);
68+
69+
this.constructor.overwriteObjectFieldValue(abstractPath, value, object)
70+
}
71+
72+
return object;
73+
}
74+
75+
static getEnvironmentValue(index) {
76+
return process.env[index] || undefined;
77+
}
78+
79+
static getMergedData(data, overwrittenData) {
80+
return deepmerge(data, overwrittenData);
81+
}
82+
83+
static overwriteObjectFieldValue(abstractPath, value, object, delimiter) {
84+
if (undefined === value) {
85+
return;
86+
}
87+
88+
delimiter = undefined !== delimiter ? delimiter : '.';
89+
90+
let indexes = abstractPath.split(delimiter),
91+
lastPartIndex = indexes.length - 1;
92+
93+
for (let i = 0; i <= lastPartIndex; i++) {
94+
let index = indexes[i];
95+
96+
if (i === lastPartIndex) {
97+
object[index] = value;
98+
break;
99+
}
100+
101+
if (undefined === object[index]) {
102+
object[index] = {};
103+
}
104+
105+
object = object[index];
106+
}
107+
}
108+
};

tests/fixtures/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"extra": {
3+
"node_parameter_handler": [
4+
{
5+
"source": "tests/fixtures/settings.json.dist",
6+
"output": "var/settings.json",
7+
"envMap": {
8+
"touched": "BASE_URL",
9+
"test.touched": "PWD",
10+
"test.test.touched": "HOME"
11+
}
12+
}
13+
]
14+
}
15+
}

tests/fixtures/settings.json.dist

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"touched": "to be replaced",
3+
"untouched": "untouched",
4+
"test": {
5+
"touched": "to be replaced",
6+
"untouched": "untouched",
7+
"test": {
8+
"touched": "to be replaced",
9+
"untouched": "untouched"
10+
}
11+
}
12+
}

var/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)