Skip to content

Commit 1e8d278

Browse files
Merge pull request #2 from eugene-matvejev/v1.1
init commit
2 parents 45f275b + 502e24f commit 1e8d278

File tree

9 files changed

+198
-36
lines changed

9 files changed

+198
-36
lines changed

.gitignore

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,4 @@
1-
# Logs
2-
logs
3-
*.log
4-
npm-debug.log*
5-
6-
# Runtime data
7-
pids
8-
*.pid
9-
*.seed
10-
11-
# Directory for instrumented libs generated by jscoverage/JSCover
12-
lib-cov
13-
14-
# Coverage directory used by tools like istanbul
15-
coverage
16-
17-
# nyc test coverage
18-
.nyc_output
19-
20-
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
21-
.grunt
22-
23-
# node-waf configuration
24-
.lock-wscript
25-
26-
# Compiled binary addons (http://nodejs.org/api/addons.html)
27-
build/Release
28-
29-
# Dependency directories
301
node_modules
31-
jspm_packages
32-
33-
# Optional npm cache directory
34-
.npm
35-
36-
# Optional REPL history
37-
.node_repl_history
2+
lib
3+
var
4+
!var/.gitkeep

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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
"repository": {
7+
"type": "git",
8+
"url": "git+https://github.com/explore-node-js/node.js-parameter-handler.git"
9+
},
10+
"keywords": [
11+
"config handler, parameter handler"
12+
],
13+
"author": "Eugene Matvejev <[email protected]>",
14+
"license": "MIT",
15+
"bugs": {
16+
"url": "https://github.com/explore-node-js/node.js-parameter-handler/issues"
17+
},
18+
"homepage": "https://github.com/explore-node-js/node.js-parameter-handler#readme",
19+
"dependencies": {
20+
"deepmerge": "^1.3.1",
21+
"jsonfile": "^2.4.0"
22+
},
23+
"devDependencies": {
24+
"jest": "^18.1.0"
25+
},
26+
"scripts": {
27+
"test": "node node_modules/jest/bin/jest.js"
28+
}
29+
}

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+
}

tests/processor.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"use strict";
2+
3+
const Processor = require('../src/processor');
4+
const config = require('./fixtures/package.json').extra.node_parameter_handler;
5+
const cwd = process.cwd();
6+
7+
describe('processor', () => {
8+
describe('::resolvePath', () => {
9+
const processor = new Processor(config, cwd);
10+
11+
[ '/', 'test', 'test/', 'test/test', '/test/test' ].forEach(path => {
12+
it(`should be converted '${path}' to absolute path`, () => {
13+
expect(processor.resolvePath(path)).toMatch(/^\/.*/);
14+
})
15+
});
16+
})
17+
});

var/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)