Skip to content

Instantly share code, notes, and snippets.

@orzFly
Last active August 29, 2015 14:23
Show Gist options
  • Save orzFly/e1730312f5656b43873c to your computer and use it in GitHub Desktop.
Save orzFly/e1730312f5656b43873c to your computer and use it in GitHub Desktop.
node-config-loader
_ = require 'lodash'
fs = require 'fs'
class Config # Singleton
constructor: (env) ->
env || = process.env.NODE_ENV
env || = 'development'
@current = @load(env)
@env = env
@onInit()
load: (env = @env) ->
config = {}
mergedParts = []
mergePart = (name, part) ->
return unless part
config = _.merge config, part
mergedParts.push name
mergePart "config.default.base", @default.base
mergePart "config.default.override.#{env}", @default.override?[env]
mergePart "config.local.base", @local?.base
mergePart "config.local.override.#{env}", @local?.override?[env]
config.env = env
config._mergedParts = mergedParts
@onLoad config
onLoad: (config) ->
winston = require 'winston'
winston.level = config.debugLevel
winston.info "配置加载完成"
for i in config._mergedParts
winston.info "配置文件包含 #{i}"
config
onInit: () ->
Q = require 'bluebird-q'
if @current.longStackSupport
Q.longStackSupport = true
process.env.BLUEBIRD_DEBUG = 1
default: require './config.default.coffee'
local: require './config.local.coffee' if fs.existsSync('./config.local.coffee')
module.exports = new Config()
module.exports =
base: # 不要用 env 这个 key,因为他会被覆盖为当前配置的名字。
debugLevel: 'debug'
longStackSupport: true
override:
production:
debugLevel: 'info'
longStackSupport: false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment