1 | var fs = require('fs');
|
2 | var globals = require('./globals');
|
3 | var APP_PATH = globals.appPath;
|
4 | var glob = require('glob');
|
5 | var extend = require('node.extend');
|
6 | var _ = require('underscore');
|
7 | var defaultServiceConfig = require(APP_PATH + 'default-service-config.json');
|
8 | var jsonCascade = require('json-cascade');
|
9 |
|
10 | var cache = {
|
11 | diffs : {}
|
12 | , templates : {}
|
13 | , models : {}
|
14 | , views : {}
|
15 | , rawServiceConfigs : {}
|
16 | , serviceTemplates : {}
|
17 | , serviceConfigs : {}
|
18 | , services : null
|
19 | , forwards : null
|
20 | , scenarios : null
|
21 | };
|
22 |
|
23 |
|
24 | var util = {
|
25 |
|
26 | getDiff : function(path) {
|
27 |
|
28 | if(cache.diffs[path]) {
|
29 | return cache.diffs[path];
|
30 | }
|
31 |
|
32 | var model = {};
|
33 | var path = APP_PATH + 'diffs' + path;
|
34 |
|
35 | if(fs.existsSync(path)) {
|
36 | model = JSON.parse(fs.readFileSync(path, {encoding : "utf8"}));
|
37 | }
|
38 |
|
39 | cache.diffs[path] = model;
|
40 | return model;
|
41 | },
|
42 |
|
43 | getTemplate : function (path) {
|
44 |
|
45 | if(cache.templates[path]) {
|
46 | return cache.templates[path];
|
47 | }
|
48 |
|
49 | var model = {};
|
50 | var path = APP_PATH + 'templates' + path;
|
51 |
|
52 | if(fs.existsSync(path)) {
|
53 | model = JSON.parse(fs.readFileSync(path, {encoding : "utf8"}));
|
54 | }
|
55 |
|
56 | cache.templates[path] = model;
|
57 | return model;
|
58 | },
|
59 |
|
60 | getModel : function(path) {
|
61 |
|
62 | if(cache.models[path]) {
|
63 | return cache.models[path];
|
64 | }
|
65 |
|
66 | var model = {};
|
67 | var path = APP_PATH + 'models' + path;
|
68 |
|
69 | if(fs.existsSync(path)) {
|
70 | model = JSON.parse(fs.readFileSync(path, {encoding : "utf8"}));
|
71 | }
|
72 |
|
73 | cache.models[path] = model;
|
74 | return model;
|
75 | },
|
76 |
|
77 | getState : function(scenarioName, url) {
|
78 |
|
79 | if(!cache.scenarios) {
|
80 | var path = APP_PATH + 'scenarios.json';
|
81 | cache.scenarios = JSON.parse(fs.readFileSync(path, {encoding : "utf8"}));
|
82 |
|
83 | }
|
84 |
|
85 | return cache.scenarios[scenarioName][url];
|
86 | },
|
87 |
|
88 | getForwards : function() {
|
89 | if(!cache.forwards) {
|
90 | var path = APP_PATH + 'services.json';
|
91 | cache.forwards = JSON.parse(fs.readFileSync(path, {encoding : "utf8"})).forwards;
|
92 | }
|
93 |
|
94 | return cache.forwards;
|
95 | },
|
96 |
|
97 | getServices : function() {
|
98 | if(!cache.services) {
|
99 | var path = APP_PATH + 'services.json';
|
100 | cache.services = JSON.parse(fs.readFileSync(path, {encoding : "utf8"})).services;
|
101 | }
|
102 |
|
103 | return cache.services;
|
104 | },
|
105 |
|
106 | getServiceDir : function(url) {
|
107 | return util.getServices()[url];
|
108 | },
|
109 |
|
110 | getRawServiceConfig : function(serviceDir) {
|
111 |
|
112 | if(!cache.rawServiceConfigs[serviceDir]) {
|
113 | var searchPath = APP_PATH + 'services/' + serviceDir + '/*-config.json';
|
114 | var filePath = glob.sync(searchPath)[0];
|
115 | if(filePath)
|
116 | cache.rawServiceConfigs[serviceDir] = loadJson(filePath);
|
117 | else
|
118 | cache.rawServiceConfigs[serviceDir] = {};
|
119 | }
|
120 |
|
121 | return cache.rawServiceConfigs[serviceDir];
|
122 | },
|
123 |
|
124 | getServiceTemplate : function(serviceDir, templateName) {
|
125 |
|
126 | if(!cache.serviceTemplates[serviceDir]) {
|
127 |
|
128 | if(templateName) {
|
129 | var templatePath = APP_PATH + 'services/' + serviceDir + '/' + templateName;
|
130 | cache.serviceTemplates[serviceDir] = require(templatePath);
|
131 | } else {
|
132 | var searchPath = APP_PATH + 'services/' + serviceDir + '/*-template.json';
|
133 | var filePath = glob.sync(searchPath)[0];
|
134 | cache.serviceTemplates[serviceDir] = loadJson(filePath);
|
135 | }
|
136 |
|
137 | }
|
138 | return cache.serviceTemplates[serviceDir];
|
139 | },
|
140 |
|
141 | getServiceConfig : function(serviceDir) {
|
142 |
|
143 | if(!cache.serviceConfigs[serviceDir]) {
|
144 | var rawConfig = util.getRawServiceConfig(serviceDir);
|
145 | var out = generateConfig(rawConfig, defaultServiceConfig);
|
146 |
|
147 | cache.serviceConfigs[serviceDir] = out;
|
148 | }
|
149 |
|
150 | return cache.serviceConfigs[serviceDir];
|
151 | },
|
152 |
|
153 | getServiceDiff : function(serviceDir, diffFileName) {
|
154 | if(!diffFileName)
|
155 | return null;
|
156 |
|
157 | var diffPath = APP_PATH + 'services' + '/' + serviceDir + '/' + diffFileName;
|
158 | return require(diffPath);
|
159 | }
|
160 | }
|
161 |
|
162 | function loadJson(path) {
|
163 | return JSON.parse(fs.readFileSync(path, {encoding : "utf8"}));
|
164 | }
|
165 |
|
166 | function generateConfig(rawConfig, defaults) {
|
167 |
|
168 | var allStates = _.union(_.keys(rawConfig.states), _.keys(defaults.defaultStates));
|
169 | var out = {
|
170 | verbs: null,
|
171 | states : {}
|
172 | };
|
173 |
|
174 | rawConfig.states = rawConfig.states || {};
|
175 |
|
176 |
|
177 | _.each(allStates, function(state) {
|
178 |
|
179 | var a = defaults.defaultState,
|
180 | b = defaults.defaultStates[state] || {},
|
181 | c = rawConfig,
|
182 | d = rawConfig.states[state] || {};
|
183 |
|
184 | out.states[state] = {};
|
185 |
|
186 | _.each(a, function(val, key) {
|
187 | out.states[state][key] = d[key] || c[key] || b[key] || a[key];
|
188 | });
|
189 |
|
190 | });
|
191 |
|
192 | out.verbs = rawConfig.verbs || defaults.verbs;
|
193 |
|
194 | return out;
|
195 | }
|
196 |
|
197 | module.exports = util;
|
198 |
|