import { spawnSync } from 'child_process'; import fs from 'fs'; import { interopRequireDefault } from 'jest-util'; import path from 'path'; export default function parseFeature(cwd: string, featurePath: string, extensions: string[]) { const source = fs.readFileSync(featurePath, 'utf8'); const varMapExts = extensions.filter((ext) => ext !== 'feature'); const varMapFileName = process.env.CUCUMBER_ENV ? `**/${path.basename(featurePath, path.extname(featurePath))}.${process.env.CUCUMBER_ENV}.vars` : `**/${path.basename(featurePath, path.extname(featurePath))}.vars`; // scan relative directories to find any file that matches the feature file name, // but as another extension const varMapPaths = spawnSync( 'node', [ path.resolve(__dirname, './getPaths.js'), cwd, varMapFileName, JSON.stringify(varMapExts) ], { encoding: 'utf-8' } ).stdout; const varMapLocation = JSON.parse(varMapPaths)[0]; // load the var map file const varMapFile = varMapLocation ? interopRequireDefault(require(varMapLocation)).default as { [name: string]: string | boolean | Date | number } : null; // interpolate the feature file with the varMapFile const tmpSource = varMapFile ? Object.entries(varMapFile).reduce((acc, [key, value]) => ( acc.replace(new RegExp('\\$' + key, 'g'), value.toString()) ), source + '') : source; const tmpPath = path.resolve(cwd, './node_modules/.tmp'); const featureSourcePath = tmpSource !== source ? path.resolve(tmpPath, path.basename(featurePath)) : featurePath; if (tmpSource !== source) { if (!fs.existsSync(tmpPath)) { fs.mkdirSync(tmpPath); } if (fs.existsSync(featureSourcePath)) { fs.unlinkSync(featureSourcePath); } // write the temp feature file to tmp directory fs.writeFileSync(featureSourcePath, tmpSource); return featureSourcePath; } return featurePath; }