Skip to content

Commit

Permalink
Lint untracked files with yarn linc (#11665)
Browse files Browse the repository at this point in the history
* Lint untracked files with yarn linc (#11646)

* Run prettier on untracked files

* Unify code for listing changed files into shared utility
  • Loading branch information
accordeiro authored and gaearon committed Nov 27, 2017
1 parent 878feeb commit 158f040
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 39 deletions.
25 changes: 2 additions & 23 deletions scripts/prettier/index.js
Expand Up @@ -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';
Expand Down Expand Up @@ -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 => {
Expand Down
36 changes: 36 additions & 0 deletions 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;
19 changes: 3 additions & 16 deletions scripts/tasks/linc.js
Expand Up @@ -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);
Expand Down

0 comments on commit 158f040

Please sign in to comment.