Created
December 1, 2012 08:23
-
-
Save cou929/4181085 to your computer and use it in GitHub Desktop.
node.js の project のための config ファイルを管理する.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jslint indent: 4*/ | |
/*jslint node: true */ | |
'use strict'; | |
var fs = require('fs'), | |
conf = {}, | |
conf_dir = '/config'; | |
conf.home = process.env.NODE_HOME || '.'; | |
conf.env = process.env.NODE_ENV || 'development'; | |
conf = extend(conf, readJSON(conf.home + conf_dir + '/config.json')); | |
if (fs.existsSync(conf.home + conf_dir + '/config_' + conf.env + '.json')) { | |
conf = extend(conf, readJSON(conf.home + conf_dir + '/config_' + conf.env + '.json')); | |
} | |
if (fs.existsSync(conf.home + conf_dir + '/config_local.json')) { | |
conf = extend(conf, readJSON(conf.home + conf_dir + '/config_local.json')); | |
} | |
module.exports = conf; | |
function readJSON(path) { | |
return JSON.parse(fs.readFileSync(path, 'UTF-8')); | |
} | |
function extend(target, obj) { | |
var result, | |
obj_keys = Object.keys(obj); | |
result = Object.keys(target).length ? clone(target) : {}; | |
obj_keys.forEach(function (key) { | |
Object.defineProperty(result, key, Object.getOwnPropertyDescriptor(obj, key)); | |
}); | |
return result; | |
} | |
function clone(obj) { | |
return extend({}, obj); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment