-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
154 lines (127 loc) · 4.53 KB
/
index.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { stringify } from 'javascript-stringify'
import * as editorconfigs from './templates/editorconfigs.js'
import * as prettierrcs from './templates/prettierrcs.js'
import versionMap from './versionMap.cjs'
const CREATE_ALIAS_SETTING_PLACEHOLDER = 'CREATE_ALIAS_SETTING_PLACEHOLDER'
export { CREATE_ALIAS_SETTING_PLACEHOLDER }
function stringifyJS (value, styleGuide) {
// eslint-disable-next-line no-shadow
const result = stringify(value, (val, indent, stringify, key) => {
if (key === 'CREATE_ALIAS_SETTING_PLACEHOLDER') {
return `(${stringify(val)})`
}
return stringify(val)
}, 2)
return result.replace(
'CREATE_ALIAS_SETTING_PLACEHOLDER: ',
`...require('@vue/eslint-config-${styleGuide}/createAliasSetting')`
)
}
const isObject = (val) => val && typeof val === 'object'
const mergeArrayWithDedupe = (a, b) => Array.from(new Set([...a, ...b]))
/**
* Recursively merge the content of the new object to the existing one
* @param {Object} target the existing object
* @param {Object} obj the new object
*/
export function deepMerge (target, obj) {
for (const key of Object.keys(obj)) {
const oldVal = target[key]
const newVal = obj[key]
if (Array.isArray(oldVal) && Array.isArray(newVal)) {
target[key] = mergeArrayWithDedupe(oldVal, newVal)
} else if (isObject(oldVal) && isObject(newVal)) {
target[key] = deepMerge(oldVal, newVal)
} else {
target[key] = newVal
}
}
return target
}
// This is also used in `create-vue`
export default function createConfig ({
vueVersion = '3.x', // '2.x' | '3.x' (TODO: 2.7 / vue-demi)
styleGuide = 'default', // default | airbnb | typescript
hasTypeScript = false, // js | ts
needsPrettier = false, // true | false
additionalConfig = {}, // e.g. Cypress, createAliasSetting for Airbnb, etc.
additionalDependencies = {} // e.g. eslint-plugin-cypress
}) {
// This is the pkg object to extend
const pkg = { devDependencies: {} }
const addDependency = (name) => {
pkg.devDependencies[name] = versionMap[name]
}
addDependency('eslint')
addDependency('eslint-plugin-vue')
if (styleGuide !== 'default' || hasTypeScript || needsPrettier) {
addDependency('@rushstack/eslint-patch')
}
const language = hasTypeScript ? 'typescript' : 'javascript'
const eslintConfig = {
root: true,
extends: [
vueVersion.startsWith('2')
? 'plugin:vue/essential'
: 'plugin:vue/vue3-essential'
]
}
const addDependencyAndExtend = (name) => {
addDependency(name)
eslintConfig.extends.push(name)
}
switch (`${styleGuide}-${language}`) {
case 'default-javascript':
eslintConfig.extends.push('eslint:recommended')
break
case 'default-typescript':
eslintConfig.extends.push('eslint:recommended')
addDependencyAndExtend('@vue/eslint-config-typescript')
break
case 'airbnb-javascript':
case 'standard-javascript':
addDependencyAndExtend(`@vue/eslint-config-${styleGuide}`)
break
case 'airbnb-typescript':
case 'standard-typescript':
addDependencyAndExtend(`@vue/eslint-config-${styleGuide}-with-typescript`)
break
default:
throw new Error(`unexpected combination of styleGuide and language: ${styleGuide}-${language}`)
}
deepMerge(pkg.devDependencies, additionalDependencies)
deepMerge(eslintConfig, additionalConfig)
if (needsPrettier) {
addDependency('prettier')
addDependency('@vue/eslint-config-prettier')
eslintConfig.extends.push('@vue/eslint-config-prettier/skip-formatting')
}
const files = {
'.eslintrc.cjs': ''
}
if (styleGuide === 'default') {
// Both Airbnb & Standard have already set `env: node`
files['.eslintrc.cjs'] += '/* eslint-env node */\n'
// Both Airbnb & Standard have already set `ecmaVersion`
// The default in eslint-plugin-vue is 2020, which doesn't support top-level await
eslintConfig.parserOptions = {
ecmaVersion: 'latest'
}
}
if (pkg.devDependencies['@rushstack/eslint-patch']) {
files['.eslintrc.cjs'] += "require('@rushstack/eslint-patch/modern-module-resolution')\n\n"
}
files['.eslintrc.cjs'] += `module.exports = ${stringifyJS(eslintConfig, styleGuide)}\n`
// .editorconfig & .prettierrc.json
if (editorconfigs[styleGuide]) {
files['.editorconfig'] = editorconfigs[styleGuide]
}
if (needsPrettier) {
// Prettier recommends an explicit configuration file to let the editor know that it's used.
files['.prettierrc.json'] = JSON.stringify(prettierrcs[styleGuide], undefined, 2)
}
return {
pkg,
files
}
}