regressify
Version:
Visual regression tests support
44 lines • 1.6 kB
JavaScript
import fs from 'fs';
import path from 'path';
import YAML from 'js-yaml';
import { getStringArg } from './helpers.js';
import slash from 'slash';
export function getReplacementProfileName(args) {
const profileArg = getStringArg(args, '--replacement-profile') ?? getStringArg(args, 'replacement-profile');
if (profileArg) {
return profileArg;
}
const profileEnv = process.env.REPLACEMENT_PROFILE;
if (profileEnv) {
return profileEnv;
}
return 'default';
}
export 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 function applyReplacements(url, replacements) {
if (!replacements?.length) {
return url;
}
let testUrl = url;
for (const replacement of replacements) {
testUrl = replacement.regex
? testUrl.replace(new RegExp(replacement.ref, replacement.flags), replacement.test)
: testUrl.replace(replacement.ref, replacement.test);
}
return testUrl;
}
export const getTestUrl = (args, url, isRef) => {
if (isRef) {
return url;
}
return applyReplacements(url, getReplacementProfile(args));
};
//# sourceMappingURL=replacements.js.map