Skip to content

Commit 0a5c7cb

Browse files
fix(tools): stop multiple prettier runs on large commits (freeCodeCamp#44124)
1 parent cb37313 commit 0a5c7cb

File tree

1 file changed

+39
-18
lines changed

1 file changed

+39
-18
lines changed

.lintstagedrc.js

+39-18
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,52 @@ const { ESLint } = require('eslint');
22

33
const cli = new ESLint();
44

5+
// This lets us abort if we've already run a stage for all files
6+
const completedStages = new Set();
7+
58
// if a lot of files are changed, it's faster to run prettier/eslint on the
69
// whole project than to run them on each file separately
710
module.exports = {
811
'*.(js|ts|tsx)': async files => {
12+
if (completedStages.has('js')) return [];
13+
914
const ignoredIds = await Promise.all(
1015
files.map(file => cli.isPathIgnored(file))
1116
);
1217
const lintableFiles = files.filter((_, i) => !ignoredIds[i]);
13-
return files.length > 10
14-
? ['eslint --max-warnings=0 --cache --fix .', 'prettier --write .']
15-
: [
16-
'eslint --max-warnings=0 --cache --fix ' + lintableFiles.join(' '),
17-
...files.map(filename => `prettier --write '${filename}'`)
18-
];
18+
if (files.length > 10) {
19+
completedStages.add('js');
20+
return ['eslint --max-warnings=0 --cache --fix .', 'prettier --write .'];
21+
} else {
22+
return [
23+
'eslint --max-warnings=0 --cache --fix ' + lintableFiles.join(' '),
24+
...files.map(filename => `prettier --write '${filename}'`)
25+
];
26+
}
27+
},
28+
'*.!(js|ts|tsx)': files => {
29+
if (completedStages.has('not-js')) return [];
30+
31+
if (files.length > 10) {
32+
completedStages.add('not-js');
33+
return 'prettier --write .';
34+
} else {
35+
return files.map(
36+
filename => `prettier --write --ignore-unknown '${filename}'`
37+
);
38+
}
1939
},
20-
'*.!(js|ts|tsx)': files =>
21-
files.length > 10
22-
? 'prettier --write .'
23-
: files.map(
24-
filename => `prettier --write --ignore-unknown '${filename}'`
25-
),
26-
'./curriculum/challenges/**/*.md': files =>
27-
files.length > 10
28-
? 'npm run lint:challenges'
29-
: files.map(
30-
filename => `node ./tools/scripts/lint/index.js '${filename}'`
31-
)
40+
41+
'./curriculum/challenges/**/*.md': files => {
42+
if (completedStages.has('markdown')) return [];
43+
44+
if (files.length > 10) {
45+
completedStages.add('markdown');
46+
return 'npm run lint:challenges';
47+
} else {
48+
return files.map(
49+
filename => `node ./tools/scripts/lint/index.js '${filename}'`
50+
);
51+
}
52+
}
3253
};

0 commit comments

Comments
 (0)