config-extends
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -14,5 +14,5 @@ { | ||
], | ||
"program": "${workspaceFolder}\\test\\test" | ||
"program": "${workspaceFolder}/test/test" | ||
} | ||
] | ||
} |
@@ -7,12 +7,12 @@ const Path = require('path'); | ||
{ | ||
static async load(path,format='yaml'){ | ||
static async apply(source,target){ | ||
let config = {}; | ||
if(Fs.lstatSync(path).isDirectory()){ | ||
let _format = '.'+format; | ||
let list = await ConfigExtends._getFiles(path,'',_format); | ||
if(Fs.lstatSync(source).isDirectory()){ | ||
let list = await ConfigExtends._getFiles(source,''); | ||
for(let i=0;i<list.length;i++){ | ||
let filePath = list[i]; | ||
let fullPath = Path.join(path,filePath); | ||
let fullPath = Path.join(source,filePath); | ||
let extension = Path.parse(fullPath).ext.toLocaleLowerCase(); | ||
let content = Fs.readFileSync(fullPath,'utf8'); | ||
let data = format=='yaml' || format == 'yml'?Yaml.safeLoad(content):content; | ||
let data = extension=='.yaml' || extension == '.yml'?Yaml.safeLoad(content):JSON.parse(content); | ||
let parseFilePath = Path.parse(filePath); | ||
@@ -26,9 +26,21 @@ let a = parseFilePath.dir.split(Path.sep); | ||
}else{ | ||
let content = Fs.readFileSync(path,'utf8'); | ||
config = format=='yaml' || format == 'yml'?Yaml.safeLoad(content):content; | ||
let extension = Path.parse(source).ext.toLocaleLowerCase(); | ||
let content = Fs.readFileSync(source,'utf8'); | ||
config = extension=='.yaml' || extension == '.yml'?Yaml.safeLoad(content):JSON.parse(content); | ||
config = ConfigExtends.extends(config); | ||
} | ||
if(target){ | ||
let extension = Path.parse(target).ext.toLocaleLowerCase(); | ||
let content = extension=='.yaml' || extension == '.yml'?Yaml.safeDump(config):JSON.stringify(config,null,2); | ||
Fs.writeFileSync(target,content,{encoding: "utf8"}); | ||
} | ||
return config; | ||
} | ||
static async _getFiles(rootPath,relativePath,format){ | ||
static extends(source,...args){ | ||
let _config =Object.assign(source,args); | ||
for(let k in _config)_config[k] =ConfigExtends._completeObject(_config,_config[k]); | ||
for(let k in _config)ConfigExtends._removeFlags(_config[k]); | ||
return _config; | ||
} | ||
static async _getFiles(rootPath,relativePath){ | ||
let files = [] | ||
@@ -40,16 +52,11 @@ let list = Fs.readdirSync(Path.join(rootPath,relativePath)); | ||
if(!Fs.lstatSync(fullPath).isDirectory()){ | ||
if(Path.parse(fullPath).ext.toLocaleLowerCase() == format.toLocaleLowerCase() ) | ||
let extension = Path.parse(fullPath).ext.toLocaleLowerCase(); | ||
if(extension == '.yaml' || extension == '.yml' || extension == '.json') | ||
files.push(Path.join(relativePath,item)) | ||
}else | ||
{ | ||
files =files.concat(await ConfigExtends._getFiles(rootPath,Path.join(relativePath,item),format)); | ||
files =files.concat(await ConfigExtends._getFiles(rootPath,Path.join(relativePath,item))); | ||
} | ||
} | ||
return files; | ||
} | ||
static extends(source,...args){ | ||
let _config =Object.assign(source,args); | ||
for(let k in _config)_config[k] =ConfigExtends._completeObject(_config,_config[k]); | ||
for(let k in _config)ConfigExtends._removeFlags(_config[k]); | ||
return _config; | ||
} | ||
@@ -82,3 +89,2 @@ static _completeObject(config,obj){ | ||
} | ||
static _extend(obj, base){ | ||
@@ -117,4 +123,3 @@ if(Array.isArray(base)) | ||
ConfigExtends._removeFlags(obj[k]); | ||
} | ||
} | ||
} |
{ | ||
"name": "config-extends", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Allow to extend yaml or json files configuration", | ||
"main": "./lib/config-extends", | ||
"bin": { | ||
"config-extends": "bin/config-extends" | ||
}, | ||
"scripts": { | ||
@@ -27,3 +30,4 @@ "test": "mocha" | ||
"dependencies": { | ||
"js-yaml": "^3.14.1" | ||
"js-yaml": "^3.14.1", | ||
"minimist": "^1.2.5" | ||
}, | ||
@@ -30,0 +34,0 @@ "devDependencies": { |
305
README.md
@@ -17,7 +17,7 @@ # Config Extends | ||
### .load(path,format='yaml') [asynchronous] | ||
### .apply(source,target) [asynchronous] | ||
This api allows to extend configuration from a file or several files from a path | ||
- Paramas: | ||
- path : object of file or folder | ||
- format : file format [yaml|json] | ||
- source : path of file or folder | ||
- target : file result (optional) | ||
@@ -27,7 +27,14 @@ Note: Only node.js | ||
## Installation | ||
### module for node.js | ||
``` | ||
npm i config-extends | ||
npm install config-extends | ||
``` | ||
### CLI executable | ||
If you want to extends from CLI, install config-extends globally: | ||
``` | ||
npm install -g config-extends | ||
``` | ||
## Usage | ||
### Bundled YAML library for node.js | ||
### From library for node.js | ||
``` | ||
@@ -44,3 +51,3 @@ const ConfigExtends = require("config-extends") | ||
### Bundled library for browsers | ||
### From library for browsers | ||
``` | ||
@@ -56,4 +63,20 @@ <script src="config-extends.min.js"></script> | ||
``` | ||
### From CLI | ||
extends | ||
``` | ||
usage: config-extends extends -s | ||
arguments: | ||
-s, --source Input json (serialized). | ||
``` | ||
apply | ||
``` | ||
usage: config-extends apply -s [-t] | ||
arguments: | ||
-s, --source Input path of folder or file. | ||
-t, --target output file (optional). | ||
``` | ||
## Examples | ||
@@ -70,15 +93,7 @@ | ||
``` | ||
Result: | ||
result: | ||
``` | ||
{ | ||
"data": { | ||
"d": 3, | ||
"e": 4, | ||
"a": 1, | ||
"b": 2 | ||
}, | ||
"base": { | ||
"a": 1, | ||
"b": 2 | ||
} | ||
"data": {"d": 3,"e": 4,"a": 1,"b": 2 }, | ||
"base": {"a": 1,"b": 2} | ||
} | ||
@@ -98,22 +113,10 @@ ``` | ||
``` | ||
Result: | ||
result: | ||
``` | ||
"d": 3, | ||
"e": 4, | ||
"a": 1, | ||
"b": 2 | ||
}, | ||
"2": { | ||
"f": 3, | ||
"g": 4, | ||
"d": 3, | ||
"e": 4, | ||
"a": 1, | ||
"b": 2 | ||
} | ||
{ | ||
"data": { | ||
"1":{"d": 3,"e": 4,"a": 1,"b": 2 }, | ||
"2":{"f": 3,"g": 4,"d": 3,"e": 4,"a": 1,"b": 2} | ||
}, | ||
"base": { | ||
"a": 1, | ||
"b": 2 | ||
} | ||
"base": {"a": 1,"b": 2} | ||
} | ||
@@ -134,35 +137,61 @@ ``` | ||
``` | ||
Result: | ||
result: | ||
``` | ||
{ | ||
"data": { | ||
"1": { | ||
"d": 3, | ||
"e": 4, | ||
"a": 1, | ||
"b": 2 | ||
}, | ||
"2": { | ||
"f": 3, | ||
"g": 4, | ||
"a": 1, | ||
"b": 2, | ||
"h": "a", | ||
"i": "b" | ||
} | ||
"1": {"d": 3,"e": 4,"a": 1,"b": 2}, | ||
"2": {"f": 3,"g": 4,"a": 1,"b": 2,"h": "a","i": "b"} | ||
}, | ||
"base": { | ||
"a": 1, | ||
"b": 2 | ||
}, | ||
"base2": { | ||
"h": "a", | ||
"i": "b" | ||
"base": {"a": 1,"b": 2 }, | ||
"base2": {"h": "a","i": "b"} | ||
} | ||
``` | ||
### apply from path | ||
``` | ||
let config = await ConfigExtends.apply(path.join(__dirname,'test-1')); | ||
console.log(JSON.stringify(config,null,2)); | ||
``` | ||
structure folder | ||
- test-1 | ||
- folder1 | ||
- file1.yaml content => c: 3 | ||
- folder2 | ||
- file1.yaml content => _extends: [file,folder1.file1] | ||
d: 1 | ||
- file2.yaml content => _extends: folder2.file1 | ||
e: 1 | ||
- file.yaml content => a: 1 | ||
b: "b" | ||
result | ||
``` | ||
{ | ||
file: { a: 1, b: 'b' }, | ||
folder1: { file1: { c: 3 } }, | ||
folder2: { | ||
file1: { d: 1, a: 1, b: 'b', c: 3 }, | ||
file2: { e: 1, d: 1, a: 1, b: 'b', c: 3 } | ||
} | ||
} | ||
``` | ||
### apply from path CLI | ||
``` | ||
$ config-extends apply -s ./test/test-1 | ||
``` | ||
result: | ||
``` | ||
{ | ||
file: { a: 1, b: 'b' }, | ||
folder1: { file1: { c: 3 } }, | ||
folder2: { | ||
file1: { d: 1, a: 1, b: 'b', c: 3 }, | ||
file2: { e: 1, d: 1, a: 1, b: 'b', c: 3 } | ||
} | ||
} | ||
``` | ||
### extends from single file | ||
### apply from single file | ||
``` | ||
let config = await configExtends.load(path.join(__dirname,'raspberry.yaml')); | ||
let config = await configExtends.apply(path.join(__dirname,'raspberry.yaml')); | ||
console.log(JSON.stringify(config.version,null,2)); | ||
@@ -232,153 +261,25 @@ ``` | ||
Result: | ||
result: | ||
``` | ||
{ | ||
"PiA": { | ||
"wireless": false, | ||
"measure": { | ||
"high": 85.6, | ||
"long": 56.5 | ||
}, | ||
"ethernet": false | ||
}, | ||
"PiB": { | ||
"wireless": false, | ||
"measure": { | ||
"high": 85.6, | ||
"long": 56.5 | ||
}, | ||
"ethernet": true | ||
}, | ||
"PiA+": { | ||
"measure": { | ||
"high": 85.6, | ||
"long": 56.5 | ||
}, | ||
"wireless": false, | ||
"ethernet": false | ||
}, | ||
"PiB+": { | ||
"wireless": false, | ||
"measure": { | ||
"high": 85.6, | ||
"long": 56.5 | ||
}, | ||
"ethernet": true | ||
}, | ||
"Pi2B": { | ||
"measure": { | ||
"high": 85.6, | ||
"long": 56.5 | ||
} | ||
}, | ||
"Pi3A": { | ||
"wireless": true, | ||
"measure": { | ||
"high": 85.6, | ||
"long": 56.5 | ||
}, | ||
"ethernet": false | ||
}, | ||
"Pi3A+": { | ||
"wireless": true, | ||
"measure": { | ||
"high": 85.6, | ||
"long": 56.5 | ||
}, | ||
"ethernet": false | ||
}, | ||
"Pi3B+": { | ||
"wireless": true, | ||
"measure": { | ||
"high": 85.6, | ||
"long": 56.5 | ||
}, | ||
"ethernet": true | ||
}, | ||
"Pi4B1G": { | ||
"memory": "1G", | ||
"wireless": true, | ||
"measure": { | ||
"high": 85.6, | ||
"long": 56.5 | ||
}, | ||
"ethernet": true | ||
}, | ||
"Pi4B2G": { | ||
"memory": "2G", | ||
"wireless": true, | ||
"measure": { | ||
"high": 85.6, | ||
"long": 56.5 | ||
}, | ||
"ethernet": true | ||
}, | ||
"Pi4B4G": { | ||
"memory": "4G", | ||
"wireless": true, | ||
"measure": { | ||
"high": 85.6, | ||
"long": 56.5 | ||
}, | ||
"ethernet": true | ||
}, | ||
"Pi4B8G": { | ||
"memory": "8G", | ||
"wireless": true, | ||
"measure": { | ||
"high": 85.6, | ||
"long": 56.5 | ||
}, | ||
"ethernet": true | ||
} | ||
"PiA": {"wireless": false,"measure": {"high": 85.6,"long": 56.5 },"ethernet": false}, | ||
"PiB": {"wireless": false,"measure": {"high": 85.6,"long": 56.5 },"ethernet": true }, | ||
"PiA+": {"measure": {"high": 85.6,"long": 56.5},"wireless": false,"ethernet": false }, | ||
"PiB+": {"wireless": false,"measure": {"high": 85.6,"long": 56.5},"ethernet": true }, | ||
"Pi2B": {"measure": {"high": 85.6,"long": 56.5}}, | ||
"Pi3A": {"wireless": true,"measure": {"high": 85.6,"long": 56.5},"ethernet": false }, | ||
"Pi3A+":{"wireless": true,"measure": {"high": 85.6,"long": 56.5},"ethernet": false }, | ||
"Pi3B+":{"wireless": true,"measure": {"high": 85.6,"long": 56.5},"ethernet": true }, | ||
"Pi4B1G":{"memory": "1G","wireless": true,"measure": {"high": 85.6,"long": 56.5},"ethernet": true}, | ||
"Pi4B2G":{"memory": "2G","wireless": true,"measure": {"high": 85.6,"long": 56.5},"ethernet": true}, | ||
"Pi4B4G":{"memory": "4G","wireless": true,"measure": {"high": 85.6,"long": 56.5 },"ethernet": true}, | ||
"Pi4B8G": {"memory": "8G","wireless": true,"measure": {"high": 85.6,"long": 56.5 },"ethernet": true} | ||
} | ||
``` | ||
### extends from path | ||
``` | ||
let config = await ConfigExtends.load(path.join(__dirname,'test-1')); | ||
console.log(JSON.stringify(config,null,2)); | ||
``` | ||
structure folder | ||
- test-1 | ||
- folder1 | ||
- file1.yaml content => c: 3 | ||
- folder2 | ||
- file1.yaml content => _extends: [file,folder1.file1] | ||
d: 1 | ||
- file2.yaml content => _extends: [folder2.file1] | ||
e: 1 | ||
- file.yaml content => a: 1 | ||
b: "b" | ||
Result | ||
## Test | ||
``` | ||
{ | ||
"file": { | ||
"a": 1, | ||
"b": "b" | ||
}, | ||
"folder1": { | ||
"file1": { | ||
"c": 3 | ||
} | ||
}, | ||
"folder2": { | ||
"file1": { | ||
"d": 1, | ||
"a": 1, | ||
"b": "b", | ||
"c": 3 | ||
}, | ||
"file2": { | ||
"e": 1, | ||
"d": 1, | ||
"a": 1, | ||
"b": "b", | ||
"c": 3 | ||
} | ||
} | ||
} | ||
npm test | ||
``` | ||
@@ -53,3 +53,3 @@ const path = require('path'); | ||
//example from folder | ||
let config = await ConfigExtends.load(path.join(__dirname,'test-1')); | ||
let config = await ConfigExtends.apply(path.join(__dirname,'test-1'),'result'); | ||
console.log(JSON.stringify(config,null,2)); | ||
@@ -56,0 +56,0 @@ } |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22383
27
294
2
278
+ Addedminimist@^1.2.5
+ Addedminimist@1.2.8(transitive)