UNPKG

regressify

Version:
38 lines 1.45 kB
import fs from 'fs'; import path from 'path'; import YAML from 'js-yaml'; import { getStringArg } from './helpers.js'; import slash from 'slash'; function getReplacementProfileName(args) { const profileArg = getStringArg(args, 'replacement-profile'); if (profileArg) { return profileArg; } const profileEnv = process.env.REPLACEMENT_PROFILE; if (profileEnv) { return profileEnv; } return 'default'; } function getReplacementProfile(args) { const replacementProfileName = getReplacementProfileName(args); const replacementProfilePath = slash(path.join(process.cwd(), 'common', '_replacement-profiles.yaml')); if (!fs.existsSync(replacementProfilePath)) { throw "Replacement profile doesn't exist: " + replacementProfilePath; } const profiles = YAML.load(fs.readFileSync(replacementProfilePath, 'utf-8')); return profiles.profiles[replacementProfileName]; } export const getTestUrl = (args, url, isRef) => { const replacementProfile = getReplacementProfile(args); if (isRef || !replacementProfile) { return url; } let testUrl = url; replacementProfile.forEach((e) => { // console.log('Replacing: ', e.ref, ' with ', e.test, ' regex: ', e.regex); return (testUrl = e.regex ? testUrl.replace(new RegExp(e.ref, e.flags), e.test) : testUrl.replace(e.ref, e.test)); }); return testUrl; }; //# sourceMappingURL=replacements.js.map