File size: 1,908 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
import { promises as fs } from 'fs'
import { bold, green } from '../picocolors'
import os from 'os'
import path from 'path'
import * as CommentJson from 'next/dist/compiled/comment-json'
import type { ConfigAvailable } from './hasEslintConfiguration'

import * as Log from '../../build/output/log'

export async function writeDefaultConfig(
  baseDir: string,
  { exists, emptyEslintrc, emptyPkgJsonConfig }: ConfigAvailable,
  selectedConfig: any,
  eslintrcFile: string | null,
  pkgJsonPath: string | null,
  packageJsonConfig: { eslintConfig: any } | null
) {
  if (!exists && emptyEslintrc && eslintrcFile) {
    const ext = path.extname(eslintrcFile)

    let newFileContent
    if (ext === '.yaml' || ext === '.yml') {
      newFileContent = "extends: 'next'"
    } else {
      newFileContent = CommentJson.stringify(selectedConfig, null, 2)

      if (ext === '.js') {
        newFileContent = 'module.exports = ' + newFileContent
      }
    }

    await fs.writeFile(eslintrcFile, newFileContent + os.EOL)

    Log.info(
      `We detected an empty ESLint configuration file (${bold(
        path.basename(eslintrcFile)
      )}) and updated it for you!`
    )
  } else if (!exists && emptyPkgJsonConfig && packageJsonConfig) {
    packageJsonConfig.eslintConfig = selectedConfig

    if (pkgJsonPath)
      await fs.writeFile(
        pkgJsonPath,
        CommentJson.stringify(packageJsonConfig, null, 2) + os.EOL
      )

    Log.info(
      `We detected an empty ${bold(
        'eslintConfig'
      )} field in package.json and updated it for you!`
    )
  } else if (!exists) {
    await fs.writeFile(
      path.join(baseDir, '.eslintrc.json'),
      CommentJson.stringify(selectedConfig, null, 2) + os.EOL
    )

    console.log(
      green(
        `We created the ${bold(
          '.eslintrc.json'
        )} file for you and included your selected configuration.`
      )
    )
  }
}