Skip to content

Commit 5eee59b

Browse files
authored
Merge pull request #269 from HRanjan-11/CYP-867
support .env file for environment variables.
2 parents 27e9160 + 48f8018 commit 5eee59b

File tree

6 files changed

+66
-5
lines changed

6 files changed

+66
-5
lines changed

commands/utils/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ module.exports = {
1212
LAMBDA_CONFIG: "./lambdatest-config.json",
1313
SUPPORTED_CYPRESS_VERSIONS: ["5", "6"],
1414
WHITELISTED_ENV_VARS: ["CI_BUILD_ID", "PERCY_TOKEN", "QASE_REPORT", "QASE_RUN_ID", "QASE_RUN_NAME", "QASE_RUN_DESCRIPTION", "QASE_API_TOKEN", "QASE_API_BASE_URL", "QASE_ENVIRONMENT_ID", "QASE_SCREENSHOT_FOLDER", "QASE_SCREENSHOT_SENDING", "QASE_RUN_COMPLETE"],
15+
BLOCKED_ENV_VARS: ["MANPATH", "NVM_CD_FLAGS", "TERM", "SHELL", "TMPDIR", "SSH_CLIENT", "NVM_PATH", "SSH_TTY", "NVM_DIR",
16+
"USER", "PATH", "NVM_NODEJS_ORG_MIRROR", "PWD", "LANG", "SHLVL", "HOME", "LOGNAME", "SSH_CONNECTION", "LC_CTYPE", "NVM_BIN", "NVM_IOJS_ORG_MIRROR", "OLDPWD"],
1517
BUILD_END_STATES:
1618
"&status=running,queued,created,initiated,pqueued,error,lambda error,failed,completed,queue_timeout,idle_timeout,stopped,cancelled,passed,timeout,inactive",
1719
BUILD_ERROR_STATES: "&status=error,lambda error,failed",

commands/utils/set_args.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const fs = require("fs");
55
const path = require("path");
66
const process = require("process");
77
const { type } = require("os");
8+
const dotenv = require('dotenv')
89

910
function write_file(file_path, content) {
1011
fs.writeFileSync(file_path, content, function (err) {
@@ -385,6 +386,36 @@ function sync_args_from_cmd(args) {
385386
}
386387
}
387388
}
389+
let dot_env_vars = undefined;
390+
if ("sys-env-keys" in args) {
391+
dot_env_vars = args["sys-env-keys"];
392+
} else if (lt_config["run_settings"] && lt_config["run_settings"]["sys_env_keys"]) {
393+
dot_env_vars = lt_config["run_settings"]["sys_env_keys"];
394+
}
395+
let parsedEnv,envFile;
396+
let envFilePath = path.join(".", `.env`)
397+
if (dot_env_vars) {
398+
dot_env_vars = dot_env_vars.trim();
399+
dot_env_vars = dot_env_vars.split(",");
400+
if ("envfl" in args) {
401+
envFilePath = args["envfl"];
402+
} else if (lt_config["run_settings"]["env_file"]) {
403+
envFilePath = lt_config["run_settings"]["env_file"];
404+
}
405+
406+
try {
407+
envFile = fs.readFileSync(envFilePath, {encoding: 'utf8'})
408+
parsedEnv = dotenv.parse(envFile)
409+
for (index in dot_env_vars) {
410+
let envKey = dot_env_vars[index]
411+
let envValue = parsedEnv[envKey]
412+
envs[envKey] = envValue
413+
}
414+
} catch (err) {
415+
console.error("error in fetching environment variables from .env file",err);
416+
}
417+
}
418+
388419
lt_config["run_settings"]["sys_envs"] = envs;
389420

390421
if ("exclude_specs" in lt_config["run_settings"]) {

commands/utils/validate.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,9 @@ module.exports = validate_config = function (lt_config, validation_configs) {
375375
let envValue;
376376
Object.keys(sys_envs).forEach(function (envKey) {
377377
envValue = sys_envs[envKey];
378-
if (envKey && !constants.WHITELISTED_ENV_VARS.includes(envKey)) {
378+
if (envKey && constants.BLOCKED_ENV_VARS.includes(envKey)) {
379379
reject(
380-
`Usage of unwanted environment variable detected. Allowed variables are - ${constants.WHITELISTED_ENV_VARS}`
380+
`Usage of unwanted environment variable detected. Blocked variables are - ${constants.BLOCKED_ENV_VARS}`
381381
);
382382
}
383383
if (envValue == undefined || envValue === "") {

index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,16 @@ const argv = require("yargs")
158158
describe: "system environment variables",
159159
type: "string",
160160
})
161+
.option("sys-env-keys", {
162+
alias: "sys-env-keys",
163+
describe: "system environment variables from .env file",
164+
type: "string",
165+
})
166+
.option("envfl", {
167+
alias: "env_file",
168+
describe: "path of .env file",
169+
type: "string",
170+
})
161171
.option("npm-f", {
162172
alias: "npm-force",
163173
describe: "force npm install",

package-lock.json

Lines changed: 19 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lambdatest-cypress-cli",
3-
"version": "3.0.18",
3+
"version": "3.0.19",
44
"description": "The lambdatest-cypress-cli is LambdaTest's command-line interface (CLI) aimed to help you run your Cypress tests on LambdaTest platform.",
55
"homepage": "https://github.com/LambdaTest/lambdatest-cypress-cli",
66
"author": "LambdaTest <[email protected]>",
@@ -20,6 +20,7 @@
2020
"@lambdatest/node-tunnel": "latest",
2121
"archiver": "^5.1.0",
2222
"async": "^3.2.3",
23+
"dotenv": "^16.3.1",
2324
"glob": "^7.1.6",
2425
"mochawesome": "^7.1.3",
2526
"node-stream-zip": "^1.15.0",

0 commit comments

Comments
 (0)