2021-04-30 13:09:07 +02:00
|
|
|
import { promises as fs } from 'fs'
|
|
|
|
import chalk from 'chalk'
|
|
|
|
import os from 'os'
|
|
|
|
import path from 'path'
|
|
|
|
import * as CommentJson from 'next/dist/compiled/comment-json'
|
2021-08-04 23:53:15 +02:00
|
|
|
import { ConfigAvailable } from './hasEslintConfiguration'
|
|
|
|
|
|
|
|
import * as Log from '../../build/output/log'
|
2021-04-30 13:09:07 +02:00
|
|
|
|
|
|
|
export async function writeDefaultConfig(
|
2021-08-04 23:53:15 +02:00
|
|
|
baseDir: string,
|
|
|
|
{ exists, emptyEslintrc, emptyPkgJsonConfig }: ConfigAvailable,
|
|
|
|
selectedConfig: any,
|
2021-04-30 13:09:07 +02:00
|
|
|
eslintrcFile: string | null,
|
2021-06-03 14:01:24 +02:00
|
|
|
pkgJsonPath: string | null,
|
|
|
|
packageJsonConfig: { eslintConfig: any } | null
|
2021-04-30 13:09:07 +02:00
|
|
|
) {
|
2021-08-04 23:53:15 +02:00
|
|
|
if (!exists && emptyEslintrc && eslintrcFile) {
|
|
|
|
const ext = path.extname(eslintrcFile)
|
2021-04-30 13:09:07 +02:00
|
|
|
|
2021-08-04 23:53:15 +02:00
|
|
|
let newFileContent
|
|
|
|
if (ext === '.yaml' || ext === '.yml') {
|
|
|
|
newFileContent = "extends: 'next'"
|
|
|
|
} else {
|
|
|
|
newFileContent = CommentJson.stringify(selectedConfig, null, 2)
|
2021-06-03 14:01:24 +02:00
|
|
|
|
2021-08-04 23:53:15 +02:00
|
|
|
if (ext === '.js') {
|
|
|
|
newFileContent = 'module.exports = ' + newFileContent
|
2021-04-30 13:09:07 +02:00
|
|
|
}
|
2021-06-03 14:01:24 +02:00
|
|
|
}
|
2021-04-30 13:09:07 +02:00
|
|
|
|
2021-08-04 23:53:15 +02:00
|
|
|
await fs.writeFile(eslintrcFile, newFileContent + os.EOL)
|
|
|
|
|
|
|
|
Log.info(
|
|
|
|
`We detected an empty ESLint configuration file (${chalk.bold(
|
|
|
|
path.basename(eslintrcFile)
|
|
|
|
)}) and updated it for you!`
|
|
|
|
)
|
|
|
|
} else if (!exists && emptyPkgJsonConfig && packageJsonConfig) {
|
|
|
|
packageJsonConfig.eslintConfig = selectedConfig
|
2021-06-03 14:01:24 +02:00
|
|
|
|
2021-08-04 23:53:15 +02:00
|
|
|
if (pkgJsonPath)
|
|
|
|
await fs.writeFile(
|
|
|
|
pkgJsonPath,
|
|
|
|
CommentJson.stringify(packageJsonConfig, null, 2) + os.EOL
|
2021-06-03 14:01:24 +02:00
|
|
|
)
|
2021-08-04 23:53:15 +02:00
|
|
|
|
|
|
|
Log.info(
|
|
|
|
`We detected an empty ${chalk.bold(
|
|
|
|
'eslintConfig'
|
|
|
|
)} field in package.json and updated it for you!`
|
|
|
|
)
|
|
|
|
} else if (!exists) {
|
2021-04-30 13:09:07 +02:00
|
|
|
await fs.writeFile(
|
2021-08-04 23:53:15 +02:00
|
|
|
path.join(baseDir, '.eslintrc.json'),
|
|
|
|
CommentJson.stringify(selectedConfig, null, 2) + os.EOL
|
2021-04-30 13:09:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
console.log(
|
2021-06-03 14:01:24 +02:00
|
|
|
chalk.green(
|
|
|
|
`We created the ${chalk.bold(
|
2021-07-05 10:58:09 +02:00
|
|
|
'.eslintrc.json'
|
2021-08-04 23:53:15 +02:00
|
|
|
)} file for you and included your selected configuration.`
|
2021-06-03 14:01:24 +02:00
|
|
|
)
|
2021-04-30 13:09:07 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|