UNPKG

1.85 kBtext/coffeescriptView Raw
1path = require "path"
2
3# read a config file
4readConfig = (f, cb) ->
5 try
6 m = require f
7 catch e
8 return cb e
9 cb null, m
10
11class MainConfig
12
13 constructor: ({@start, @install, @plugins, @cron, servers}) ->
14 # normalize to an array for multi server deploys
15 @layers = []
16 for name, layer of servers
17 @layers[name] = @normalizeLayer layer
18
19 getLayerByName: (name) ->
20 @layers[name] || throw new Error "Cannot find server named #{name}. Check your config file"
21
22 getStart: ->
23 @start
24
25 getInstall: ->
26 @install
27
28 getPlugins: ->
29 @plugins
30
31 disablePlugins: ->
32 @plugins = null
33
34 getLayerNames: ->
35 Object.keys @layers
36
37 #returns false if not defined
38 getCron: ->
39 if @cron? then @normalizeCron @cron else false
40
41 normalizeCron: (cron) ->
42 # support the old syntax
43 if typeof cron == "string"
44 console.log "using deprecated cron syntax"
45 matches = cron.match(/([0-9\s\*]+)\s+(.*)/)
46 if not matches? then throw new Error "Invalid Cron: #{@cron}"
47 warning =
48 """
49 you should switch your cron to instead be the following in ggg.js:
50 cron: { cronName: {time: '#{matches[1]}', command: '#{matches[2]}' } }
51 """
52 console.log warning
53 cron = {default: {time: matches[1], command: matches[2]}}
54
55 return cron
56
57 normalizeLayer: (config) ->
58 if typeof config == "string"
59 config = {
60 hosts: [config]
61 }
62 else if Array.isArray config
63 config = {
64 hosts: config
65 }
66 else if typeof config.hosts == "string"
67 config.hosts = [config.hosts]
68
69 return config
70
71 @loadFromFile = (file, cb) ->
72 readConfig file, (err, config) ->
73 if err? then return cb new Error "Could not load config file #{file}. Are you sure it exists?"
74 cb null, new MainConfig config
75
76module.exports = MainConfig