Created
November 20, 2019 20:27
-
-
Save patricksevat/fe32c37ed0829079e181f8bc722108ad 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({ smoke, target }) { | |
if (!smoke && target === 'desktop') { | |
// ❌ #smoke and ❌ #mobile | |
return { | |
glob: /^.*(describe|it)\((?!.*(#smoke|#mobile|#perfecto|#handheld)).*/gm, | |
grep: /^(?!.*(#smoke|#mobile|#perfecto|#handheld)).*/, | |
}; | |
} else if (smoke && target === 'desktop') { | |
// ✅ #smoke, ❌ #mobile | |
return { | |
glob: /^.*(describe|it)\((?!.*(#mobile|#perfecto|#handheld)).*(#smoke).*/gm, | |
grep: /^(?!.*(#mobile|#perfecto|#handheld)).*(#smoke).*/, | |
}; | |
} else if (!smoke && target === 'mobile') { | |
// ❌ #smoke, ❌ #desktop, ✅ #mobile | |
return { | |
glob: /^.*(describe|it)\((?!.*(#smoke|#desktop)).*(#mobile|#handheld).*/gm, | |
grep: /^(?!.*(#smoke|#desktop)).*(#mobile|#handheld).*/ | |
}; | |
} else if (smoke && target === 'mobile') { | |
// ✅ #smoke, ❌ #desktop, ✅ #mobile | |
return { | |
glob: /^.*(describe|it)\((?!.*(#desktop)).*(#smoke|#perfecto).*/gm, | |
grep: /^(?!.*(#desktop)).*(#smoke|#perfecto).*/ | |
}; | |
} 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