2021-08-04 23:53:15 +02:00
|
|
|
import { promises as fs } from 'fs'
|
|
|
|
|
|
|
|
export type ConfigAvailable = {
|
|
|
|
exists: boolean
|
|
|
|
emptyEslintrc?: boolean
|
|
|
|
emptyPkgJsonConfig?: boolean
|
|
|
|
firstTimeSetup?: true
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function hasEslintConfiguration(
|
|
|
|
eslintrcFile: string | null,
|
|
|
|
packageJsonConfig: { eslintConfig: any } | null
|
|
|
|
): Promise<ConfigAvailable> {
|
|
|
|
const configObject = {
|
|
|
|
exists: false,
|
|
|
|
emptyEslintrc: false,
|
|
|
|
emptyPkgJsonConfig: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eslintrcFile) {
|
|
|
|
const content = await fs.readFile(eslintrcFile, { encoding: 'utf8' }).then(
|
|
|
|
(txt) => txt.trim().replace(/\n/g, ''),
|
|
|
|
() => null
|
|
|
|
)
|
|
|
|
|
|
|
|
if (
|
|
|
|
content === '' ||
|
|
|
|
content === '{}' ||
|
|
|
|
content === '---' ||
|
|
|
|
content === 'module.exports = {}'
|
|
|
|
) {
|
|
|
|
return { ...configObject, emptyEslintrc: true }
|
|
|
|
}
|
2022-08-23 20:42:36 +02:00
|
|
|
return { ...configObject, exists: true }
|
2021-08-04 23:53:15 +02:00
|
|
|
} else if (packageJsonConfig?.eslintConfig) {
|
|
|
|
if (Object.entries(packageJsonConfig?.eslintConfig).length === 0) {
|
|
|
|
return {
|
|
|
|
...configObject,
|
|
|
|
emptyPkgJsonConfig: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-23 20:42:36 +02:00
|
|
|
return configObject
|
2021-08-04 23:53:15 +02:00
|
|
|
}
|