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
124 lines (103 loc) Β· 3.18 KB
/
Copy pathindex.js
File metadata and controls
124 lines (103 loc) Β· 3.18 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'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} config config object or path to config file
* @param {Object} allowOverrides
* @param {Boolean} allowOverrides.env
* @param {Boolean} allowOverrides.cli
*/
function Config(config, allowOverrides) {
allowOverrides = _.defaults(allowOverrides || {}, {
env: false,
cli: false
});
var env = allowOverrides.env? process.env : {},
argv = allowOverrides.cli? process.argv : [];
if (!config || _.isString(config)) {
config = readConfig(config);
}
var parsed = parseOptions({
options: config,
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);
};
Config.prototype.isCoverageEnabled = function() {
return this.system.coverage.enabled;
};
function readConfig(filePath) {
filePath = filePath || getDefaultConfig();
var config = /\.yml$/.test(filePath)? readYAMLFile(filePath) : requireModule(filePath),
configDir = path.dirname(filePath);
if (_.has(config, 'system.projectRoot')) {
config.system.projectRoot = path.resolve(configDir, config.system.projectRoot);
} else {
_.set(config, 'system.projectRoot', configDir);
}
return config;
}
function getDefaultConfig() {
var configFile = _.find(fs.readdirSync('./'), function(file) {
return /^\.gemini(\.conf)?\.(yml|js|json)$/.test(file);
});
if (!configFile) {
throw new GeminiError('No config found');
}
return configFile;
}
function requireModule(file) {
try {
return require(file);
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
throw new GeminiError('Config file does not exist: ' + file);
}
throw e;
4F2F
}
}
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;