forked from eslint/create-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig-generator.js
288 lines (247 loc) · 11.1 KB
/
config-generator.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
* @fileoverview to generate config files.
* @author 唯然<[email protected]>
*/
import process from "node:process";
import path from "node:path";
import { spawnSync } from "node:child_process";
import { writeFile } from "node:fs/promises";
import enquirer from "enquirer";
import { isPackageTypeModule, installSyncSaveDev, fetchPeerDependencies, findPackageJson } from "./utils/npm-utils.js";
import { getShorthandName } from "./utils/naming.js";
import * as log from "./utils/logging.js";
/**
* Class representing a ConfigGenerator.
*/
export class ConfigGenerator {
/**
* Create a ConfigGenerator.
* @param {Object} options The options for the ConfigGenerator.
* @param {string} options.cwd The current working directory.
* @param {Object} options.answers The answers provided by the user.
* @returns {ConfigGenerator} The ConfigGenerator instance.
*/
constructor(options) {
this.cwd = options.cwd;
this.packageJsonPath = options.packageJsonPath || findPackageJson(this.cwd);
this.answers = options.answers || {};
this.result = {
devDependencies: ["eslint"],
configFilename: "eslint.config.js",
configContent: "",
installFlags: ["-D"]
};
}
/**
* Prompt the user for input.
* @returns {void}
*/
async prompt() {
const questions = [
{
type: "select",
name: "purpose",
message: "How would you like to use ESLint?",
initial: 1,
choices: [
{ message: "To check syntax only", name: "syntax" },
{ message: "To check syntax and find problems", name: "problems" }
]
},
{
type: "select",
name: "moduleType",
message: "What type of modules does your project use?",
initial: 0,
choices: [
{ message: "JavaScript modules (import/export)", name: "esm" },
{ message: "CommonJS (require/exports)", name: "commonjs" },
{ message: "None of these", name: "script" }
]
},
{
type: "select",
name: "framework",
message: "Which framework does your project use?",
initial: 0,
choices: [
{ message: "React", name: "react" },
{ message: "Vue.js", name: "vue" },
{ message: "None of these", name: "none" }
]
},
{
type: "select",
name: "language",
message: "Does your project use TypeScript?",
choices: [
{ message: "No", name: "javascript" },
{ message: "Yes", name: "typescript" }
],
initial: 0
},
{
type: "multiselect",
name: "env",
message: "Where does your code run?",
hint: "(Press <space> to select, <a> to toggle all, <i> to invert selection)",
initial: 0,
choices: [
{ message: "Browser", name: "browser" },
{ message: "Node", name: "node" }
]
}
];
const answers = await enquirer.prompt(questions);
Object.assign(this.answers, answers);
}
/**
* Calculate the configuration based on the user's answers.
* @returns {void}
*/
async calc() {
const isESMModule = isPackageTypeModule(this.packageJsonPath);
this.result.configFilename = isESMModule ? "eslint.config.js" : "eslint.config.mjs";
this.answers.config = typeof this.answers.config === "string"
? { packageName: this.answers.config, type: "flat" }
: this.answers.config;
const extensions = []; // file extensions to lint, the default is ["js", "mjs", "cjs"]
let importContent = "";
const helperContent = `import path from "path";
import { fileURLToPath } from "url";
import { FlatCompat } from "@eslint/eslintrc";
import pluginJs from "@eslint/js";
// mimic CommonJS variables -- not needed if using CommonJS
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: pluginJs.configs.recommended});
`;
let exportContent = "";
let needCompatHelper = false;
if (this.answers.moduleType === "commonjs" || this.answers.moduleType === "script") {
exportContent += ` {files: ["**/*.js"], languageOptions: {sourceType: "${this.answers.moduleType}"}},\n`;
}
if (this.answers.env?.length > 0) {
this.result.devDependencies.push("globals");
importContent += "import globals from \"globals\";\n";
const envContent = {
browser: "globals: globals.browser",
node: "globals: globals.node",
"browser,node": "globals: {...globals.browser, ...globals.node}"
};
exportContent += ` {languageOptions: { ${envContent[this.answers.env.join(",")]} }},\n`;
}
if (this.answers.purpose === "syntax") {
// no need to install any plugin
} else if (this.answers.purpose === "problems") {
this.result.devDependencies.push("@eslint/js");
importContent += "import pluginJs from \"@eslint/js\";\n";
exportContent += " pluginJs.configs.recommended,\n";
}
if (this.answers.language === "typescript") {
extensions.push("ts");
this.result.devDependencies.push("typescript-eslint");
importContent += "import tseslint from \"typescript-eslint\";\n";
exportContent += " ...tseslint.configs.recommended,\n";
}
if (this.answers.framework === "vue") {
extensions.push("vue");
this.result.devDependencies.push("eslint-plugin-vue");
importContent += "import pluginVue from \"eslint-plugin-vue\";\n";
exportContent += " ...pluginVue.configs[\"flat/essential\"],\n";
// https://eslint.vuejs.org/user-guide/#how-to-use-a-custom-parser
if (this.answers.language === "typescript") {
exportContent += " {files: [\"**/*.vue\"], languageOptions: {parserOptions: {parser: tseslint.parser}}},\n";
}
}
if (this.answers.framework === "react") {
extensions.push("jsx");
if (this.answers.language === "typescript") {
extensions.push("tsx");
}
this.result.devDependencies.push("eslint-plugin-react");
importContent += "import pluginReact from \"eslint-plugin-react\";\n";
exportContent += " pluginReact.configs.flat.recommended,\n";
}
if (this.answers.config) {
const config = this.answers.config;
this.result.devDependencies.push(config.packageName);
// install peer dependencies - it's needed for most eslintrc-style shared configs.
const peers = await fetchPeerDependencies(config.packageName);
if (peers !== null) {
const eslintIndex = peers.findIndex(dep => (dep.startsWith("eslint@")));
if (eslintIndex === -1) {
// eslint is not in the peer dependencies
this.result.devDependencies.push(...peers);
} else {
// eslint is in the peer dependencies => overwrite eslint version
this.result.devDependencies[0] = peers[eslintIndex];
peers.splice(eslintIndex, 1);
this.result.devDependencies.push(...peers);
}
}
if (config.type === "flat" || config.type === void 0) {
importContent += `import config from "${config.packageName}";\n`;
exportContent += " ...[].concat(config),\n";
} else if (config.type === "eslintrc") {
needCompatHelper = true;
const shorthandName = getShorthandName(config.packageName, "eslint-config");
exportContent += ` ...compat.extends("${shorthandName}"),\n`;
}
}
if (needCompatHelper) {
this.result.devDependencies.push("@eslint/eslintrc", "@eslint/js");
}
const lintFilesConfig = extensions.length > 0 ? ` {files: ["**/*.{${["js", "mjs", "cjs", ...extensions]}}"]},\n` : "";
exportContent = `${lintFilesConfig}${exportContent}`;
this.result.configContent = `${importContent}
${needCompatHelper ? helperContent : ""}
/** @type {import('eslint').Linter.Config[]} */
export default [\n${exportContent || " {}\n"}];`; // defaults to `[{}]` to avoid empty config warning
}
/**
* Output the configuration.
* @returns {void}
*/
async output() {
log.info("The config that you've selected requires the following dependencies:\n");
log.info(this.result.devDependencies.join(", "));
const questions = [{
type: "toggle",
name: "executeInstallation",
message: "Would you like to install them now?",
enabled: "Yes",
disabled: "No",
initial: 1
}, {
type: "select",
name: "packageManager",
message: "Which package manager do you want to use?",
initial: 0,
choices: ["npm", "yarn", "pnpm", "bun"],
skip() {
return this.state.answers.executeInstallation === false;
}
}];
const { executeInstallation, packageManager } = (await enquirer.prompt(questions));
const configPath = path.join(this.cwd, this.result.configFilename);
if (executeInstallation === true) {
log.info("☕️Installing...");
installSyncSaveDev(this.result.devDependencies, packageManager, this.result.installFlags);
await writeFile(configPath, this.result.configContent);
// import("eslint") won't work in some cases.
// refs: https://github.com/eslint/create-config/issues/8, https://github.com/eslint/create-config/issues/12
const eslintBin = path.join(this.packageJsonPath, "../node_modules/eslint/bin/eslint.js");
const result = spawnSync(process.execPath, [eslintBin, "--fix", "--quiet", configPath], { encoding: "utf8" });
if (result.error || result.status !== 0) {
log.error("A config file was generated, but the config file itself may not follow your linting rules.");
} else {
log.info(`Successfully created ${configPath} file.`);
}
} else {
await writeFile(configPath, this.result.configContent);
log.info(`Successfully created ${configPath} file.`);
log.warn("You will need to install the dependencies yourself.");
}
}
}