1 |
|
2 | (function() {
|
3 | var MainConfig, path, readConfig;
|
4 |
|
5 | path = require("path");
|
6 |
|
7 | readConfig = function(f, cb) {
|
8 | var m;
|
9 | try {
|
10 | m = require(f);
|
11 | } catch (e) {
|
12 | return cb(e);
|
13 | }
|
14 | return cb(null, m);
|
15 | };
|
16 |
|
17 | MainConfig = (function() {
|
18 |
|
19 | function MainConfig(_arg) {
|
20 | var layer, name, servers;
|
21 | this.start = _arg.start, this.install = _arg.install, this.plugins = _arg.plugins, this.cron = _arg.cron, servers = _arg.servers;
|
22 | this.layers = [];
|
23 | for (name in servers) {
|
24 | layer = servers[name];
|
25 | this.layers[name] = this.normalizeLayer(layer);
|
26 | }
|
27 | }
|
28 |
|
29 | MainConfig.prototype.getLayerByName = function(name) {
|
30 | return this.layers[name] || (function() {
|
31 | throw new Error("Cannot find server named " + name + ". Check your config file");
|
32 | })();
|
33 | };
|
34 |
|
35 | MainConfig.prototype.getStart = function() {
|
36 | return this.start;
|
37 | };
|
38 |
|
39 | MainConfig.prototype.getInstall = function() {
|
40 | return this.install;
|
41 | };
|
42 |
|
43 | MainConfig.prototype.getPlugins = function() {
|
44 | return this.plugins;
|
45 | };
|
46 |
|
47 | MainConfig.prototype.disablePlugins = function() {
|
48 | return this.plugins = null;
|
49 | };
|
50 |
|
51 | MainConfig.prototype.getLayerNames = function() {
|
52 | return Object.keys(this.layers);
|
53 | };
|
54 |
|
55 | MainConfig.prototype.getCron = function() {
|
56 | if (this.cron != null) {
|
57 | return this.normalizeCron(this.cron);
|
58 | } else {
|
59 | return false;
|
60 | }
|
61 | };
|
62 |
|
63 | MainConfig.prototype.normalizeCron = function(cron) {
|
64 | var matches, warning;
|
65 | if (typeof cron === "string") {
|
66 | console.log("using deprecated cron syntax");
|
67 | matches = cron.match(/([0-9\s\*]+)\s+(.*)/);
|
68 | if (!(matches != null)) {
|
69 | throw new Error("Invalid Cron: " + this.cron);
|
70 | }
|
71 | warning = "you should switch your cron to instead be the following in ggg.js:\n cron: { cronName: {time: '" + matches[1] + "', command: '" + matches[2] + "' } }";
|
72 | console.log(warning);
|
73 | cron = {
|
74 | "default": {
|
75 | time: matches[1],
|
76 | command: matches[2]
|
77 | }
|
78 | };
|
79 | }
|
80 | return cron;
|
81 | };
|
82 |
|
83 | MainConfig.prototype.normalizeLayer = function(config) {
|
84 | if (typeof config === "string") {
|
85 | config = {
|
86 | hosts: [config]
|
87 | };
|
88 | } else if (Array.isArray(config)) {
|
89 | config = {
|
90 | hosts: config
|
91 | };
|
92 | } else if (typeof config.hosts === "string") {
|
93 | config.hosts = [config.hosts];
|
94 | }
|
95 | return config;
|
96 | };
|
97 |
|
98 | MainConfig.loadFromFile = function(file, cb) {
|
99 | return readConfig(file, function(err, config) {
|
100 | if (err != null) {
|
101 | return cb(new Error("Could not load config file " + file + ". Are you sure it exists?"));
|
102 | }
|
103 | return cb(null, new MainConfig(config));
|
104 | });
|
105 | };
|
106 |
|
107 | return MainConfig;
|
108 |
|
109 | })();
|
110 |
|
111 | module.exports = MainConfig;
|
112 |
|
113 | }).call(this);
|