Skip to content

Commit 4cd4795

Browse files
init commit. tests will be added later
1 parent 45f275b commit 4cd4795

File tree

9 files changed

+369
-0
lines changed

9 files changed

+369
-0
lines changed

index.js

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

lib/file.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8+
9+
var File = function File() {
10+
_classCallCheck(this, File);
11+
};
12+
13+
exports.default = File;

lib/processor.js

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
8+
9+
var _file = require("./file");
10+
11+
var _file2 = _interopRequireDefault(_file);
12+
13+
var _fs = require("fs");
14+
15+
var _fs2 = _interopRequireDefault(_fs);
16+
17+
var _deepmerge = require("deepmerge");
18+
19+
var _deepmerge2 = _interopRequireDefault(_deepmerge);
20+
21+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22+
23+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
24+
25+
var Processor = function () {
26+
// config;
27+
// cwd;
28+
// files;
29+
30+
function Processor(config, cwd) {
31+
var files = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
32+
33+
_classCallCheck(this, Processor);
34+
}
35+
36+
_createClass(Processor, [{
37+
key: "process",
38+
value: function process() {
39+
var _iteratorNormalCompletion = true;
40+
var _didIteratorError = false;
41+
var _iteratorError = undefined;
42+
43+
try {
44+
for (var _iterator = this.config[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
45+
var config = _step.value;
46+
47+
var file = this.processFile(config);
48+
49+
this.files.push(file);
50+
}
51+
} catch (err) {
52+
_didIteratorError = true;
53+
_iteratorError = err;
54+
} finally {
55+
try {
56+
if (!_iteratorNormalCompletion && _iterator.return) {
57+
_iterator.return();
58+
}
59+
} finally {
60+
if (_didIteratorError) {
61+
throw _iteratorError;
62+
}
63+
}
64+
}
65+
}
66+
}, {
67+
key: "processFile",
68+
value: function processFile(config) {
69+
var file = new _file2.default();
70+
71+
var pathSource = this.resolvePath(config.source),
72+
pathOutput = this.resolvePath(config.output);
73+
74+
var packageJsonPath = this.resolvePath(pathSource),
75+
packageJsonContent = _fs2.default.readFileSync(packageJsonPath);
76+
77+
/** @param {{extra: {}}} content */
78+
var packageJson = JSON.parse(packageJsonContent),
79+
solvedJson = this.resolveOverwritten(config),
80+
completedJson = this.constructor.getMergedData(packageJson, solvedJson);
81+
82+
file.source = pathSource;
83+
file.output = pathOutput;
84+
file.contex = completedJson;
85+
86+
return file;
87+
}
88+
89+
/**
90+
* @param {string} path
91+
*/
92+
93+
}, {
94+
key: "resolvePath",
95+
value: function resolvePath(path) {
96+
if ('/' === path.charAt(0)) {
97+
return path;
98+
}
99+
100+
return this.cwd + "/" + path;
101+
}
102+
}, {
103+
key: "resolveOverwritten",
104+
value: function resolveOverwritten(params) {
105+
var object = {};
106+
107+
var _iteratorNormalCompletion2 = true;
108+
var _didIteratorError2 = false;
109+
var _iteratorError2 = undefined;
110+
111+
try {
112+
for (var _iterator2 = Object.keys(params)[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
113+
var abstractPath = _step2.value;
114+
115+
var envVariable = params[abstractPath],
116+
value = this.constructor.getEnvironmentValue(envVariable);
117+
118+
this.constructor.overwriteObjectFieldValue(abstractPath, value, object);
119+
}
120+
} catch (err) {
121+
_didIteratorError2 = true;
122+
_iteratorError2 = err;
123+
} finally {
124+
try {
125+
if (!_iteratorNormalCompletion2 && _iterator2.return) {
126+
_iterator2.return();
127+
}
128+
} finally {
129+
if (_didIteratorError2) {
130+
throw _iteratorError2;
131+
}
132+
}
133+
}
134+
}
135+
}], [{
136+
key: "getEnvironmentValue",
137+
value: function getEnvironmentValue(index) {
138+
return process.env[index] || undefined;
139+
}
140+
}, {
141+
key: "getMergedData",
142+
value: function getMergedData(data, overwrittenData) {
143+
return (0, _deepmerge2.default)(data, overwrittenData);
144+
}
145+
}, {
146+
key: "overwriteObjectFieldValue",
147+
value: function overwriteObjectFieldValue(abstractPath, value, object, delimiter) {
148+
if (undefined === value) {
149+
return;
150+
}
151+
152+
delimiter = undefined !== delimiter ? delimiter : '.';
153+
154+
var indexes = abstractPath.split(delimiter),
155+
lastPartIndex = indexes.length - 1;
156+
157+
for (var i = 0; i <= lastPartIndex; i++) {
158+
var index = indexes[i];
159+
160+
if (i === lastPartIndex) {
161+
object[index] = value;
162+
break;
163+
}
164+
165+
if (undefined === object[index]) {
166+
object[index] = {};
167+
}
168+
169+
object = object[index];
170+
}
171+
}
172+
}]);
173+
174+
return Processor;
175+
}();
176+
177+
exports.default = Processor;

package.json

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

src/file.js

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

src/processor.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
let File = require('./file');
2+
let fs = require('fs');
3+
let deepmerge = require('deepmerge');
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)