forked from codeceptjs/CodeceptJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
133 lines (117 loc) · 2.89 KB
/
config.js
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
125
126
127
128
129
130
131
132
133
const fs = require('fs');
const path = require('path');
const {
fileExists,
isFile,
deepMerge,
deepClone,
} = require('./utils');
const defaultConfig = {
output: './_output',
helpers: {},
include: {},
mocha: {},
bootstrap: null,
teardown: null,
hooks: [],
gherkin: {},
plugins: {
screenshotOnFail: {
enabled: true, // will be disabled by default in 2.0
},
},
};
let hooks = [];
let config = {};
/**
* Current configuration
*/
class Config {
/**
* Create a config with default options
*
* @param {*} newConfig
* @return {Object<string, *>}
*/
static create(newConfig) {
config = deepMerge(deepClone(defaultConfig), newConfig);
hooks.forEach(f => f(config));
return config;
}
/**
* Load config from a file.
* If js file provided: require it and get .config key
* If json file provided: load and parse JSON
* If directory provided:
* * try to load `codecept.conf.js` from it
* * try to load `codecept.json` from it
* If none of above: fail.
*
* @param {string} configFile
* @return {*}
*/
static load(configFile) {
configFile = path.resolve(configFile || '.');
if (!fileExists(configFile)) {
throw new Error(`Config file ${configFile} does not exist. Execute 'codeceptjs init' to create config`);
}
// is config file
if (isFile(configFile)) {
return loadConfigFile(configFile);
}
// is path to directory
const jsConfig = path.join(configFile, 'codecept.conf.js');
if (isFile(jsConfig)) {
return loadConfigFile(jsConfig);
}
const jsonConfig = path.join(configFile, 'codecept.json');
if (isFile(jsonConfig)) {
return loadConfigFile(jsonConfig);
}
throw new Error(`Can not load config from ${jsConfig} or ${jsonConfig}\nCodeceptJS is not initialized in this dir. Execute 'codeceptjs init' to start`);
}
/**
* Get current config.
* @param {string} key
* @param {*} val
* @return {*}
*/
static get(key, val) {
if (key) {
return config[key] || val;
}
return config;
}
static addHook(fn) {
hooks.push(fn);
}
/**
* Appends values to current config
*
* @param {Object<string, *>} additionalConfig
* @return {Object<string, *>}
*/
static append(additionalConfig) {
return config = deepMerge(config, additionalConfig);
}
/**
* Resets config to default
* @return {Object<string, *>}
*/
static reset() {
hooks = [];
return config = { ...defaultConfig };
}
}
module.exports = Config;
function loadConfigFile(configFile) {
// .conf.js config file
if (path.extname(configFile) === '.js') {
return Config.create(require(configFile).config);
}
// json config provided
if (path.extname(configFile) === '.json') {
return Config.create(JSON.parse(fs.readFileSync(configFile, 'utf8')));
}
throw new Error(`Config file ${configFile} can't be loaded`);
}