@cucumber/cucumber
Version:
The official JavaScript implementation of Cucumber.
102 lines • 4.88 kB
JavaScript
;
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");
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);
let definitions;
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 '.mjs':
logger.debug(`Loading configuration file "${file}" as ESM 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;
default:
throw new Error(`Unsupported configuration file extension "${extension}"`);
}
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