@samitouri / QOS-React / commits / e736b4c4b9

"yarn prettier" only checks changed files (#9670)

CI still checks all of them.

Ben Alpert committed May 11, 2017 at 14:47 UTC e736b4c4b980ccba0c134a739ef9a3153b0502bf
2 files changed +24 -3
package.json
+2 -1
@@ -112,7 +112,8 @@
112 "postinstall": "node node_modules/fbjs-scripts/node/check-dev-engines.js package.json",
113 "test": "jest",
114 "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",
117 "version-check": "node ./scripts/tasks/version-check.js"
118 },
119 "jest": {
scripts/prettier/index.js
+22 -2
@@ -15,7 +15,10 @@ const glob = require('glob');
15 const path = require('path');
16 const execFileSync = require('child_process').execFileSync;
17
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 +
22 const isWindows = process.platform === 'win32';
23 const prettier = isWindows ? 'prettier.cmd' : 'prettier';
24 const prettierCmd = path.resolve(
@@ -49,6 +52,17 @@ function exec(command, args) {
52 return execFileSync(command, args, options).toString();
53 }
54
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 +
66 Object.keys(config).forEach(key => {
67 const patterns = config[key].patterns;
68 const options = config[key].options;
@@ -57,7 +71,13 @@ Object.keys(config).forEach(key => {
71 const globPattern = patterns.length > 1
72 ? `{${patterns.join(',')}}`
73 : `${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 + }
81
82 const args = Object.keys(defaultOptions).map(
83 k => `--${k}=${(options && options[k]) || defaultOptions[k]}`