UNPKG

@cucumber/cucumber

Version:

The official JavaScript implementation of Cucumber.

131 lines 5.92 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.fromFile = fromFile; const node_fs_1 = __importDefault(require("node:fs")); const node_path_1 = __importDefault(require("node:path")); const node_util_1 = require("node:util"); const node_url_1 = require("node:url"); const yaml_1 = __importDefault(require("yaml")); const merge_configurations_1 = require("./merge_configurations"); const parse_configuration_1 = require("./parse_configuration"); const SUPPORTED_EXTENSIONS = [ '.json', '.yaml', '.yml', '.js', '.cjs', '.mjs', '.ts', '.cts', '.mts', ]; async function fromFile(logger, cwd, file, profiles = []) { let definitions = await loadFile(logger, cwd, file); const defaultDefinition = definitions.default; if (defaultDefinition) { if (typeof defaultDefinition === 'function') { logger.debug('Default function found; loading profiles'); definitions = await handleDefaultFunctionDefinition(definitions, defaultDefinition); } } else { logger.debug('No default profile defined in configuration file'); definitions.default = {}; } if (profiles.length < 1) { logger.debug('No profiles specified; using default profile'); profiles = ['default']; } const definedKeys = Object.keys(definitions); profiles.forEach((profileKey) => { if (!definedKeys.includes(profileKey)) { throw new Error(`Requested profile "${profileKey}" doesn't exist`); } }); return (0, merge_configurations_1.mergeConfigurations)({}, ...profiles.map((profileKey) => (0, parse_configuration_1.parseConfiguration)(logger, `Profile "${profileKey}"`, definitions[profileKey]))); } async function handleDefaultFunctionDefinition(definitions, defaultDefinition) { if (Object.keys(definitions).length > 1) { throw new Error('Invalid profiles specified: if a default function definition is provided, no other static profiles should be specified'); } const definitionsFromDefault = await defaultDefinition(); return { default: {}, ...definitionsFromDefault, }; } async function loadFile(logger, cwd, file) { const filePath = node_path_1.default.join(cwd, file); const extension = node_path_1.default.extname(filePath); if (!SUPPORTED_EXTENSIONS.includes(extension)) { throw new Error(`Unsupported configuration file extension "${extension}"`); } let definitions; try { switch (extension) { case '.json': definitions = JSON.parse(await (0, node_util_1.promisify)(node_fs_1.default.readFile)(filePath, { encoding: 'utf-8' })); break; case '.yaml': case '.yml': definitions = yaml_1.default.parse(await (0, node_util_1.promisify)(node_fs_1.default.readFile)(filePath, { encoding: 'utf-8' })); break; case '.cjs': logger.debug(`Loading configuration file "${file}" as CommonJS based on extension`); // eslint-disable-next-line @typescript-eslint/no-require-imports definitions = require(filePath); break; case '.cts': logger.debug(`Loading configuration file "${file}" as TypeScript based on extension`); // eslint-disable-next-line @typescript-eslint/no-require-imports definitions = require(filePath); break; case '.mjs': logger.debug(`Loading configuration file "${file}" as ESM based on extension`); definitions = await import((0, node_url_1.pathToFileURL)(filePath).toString()); break; case '.mts': case '.ts': logger.debug(`Loading configuration file "${file}" as TypeScript based on extension`); definitions = await import((0, node_url_1.pathToFileURL)(filePath).toString()); break; case '.js': { const parentPackage = await readPackageJson(filePath); if (!parentPackage) { logger.debug(`Loading configuration file "${file}" as CommonJS based on absence of a parent package`); // eslint-disable-next-line @typescript-eslint/no-require-imports definitions = require(filePath); } else if (parentPackage.type === 'module') { logger.debug(`Loading configuration file "${file}" as ESM based on "${parentPackage.name}" package type`); definitions = await import((0, node_url_1.pathToFileURL)(filePath).toString()); } else { logger.debug(`Loading configuration file "${file}" as CommonJS based on "${parentPackage.name}" package type`); // eslint-disable-next-line @typescript-eslint/no-require-imports definitions = require(filePath); } } break; } } catch (error) { throw new Error(`Configuration file "${file}" failed to load/parse`, { cause: error, }); } if (typeof definitions !== 'object') { throw new Error(`Configuration file ${filePath} does not export an object`); } return definitions; } async function readPackageJson(filePath) { const { readPackageUp } = await import('read-package-up'); const parentPackage = await readPackageUp({ cwd: node_path_1.default.dirname(filePath) }); return parentPackage?.packageJson; } //# sourceMappingURL=from_file.js.map