-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
180 lines (159 loc) · 4.74 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// @ts-check
import renderEjsFile from './renderEjsFile.js'
import thisPackage from './package.json' with { type: 'json' }
const versionMap = thisPackage.devDependencies
// This is also used in `create-vue`
export default function createConfig({
styleGuide = 'default', // default ~~| airbnb | standard~~ only the default is supported for now
hasTypeScript = false,
needsPrettier = false,
needsOxlint = false,
additionalConfigs = [],
}) {
// This is the pkg object to extend
const pickDependencies = (/** @type {string[]} */ keys) =>
pickKeysFromObject(versionMap, keys)
const pkg = {
devDependencies: pickDependencies(['eslint', 'eslint-plugin-vue']),
scripts: {},
}
const fileExtensions = ['vue']
if (hasTypeScript) {
// TODO: allowJs option
fileExtensions.unshift('ts', 'mts', 'tsx')
additionalConfigs.unshift({
devDependencies: pickDependencies(['@vue/eslint-config-typescript']),
afterVuePlugin: [
{
importer:
"import vueTsEslintConfig from '@vue/eslint-config-typescript'",
// TODO: supportedScriptLangs
content: '...vueTsEslintConfig(),',
},
],
})
} else {
fileExtensions.unshift('js', 'mjs', 'jsx')
additionalConfigs.unshift({
devDependencies: pickDependencies(['@eslint/js']),
beforeVuePlugin: [
{
importer: "import js from '@eslint/js'",
content: 'js.configs.recommended,',
},
],
})
}
if (needsOxlint) {
additionalConfigs.push({
devDependencies: pickDependencies([
'oxlint',
'eslint-plugin-oxlint',
'npm-run-all2',
]),
afterVuePlugin: [
{
importer: "import oxlint from 'eslint-plugin-oxlint'",
content: "oxlint.configs['flat/recommended'],",
},
],
})
pkg.scripts['lint:oxlint'] = 'oxlint . --fix -D correctness --ignore-path .gitignore'
pkg.scripts['lint:eslint'] = 'eslint . --fix'
pkg.scripts.lint = 'run-s lint:*'
} else {
pkg.scripts.lint = 'eslint . --fix'
}
if (needsPrettier) {
additionalConfigs.push({
devDependencies: pickDependencies([
'prettier',
'@vue/eslint-config-prettier',
]),
afterVuePlugin: [
{
importer:
"import skipFormatting from '@vue/eslint-config-prettier/skip-formatting'",
content: 'skipFormatting,',
},
],
})
// Default to only format the `src/` directory to avoid too much noise, and
// the need for a `.prettierignore` file.
// Users can still append any paths they'd like to format to the command,
// e.g. `npm run format cypress/`.
pkg.scripts.format = 'prettier --write src/'
}
const configsBeforeVuePlugin = [],
configsAfterVuePlugin = []
for (const config of additionalConfigs) {
deepMerge(pkg.devDependencies, config.devDependencies ?? {})
configsBeforeVuePlugin.push(...(config.beforeVuePlugin ?? []))
configsAfterVuePlugin.push(...(config.afterVuePlugin ?? []))
}
const templateData = {
styleGuide,
needsPrettier,
fileExtensions,
configsBeforeVuePlugin,
configsAfterVuePlugin,
}
const files = {
'eslint.config.js': renderEjsFile(
'./templates/eslint.config.js.ejs',
templateData,
),
'.editorconfig': renderEjsFile(
'./templates/_editorconfig.ejs',
templateData,
),
}
// .editorconfig & .prettierrc.json
if (needsPrettier) {
// Prettier recommends an explicit configuration file to let the editor know that it's used.
files['.prettierrc.json'] = renderEjsFile(
'./templates/_prettierrc.json.ejs',
templateData,
)
}
return {
pkg,
files,
}
}
/**
* Picks specified keys from an object.
*
* @param {Object} obj - The source object.
* @param {string[]} keys - The keys to pick from the object.
* @returns {Object} - A new object with the picked keys.
*/
function pickKeysFromObject(obj, keys) {
return keys.reduce((acc, key) => {
if (key in obj) {
acc[key] = obj[key]
}
return acc
}, {})
}
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
}