New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

fileconfig

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fileconfig - npm Package Compare versions

Comparing version 1.0.2 to 1.0.3

lib/getter.js

109

lib/fileconfig.js

@@ -9,81 +9,68 @@ /**

var util = require('util');
var fs = require('fs');
var useme = require('useme');
var path = require('path');
var jsonfile = require('jsonfile');
var YAML = require('yamljs');
function initnode(path) {
var jsonpath = path + '.json';
var ymlpath = path + '.yml';
var stats;
if (fs.existsSync(path)) {
stats = fs.lstatSync(path);
if (stats.isDirectory()) {
return new FileConfig(path);
}
} else if (fs.existsSync(jsonpath)) {
stats = fs.lstatSync(jsonpath);
if (stats.isFile()){
// expects valid json string
// no curve balls please
return jsonfile.readFileSync(jsonpath);
}
} else if (fs.existsSync(ymlpath)) {
stats = fs.lstatSync(ymlpath);
if (stats.isFile()) {
// expects valid yml string
// no curve balls please
return YAML.load(ymlpath);
}
}
}
var getter = require('./getter');
function resolveData (data, queue) {
if (data instanceof FileConfig) {
return resolveComponent(data, queue);
} else {
return resolveObject(data, queue);
}
function Component(path, value, leaf, format) {
this.path = path;
this.value = value;
this.leaf = leaf;
this.format = format;
}
function resolveComponent (component, queue) {
if (queue.length === 0) {
return component;
}
var property = queue.shift();
var refresh = property.indexOf('#') === 0;
var name = refresh === true ? property.substring(1) : property;
var data
// self
if (name.match(/^@(value|path)$/)) {
data = component[name.substring(1)];
} else {
if (refresh === true || !(name in component.value)) {
component.value[name] = initnode(util.format('%s/%s', component.path, name));
Component.prototype.$persist = function () {
// for now only persist leaves (files)
// later handle non leaves ... depth ? anyone
if (this['@leaf']) {
switch (this['@format']) {
case 'json':
jsonfile.writeFileSync(this['@path'], this['@value']);
break;
case 'yml' :
fs.writeFileSync(this['@path'], YAML.stringify(this['@value'], 2));
break;
default :
break;
}
data = component.value[name];
}
return resolveData(data, queue);
}
function resolveObject (object, queue) {
if (queue.length === 0) {
return object;
function makeComponent(location) {
var jsonpath = location + '.json';
var ymlpath = location + '.yml';
var stats;
if (fs.existsSync(location)) {
stats = fs.lstatSync(location);
if (stats.isDirectory()) {
var component = makeComponent(path.resolve(location, 'this'));
var value = component === undefined ? {} : component.value;
var format = component === undefined ? 'json' : component.format;
return new Component(location, value, false, format);
}
} else if (fs.existsSync(jsonpath)) {
return new Component(jsonpath, jsonfile.readFileSync(jsonpath), true, 'json');
} else if (fs.existsSync(ymlpath)) {
return new Component(ymlpath, YAML.load(ymlpath), true, 'yml');
}
return resolveData(object[queue.shift()], queue);
}
function lookup (component, property) {
return resolveComponent(component, property.split('/'))
return getter.get(component, property, FileConfig);
}
function FileConfig (path) {
var component = {
path : path,
value : useme.val(initnode(path + '/this'), {})
};
function replace (component, property, value) {
var queue = property.split('/');
var targetProperty = queue.pop();
var target = getter.resolveComponent(component, queue, FileConfig);
target.value[targetProperty] = value;
}
function FileConfig (location) {
var component = makeComponent(path.resolve(location));
return Proxy(component, {
get : lookup
get : lookup,
set : replace
});

@@ -90,0 +77,0 @@ }

{
"name": "fileconfig",
"version": "1.0.2",
"version": "1.0.3",
"description": "json, yml file configuration loader",

@@ -10,2 +10,5 @@ "main": "lib/fileconfig.js",

},
"bin": {
"fileconfig": "node lib/serve.js"
},
"repository": {

@@ -27,10 +30,13 @@ "type": "git",

"dependencies": {
"express": "^4.12.3",
"harmony-reflect": "^1.1.2",
"jsonfile": "^2.0.0",
"useme": "^1.0.0",
"serve-index": "^1.6.3",
"serve-static": "^1.9.2",
"yamljs": "^0.2.1"
},
"devDependencies": {
"nodeunit": "^0.9.1"
"nodeunit": "^0.9.1",
"nodeunit-express": "0.0.5"
}
}

@@ -7,7 +7,6 @@ # fileconfig

Exprimenting with json configuration file loading
Exprimenting with configuration file loading
## Install it
## Installation
```

@@ -19,4 +18,12 @@ npm install fileconfig -g

## Usage
## Use it
### binaries
#### fileconfig
fires up a directory listing of *NODE_FILECONFIG_DIR* directory
### JS library
assuming you have a configuration folder as follow :

@@ -26,7 +33,2 @@

+ /path/to/config/folder
+ apps
+ demo
this.json
+ users
hasnaer.json
+ servers

@@ -41,18 +43,2 @@ + share

- apps/demo/this.json
```json
{
"name" : "Demo Application"
}
```
- apps/demo/users/hasnaer.json
```json
{
"fullname" : "Hasnae Rehioui"
}
```
- servers/share/rnd/alpha.yml

@@ -75,12 +61,20 @@ ```yaml

var demoApp = config.apps.demo;
console.log(demoApp.name);
var defaultServer = config.servers.share.default;
console.log(defaultServer.name);
// if at some point name property was edited in apps/demo/this.json
// refresh using # prefix
demoApp = config.apps['#demo']
// if at some point name property was edited in servers/share/default.yml
// or edited at runtime as follow
defaultServer.port = 8080;
// refresh from file using # prefix
defaultServer = config.servers.share['#default'];
// you can persist/store config objects to file
defaultServer.name = "Share Aplha Server R&D";
defaultServer.$persist();
```
I also use it in my scripts, for instance :
**Example use case**
a command line program to serve a directory listing, the directories are configured
- ~/bin/nodeHarmony

@@ -92,2 +86,3 @@

```
*fileconfig relies on node harmony features, hence it requires script to be run with harmony flag on*

@@ -108,3 +103,3 @@ - ~/bin/serve

.action(function () {
var serverConfig = config.servers[program.client];
var serverConfig = config.servers.share[program.client];
var share = express();

@@ -111,0 +106,0 @@ share.use(serveIndex(serverConfig.dir, { icons : true, view : 'details' }));

{
"name": "Data share",
"port": 9090
"port": 9090,
"owner": {
"org": "labset"
}
}

@@ -11,4 +11,6 @@ /**

var jsonfile = require('jsonfile');
var request = require('nodeunit-express');
var config = FileConfig.global();
exports['fileconfig.basic'] = function (test) {

@@ -42,9 +44,63 @@ test.equal(config.server.share.port, 9090);

exports['fileconfig.pathparam'] = function (test) {
exports['fileconfig.#1.pathparam'] = function (test) {
test.equals(config['server/share/port'],8080);
test.equals(config['server/#share/port'], 9090);
test.equals(config.server['ftp/port'], 7070);
var ftp = config['server/@value/ftp']
var ftp = config['server/@value/ftp'];
test.equals(ftp.port, 7070);
test.done();
};
exports['fileconfig.#5.FCInstance'] = function (test) {
var share = config.server.share;
test.equals(share['@value'].port, share.port);
test.done();
};
exports['fileconfig.#7.inspect'] = function (test) {
var server = config.server;
test.equals(server.inspect, server.inspect);
test.done();
};
exports['fileconfig.#3.ReadWrite.json'] = function (test) {
var share = config.server.share;
test.equals(share.port, 9090);
share.port = 10001;
test.equals(share.port, 10001);
share.$persist();
share = config.server['#share'];
test.equals(share.port, 10001);
share.port = 9090;
share.$persist();
test.done();
};
exports['fileconfig.#3.ReadWrite.yml'] = function (test) {
var ftp = config.server.ftp;
test.equals(ftp.port, 7070);
ftp.port = 20002;
test.equals(ftp.port, 20002);
ftp.$persist();
ftp = config.server['#ftp'];
test.equals(ftp.port, 20002);
ftp.port = 7070;
ftp.$persist();
test.done();
};
var app = require('../lib/serve');
exports['fileconfig.#10.serve'] = function (test) {
var express = request(app);
express
.get('/server/share.json')
.expect(function (response) {
test.equal(response.statusCode, 200);
test.equal(response.headers['content-type'], 'application/json');
test.done();
express.close();
});
};

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