-
Notifications
You must be signed in to change notification settings - Fork 504
/
createEslintConfig.ts
63 lines (60 loc) · 1.63 KB
/
createEslintConfig.ts
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
import fs from 'fs';
import path from 'path';
import { CLIEngine } from 'eslint';
import reactAppConfig from 'eslint-config-react-app';
interface CreateEslintConfigArgs {
rootDir: string;
writeFile: boolean;
}
export function createEslintConfig({
rootDir,
writeFile,
}: CreateEslintConfigArgs): CLIEngine.Options['baseConfig'] {
/*
This config could change to
{
extends: [
'react-app',
'prettier/@typescript-eslint',
'plugin:prettier/recommended'
]
}
after https://github.com/facebook/create-react-app/commit/24489ac0a667af416f1d59dd806dfc2623aabe36 is released
eslint-config-react-app defines overrides as an object instead of an array, which is not supported in eslint@6.
*/
const config = {
...reactAppConfig,
extends: ['prettier/@typescript-eslint', 'plugin:prettier/recommended'],
overrides: undefined,
parser: require.resolve('@typescript-eslint/parser'),
parserOptions: reactAppConfig.overrides.parserOptions,
plugins: [
...reactAppConfig.plugins,
...reactAppConfig.overrides.plugins,
'no-for-of-loops',
],
rules: {
...reactAppConfig.rules,
...reactAppConfig.overrides.rules,
},
};
if (writeFile) {
const file = path.join(rootDir, '.eslintrc.js');
if (fs.existsSync(file)) {
console.error(
'Error trying to save the Eslint configuration file:',
`${file} already exists.`
);
} else {
try {
fs.writeFileSync(
file,
`module.exports = ${JSON.stringify(config, null, 2)}`
);
} catch (e) {
console.error(e);
}
}
}
return config;
}