diff --git a/scripts/prettier/index.js b/scripts/prettier/index.js index e7f589c7c600..fa3775619847 100644 --- a/scripts/prettier/index.js +++ b/scripts/prettier/index.js @@ -11,9 +11,9 @@ const chalk = require('chalk'); const glob = require('glob'); -const execFileSync = require('child_process').execFileSync; const prettier = require('prettier'); const fs = require('fs'); +const listChangedFiles = require('../shared/listChangedFiles'); const mode = process.argv[2] || 'check'; const shouldWrite = mode === 'write' || mode === 'write-changed'; @@ -56,28 +56,7 @@ const config = { }, }; -function exec(command, args) { - console.log('> ' + [command].concat(args).join(' ')); - var options = { - cwd: process.cwd(), - env: process.env, - stdio: 'pipe', - encoding: 'utf-8', - }; - return execFileSync(command, args, options); -} - -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) -); - +var changedFiles = listChangedFiles(); let didWarn = false; let didError = false; Object.keys(config).forEach(key => { diff --git a/scripts/shared/listChangedFiles.js b/scripts/shared/listChangedFiles.js new file mode 100644 index 000000000000..a98ed2db6450 --- /dev/null +++ b/scripts/shared/listChangedFiles.js @@ -0,0 +1,36 @@ +/** + * Copyright (c) 2013-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +'use strict'; + +const execFileSync = require('child_process').execFileSync; + +const exec = (command, args) => { + console.log('> ' + [command].concat(args).join(' ')); + var options = { + cwd: process.cwd(), + env: process.env, + stdio: 'pipe', + encoding: 'utf-8', + }; + return execFileSync(command, args, options); +}; +const execGitCmd = args => + exec('git', args) + .trim() + .toString() + .split('\n'); + +const mergeBase = execGitCmd(['merge-base', 'HEAD', 'master']); + +const listChangedFiles = () => { + return new Set([ + ...execGitCmd(['diff', '--name-only', '--diff-filter=ACMRTUB', mergeBase]), + ...execGitCmd(['ls-files', '--others', '--exclude-standard']), + ]); +}; + +module.exports = listChangedFiles; diff --git a/scripts/tasks/linc.js b/scripts/tasks/linc.js index 7ef9cf2429ba..2a0625ec0abd 100644 --- a/scripts/tasks/linc.js +++ b/scripts/tasks/linc.js @@ -8,22 +8,9 @@ 'use strict'; const lintOnFiles = require('../eslint'); -const execFileSync = require('child_process').execFileSync; -const mergeBase = execFileSync('git', ['merge-base', 'HEAD', 'master'], { - stdio: 'pipe', - encoding: 'utf-8', -}).trim(); -const changedFiles = execFileSync( - 'git', - ['diff', '--name-only', '--diff-filter=ACMRTUB', mergeBase], - { - stdio: 'pipe', - encoding: 'utf-8', - } -) - .trim() - .toString() - .split('\n'); +const listChangedFiles = require('../shared/listChangedFiles'); + +const changedFiles = [...listChangedFiles()]; const jsFiles = changedFiles.filter(file => file.match(/.js$/g)); const report = lintOnFiles(jsFiles);