This repository was archived by the owner on Sep 21, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
100 lines (85 loc) · 2.65 KB
/
Copy pathindex.js
File metadata and controls
100 lines (85 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
'use strict';
var path = require('path'),
fs = require('fs'),
yaml = require('js-yaml'),
_ = require('lodash'),
GeminiError = require('../errors/gemini-error'),
BrowserConfig = require('./browser-config'),
parseOptions = require('./options');
/**
* @param {Object|String} configData data of the config or path to file
* @param {Object} allowOverrides
* @param {Boolean} allowOverrides.env
* @param {Boolean} allowOverrides.cli
*/
function Config(configData, allowOverrides) {
allowOverrides = _.defaults(allowOverrides || {}, {
env: false,
cli: false
});
var env = allowOverrides.env? process.env : {},
argv = allowOverrides.cli? process.argv : [];
if (_.isString(configData)) {
configData = readConfigData(configData);
}
var parsed = parseOptions({
options: configData,
env: env,
argv: argv
});
this.system = parsed.system;
this.sets = parsed.sets;
this._configs = _.mapValues(parsed.browsers, function(data, id) {
return new BrowserConfig(id, this.system, data);
}.bind(this));
}
Config.prototype.forBrowser = function(id) {
return this._configs[id];
};
Config.prototype.getBrowserIds = function() {
return Object.keys(this._configs);
};
Object.defineProperty(Config.prototype, 'coverageEnabled', {
get: function() {
return this.system.coverage.enabled;
}
});
function readConfigData(filePath) {
var configData = readYAMLFile(filePath),
configDir = path.dirname(filePath);
if (_.has(configData, 'system.projectRoot')) {
configData.system.projectRoot = path.resolve(configDir, configData.system.projectRoot);
} else {
_.set(configData, 'system.projectRoot', configDir);
}
return configData;
}
function readYAMLFile(configPath) {
var text = readFile(configPath);
return parseYAML(text, configPath);
}
function readFile(configPath) {
try {
return fs.readFileSync(configPath, 'utf8');
} catch (e) {
if (e.code === 'ENOENT') {
throw new GeminiError(
'Config file does not exist: ' + configPath,
'Specify config file or configure your project by following\nthe instructions:\n\n' +
'https://github.com/bem/gemini#configuration'
);
}
throw e;
}
}
function parseYAML(source, filename) {
try {
return yaml.safeLoad(source);
} catch (e) {
throw new GeminiError('Error while parsing a config file: ' + filename + '\n' +
e.reason + ' ' + e.mark,
'Gemini config should be valid YAML file.'
);
}
}
module.exports = Config;