UNPKG

1.35 kBJavaScriptView Raw
1var path = require('path');
2var fs = require('fs');
3var _ = require('underscore');
4
5// A wrapper class for `smart.json` configurations
6
7Config = function(root) {
8
9 // Build up path
10 var configPath = path.join(root, 'smart.json');
11
12 // Make sure path exists
13 if (fs.existsSync(configPath)) {
14
15 var config = this._parseConfig(configPath);
16
17 // Add all settings in config to this object
18 _.extend(this, config);
19
20 // store where we got this from, so we can absolutize later
21 if (this.meteor)
22 this.meteor.root = root;
23 _.each(this.packages, function(pkg) { pkg.root = root; });
24 }
25};
26
27Config.prototype._parseConfig = function(configPath) {
28 // Read the config and parse it
29 var config;
30
31 try {
32
33 config = fs.readFileSync(configPath).toString();
34 config = JSON.parse(config);
35 config = this._expandPackages(config);
36
37 return config;
38 } catch(e) {
39 throw('Error: there was a problem parsing ' + configPath + ':\n' + e.message);
40 }
41};
42
43Config.prototype._expandPackages = function(config) {
44 if (config.packages) {
45 _.each(config.packages, function(pkg, name) {
46 if (_.isString(config.packages[name])) {
47 var version = config.packages[name];
48 config.packages[name] = {
49 version: version
50 };
51 }
52 });
53 }
54
55 return config;
56};
57
58module.exports = Config;