1 | var path = require('path');
|
2 | var fs = require('fs');
|
3 | var _ = require('underscore');
|
4 |
|
5 |
|
6 |
|
7 | Config = function(root) {
|
8 |
|
9 |
|
10 | var configPath = path.join(root, 'smart.json');
|
11 |
|
12 |
|
13 | if (fs.existsSync(configPath)) {
|
14 |
|
15 | var config = this._parseConfig(configPath);
|
16 |
|
17 |
|
18 | _.extend(this, config);
|
19 |
|
20 |
|
21 | if (this.meteor)
|
22 | this.meteor.root = root;
|
23 | _.each(this.packages, function(pkg) { pkg.root = root; });
|
24 | }
|
25 | };
|
26 |
|
27 | Config.prototype._parseConfig = function(configPath) {
|
28 |
|
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 |
|
43 | Config.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 |
|
58 | module.exports = Config;
|