-
Notifications
You must be signed in to change notification settings - Fork 473
/
Copy pathrenderEslint.ts
68 lines (57 loc) · 2.17 KB
/
renderEslint.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
64
65
66
67
68
import * as fs from 'node:fs'
import * as path from 'node:path'
import type { Linter } from 'eslint'
import createESLintConfig from '@vue/create-eslint-config'
import sortDependencies from './sortDependencies'
import deepMerge from './deepMerge'
import eslintTemplatePackage from '../template/eslint/package.json' assert { type: 'json' }
const eslintDeps = eslintTemplatePackage.devDependencies
export default function renderEslint(
rootDir,
{ needsTypeScript, needsCypress, needsCypressCT, needsPrettier }
) {
const additionalConfig: Linter.Config = {}
const additionalDependencies = {}
if (needsCypress) {
additionalConfig.overrides = [
{
files: needsCypressCT
? [
'**/__tests__/*.{cy,spec}.{js,ts,jsx,tsx}',
'cypress/e2e/**/*.{cy,spec}.{js,ts,jsx,tsx}'
]
: ['cypress/e2e/**/*.{cy,spec}.{js,ts,jsx,tsx}'],
extends: ['plugin:cypress/recommended']
}
]
additionalDependencies['eslint-plugin-cypress'] = eslintDeps['eslint-plugin-cypress']
}
const { pkg, files } = createESLintConfig({
vueVersion: '3.x',
// we currently don't support other style guides
styleGuide: 'default',
hasTypeScript: needsTypeScript,
needsPrettier,
additionalConfig,
additionalDependencies
})
// update package.json
const packageJsonPath = path.resolve(rootDir, 'package.json')
const existingPkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
const updatedPkg = sortDependencies(
deepMerge(deepMerge(existingPkg, pkg), {
scripts: {
// Note that we reuse .gitignore here to avoid duplicating the configuration
lint: needsTypeScript
? 'eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore'
: 'eslint . --ext .vue,.js,.jsx,.cjs,.mjs --fix --ignore-path .gitignore'
}
})
)
fs.writeFileSync(packageJsonPath, JSON.stringify(updatedPkg, null, 2) + '\n', 'utf-8')
// write to .eslintrc.cjs, .prettierrc.json, etc.
for (const [fileName, content] of Object.entries(files)) {
const fullPath = path.resolve(rootDir, fileName)
fs.writeFileSync(fullPath, content as string, 'utf-8')
}
}