Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@
"postinstall": "node node_modules/fbjs-scripts/node/check-dev-engines.js package.json",
"test": "jest",
"flow": "node ./scripts/tasks/flow.js",
"prettier": "node ./scripts/prettier/index.js write",
"prettier": "node ./scripts/prettier/index.js write-changed",
"prettier-all": "node ./scripts/prettier/index.js write",
"version-check": "node ./scripts/tasks/version-check.js"
},
"jest": {
Expand Down
24 changes: 22 additions & 2 deletions scripts/prettier/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ const glob = require('glob');
const path = require('path');
const execFileSync = require('child_process').execFileSync;

const shouldWrite = process.argv[2] === 'write';
const mode = process.argv[2] || 'check';
const shouldWrite = mode === 'write' || mode === 'write-changed';
const onlyChanged = mode === 'check-changed' || mode === 'write-changed';

const isWindows = process.platform === 'win32';
const prettier = isWindows ? 'prettier.cmd' : 'prettier';
const prettierCmd = path.resolve(
Expand Down Expand Up @@ -49,6 +52,17 @@ function exec(command, args) {
return execFileSync(command, args, options).toString();
}

var mergeBase = exec('git', ['merge-base', 'HEAD', 'master']).trim();
var changedFiles = new Set(
exec('git', [
'diff',
'-z',
'--name-only',
'--diff-filter=ACMRTUB',
mergeBase,
]).match(/[^\0]+/g)
);

Object.keys(config).forEach(key => {
const patterns = config[key].patterns;
const options = config[key].options;
Expand All @@ -57,7 +71,13 @@ Object.keys(config).forEach(key => {
const globPattern = patterns.length > 1
? `{${patterns.join(',')}}`
: `${patterns.join(',')}`;
const files = glob.sync(globPattern, {ignore});
const files = glob
.sync(globPattern, {ignore})
.filter(f => !onlyChanged || changedFiles.has(f));

if (!files.length) {
return;
}

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