Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

config-extends

Package Overview
Dependencies
Maintainers
3
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

config-extends - npm Package Compare versions

Comparing version 0.0.7 to 0.0.8

debug.js

2

.vscode/launch.json

@@ -14,5 +14,5 @@ {

],
"program": "${workspaceFolder}/test/test"
"program": "${workspaceFolder}/debug"
}
]
}

@@ -8,15 +8,14 @@ const Path = require('path');

static async apply(source,target){
let config = {};
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(source,filePath);
let config = {},files;
let sourcePath = Path.resolve(source);
if(!Fs.existsSync(sourcePath))throw sourcePath+" nor exists!";
if(ConfigExtends._isDirectory(sourcePath)){
files = await ConfigExtends._getFiles(sourcePath,'');
for(let i=0;i<files.length;i++){
let filePath = files[i];
let fullPath = Path.join(sourcePath,filePath);
let extension = Path.parse(fullPath).ext.toLocaleLowerCase();
let content = Fs.readFileSync(fullPath,'utf8');
let data = extension=='.yaml' || extension == '.yml'?Yaml.safeLoad(content):JSON.parse(content);
let parseFilePath = Path.parse(filePath);
let a = parseFilePath.dir.split(Path.sep);
let b = parseFilePath.name.split('.');
let names = a != ''?a.concat(b):b;
let names = ConfigExtends._getNames(filePath);
ConfigExtends._setData(config,names,data);

@@ -26,14 +25,41 @@ }

}else{
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);
let extension = Path.parse(sourcePath).ext.toLocaleLowerCase();
let content = Fs.readFileSync(sourcePath,'utf8');
let source = extension=='.yaml' || extension == '.yml'?Yaml.safeLoad(content):JSON.parse(content);
config = ConfigExtends.extends(source);
}
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"});
let targetPath = Path.resolve(target);
if(ConfigExtends._isDirectory(targetPath)){
for(let i=0;i<files.length;i++){
let filePath = files[i];
let names = ConfigExtends._getNames(filePath);
let fileData = this._getData(config,names);
let fullPath = Path.join(targetPath,filePath);
ConfigExtends._writeFile(fullPath,fileData);
}
}
else{
ConfigExtends._writeFile(targetPath,config);
}
}
return config;
}
static _isDirectory(path){
return (Fs.existsSync(path))?Fs.lstatSync(path).isDirectory():Path.parse(path).ext.toLocaleLowerCase() == '';
}
static _getNames(path){
let parsePath = Path.parse(path);
let a = parsePath.dir.split(Path.sep);
let b = parsePath.name.split('.');
return a != ''?a.concat(b):b;
}
static _writeFile(path,data){
let extension = Path.parse(path).ext.toLocaleLowerCase();
let content = extension=='.yaml' || extension == '.yml'?Yaml.safeDump(data,{ noRefs: true }):JSON.stringify(data,null,2);
if(!Fs.existsSync(Path.dirname(path)))
Fs.mkdirSync(Path.dirname(path), { recursive: true });
Fs.writeFileSync(path,content,{encoding: "utf8"});
}
static extends(source,...args){

@@ -68,3 +94,3 @@ let _config =Object.assign(source,args);

let baseFullname = _extends[i];
let base = ConfigExtends._getData(config,baseFullname);
let base = ConfigExtends._getData(config,baseFullname.split('.'));
if(base===undefined)throw baseFullname+" not found";

@@ -77,3 +103,3 @@ if(!base['_completed'])base=ConfigExtends._completeObject(config,base);

let baseFullname = _extends;
let base = ConfigExtends._getData(config,baseFullname);
let base = ConfigExtends._getData(config,baseFullname.split('.'));
if(base===undefined)throw baseFullname+" not found";

@@ -109,4 +135,3 @@ if(!base['_completed'])base=ConfigExtends._completeObject(config,base);

}
static _getData(config,fullname){
let names = fullname.split('.');
static _getData(config,names){
let _config = config;

@@ -113,0 +138,0 @@ for(let i=0;i<names.length;i++){

{
"name": "config-extends",
"version": "0.0.7",
"version": "0.0.8",
"description": "Allow to extend yaml or json files configuration",

@@ -35,4 +35,5 @@ "main": "./lib/config-extends",

"mocha": "^8.2.1",
"chai": "^4.2.0"
"chai": "^4.2.0",
"dir-compare": "^2.4.0"
}
}

@@ -1,59 +0,108 @@

const path = require('path');
const assert = require('assert');
const dircompare = require('dir-compare');
const ConfigExtends = require('../lib/config-extends');
var assert = require('assert');
// var assert = require('assert');
// describe('Extends simple', function() {
// describe('Extend {data:{_extends:"base",d:3,e:4},base: {a:1,b:2}}', function() {
// let source ={data:{_extends:'base',d:3,e:4},base: {a:1,b:2}};
// let result = JSON.stringify(ConfigExtends.extends(source));
// let expected = JSON.stringify({data:{d:3,e:4,a:1,b:2},base:{a:1,b:2}});
// it('should return {data:{d:3,e:4,a:1,b:2},base:{a:1,b:2}}', function() {
// assert.strictEqual(result,expected);
// });
// });
// });
describe('extends', function() {
describe('Simple extension', function() {
let source ={
data : {_extends:'base',d:3,e:4},
base: {a:1,b:2}
};
let expected = JSON.stringify({data:{d:3,e:4,a:1,b:2},
base:{a:1,b:2}
});
let result = JSON.stringify(ConfigExtends.extends(source));
it('should return '+expected, function() {
assert.strictEqual(result,expected);
});
});
describe('Chain extension', function() {
let source ={
data: { 1: {_extends:'base',d:3,e:4},
2: {_extends:'data.1', f:3,g:4},
},
base: {a:1,b:2}
}
let expected = JSON.stringify({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}
});
let result = JSON.stringify(ConfigExtends.extends(source));
it('should return '+expected, function() {
assert.strictEqual(result,expected);
});
});
describe('Multiple extension', function() {
let source ={
data: { 1: {_extends:'base',d:3,e:4},
2: {_extends:['base','base2'], f:3,g:4},
},
base: {a:1,b:2},
base2: {h:'a',i:'b'}
};
let expected = JSON.stringify({data:{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"}
});
let result = JSON.stringify(ConfigExtends.extends(source));
it('should return '+expected, function() {
assert.strictEqual(result,expected);
});
});
});
describe('apply', function() {
describe('Extends single file', function() {
let fileSource ='test/raspberry/source/raspberry.yaml';
let filetTarget ='test/raspberry/target/raspberry.yaml';
let target ='test/raspberry/target';
let toCompare ='test/raspberry/to-compare';
let expected = 'identical';
it(expected, async ()=> {
let config = await ConfigExtends.apply(fileSource,filetTarget);
let compareResult = await dircompare.compare(target,toCompare,{compareContent:true});
let result = compareResult.same? 'identical' : 'different';
assert.strictEqual(result,expected);
});
});
describe('Extends simple folder', function() {
let source ='test/folder/source';
let target ='test/folder/target';
let toCompare ='test/folder/to-compare';
let expected = 'identical';
it(expected, async ()=> {
let config = await ConfigExtends.apply(source,target);
let compareResult = await dircompare.compare(target,toCompare,{compareContent:true});
let result = compareResult.same? 'identical' : 'different';
assert.strictEqual(result,expected);
});
});
describe('Extends tetris folder', function() {
let source ='test/tetris/source';
let target ='test/tetris/target';
let toCompare ='test/tetris/to-compare';
let expected = 'identical';
it(expected, async ()=> {
let config = await ConfigExtends.apply(source,target);
let compareResult = await dircompare.compare(target,toCompare,{compareContent:true});
let result = compareResult.same? 'identical' : 'different';
assert.strictEqual(result,expected);
});
});
describe('Extends pacman folder', function() {
let source ='test/pacman/source';
let target ='test/pacman/target';
let toCompare ='test/pacman/to-compare';
let expected = 'identical';
it(expected, async ()=> {
let config = await ConfigExtends.apply(source,target);
let compareResult = await dircompare.compare(target,toCompare,{compareContent:true});
let result = compareResult.same? 'identical' : 'different';
assert.strictEqual(result,expected);
});
});
});
// let source ={
// data : {_extends:'base',d:3,e:4},
// base: {a:1,b:2}
// };
// let config = ConfigExtends.extends(source);
// console.log(JSON.stringify(config,null,2));
// let source ={
// data: { 1: {_extends:'base',d:3,e:4},
// 2: {_extends:'data.1', f:3,g:4},
// },
// base: {a:1,b:2}
// }
// let config = ConfigExtends.extends(source);
// console.log(JSON.stringify(config,null,2));
// let source ={
// data: { 1: {_extends:'base',d:3,e:4},
// 2: {_extends:['base','base2'], f:3,g:4},
// },
// base: {a:1,b:2},
// base2: {h:'a',i:'b'}
// };
// let config = ConfigExtends.extends(source);
// console.log(JSON.stringify(config,null,2));
(async () => {
try {
// // let config = await ConfigExtends.load(path.join(__dirname,'raspberry.yaml'));
// // console.log(JSON.stringify(config.version));
//example from folder
let config = await ConfigExtends.apply(path.join(__dirname,'test-1'),'result');
console.log(JSON.stringify(config,null,2));
}
catch (error) {
console.error(error);
}
})();

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc