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