fileconfig



Exprimenting with configuration file loading
Install it
npm install fileconfig -g
echo "export NODE_FILECONFIG_DIR=$myconfigpath" >> ~/.profile
source ~/.profile
Use it
1. binaries
fileconfig
fires up a directory listing of NODE_FILECONFIG_DIR directory
2. JS library
assuming you have a configuration folder as follow :
+ /path/to/config/folder
+ servers
+ share
default.yml > links to rnd/alpha.yml
+ rnd
alpha.yml
+ qa
beta.yml
+ users
hasnaer.yml
- servers/share/rnd/alpha.yml
name: "Share Alpha Server"
port: 9090
dir: "${env.SHARE_DIR}"
owner : ${/users/hasnaer}
- it is possible to reference environment variables using
${env.<VAR>}
- it is possible to reference other components from root context using
${/<COMPONENT>}
- it is possible to reference other components from relative context using
${./<COMPONENT>}
Then you can fetch data as follow :
var FileConfig = require('fileconfig');
var config = new FileConfig("/path/to/config/folder");
var defaultServer = config.servers.share.default;
console.log(defaultServer.name);
console.log(defaultServer.owner);
defaultServer.port = 8080;
defaultServer = config.servers.share['#default'];
defaultServer.name = "Share Aplha Server R&D";
defaultServer.$persist();
Example use case
a command line program to serve a directory listing, the directories are configured
#! /usr/bin/env bash
node --harmony $@
fileconfig relies on node harmony features, hence it requires script to be run with harmony flag on
#! /usr/bin/env nodeHarmony
var config = require('fileconfig').global();
var program = require('commander');
var express = require('express');
var serveIndex = require('serve-index');
program
.command('share')
.option('-c, --client', 'select share client', 'default')
.action(function () {
var serverConfig = config.servers.share[program.client];
var share = express();
share.use(serveIndex(serverConfig.dir, { icons : true, view : 'details' }));
var server = share.listen(serverConfig.port,
function () {
console.log(serverConfig.name + ' is running on port ' + server.address().port);
});
});
program.parse(process.argv);
Now I can run the following commands
serve share
serve share -c qa/beta