Skip to content

Commit e736b4c

Browse files
authored
"yarn prettier" only checks changed files (facebook#9670)
CI still checks all of them.
1 parent fe750c9 commit e736b4c

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@
112112
"postinstall": "node node_modules/fbjs-scripts/node/check-dev-engines.js package.json",
113113
"test": "jest",
114114
"flow": "node ./scripts/tasks/flow.js",
115-
"prettier": "node ./scripts/prettier/index.js write",
115+
"prettier": "node ./scripts/prettier/index.js write-changed",
116+
"prettier-all": "node ./scripts/prettier/index.js write",
116117
"version-check": "node ./scripts/tasks/version-check.js"
117118
},
118119
"jest": {

scripts/prettier/index.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ const glob = require('glob');
1515
const path = require('path');
1616
const execFileSync = require('child_process').execFileSync;
1717

18-
const shouldWrite = process.argv[2] === 'write';
18+
const mode = process.argv[2] || 'check';
19+
const shouldWrite = mode === 'write' || mode === 'write-changed';
20+
const onlyChanged = mode === 'check-changed' || mode === 'write-changed';
21+
1922
const isWindows = process.platform === 'win32';
2023
const prettier = isWindows ? 'prettier.cmd' : 'prettier';
2124
const prettierCmd = path.resolve(
@@ -49,6 +52,17 @@ function exec(command, args) {
4952
return execFileSync(command, args, options).toString();
5053
}
5154

55+
var mergeBase = exec('git', ['merge-base', 'HEAD', 'master']).trim();
56+
var changedFiles = new Set(
57+
exec('git', [
58+
'diff',
59+
'-z',
60+
'--name-only',
61+
'--diff-filter=ACMRTUB',
62+
mergeBase,
63+
]).match(/[^\0]+/g)
64+
);
65+
5266
Object.keys(config).forEach(key => {
5367
const patterns = config[key].patterns;
5468
const options = config[key].options;
@@ -57,7 +71,13 @@ Object.keys(config).forEach(key => {
5771
const globPattern = patterns.length > 1
5872
? `{${patterns.join(',')}}`
5973
: `${patterns.join(',')}`;
60-
const files = glob.sync(globPattern, {ignore});
74+
const files = glob
75+
.sync(globPattern, {ignore})
76+
.filter(f => !onlyChanged || changedFiles.has(f));
77+
78+
if (!files.length) {
79+
return;
80+
}
6181

6282
const args = Object.keys(defaultOptions).map(
6383
k => `--${k}=${(options && options[k]) || defaultOptions[k]}`

0 commit comments

Comments
 (0)