Skip to content

Commit 158f040

Browse files
accordeirogaearon
authored andcommitted
Lint untracked files with yarn linc (#11665)
* Lint untracked files with yarn linc (#11646) * Run prettier on untracked files * Unify code for listing changed files into shared utility
1 parent 878feeb commit 158f040

File tree

3 files changed

+41
-39
lines changed

3 files changed

+41
-39
lines changed

scripts/prettier/index.js

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
const chalk = require('chalk');
1313
const glob = require('glob');
14-
const execFileSync = require('child_process').execFileSync;
1514
const prettier = require('prettier');
1615
const fs = require('fs');
16+
const listChangedFiles = require('../shared/listChangedFiles');
1717

1818
const mode = process.argv[2] || 'check';
1919
const shouldWrite = mode === 'write' || mode === 'write-changed';
@@ -56,28 +56,7 @@ const config = {
5656
},
5757
};
5858

59-
function exec(command, args) {
60-
console.log('> ' + [command].concat(args).join(' '));
61-
var options = {
62-
cwd: process.cwd(),
63-
env: process.env,
64-
stdio: 'pipe',
65-
encoding: 'utf-8',
66-
};
67-
return execFileSync(command, args, options);
68-
}
69-
70-
var mergeBase = exec('git', ['merge-base', 'HEAD', 'master']).trim();
71-
var changedFiles = new Set(
72-
exec('git', [
73-
'diff',
74-
'-z',
75-
'--name-only',
76-
'--diff-filter=ACMRTUB',
77-
mergeBase,
78-
]).match(/[^\0]+/g)
79-
);
80-
59+
var changedFiles = listChangedFiles();
8160
let didWarn = false;
8261
let didError = false;
8362
Object.keys(config).forEach(key => {

scripts/shared/listChangedFiles.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (c) 2013-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
'use strict';
8+
9+
const execFileSync = require('child_process').execFileSync;
10+
11+
const exec = (command, args) => {
12+
console.log('> ' + [command].concat(args).join(' '));
13+
var options = {
14+
cwd: process.cwd(),
15+
env: process.env,
16+
stdio: 'pipe',
17+
encoding: 'utf-8',
18+
};
19+
return execFileSync(command, args, options);
20+
};
21+
const execGitCmd = args =>
22+
exec('git', args)
23+
.trim()
24+
.toString()
25+
.split('\n');
26+
27+
const mergeBase = execGitCmd(['merge-base', 'HEAD', 'master']);
28+
29+
const listChangedFiles = () => {
30+
return new Set([
31+
...execGitCmd(['diff', '--name-only', '--diff-filter=ACMRTUB', mergeBase]),
32+
...execGitCmd(['ls-files', '--others', '--exclude-standard']),
33+
]);
34+
};
35+
36+
module.exports = listChangedFiles;

scripts/tasks/linc.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,9 @@
88
'use strict';
99

1010
const lintOnFiles = require('../eslint');
11-
const execFileSync = require('child_process').execFileSync;
12-
const mergeBase = execFileSync('git', ['merge-base', 'HEAD', 'master'], {
13-
stdio: 'pipe',
14-
encoding: 'utf-8',
15-
}).trim();
16-
const changedFiles = execFileSync(
17-
'git',
18-
['diff', '--name-only', '--diff-filter=ACMRTUB', mergeBase],
19-
{
20-
stdio: 'pipe',
21-
encoding: 'utf-8',
22-
}
23-
)
24-
.trim()
25-
.toString()
26-
.split('\n');
11+
const listChangedFiles = require('../shared/listChangedFiles');
12+
13+
const changedFiles = [...listChangedFiles()];
2714
const jsFiles = changedFiles.filter(file => file.match(/.js$/g));
2815

2916
const report = lintOnFiles(jsFiles);

0 commit comments

Comments
 (0)