|
| 1 | +import { execa } from 'execa'; |
| 2 | + |
| 3 | +const fileExtensions = ['.tsx', '.ts']; // linted file types |
| 4 | + |
| 5 | +// This is the difference from where the top of the git repo is and where this script is run from. Will |
| 6 | +// need to be updated for other repos. |
| 7 | +const repoPath = ''; |
| 8 | + |
| 9 | +// Default path to the directory to be checked for changes |
| 10 | +let lintPath = 'src/'; |
| 11 | + |
| 12 | +// Default lint target (--fix changes to lint-fix) |
| 13 | +let npmTarget = 'lint'; |
| 14 | + |
| 15 | +let currentBranch = false; |
| 16 | + |
| 17 | +// Parameters all optional. |
| 18 | +// --fix: this will perform eslint --fix instead of regular eslint |
| 19 | +// --currentBranch: this will perform eslint on the files that have been changed in this branch compared |
| 20 | +// to the master branch. Can be run with our without --fix. Will ignore any file paths passed in. |
| 21 | +// File path: if wanting to do a lint-diff on a specific directory or file otherwise defaults to src/ |
| 22 | +if (process.argv.length > 2) { |
| 23 | + for (let i=2; i<process.argv.length; i++) { |
| 24 | + switch(process.argv[i]) { |
| 25 | + case '--fix': |
| 26 | + npmTarget = 'lint-fix'; |
| 27 | + break; |
| 28 | + case '--currentBranch': |
| 29 | + currentBranch = true; |
| 30 | + break; |
| 31 | + default: |
| 32 | + lintPath = process.argv[i]; |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +(async () => { |
| 38 | + let files; |
| 39 | + let stdout; // This is the supported way to pipe stdout to a local variable |
| 40 | + if (currentBranch) { |
| 41 | + // Get name of the current branch |
| 42 | + ({ stdout } = await execa('git', ['rev-parse', '--abbrev-ref', 'HEAD'])); |
| 43 | + if (!stdout) { |
| 44 | + console.error('Error finding git branch name'); |
| 45 | + } else { |
| 46 | + console.log('Checking for updated files in branch: ', stdout); |
| 47 | + } |
| 48 | + |
| 49 | + const branch = stdout; |
| 50 | + |
| 51 | + // Diff current branch against develop to get changed file names |
| 52 | + ({stdout} = await execa('git', ['diff', 'develop...' + branch, '--name-only', '--diff-filter=AM'])); |
| 53 | + if (!stdout) { |
| 54 | + console.log('No changed files in branch ' + branch); |
| 55 | + } |
| 56 | + |
| 57 | + files = stdout; |
| 58 | + } else { |
| 59 | + // Diff uncommitted changes against committed to |
| 60 | + ({stdout} = await execa('git', ['diff', '--name-only', '--diff-filter=AM', lintPath])); |
| 61 | + if (!stdout) { |
| 62 | + console.log('No changed files at ' + lintPath); |
| 63 | + } |
| 64 | + |
| 65 | + files = stdout; |
| 66 | + } |
| 67 | + |
| 68 | + if (files) { |
| 69 | + let filtered = stdout.split('\n'); |
| 70 | + |
| 71 | + // Filter by file extension and file path |
| 72 | + filtered = filtered.filter(file => { |
| 73 | + file = file.trim(); |
| 74 | + const correctPath = file.startsWith(repoPath + 'src/'); |
| 75 | + const correctExt = fileExtensions.findIndex(ext => (file.endsWith(ext))) !== -1; |
| 76 | + return correctPath && correctExt |
| 77 | + }); |
| 78 | + |
| 79 | + if (filtered.length < 1) { |
| 80 | + console.log('No changed files match the file extension.') |
| 81 | + } |
| 82 | + else { |
| 83 | + // Remove file path relative to git repo |
| 84 | + filtered = filtered.map(file => { |
| 85 | + return file.substring(repoPath.length); |
| 86 | + }); |
| 87 | + |
| 88 | + console.log('Linting files:\n', filtered); |
| 89 | + |
| 90 | + // File paths need parens to resolve correctly |
| 91 | + const param = "\"" + filtered.join("\" \"") + "\""; |
| 92 | + |
| 93 | + try { |
| 94 | + await execa('npm', ['run', npmTarget, param], {shell: true}).stdout.pipe(process.stdout); |
| 95 | + } |
| 96 | + catch (error) { |
| 97 | + console.error("Lint error: ", error); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +})(); |
0 commit comments