@2030/eslint-config 2.0.3 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/index.js CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env node
2
- import '../dist/cli.js'
2
+ import '../dist/cli.mjs'
package/bin/index.mjs ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ import '../dist/cli.mjs'
package/dist/cli.d.mts ADDED
@@ -0,0 +1 @@
1
+ export { };
package/dist/cli.mjs ADDED
@@ -0,0 +1,340 @@
1
+ import process from "node:process";
2
+ import fs from "node:fs/promises";
3
+ import fs$1 from "node:fs";
4
+ import path from "node:path";
5
+ import * as p from "@clack/prompts";
6
+ import c, { green } from "ansis";
7
+ import { cac } from "cac";
8
+ import parse from "parse-gitignore";
9
+ import { execSync } from "node:child_process";
10
+
11
+ //#region package.json
12
+ var version = "3.0.0";
13
+
14
+ //#endregion
15
+ //#region src/cli/constants.ts
16
+ const vscodeSettingsString = `
17
+ // Disable the default formatter, use eslint instead
18
+ "prettier.enable": false,
19
+ "editor.formatOnSave": false,
20
+
21
+ // Auto fix
22
+ "editor.codeActionsOnSave": {
23
+ "source.fixAll.eslint": "explicit",
24
+ "source.organizeImports": "never"
25
+ },
26
+
27
+ // Silent the stylistic rules in your IDE, but still auto fix them
28
+ "eslint.rules.customizations": [
29
+ { "rule": "style/*", "severity": "off", "fixable": true },
30
+ { "rule": "format/*", "severity": "off", "fixable": true },
31
+ { "rule": "*-indent", "severity": "off", "fixable": true },
32
+ { "rule": "*-spacing", "severity": "off", "fixable": true },
33
+ { "rule": "*-spaces", "severity": "off", "fixable": true },
34
+ { "rule": "*-order", "severity": "off", "fixable": true },
35
+ { "rule": "*-dangle", "severity": "off", "fixable": true },
36
+ { "rule": "*-newline", "severity": "off", "fixable": true },
37
+ { "rule": "*quotes", "severity": "off", "fixable": true },
38
+ { "rule": "*semi", "severity": "off", "fixable": true }
39
+ ],
40
+
41
+ // Enable eslint for all supported languages
42
+ "eslint.validate": [
43
+ "javascript",
44
+ "javascriptreact",
45
+ "typescript",
46
+ "typescriptreact",
47
+ "vue",
48
+ "html",
49
+ "markdown",
50
+ "json",
51
+ "json5",
52
+ "jsonc",
53
+ "yaml",
54
+ "toml",
55
+ "xml",
56
+ "gql",
57
+ "graphql",
58
+ "astro",
59
+ "svelte",
60
+ "css",
61
+ "less",
62
+ "scss",
63
+ "pcss",
64
+ "postcss"
65
+ ]
66
+ `;
67
+ const frameworkOptions = [
68
+ {
69
+ label: c.green("Vue"),
70
+ value: "vue"
71
+ },
72
+ {
73
+ label: c.cyan("React"),
74
+ value: "react"
75
+ },
76
+ {
77
+ label: c.red("Svelte"),
78
+ value: "svelte"
79
+ },
80
+ {
81
+ label: c.magenta("Astro"),
82
+ value: "astro"
83
+ },
84
+ {
85
+ label: c.cyan("Solid"),
86
+ value: "solid"
87
+ },
88
+ {
89
+ label: c.blue("Slidev"),
90
+ value: "slidev"
91
+ }
92
+ ];
93
+ const frameworks = frameworkOptions.map(({ value }) => value);
94
+ const extraOptions = [{
95
+ hint: "Use external formatters (Prettier and/or dprint) to format files that ESLint cannot handle yet (.css, .html, etc)",
96
+ label: c.red("Formatter"),
97
+ value: "formatter"
98
+ }, {
99
+ label: c.cyan("UnoCSS"),
100
+ value: "unocss"
101
+ }];
102
+ const extra = extraOptions.map(({ value }) => value);
103
+ const dependenciesMap = {
104
+ astro: ["eslint-plugin-astro", "astro-eslint-parser"],
105
+ formatter: ["eslint-plugin-format"],
106
+ formatterAstro: ["prettier-plugin-astro"],
107
+ nextjs: ["@next/eslint-plugin-next"],
108
+ react: [
109
+ "@eslint-react/eslint-plugin",
110
+ "eslint-plugin-react-hooks",
111
+ "eslint-plugin-react-refresh"
112
+ ],
113
+ slidev: ["prettier-plugin-slidev"],
114
+ solid: ["eslint-plugin-solid"],
115
+ svelte: ["eslint-plugin-svelte", "svelte-eslint-parser"],
116
+ unocss: ["@unocss/eslint-plugin"],
117
+ vue: []
118
+ };
119
+
120
+ //#endregion
121
+ //#region src/cli/utils.ts
122
+ function isGitClean() {
123
+ try {
124
+ execSync("git diff-index --quiet HEAD --");
125
+ return true;
126
+ } catch {
127
+ return false;
128
+ }
129
+ }
130
+ function getEslintConfigContent(mainConfig, additionalConfigs) {
131
+ return `
132
+ import jun from '@2030/eslint-config'
133
+
134
+ export default jun({
135
+ ${mainConfig}
136
+ }${additionalConfigs?.map((config) => `,{\n${config}\n}`)})
137
+ `.trimStart();
138
+ }
139
+
140
+ //#endregion
141
+ //#region src/cli/stages/update-eslint-files.ts
142
+ async function updateEslintFiles(result) {
143
+ const cwd = process.cwd();
144
+ const pathESLintIgnore = path.join(cwd, ".eslintignore");
145
+ const pathPackageJSON = path.join(cwd, "package.json");
146
+ const pkgContent = await fs.readFile(pathPackageJSON, "utf-8");
147
+ const configFileName = JSON.parse(pkgContent).type === "module" ? "eslint.config.js" : "eslint.config.mjs";
148
+ const pathFlatConfig = path.join(cwd, configFileName);
149
+ const eslintIgnores = [];
150
+ if (fs$1.existsSync(pathESLintIgnore)) {
151
+ p.log.step(c.cyan`Migrating existing .eslintignore`);
152
+ const globs = parse(await fs.readFile(pathESLintIgnore, "utf-8")).globs();
153
+ for (const glob of globs) if (glob.type === "ignore") eslintIgnores.push(...glob.patterns);
154
+ else if (glob.type === "unignore") eslintIgnores.push(...glob.patterns.map((pattern) => `!${pattern}`));
155
+ }
156
+ const configLines = [];
157
+ if (eslintIgnores.length) configLines.push(`ignores: ${JSON.stringify(eslintIgnores)},`);
158
+ if (result.extra.includes("formatter")) configLines.push(`formatters: true,`);
159
+ if (result.extra.includes("unocss")) configLines.push(`unocss: true,`);
160
+ for (const framework of result.frameworks) configLines.push(`${framework}: true,`);
161
+ const eslintConfigContent = getEslintConfigContent(configLines.map((i) => ` ${i}`).join("\n"), []);
162
+ await fs.writeFile(pathFlatConfig, eslintConfigContent);
163
+ p.log.success(c.green`Created ${configFileName}`);
164
+ const files = fs$1.readdirSync(cwd);
165
+ const legacyConfig = [];
166
+ files.forEach((file) => {
167
+ if (/eslint|prettier/.test(file) && !/eslint\.config\./.test(file)) legacyConfig.push(file);
168
+ });
169
+ if (legacyConfig.length) p.note(c.dim(legacyConfig.join(", ")), "You can now remove those files manually");
170
+ }
171
+
172
+ //#endregion
173
+ //#region src/cli/constants-generated.ts
174
+ const versionsMap = {
175
+ "@eslint-react/eslint-plugin": "2.7.0",
176
+ "@next/eslint-plugin-next": "16.1.2",
177
+ "@unocss/eslint-plugin": "66.6.0",
178
+ "astro-eslint-parser": "1.2.2",
179
+ "eslint": "9.39.2",
180
+ "eslint-plugin-astro": "1.5.0",
181
+ "eslint-plugin-format": "1.3.1",
182
+ "eslint-plugin-react-hooks": "7.0.1",
183
+ "eslint-plugin-react-refresh": "0.4.26",
184
+ "eslint-plugin-solid": "0.14.5",
185
+ "eslint-plugin-svelte": "3.14.0",
186
+ "prettier-plugin-astro": "0.14.1",
187
+ "prettier-plugin-slidev": "1.0.5",
188
+ "svelte-eslint-parser": "1.4.1"
189
+ };
190
+
191
+ //#endregion
192
+ //#region src/cli/stages/update-package-json.ts
193
+ async function updatePackageJson(result) {
194
+ const cwd = process.cwd();
195
+ const pathPackageJSON = path.join(cwd, "package.json");
196
+ p.log.step(c.cyan`Bumping @2030/eslint-config to v${version}`);
197
+ const pkgContent = await fs.readFile(pathPackageJSON, "utf-8");
198
+ const pkg = JSON.parse(pkgContent);
199
+ pkg.devDependencies ??= {};
200
+ pkg.devDependencies["@2030/eslint-config"] = `^${version}`;
201
+ pkg.devDependencies.eslint ??= versionsMap.eslint;
202
+ const addedPackages = [];
203
+ if (result.extra.length) result.extra.forEach((item) => {
204
+ switch (item) {
205
+ case "formatter":
206
+ [...dependenciesMap.formatter, ...result.frameworks.includes("astro") ? dependenciesMap.formatterAstro : []].forEach((f) => {
207
+ if (!f) return;
208
+ pkg.devDependencies[f] = versionsMap[f];
209
+ addedPackages.push(f);
210
+ });
211
+ break;
212
+ case "unocss":
213
+ dependenciesMap.unocss.forEach((f) => {
214
+ pkg.devDependencies[f] = versionsMap[f];
215
+ addedPackages.push(f);
216
+ });
217
+ break;
218
+ }
219
+ });
220
+ for (const framework of result.frameworks) {
221
+ const deps = dependenciesMap[framework];
222
+ if (deps) deps.forEach((f) => {
223
+ pkg.devDependencies[f] = versionsMap[f];
224
+ addedPackages.push(f);
225
+ });
226
+ }
227
+ if (addedPackages.length) p.note(c.dim(addedPackages.join(", ")), "Added packages");
228
+ await fs.writeFile(pathPackageJSON, JSON.stringify(pkg, null, 2));
229
+ p.log.success(c.green`Changes wrote to package.json`);
230
+ }
231
+
232
+ //#endregion
233
+ //#region src/cli/stages/update-vscode-settings.ts
234
+ async function updateVscodeSettings(result) {
235
+ const cwd = process.cwd();
236
+ if (!result.updateVscodeSettings) return;
237
+ const dotVscodePath = path.join(cwd, ".vscode");
238
+ const settingsPath = path.join(dotVscodePath, "settings.json");
239
+ if (!fs$1.existsSync(dotVscodePath)) await fs.mkdir(dotVscodePath, { recursive: true });
240
+ if (!fs$1.existsSync(settingsPath)) {
241
+ await fs.writeFile(settingsPath, `{${vscodeSettingsString}}\n`, "utf-8");
242
+ p.log.success(green`Created .vscode/settings.json`);
243
+ } else {
244
+ let settingsContent = await fs.readFile(settingsPath, "utf8");
245
+ settingsContent = settingsContent.trim().replace(/\s*\}$/, "");
246
+ settingsContent += settingsContent.endsWith(",") || settingsContent.endsWith("{") ? "" : ",";
247
+ settingsContent += `${vscodeSettingsString}}\n`;
248
+ await fs.writeFile(settingsPath, settingsContent, "utf-8");
249
+ p.log.success(green`Updated .vscode/settings.json`);
250
+ }
251
+ }
252
+
253
+ //#endregion
254
+ //#region src/cli/run.ts
255
+ async function run(options = {}) {
256
+ const argSkipPrompt = !!process.env.SKIP_PROMPT || options.yes;
257
+ const argTemplate = options.frameworks?.map((m) => m?.trim()).filter(Boolean);
258
+ const argExtra = options.extra?.map((m) => m?.trim()).filter(Boolean);
259
+ if (fs$1.existsSync(path.join(process.cwd(), "eslint.config.js"))) {
260
+ p.log.warn(c.yellow`eslint.config.js already exists, migration wizard exited.`);
261
+ return process.exit(1);
262
+ }
263
+ let result = {
264
+ extra: argExtra ?? [],
265
+ frameworks: argTemplate ?? [],
266
+ uncommittedConfirmed: false,
267
+ updateVscodeSettings: true
268
+ };
269
+ if (!argSkipPrompt) {
270
+ result = await p.group({
271
+ uncommittedConfirmed: () => {
272
+ if (argSkipPrompt || isGitClean()) return Promise.resolve(true);
273
+ return p.confirm({
274
+ initialValue: false,
275
+ message: "There are uncommitted changes in the current repository, are you sure to continue?"
276
+ });
277
+ },
278
+ frameworks: ({ results }) => {
279
+ const isArgTemplateValid = typeof argTemplate === "string" && !!frameworks.includes(argTemplate);
280
+ if (!results.uncommittedConfirmed || isArgTemplateValid) return;
281
+ const message = !isArgTemplateValid && argTemplate ? `"${argTemplate}" isn't a valid template. Please choose from below: ` : "Select a framework:";
282
+ return p.multiselect({
283
+ message: c.reset(message),
284
+ options: frameworkOptions,
285
+ required: false
286
+ });
287
+ },
288
+ extra: ({ results }) => {
289
+ const isArgExtraValid = argExtra?.length && !argExtra.filter((element) => !extra.includes(element)).length;
290
+ if (!results.uncommittedConfirmed || isArgExtraValid) return;
291
+ const message = !isArgExtraValid && argExtra ? `"${argExtra}" isn't a valid extra util. Please choose from below: ` : "Select a extra utils:";
292
+ return p.multiselect({
293
+ message: c.reset(message),
294
+ options: extraOptions,
295
+ required: false
296
+ });
297
+ },
298
+ updateVscodeSettings: ({ results }) => {
299
+ if (!results.uncommittedConfirmed) return;
300
+ return p.confirm({
301
+ initialValue: true,
302
+ message: "Update .vscode/settings.json for better VS Code experience?"
303
+ });
304
+ }
305
+ }, { onCancel: () => {
306
+ p.cancel("Operation cancelled.");
307
+ process.exit(0);
308
+ } });
309
+ if (!result.uncommittedConfirmed) return process.exit(1);
310
+ }
311
+ await updatePackageJson(result);
312
+ await updateEslintFiles(result);
313
+ await updateVscodeSettings(result);
314
+ p.log.success(c.green`Setup completed`);
315
+ p.outro(`Now you can update the dependencies by run ${c.blue("pnpm install")} and run ${c.blue("eslint --fix")}\n`);
316
+ }
317
+
318
+ //#endregion
319
+ //#region src/cli/index.ts
320
+ function header() {
321
+ console.log("\n");
322
+ p.intro(`${c.green`@2030/eslint-config `}${c.dim`v${version}`}`);
323
+ }
324
+ const cli = cac("@2030/eslint-config");
325
+ cli.command("", "Run the initialization or migration").option("--yes, -y", "Skip prompts and use default values", { default: false }).option("--template, -t <template>", "Use the framework template for optimal customization: vue / react / svelte / astro", { type: [] }).option("--extra, -e <extra>", "Use the extra utils: formatter / perfectionist / unocss", { type: [] }).action(async (args) => {
326
+ header();
327
+ try {
328
+ await run(args);
329
+ } catch (error) {
330
+ p.log.error(c.inverse.red(" Failed to migrate "));
331
+ p.log.error(c.red`�?${String(error)}`);
332
+ process.exit(1);
333
+ }
334
+ });
335
+ cli.help();
336
+ cli.version(version);
337
+ cli.parse();
338
+
339
+ //#endregion
340
+ export { };