Skip to content

Commit

Permalink
override with username based props
Browse files Browse the repository at this point in the history
  • Loading branch information
robb1e committed Sep 23, 2010
1 parent 7055dc3 commit 3ad3199
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -6,9 +6,9 @@ Node properties allows you to externalise configuration in a consistent manner u

From the root of your application (where the executing directory will be) create a folder called properties and a file within this called 'build.json'. Then in your application require this module and access your properties directly. An example is reading the port to start your application on:

<pre>
var props = require('properties');
console.log(props.port);
</pre>

# Future Scope

I'd like to be able to get the current executing username and read a file called username.json and override any properties from the build file to allow simple local customisation.
You can override properties by supplying a JSON file with the same name as the user who is executing the process, e.g. properties/robbieclutton.json. Any properties included here will overwrite those in the build.json file enabling local customisations.
17 changes: 15 additions & 2 deletions lib/properties.js
@@ -1,12 +1,25 @@
var fs = require('fs');

var baseProperties = process.cwd() + '/properties/build.json';
var username = process.ENV['USER'];
var base_properties_file = process.cwd() + '/properties/build.json';
var user_properties_file = process.cwd() + '/properties/' + username + '.json';
var properties = {};
var user_properties = {};

try {
properties = JSON.parse(fs.readFileSync(baseProperties, 'utf8'));
properties = JSON.parse(fs.readFileSync(base_properties_file, 'utf8'));
} catch (err){
console.log(err);
}

try {
user_properties = JSON.parse(fs.readFileSync(user_properties_file, 'utf8'));
} catch (err){
// do nothing and don't override
}

for (attrname in user_properties) {
properties[attrname] = user_properties[attrname];
}

module.exports = properties;

0 comments on commit 3ad3199

Please sign in to comment.