-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgetESLint.ts
More file actions
46 lines (42 loc) · 1.62 KB
/
getESLint.ts
File metadata and controls
46 lines (42 loc) · 1.62 KB
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
import type { ESLint as ProjectESLint, Linter as ProjectLinter } from 'eslint';
import { existsSync } from 'node:fs';
import { createRequire } from 'node:module';
import { resolve } from 'node:path';
import { cwd } from 'node:process';
import { notice } from '@actions/core';
export async function getESLint(eslintLibPath: string, configPath: string) {
const absoluteDirectory = cwd();
const require = createRequire(absoluteDirectory);
const eslintJsPath = resolve(absoluteDirectory, eslintLibPath);
if (!existsSync(eslintJsPath)) {
throw new Error(`ESLint JavaScript cannot be found at ${eslintJsPath}`);
}
notice(`Using ESLint from: ${eslintJsPath}`);
const { ESLint, loadESLint } = require(eslintJsPath) as {
ESLint: typeof ProjectESLint;
loadESLint: (() => Promise<typeof ProjectESLint>) | undefined;
};
notice(`ESLint version: ${ESLint.version}`);
if (configPath) {
const absoluteConfigPath = resolve(absoluteDirectory, configPath);
notice(`Using ESLint config from: ${absoluteConfigPath}`);
const eslint = new ESLint({ overrideConfigFile: absoluteConfigPath });
return eslint;
}
if (loadESLint) {
// ESLint 8.57.0 and later
notice('Using ESLint with default configuration');
const eslint = new (await loadESLint())();
return eslint;
}
const eslintConfig = (await new ESLint().calculateConfigForFile(
'package.json',
)) as ProjectLinter.Config | undefined;
if (!eslintConfig) {
throw new Error(
'Failed to find ESLint configuration. Please set the config-path input.',
);
}
const eslint = new ESLint({ baseConfig: eslintConfig });
return eslint;
}