UNPKG

e2ed

Version:

E2E testing framework over Playwright

42 lines (41 loc) 1.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDotEnvValuesObject = void 0; const promises_1 = require("node:fs/promises"); const internal_1 = require("../../constants/internal"); const error_1 = require("../error"); /** * Get object with values from `variables.env` file in directory with autotests. * {@link https://www.npmjs.com/package/dotenv} * @internal */ const getDotEnvValuesObject = async () => { const dotEnvText = await (0, promises_1.readFile)(internal_1.DOT_ENV_PATH, internal_1.READ_FILE_OPTIONS); const lines = dotEnvText.split('\n'); const result = Object.create(null); for (const line of lines) { const trimmedLine = line.trim(); if (line === '' || line[0] === '#') { continue; } const indexOfEqualSign = trimmedLine.indexOf('='); if (indexOfEqualSign < 1) { throw new error_1.E2edError('Incorrect name of environment variable in `variables.env`', { line }); } const name = trimmedLine.slice(0, indexOfEqualSign).trim(); if (name in result) { throw new error_1.E2edError(`Duplicate name "${name}" in \`variables.env\` file`, { firstValue: result[name], line, }); } const valueMaybeWithQuotes = trimmedLine.slice(indexOfEqualSign + 1).trim(); const firstCharacter = valueMaybeWithQuotes[0]; const isQuoted = firstCharacter === valueMaybeWithQuotes.at(-1) && (firstCharacter === '"' || firstCharacter === "'" || firstCharacter === '`'); const value = isQuoted ? valueMaybeWithQuotes.slice(1, -1) : valueMaybeWithQuotes; result[name] = value; } return result; }; exports.getDotEnvValuesObject = getDotEnvValuesObject;