Skip to content

Commit d9da2e4

Browse files
committed
added support of .env files
1 parent 27e9160 commit d9da2e4

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

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"]["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+
envKey = dot_env_vars[index]
411+
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"]) {

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: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)