Created
September 10, 2021 18:53
-
-
Save matheus1lva/2efb09a9bffe2b0998acdee0168c0340 to your computer and use it in GitHub Desktop.
find files and run lint and tests based on only files that changed
This file contains hidden or 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
#!/usr/bin/node | |
/* eslint-disable @typescript-eslint/no-unused-expressions */ | |
// eslint-disable-next-line import/no-extraneous-dependencies | |
const sh = require('shell-tag'); | |
const detectChangedFiles = () => { | |
const appFolders = ['routes', 'pages', 'modules', 'hooks']; | |
const changedFiles = sh`git diff develop --name-only`; | |
return changedFiles.split(/\n/g).filter((file) => { | |
return !file.startsWith('.') && appFolders.some(folder => file.includes(appFolders)); | |
}).join('\n').replace(/\n/g, ' ').replace(/client\//g, ' '); | |
}; | |
const findRelatedTests = () => { | |
const changedFiles = detectChangedFiles(); | |
const tests = sh`yarn jest --listTests --findRelatedTests ${changedFiles}`; | |
return tests.replace(/\n/g, ' '); | |
}; | |
const flag = process.argv.slice(2)[0]; | |
function runBasedOnFlag() { | |
const files = detectChangedFiles(); | |
if (files === '') { | |
return | |
} | |
const tests = findRelatedTests(); | |
switch (flag) { | |
case '--lint': { | |
sh`yarn eslint ${files}`; | |
break; | |
} | |
case '--test': { | |
sh`yarn test ${tests}`; | |
break; | |
} | |
default: { | |
sh`echo "no option"`; | |
} | |
} | |
} | |
runBasedOnFlag(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment