UNPKG

4.38 kBJavaScriptView Raw
1var _ = require('underscore'),
2 fu = require('./fileUtil'),
3 path = require('path'),
4 vm = require('vm');
5
6/**
7 * Reads the RAW JSON for a lumbar config file.
8 */
9exports.readConfig = function(lumbarFile) {
10 try {
11 var data = '(' + fu.readFileSync(lumbarFile) + ')';
12
13 // Yes this is totally unsafe, but we don't want the strictness of pure JSON for our
14 // config files and if you are running an untrusted lumbar file you already have concerns.
15 return vm.runInThisContext(data, lumbarFile);
16 } catch (err) {
17 var line;
18 try {
19 var esprima = require('esprima');
20 console.log(err.stack, esprima.parse(data));
21 } catch (err) {
22 if (err.lineNumber) {
23 line = ':' + err.lineNumber;
24 }
25 }
26 throw new Error('Failed to load config ' + lumbarFile + line + ': ' + err);
27 }
28};
29
30/**
31 *
32 * @name load
33 * @function This function loads the lumbar JSON file, and returns
34 * helper methods associated with accessing its specific data.
35 * @param {string} lumbarFile the location of the lumbar file.
36 * @return {Object}
37 */
38exports.load = function(lumbarFile) {
39 fu.lookupPath('');
40
41 var config = exports.readConfig(lumbarFile);
42 fu.lookupPath(path.dirname(lumbarFile));
43
44 return exports.create(config);
45};
46
47exports.create = function(config) {
48 var packageList, moduleList;
49
50 function loadPackageList() {
51 if (!config.packages) {
52 config.packages = { web: { name: '' } };
53 }
54
55 packageList = _.keys(config.packages);
56 }
57 function loadModuleList() {
58 if (!config.modules) {
59 throw new Error('No modules object defined: ' + JSON.stringify(config, undefined, 2));
60 }
61 moduleList = _.keys(config.modules);
62 }
63
64 loadPackageList();
65 loadModuleList();
66
67 return {
68 /** @typedef {Object} The raw lumbar file as a JSON object. */
69 attributes: config,
70 loadPrefix: function() {
71 return config.loadPrefix || '';
72 },
73
74 /**
75 *
76 * @name packageList
77 * @function This function returns the list of packages found
78 * in the lumbar file.
79 * @return {Array.<Object>} array of package(s).
80 */
81 packageList: function() {
82 return packageList;
83 },
84
85 /**
86 *
87 * @name combineModules
88 * @function This functions checks to see if the package, pkg,
89 * is going to combine all its modules or not.
90 * @param {string} pkg the name of the package
91 * @return {boolean} is this package destined to be combined?
92 */
93 combineModules: function(pkg) {
94 if (config && config.packages && config.packages[pkg]) {
95 return config.packages[pkg].combine;
96 }
97 return false;
98 },
99 platformList: function(pkg) {
100 if (!pkg) {
101 return config.platforms || [''];
102 } else {
103 if (config.packages[pkg]) {
104 return config.packages[pkg].platforms || this.platformList();
105 }
106 return this.platformList();
107 }
108 },
109
110 moduleList: function(pkg) {
111 return (config.packages[pkg] || {}).modules || _.keys(config.modules);
112 },
113
114 module: function(name) {
115 var ret = config.modules[name];
116 if (ret) {
117 ret.name = name;
118 }
119 return ret;
120 },
121 isAppModule: function(module) {
122 var app = config.application;
123 return (app && app.module) === (module.name || module);
124 },
125 scopedAppModuleName: function(module) {
126 var app = config.application;
127 if (this.isAppModule(module)) {
128 return 'module.exports';
129 } else {
130 var app = config.application;
131 return app && app.name;
132 }
133 },
134
135 routeList: function(module) {
136 return config.modules[module].routes;
137 },
138
139 serialize: function() {
140 function objectClone(object) {
141 var clone = object;
142
143 if (object && object.serialize) {
144 // Allow specialized objects to handle themselves
145 clone = object.serialize();
146 } else if (_.isArray(object)) {
147 clone = _.map(object, objectClone);
148 } else if (_.isObject(object)) {
149 clone = {};
150 _.each(object, function(value, name) {
151 clone[name] = objectClone(value);
152 });
153 }
154
155 // Collapse simple resources
156 if (clone && clone.src && _.keys(clone).length === 1) {
157 clone = clone.src;
158 }
159
160 return clone;
161 }
162
163 return objectClone(this.attributes);
164 }
165 };
166};
167