-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
51 lines (47 loc) · 2.36 KB
/
index.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
const chalk = require("chalk");
const separator = chalk.grey('\n====================================================================================================\n')
module.exports = (on, config, dir) => {
console.log(separator)
console.log('Starting plugin: ' + chalk.green('cypress-env\n'))
if (dir === undefined) {
console.log(chalk.red('ConfigurationError!\n') + chalk.yellow('You must specify the "__dirname" element in the config, change the require to: \n') + 'require("cypress-env")(on, config, __dirname)')
console.log(separator)
throw new Error('You must specify the "__dirname" element in the config, change the require to:\nrequire("cypress-env")(on, config, __dirname)')
}
loadLocalENV(config, dir)
return config;
};
async function getLocalEnv(config, dir) {
const envName = config.env.envName
const environmentFilename = dir + `/env.config/${envName}.json`
console.log(`Extracting local configurations from:` + chalk.cyan(` ${environmentFilename}\n`))
try {
const settings = require(environmentFilename)
const cypressSettings = require('./cySettings.json')
Object.keys(settings).forEach(item => {
if (cypressSettings.settings.includes(item)) {
config[item] = settings[item]
console.log(`${chalk.yellow(item)} : ${JSON.stringify(settings[item], null, 1)}`)
}
})
if (settings.env) {
config.env = {
...settings.env,
...config.env,
}
console.log(chalk.yellow('env ') + `: ${JSON.stringify(config.env, null, 1)}`)
}
console.log(chalk.green('\n√ ') + chalk.white('Configurations loaded correctly for the environment: ') + chalk.cyan(`< ${envName.toUpperCase()} >`))
} catch (err) {
console.error(err)
}
}
async function loadLocalENV(config, dir) {
if (config.env.envName && config.env.envName !== '$ENV' && config.env.envName !== '%ENV%') {
await getLocalEnv(config, dir)
} else if (!config.env.envName) {
console.log(chalk.green('√ ') + chalk.white('No environment configuration specified, using basic configuration!'))
} else if (config.env.envName === '$ENV' && config.env.envName === '%ENV%') {
console.log('Test configuration error dependency needed: cypress-env not configured correctly')
}
}