Skip to content

Instantly share code, notes, and snippets.

@matheus1lva
Created September 10, 2021 18:53
Show Gist options
  • Save matheus1lva/2efb09a9bffe2b0998acdee0168c0340 to your computer and use it in GitHub Desktop.
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
#!/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