-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.coffee
51 lines (40 loc) · 1.07 KB
/
index.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
fs = require 'fs'
class Configurator
constructor: ->
@defaultEnv = 'development'
# Loads configuration synchronously
sync: ->
@_loadConfigFileSync()
# Loads configuration asynchronously
async: (callback) ->
@_loadConfigFileAsync =>
callback() if callback?
# Tells configurator which environment to default to
defaultsTo: (env) ->
@defaultEnv = env
# Gets a value for the current env
get: (item) ->
@env()[item]
# Gets the current env
env: ->
env = process.env.NODE_ENV
env ?= @defaultEnv
@[env]
_loadConfigFileSync: ->
data = fs.readFileSync process.cwd()+'/config.json'
json = JSON.parse data
for key, value of json
@[key] = value
this
_loadConfigFileAsync: (callback) ->
data = fs.readFile process.cwd()+'/config.json', (err, data) =>
if err then throw err
json = JSON.parse data
for key, value of json
@[key] = value
callback()
configurator = null
module.exports = ( ->
configurator = new Configurator() unless configurator?
return configurator
).call()