Created
November 25, 2019 19:16
-
-
Save patricksevat/6c1a1644fb1fff6ee71362be07c8fc54 to your computer and use it in GitHub Desktop.
Regex / grep generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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