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
Alex Cordeiro committed
Nov 27, 2017 at 21:30 UTC
158f040d5401d0b84e6cc64c75253a0c57274f5d
3 files changed
+41
-39
scripts/prettier/index.js
+2
-23
@@ -11,9 +11,9 @@
11
12
const chalk = require('chalk');
13
const glob = require('glob');
14
-const execFileSync = require('child_process').execFileSync;
14
const prettier = require('prettier');
15
const fs = require('fs');
16
+const listChangedFiles = require('../shared/listChangedFiles');
17
18
const mode = process.argv[2] || 'check';
19
const shouldWrite = mode === 'write' || mode === 'write-changed';
@@ -56,28 +56,7 @@ const config = {
56
},
57
};
58
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();
60
let didWarn = false;
61
let didError = false;
62
Object.keys(config).forEach(key => {
scripts/shared/listChangedFiles.js
new
+36
@@ -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
+3
-16
@@ -8,22 +8,9 @@
8
'use strict';
9
10
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()];
14
const jsFiles = changedFiles.filter(file => file.match(/.js$/g));
15
16
const report = lintOnFiles(jsFiles);