-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
81 lines (63 loc) · 3 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
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
const camelcase = require('camelcase')
const clonedeep = require('lodash.clonedeep')
const pkgName = '[cypress-dotenv]'
/**
* Checks if something that was parsed as a number should be a string instead.
* Specifically for cases where a number has a leading zero
* See: https://github.com/morficus/cypress-dotenv/issues/32
*/
function checkIfTrulyNumber({ parsedValue, rawValue }) {
let correctValue = parsedValue
if (typeof parsedValue === 'number' && rawValue.startsWith('0')) {
// if env var has a leading zero, then it must be a string and not a number
correctValue = rawValue
}
return correctValue
}
/**
* Cypress dotenv plugin
*
* @param {object} cypressConfig - The cypress config object
* @param {object} dotEnvConfig - (optional) The dotenv config object, ref: https://www.npmjs.com/package/dotenv#config
* @param {boolean} all - (optional) Whether to return all env variables. If set to false (default), only env variables prefixed with CYPRESS_ are returned.
* @returns {object} The cypress config with an augmented `env` property
*/
module.exports = (cypressConfig, dotEnvConfig, all = false) => {
if (!cypressConfig) {
console.warn(`${pkgName} did not receive a Cypress config object, so no environment variables will be applied`)
return {}
}
const cypressPrefix = 'CYPRESS_'
// load the content of the .env file, then parse each variable to the correct type (string, number, boolean, etc.)
const rawEnvVars = require('dotenv').config(dotEnvConfig).parsed
// if no env vars were found, then there is nothing to do here (most likely an empty or non-existing .env file)
if (rawEnvVars === undefined || rawEnvVars === null || Object.keys(rawEnvVars).length === 0) {
return cypressConfig
}
const dotenvParseVariables = require('dotenv-parse-variables')
const parsedEnvVars = dotenvParseVariables(rawEnvVars)
const enhancedConfig = clonedeep(cypressConfig)
enhancedConfig.env = enhancedConfig.env || {}
// get the name of all env vars that relate to cypress
const cypressEnvVarKeys = all
? Object.keys(parsedEnvVars)
: Object.keys(parsedEnvVars).filter((envName) => envName.startsWith(cypressPrefix))
cypressEnvVarKeys.forEach((originalName) => {
// remove the CYPRESS_ prefix from the env var name
const pattern = new RegExp(`^${cypressPrefix}`, 'g')
const cleanName = originalName.replace(pattern, '')
// convert the env var name from snake_case to camelCase
const camelCaseName = camelcase(cleanName)
const parsedValue = parsedEnvVars[originalName]
const rawValue = process.env[originalName]
let effectiveValue = typeof parsedValue === 'string' ? rawValue : parsedValue
if (typeof effectiveValue === 'number') {
effectiveValue = checkIfTrulyNumber({ parsedValue: effectiveValue, rawValue })
}
enhancedConfig.env[cleanName] = effectiveValue
if (enhancedConfig.hasOwnProperty(camelCaseName) && camelCaseName !== 'env') {
enhancedConfig[camelCaseName] = effectiveValue
}
})
return enhancedConfig
}