-
Notifications
You must be signed in to change notification settings - Fork 2
/
eslint.js
109 lines (97 loc) · 3.39 KB
/
eslint.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// @flow
/**
* This action runs `eslint` and reports any type errors it encounters.
*
* It expects the path to the `eslint` module to be provided as the first
* and only command-line argument.
*
* It uses `send-report.js` to support both running locally (reporting to
* stdout) and under Github Actions (adding annotations to files in the GitHub
* UI).
*/
// $FlowFixMe: shhhhh
require('@babel/register'); // flow-uncovered-line
const gitChangedFiles = require('actions-utils/git-changed-files');
const getBaseRef = require('actions-utils/get-base-ref');
const {cannedGithubErrorMessage} = require('actions-utils/get-base-ref');
const core = require('@actions/core'); // flow-uncovered-line
const {exec} = require('@actions/exec'); // flow-uncovered-line
const path = require('path');
const chalk = require('chalk');
chalk.enabled = !process.env.GITHUB_TOKEN;
/*::
import type {Message} from 'actions-utils/send-report';
import type {Formatter, LintReport, LintResult} from './types.js';
*/
const eslintAnnotations = async (
eslintDirectory /*: string */,
files /*: Array<string> */,
) /*: Promise<Array<Message>> */ => {
/* flow-uncovered-block */
// Log which files are being linted.
const cwd = process.cwd();
if (files.length === 1 && files[0] === '.') {
core.info(`Linting all relevant files in ${cwd}`);
} else {
core.startGroup('Running eslint on the following files:');
for (const file of files) {
core.info(path.relative(cwd, file));
}
core.endGroup();
}
const args = [
path.resolve(eslintDirectory, 'bin', 'eslint'),
'--ext',
'.js',
'--ext',
'.jsx',
...files,
].filter(Boolean);
return await exec('node', args, {
cwd,
});
};
async function run() {
const eslintDirectory = core.getInput('eslint-lib', {required: true});
const workingDirectory = core.getInput('custom-working-directory');
const runAllIfChanged = core.getMultilineInput('run-all-if-changed');
if (workingDirectory != null && workingDirectory.trim() !== '') {
process.chdir(workingDirectory);
}
if (!eslintDirectory) {
/* flow-uncovered-block */
core.error(
`You need to have eslint installed, and pass in the directory where it is located via the variable 'eslint-lib'.`,
);
/* end flow-uncovered-block */
process.exit(1);
return;
}
const baseRef = getBaseRef();
if (!baseRef) {
core.error(cannedGithubErrorMessage()); // flow-uncovered-line
process.exit(1);
return;
}
const current = path.resolve('');
const files = await gitChangedFiles(baseRef, '.');
const shouldRunAll = runAllIfChanged.some(name =>
files.some(file => path.relative(current, file) === name),
);
const validExt = ['.js', '.jsx', '.mjs', '.ts', '.tsx'];
const jsFiles = shouldRunAll
? // Get all files
['.']
: files.filter(file => validExt.includes(path.extname(file)));
if (!jsFiles.length) {
core.info('No JavaScript files changed'); // flow-uncovered-line
core.info(`Changed files:\n - ${files.join('\n - ')}`); // flow-uncovered-line
return;
}
await eslintAnnotations(eslintDirectory, jsFiles);
}
// flow-next-uncovered-line
run().catch(err => {
core.setFailed(err); // flow-uncovered-line
process.exit(1);
});