forked from jaredpalmer/tsdx
-
Notifications
You must be signed in to change notification settings - Fork 23
/
createEslintConfig.ts
60 lines (55 loc) · 1.56 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
import fs from 'fs-extra';
import path from 'path';
import { Linter } from 'eslint';
import { PackageJson } from './types';
import { getReactVersion, resolve } from './utils';
interface CreateEslintConfigArgs {
pkg: PackageJson;
rootDir: string;
writeFile: boolean;
}
export async function createEslintConfig({
pkg,
rootDir,
writeFile,
}: CreateEslintConfigArgs): Promise<Linter.Config | void> {
const isReactLibrary = Boolean(getReactVersion(pkg));
const config = {
extends: [
path.join(__dirname, '../conf/eslint-config-react-app/index.js'),
resolve('eslint-config-prettier'),
'plugin:prettier/recommended',
],
settings: {
react: {
// Fix for https://github.com/jaredpalmer/tsdx/issues/279
version: isReactLibrary ? 'detect' : '999.999.999',
},
},
};
if (!writeFile) {
return config;
}
const file = path.join(rootDir, '.eslintrc.js');
// if the path is an abs path(e.g. "/Users/user/my-project/.eslintrc.js"),
// need to convert a rel path to app root.
config.extends[0] = './' + path.relative(rootDir, config.extends[0]);
config.extends[1] = './' + path.relative(rootDir, config.extends[1]);
try {
await fs.writeFile(
file,
`module.exports = ${JSON.stringify(config, null, 2)}`,
{ flag: 'wx' }
);
} catch (e) {
if ((e as NodeJS.ErrnoException).code === 'EEXIST') {
console.error(
'Error trying to save the Eslint configuration file:',
`${file} already exists.`
);
} else {
console.error(e);
}
return config;
}
}