Skip to content

Instantly share code, notes, and snippets.

@patricksevat
Created November 25, 2019 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patricksevat/6c1a1644fb1fff6ee71362be07c8fc54 to your computer and use it in GitHub Desktop.
Save patricksevat/6c1a1644fb1fff6ee71362be07c8fc54 to your computer and use it in GitHub Desktop.
Regex / grep generator
// getSpecFiles.js
const glob = require('glob');
const { readFileSync } = require('fs-extra');
function getFilteredSpecFiles(specs, opts = {}) {
const { glob, grep } = getRegularExpression(opts);
const fileNames = getGlobMatches(specs);
const includedSpecFiles = [];
for (const path of fileNames) {
const file = readFileSync(path, 'utf-8');
if (file.match(glob)) {
includedSpecFiles.push(path);
}
}
return includedSpecFiles;
}
function getGlobMatches(specs) {
return specs.reduce((accumulator, spec) => {
return [...accumulator, ...glob.sync(spec)];
}, []);
}
function getRegularExpression({ happyFlow, target }) {
if (!happyFlow && target === 'desktop') {
// ❌ #happy-flow and ❌ #mobile
return {
glob: /^.*(describe|it)\((?!.*(#happy-flow|#mobile)).*/gm,
grep: /^(?!.*(#happy-flow|#mobile)).*/,
};
} else if (happyFlow && target === 'desktop') {
// ✅ #happy-flow, ❌ #mobile
return {
glob: /^.*(describe|it)\((?!.*(#mobile)).*(#happy-flow).*/gm,
grep: /^(?!.*(#mobile)).*(#happy-flow).*/,
};
} else if (!happyFlow && target === 'mobile') {
// ❌ #happy-flow, ❌ #desktop, ✅ #mobile
return {
glob: /^.*(describe|it)\((?!.*(#happy-flow|#desktop)).*(#mobile).*/gm,
grep: /^(?!.*(#happy-flow|#desktop)).*(#mobile).*/
};
} else if (happyFlow && target === 'mobile') {
// ✅ #happy-flow, ❌ #desktop, ✅ #mobile
return {
glob: /^.*(describe|it)\((?!.*(#desktop)).*(#happy-flow).*/gm,
grep: /^(?!.*(#desktop)).*(#happy-flow).*/
};
} else {
throw new Error('unforeseen case');
}
}
exports.getFilteredSpecFiles = getFilteredSpecFiles;
exports.getRegularExpression = getRegularExpression;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment