@dhzh/eslint-config 1.26.0 → 1.27.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.
@@ -0,0 +1,253 @@
1
+ import process from "node:process";
2
+ import * as p from "@clack/prompts";
3
+ import c, { green } from "ansis";
4
+ import { cac } from "cac";
5
+ import fsp from "node:fs/promises";
6
+ import path from "node:path";
7
+ import fs from "node:fs";
8
+ //#region package.json
9
+ var version = "1.27.0";
10
+ var devDependencies = {
11
+ "@eslint/config-inspector": "^2.0.0",
12
+ "@prettier/plugin-xml": "^3.4.2",
13
+ "@types/eslint-plugin-tailwindcss": "^3.17.0",
14
+ "@types/node": "^24.12.2",
15
+ "@typescript-eslint/types": "^8.59.0",
16
+ "bumpp": "^11.0.1",
17
+ "bundle-require": "^5.1.0",
18
+ "eslint": "^10.2.1",
19
+ "eslint-typegen": "^2.3.1",
20
+ "lint-staged": "^16.4.0",
21
+ "simple-git-hooks": "^2.13.1",
22
+ "tailwindcss": "^4.2.4",
23
+ "tsdown": "^0.21.10",
24
+ "tsx": "^4.21.0",
25
+ "typescript": "^6.0.3"
26
+ };
27
+ //#endregion
28
+ //#region src/cli/stages/update-package-json.ts
29
+ async function updatePackageJson() {
30
+ const cwd = process.cwd();
31
+ const pathPackageJSON = path.join(cwd, "package.json");
32
+ p.log.step(c.cyan`Bumping @dhzh/eslint-config to v${version}`);
33
+ const pkgContent = await fsp.readFile(pathPackageJSON, "utf8");
34
+ const pkg = JSON.parse(pkgContent);
35
+ pkg.devDependencies ??= {};
36
+ pkg.devDependencies["@dhzh/eslint-config"] = `^${version}`;
37
+ pkg.devDependencies.eslint ??= devDependencies.eslint;
38
+ pkg.scripts ??= {};
39
+ pkg.scripts.lint = "eslint .";
40
+ pkg.scripts["lint-fix"] = "eslint --fix .";
41
+ await fsp.writeFile(pathPackageJSON, JSON.stringify(pkg, null, 2));
42
+ p.log.success(c.green`Changes wrote to package.json`);
43
+ }
44
+ //#endregion
45
+ //#region src/cli/constants.ts
46
+ const vscodeSettingsString = `
47
+ // Entry
48
+ "eslint.format.enable": true,
49
+ "eslint.ignoreUntitled": true,
50
+ "eslint.enable": true,
51
+
52
+ // Disable the default formatter, use eslint instead
53
+ "prettier.enable": false,
54
+
55
+ // Auto fix
56
+ "editor.codeActionsOnSave": {
57
+ "source.fixAll.eslint": "explicit",
58
+ "source.organizeImports": "never"
59
+ },
60
+
61
+ // Silent the stylistic rules in you IDE, but still auto fix them
62
+ // "eslint.rules.customizations": [
63
+ // { "rule": "style/*", "severity": "off", "fixable": true },
64
+ // { "rule": "format/*", "severity": "off", "fixable": true },
65
+ // { "rule": "*-indent", "severity": "off", "fixable": true },
66
+ // { "rule": "*-spacing", "severity": "off", "fixable": true },
67
+ // { "rule": "*-spaces", "severity": "off", "fixable": true },
68
+ // { "rule": "*-order", "severity": "off", "fixable": true },
69
+ // { "rule": "*-dangle", "severity": "off", "fixable": true },
70
+ // { "rule": "*-newline", "severity": "off", "fixable": true },
71
+ // { "rule": "*quotes", "severity": "off", "fixable": true },
72
+ // { "rule": "*semi", "severity": "off", "fixable": true }
73
+ // ],
74
+
75
+ // Enable eslint for all supported languages
76
+ "eslint.validate": [
77
+ "javascript",
78
+ "javascriptreact",
79
+ "typescript",
80
+ "typescriptreact",
81
+ "html",
82
+ "json",
83
+ "json5",
84
+ "jsonc",
85
+ "yaml",
86
+ "toml",
87
+ "xml",
88
+ "css",
89
+ "less",
90
+ "scss",
91
+ "pcss",
92
+ "postcss",
93
+ "gql",
94
+ "graphql",
95
+ "vue"
96
+ ],
97
+
98
+ // Enable eslint as the default formatter
99
+ "[javascript]": {
100
+ "editor.defaultFormatter": "dbaeumer.vscode-eslint"
101
+ },
102
+ "[javascriptreact]": {
103
+ "editor.defaultFormatter": "dbaeumer.vscode-eslint"
104
+ },
105
+ "[typescript]": {
106
+ "editor.defaultFormatter": "dbaeumer.vscode-eslint"
107
+ },
108
+ "[typescriptreact]": {
109
+ "editor.defaultFormatter": "dbaeumer.vscode-eslint"
110
+ },
111
+ "[html]": {
112
+ "editor.defaultFormatter": "dbaeumer.vscode-eslint"
113
+ },
114
+ "[json]": {
115
+ "editor.defaultFormatter": "dbaeumer.vscode-eslint"
116
+ },
117
+ "[jsonc]": {
118
+ "editor.defaultFormatter": "dbaeumer.vscode-eslint"
119
+ },
120
+ "[yaml]": {
121
+ "editor.defaultFormatter": "dbaeumer.vscode-eslint"
122
+ },
123
+ "[toml]": {
124
+ "editor.defaultFormatter": "dbaeumer.vscode-eslint"
125
+ },
126
+ "[xml]": {
127
+ "editor.defaultFormatter": "dbaeumer.vscode-eslint"
128
+ },
129
+ "[css]": {
130
+ "editor.defaultFormatter": "dbaeumer.vscode-eslint"
131
+ },
132
+ "[less]": {
133
+ "editor.defaultFormatter": "dbaeumer.vscode-eslint"
134
+ },
135
+ "[scss]": {
136
+ "editor.defaultFormatter": "dbaeumer.vscode-eslint"
137
+ },
138
+ "[pcss]": {
139
+ "editor.defaultFormatter": "dbaeumer.vscode-eslint"
140
+ },
141
+ "[postcss]": {
142
+ "editor.defaultFormatter": "dbaeumer.vscode-eslint"
143
+ },
144
+ "[gql]": {
145
+ "editor.defaultFormatter": "dbaeumer.vscode-eslint"
146
+ },
147
+ "[graphql]": {
148
+ "editor.defaultFormatter": "dbaeumer.vscode-eslint"
149
+ },
150
+ "[vue]": {
151
+ "editor.defaultFormatter": "dbaeumer.vscode-eslint"
152
+ }
153
+ `;
154
+ const eslintConfigContent = (options) => `import { defineConfig } from '@dhzh/eslint-config';
155
+
156
+ export default defineConfig(${options.hasNest ? `{
157
+ configs: {
158
+ json: {
159
+ packageJsonRequireType: false,
160
+ },
161
+ },
162
+ }` : ""});
163
+ `;
164
+ const npmignoreString = `# If these files (ESLint flat config files) are not included in .npmignore,
165
+ # they will be published, and importing devDependencies in published files will trigger \`n/no-unpublished-import\` errors.
166
+ eslint.config.js
167
+ eslint.config.mjs
168
+ `;
169
+ //#endregion
170
+ //#region src/cli/stages/update-eslint-config.ts
171
+ async function updateEslintConfig(options) {
172
+ const cwd = process.cwd();
173
+ const pathPackageJSON = path.join(cwd, "package.json");
174
+ const pkgContent = await fsp.readFile(pathPackageJSON, "utf8");
175
+ const configFileName = JSON.parse(pkgContent).type === "module" ? "eslint.config.js" : "eslint.config.mjs";
176
+ const pathFlatConfig = path.join(cwd, configFileName);
177
+ await fsp.writeFile(pathFlatConfig, eslintConfigContent(options));
178
+ p.log.success(c.green`Created ${configFileName}`);
179
+ }
180
+ //#endregion
181
+ //#region src/cli/stages/update-npmignore.ts
182
+ async function updateNpmignore() {
183
+ const cwd = process.cwd();
184
+ const npmignoreFileName = ".npmignore";
185
+ const npmignoreFilePath = path.join(cwd, npmignoreFileName);
186
+ let action = "Created";
187
+ try {
188
+ await fsp.access(npmignoreFilePath);
189
+ const trimmedContent = (await fsp.readFile(npmignoreFilePath, "utf8")).trimEnd();
190
+ await fsp.writeFile(npmignoreFilePath, `${trimmedContent}\n${npmignoreString}`);
191
+ action = "Updated";
192
+ } catch {
193
+ await fsp.writeFile(npmignoreFilePath, npmignoreString);
194
+ }
195
+ p.log.success(c.green`${action} ${npmignoreFileName}`);
196
+ }
197
+ //#endregion
198
+ //#region src/cli/stages/update-vscode-settings.ts
199
+ async function updateVscodeSettings() {
200
+ const cwd = process.cwd();
201
+ const dotVscodePath = path.join(cwd, ".vscode");
202
+ const settingsPath = path.join(dotVscodePath, "settings.json");
203
+ if (!fs.existsSync(dotVscodePath)) await fsp.mkdir(dotVscodePath, { recursive: true });
204
+ if (fs.existsSync(settingsPath)) {
205
+ let settingsContent = await fsp.readFile(settingsPath, "utf8");
206
+ settingsContent = settingsContent.trim().replace(/\s*\}$/, "");
207
+ settingsContent += settingsContent.endsWith(",") || settingsContent.endsWith("{") ? "" : ",";
208
+ settingsContent += `${vscodeSettingsString}}\n`;
209
+ await fsp.writeFile(settingsPath, settingsContent, "utf8");
210
+ p.log.success(green`Updated .vscode/settings.json`);
211
+ } else {
212
+ await fsp.writeFile(settingsPath, `{${vscodeSettingsString}}\n`, "utf8");
213
+ p.log.success(green`Created .vscode/settings.json`);
214
+ }
215
+ }
216
+ //#endregion
217
+ //#region src/cli/run.ts
218
+ async function run(options) {
219
+ await updatePackageJson();
220
+ await updateEslintConfig(options);
221
+ await updateNpmignore();
222
+ await updateVscodeSettings();
223
+ p.log.success(c.green`Setup completed`);
224
+ p.outro(`Now you can update the dependencies by run ${c.blue("pnpm i")} and run ${c.blue("eslint --fix")}\n`);
225
+ }
226
+ //#endregion
227
+ //#region src/cli/index.ts
228
+ const cli = cac("@dhzh/eslint-config");
229
+ cli.command("", "Run the initialization or migration").action(async () => {
230
+ console.log("\n");
231
+ p.intro(`${c.green`@dhzh/eslint-config `}${c.dim`v${version}`}`);
232
+ const options = { hasNest: false };
233
+ const hasNest = await p.confirm({
234
+ message: "Is NestJS a part of the current project?",
235
+ initialValue: false
236
+ });
237
+ if (p.isCancel(hasNest)) {
238
+ p.cancel("Operation cancelled");
239
+ throw new Error("Operation cancelled");
240
+ } else options.hasNest = hasNest;
241
+ try {
242
+ await run(options);
243
+ } catch (error) {
244
+ p.log.error(c.inverse.red(" Failed to migrate "));
245
+ p.log.error(c.red`✘ ${String(error)}`);
246
+ throw error;
247
+ }
248
+ });
249
+ cli.help();
250
+ cli.version(version);
251
+ cli.parse();
252
+ //#endregion
253
+ export {};