@coderwyd/eslint-config 1.1.0-beta.3 → 1.1.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/README.md +4 -4
- package/bin/index.js +3 -0
- package/dist/cli.cjs +268 -0
- package/dist/cli.d.cts +2 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +239 -0
- package/dist/index.cjs +413 -556
- package/dist/index.d.cts +116 -131
- package/dist/index.d.ts +116 -131
- package/dist/index.js +414 -537
- package/package.json +75 -41
package/README.md
CHANGED
|
@@ -76,8 +76,8 @@ Add the following settings to your `.vscode/settings.json`:
|
|
|
76
76
|
|
|
77
77
|
// Auto fix
|
|
78
78
|
"editor.codeActionsOnSave": {
|
|
79
|
-
"source.fixAll
|
|
80
|
-
"source.organizeImports":
|
|
79
|
+
"source.fixAll": "explicit",
|
|
80
|
+
"source.organizeImports": "never"
|
|
81
81
|
},
|
|
82
82
|
|
|
83
83
|
// Silent the stylistic rules in you IDE, but still auto fix them
|
|
@@ -187,7 +187,7 @@ import {
|
|
|
187
187
|
typescript,
|
|
188
188
|
unicorn,
|
|
189
189
|
vue,
|
|
190
|
-
|
|
190
|
+
yaml,
|
|
191
191
|
} from '@coderwyd/eslint-config'
|
|
192
192
|
|
|
193
193
|
export default [
|
|
@@ -204,7 +204,7 @@ export default [
|
|
|
204
204
|
...stylistic(),
|
|
205
205
|
...vue(),
|
|
206
206
|
...jsonc(),
|
|
207
|
-
...
|
|
207
|
+
...yaml(),
|
|
208
208
|
...markdown(),
|
|
209
209
|
]
|
|
210
210
|
```
|
package/bin/index.js
ADDED
package/dist/cli.cjs
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (let key of __getOwnPropNames(from))
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
12
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
13
|
+
}
|
|
14
|
+
return to;
|
|
15
|
+
};
|
|
16
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
17
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
18
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
19
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
20
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
24
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
|
+
|
|
26
|
+
// src/cli.ts
|
|
27
|
+
var cli_exports = {};
|
|
28
|
+
module.exports = __toCommonJS(cli_exports);
|
|
29
|
+
|
|
30
|
+
// src/cli/index.ts
|
|
31
|
+
var import_node_process2 = __toESM(require("process"), 1);
|
|
32
|
+
var import_picocolors3 = __toESM(require("picocolors"), 1);
|
|
33
|
+
var import_helpers = require("yargs/helpers");
|
|
34
|
+
var import_yargs = __toESM(require("yargs"), 1);
|
|
35
|
+
|
|
36
|
+
// src/cli/run.ts
|
|
37
|
+
var import_node_fs = __toESM(require("fs"), 1);
|
|
38
|
+
var import_promises = __toESM(require("fs/promises"), 1);
|
|
39
|
+
var import_node_path = __toESM(require("path"), 1);
|
|
40
|
+
var import_node_process = __toESM(require("process"), 1);
|
|
41
|
+
var import_prompts = __toESM(require("prompts"), 1);
|
|
42
|
+
var import_picocolors2 = __toESM(require("picocolors"), 1);
|
|
43
|
+
var import_parse_gitignore = __toESM(require("parse-gitignore"), 1);
|
|
44
|
+
|
|
45
|
+
// src/cli/constants.ts
|
|
46
|
+
var import_picocolors = __toESM(require("picocolors"), 1);
|
|
47
|
+
|
|
48
|
+
// package.json
|
|
49
|
+
var version = "1.1.0";
|
|
50
|
+
var devDependencies = {
|
|
51
|
+
"@antfu/ni": "^0.21.10",
|
|
52
|
+
"@stylistic/eslint-plugin-migrate": "^1.4.0",
|
|
53
|
+
"@types/eslint": "^8.44.7",
|
|
54
|
+
"@types/fs-extra": "^11.0.4",
|
|
55
|
+
"@types/node": "^20.9.4",
|
|
56
|
+
"@types/prompts": "^2.4.9",
|
|
57
|
+
"@types/yargs": "^17.0.32",
|
|
58
|
+
"@unocss/eslint-plugin": "^0.57.7",
|
|
59
|
+
bumpp: "^9.2.0",
|
|
60
|
+
eslint: "^8.54.0",
|
|
61
|
+
"eslint-flat-config-viewer": "^0.1.3",
|
|
62
|
+
"eslint-plugin-react": "^7.33.2",
|
|
63
|
+
"eslint-plugin-react-hooks": "^4.6.0",
|
|
64
|
+
"eslint-plugin-react-refresh": "^0.4.4",
|
|
65
|
+
esno: "^4.0.0",
|
|
66
|
+
execa: "^8.0.1",
|
|
67
|
+
"fast-glob": "^3.3.2",
|
|
68
|
+
"fs-extra": "^11.1.1",
|
|
69
|
+
"lint-staged": "^15.1.0",
|
|
70
|
+
rimraf: "^5.0.5",
|
|
71
|
+
"simple-git-hooks": "^2.9.0",
|
|
72
|
+
tsup: "^8.0.1",
|
|
73
|
+
typescript: "^5.3.2",
|
|
74
|
+
vitest: "^0.34.6"
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
// src/cli/constants.ts
|
|
78
|
+
var ARROW = import_picocolors.default.cyan("\u2192");
|
|
79
|
+
var CHECK = import_picocolors.default.green("\u2714");
|
|
80
|
+
var CROSS = import_picocolors.default.red("\u2718");
|
|
81
|
+
var WARN = import_picocolors.default.yellow("\u2139");
|
|
82
|
+
var eslintVersion = devDependencies.eslint;
|
|
83
|
+
var vscodeSettingsString = `
|
|
84
|
+
// Enable the ESlint flat config support
|
|
85
|
+
"eslint.experimental.useFlatConfig": true,
|
|
86
|
+
|
|
87
|
+
// Disable the default formatter, use eslint instead
|
|
88
|
+
"prettier.enable": false,
|
|
89
|
+
"editor.formatOnSave": false,
|
|
90
|
+
|
|
91
|
+
// Auto fix
|
|
92
|
+
"editor.codeActionsOnSave": {
|
|
93
|
+
"source.fixAll.eslint": "explicit",
|
|
94
|
+
"source.organizeImports": "never"
|
|
95
|
+
},
|
|
96
|
+
|
|
97
|
+
// Silent the stylistic rules in you IDE, but still auto fix them
|
|
98
|
+
"eslint.rules.customizations": [
|
|
99
|
+
{ "rule": "style/*", "severity": "off" },
|
|
100
|
+
{ "rule": "*-indent", "severity": "off" },
|
|
101
|
+
{ "rule": "*-spacing", "severity": "off" },
|
|
102
|
+
{ "rule": "*-spaces", "severity": "off" },
|
|
103
|
+
{ "rule": "*-order", "severity": "off" },
|
|
104
|
+
{ "rule": "*-dangle", "severity": "off" },
|
|
105
|
+
{ "rule": "*-newline", "severity": "off" },
|
|
106
|
+
{ "rule": "*quotes", "severity": "off" },
|
|
107
|
+
{ "rule": "*semi", "severity": "off" }
|
|
108
|
+
],
|
|
109
|
+
|
|
110
|
+
// Enable eslint for all supported languages
|
|
111
|
+
"eslint.validate": [
|
|
112
|
+
"javascript",
|
|
113
|
+
"javascriptreact",
|
|
114
|
+
"typescript",
|
|
115
|
+
"typescriptreact",
|
|
116
|
+
"vue",
|
|
117
|
+
"html",
|
|
118
|
+
"markdown",
|
|
119
|
+
"json",
|
|
120
|
+
"jsonc",
|
|
121
|
+
"yaml"
|
|
122
|
+
]
|
|
123
|
+
`;
|
|
124
|
+
|
|
125
|
+
// src/cli/utils.ts
|
|
126
|
+
var import_node_child_process = require("child_process");
|
|
127
|
+
function isGitClean() {
|
|
128
|
+
try {
|
|
129
|
+
(0, import_node_child_process.execSync)("git diff-index --quiet HEAD --");
|
|
130
|
+
return true;
|
|
131
|
+
} catch (error) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// src/cli/run.ts
|
|
137
|
+
async function run(options = {}) {
|
|
138
|
+
const SKIP_PROMPT = !!import_node_process.default.env.SKIP_PROMPT || options.yes;
|
|
139
|
+
const SKIP_GIT_CHECK = !!import_node_process.default.env.SKIP_GIT_CHECK;
|
|
140
|
+
const cwd = import_node_process.default.cwd();
|
|
141
|
+
const pathFlatConfig = import_node_path.default.join(cwd, "eslint.config.js");
|
|
142
|
+
const pathPackageJSON = import_node_path.default.join(cwd, "package.json");
|
|
143
|
+
const pathESLintIngore = import_node_path.default.join(cwd, ".eslintignore");
|
|
144
|
+
if (import_node_fs.default.existsSync(pathFlatConfig)) {
|
|
145
|
+
console.log(import_picocolors2.default.yellow(`${WARN} eslint.config.js already exists, migration wizard exited.`));
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
if (!SKIP_GIT_CHECK && !isGitClean())
|
|
149
|
+
throw new Error("There are uncommitted changes in the current repository, please commit them and try again");
|
|
150
|
+
console.log(import_picocolors2.default.cyan(`${ARROW} bumping @coderwyd/eslint-config to v${version}`));
|
|
151
|
+
const pkgContent = await import_promises.default.readFile(pathPackageJSON, "utf-8");
|
|
152
|
+
const pkg = JSON.parse(pkgContent);
|
|
153
|
+
pkg.devDependencies ?? (pkg.devDependencies = {});
|
|
154
|
+
pkg.devDependencies["@coderwyd/eslint-config"] = `^${version}`;
|
|
155
|
+
if (!pkg.devDependencies.eslint)
|
|
156
|
+
pkg.devDependencies.eslint = eslintVersion;
|
|
157
|
+
await import_promises.default.writeFile(pathPackageJSON, JSON.stringify(pkg, null, 2));
|
|
158
|
+
console.log(import_picocolors2.default.green(`${CHECK} changes wrote to package.json`));
|
|
159
|
+
const eslintIgnores = [];
|
|
160
|
+
if (import_node_fs.default.existsSync(pathESLintIngore)) {
|
|
161
|
+
console.log(import_picocolors2.default.cyan(`${ARROW} migrating existing .eslintignore`));
|
|
162
|
+
const content = await import_promises.default.readFile(pathESLintIngore, "utf-8");
|
|
163
|
+
const parsed = (0, import_parse_gitignore.default)(content);
|
|
164
|
+
const globs = parsed.globs();
|
|
165
|
+
for (const glob of globs) {
|
|
166
|
+
if (glob.type === "ignore")
|
|
167
|
+
eslintIgnores.push(...glob.patterns);
|
|
168
|
+
else if (glob.type === "unignore")
|
|
169
|
+
eslintIgnores.push(...glob.patterns.map((pattern) => `!${pattern}`));
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
let eslintConfigContent = "";
|
|
173
|
+
const coderwydConfig = `${eslintIgnores.length ? `ignores: ${JSON.stringify(eslintIgnores)}` : ""}`;
|
|
174
|
+
if (pkg.type === "module") {
|
|
175
|
+
eslintConfigContent = `
|
|
176
|
+
import coderwyd from '@coderwyd/eslint-config'
|
|
177
|
+
|
|
178
|
+
export default coderwyd({
|
|
179
|
+
${coderwydConfig}
|
|
180
|
+
})
|
|
181
|
+
`.trimStart();
|
|
182
|
+
} else {
|
|
183
|
+
eslintConfigContent = `
|
|
184
|
+
const coderwyd = require('@coderwyd/eslint-config').default
|
|
185
|
+
|
|
186
|
+
module.exports = coderwyd({
|
|
187
|
+
${coderwydConfig}
|
|
188
|
+
})
|
|
189
|
+
`.trimStart();
|
|
190
|
+
}
|
|
191
|
+
await import_promises.default.writeFile(pathFlatConfig, eslintConfigContent);
|
|
192
|
+
console.log(import_picocolors2.default.green(`${CHECK} created eslint.config.js`));
|
|
193
|
+
const files = import_node_fs.default.readdirSync(cwd);
|
|
194
|
+
const legacyConfig = [];
|
|
195
|
+
files.forEach((file) => {
|
|
196
|
+
if (file.includes("eslint") || file.includes("prettier"))
|
|
197
|
+
legacyConfig.push(file);
|
|
198
|
+
});
|
|
199
|
+
if (legacyConfig.length) {
|
|
200
|
+
console.log(`${WARN} you can now remove those files manually:`);
|
|
201
|
+
console.log(` ${import_picocolors2.default.dim(legacyConfig.join(", "))}`);
|
|
202
|
+
}
|
|
203
|
+
let promptResult = {
|
|
204
|
+
updateVscodeSettings: true
|
|
205
|
+
};
|
|
206
|
+
if (!SKIP_PROMPT) {
|
|
207
|
+
try {
|
|
208
|
+
promptResult = await (0, import_prompts.default)({
|
|
209
|
+
initial: true,
|
|
210
|
+
message: "Update .vscode/settings.json for better VS Code experience?",
|
|
211
|
+
name: "updateVscodeSettings",
|
|
212
|
+
type: "confirm"
|
|
213
|
+
}, {
|
|
214
|
+
onCancel: () => {
|
|
215
|
+
throw new Error(`Cancelled`);
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
} catch (cancelled) {
|
|
219
|
+
console.log(cancelled.message);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
if (promptResult?.updateVscodeSettings ?? true) {
|
|
224
|
+
const dotVscodePath = import_node_path.default.join(cwd, ".vscode");
|
|
225
|
+
const settingsPath = import_node_path.default.join(dotVscodePath, "settings.json");
|
|
226
|
+
if (!import_node_fs.default.existsSync(dotVscodePath))
|
|
227
|
+
await import_promises.default.mkdir(dotVscodePath, { recursive: true });
|
|
228
|
+
if (!import_node_fs.default.existsSync(settingsPath)) {
|
|
229
|
+
await import_promises.default.writeFile(settingsPath, `{${vscodeSettingsString}}
|
|
230
|
+
`, "utf-8");
|
|
231
|
+
console.log(import_picocolors2.default.green(`${CHECK} created .vscode/settings.json`));
|
|
232
|
+
} else {
|
|
233
|
+
let settingsContent = await import_promises.default.readFile(settingsPath, "utf8");
|
|
234
|
+
settingsContent = settingsContent.trim().replace(/\s*}$/, "");
|
|
235
|
+
settingsContent += settingsContent.endsWith(",") || settingsContent.endsWith("{") ? "" : ",";
|
|
236
|
+
settingsContent += `${vscodeSettingsString}}
|
|
237
|
+
`;
|
|
238
|
+
await import_promises.default.writeFile(settingsPath, settingsContent, "utf-8");
|
|
239
|
+
console.log(import_picocolors2.default.green(`${CHECK} updated .vscode/settings.json`));
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
console.log(import_picocolors2.default.green(`${CHECK} migration completed`));
|
|
243
|
+
console.log(`Now you can update the dependencies and run ${import_picocolors2.default.blue("eslint . --fix")}
|
|
244
|
+
`);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// src/cli/index.ts
|
|
248
|
+
function header() {
|
|
249
|
+
console.log(`
|
|
250
|
+
${import_picocolors3.default.green(`@coderwyd/eslint-config `)}${import_picocolors3.default.dim(`v${version}`)}`);
|
|
251
|
+
}
|
|
252
|
+
var instance = (0, import_yargs.default)((0, import_helpers.hideBin)(import_node_process2.default.argv)).scriptName("@coderwyd/eslint-config").usage("").command(
|
|
253
|
+
"*",
|
|
254
|
+
"Run the initialization or migration",
|
|
255
|
+
(args) => args.option("yes", { alias: "y", description: "Skip prompts and use default values", type: "boolean" }).help(),
|
|
256
|
+
async (args) => {
|
|
257
|
+
header();
|
|
258
|
+
console.log();
|
|
259
|
+
try {
|
|
260
|
+
await run(args);
|
|
261
|
+
} catch (error) {
|
|
262
|
+
console.error(import_picocolors3.default.inverse(import_picocolors3.default.red(" Failed to migrate ")));
|
|
263
|
+
console.error(import_picocolors3.default.red(`${CROSS} ${String(error)}`));
|
|
264
|
+
import_node_process2.default.exit(1);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
).showHelpOnFail(false).alias("h", "help").version("version", version).alias("v", "version");
|
|
268
|
+
instance.help().argv;
|
package/dist/cli.d.cts
ADDED
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
// src/cli/index.ts
|
|
2
|
+
import process2 from "process";
|
|
3
|
+
import c3 from "picocolors";
|
|
4
|
+
import { hideBin } from "yargs/helpers";
|
|
5
|
+
import yargs from "yargs";
|
|
6
|
+
|
|
7
|
+
// src/cli/run.ts
|
|
8
|
+
import fs from "fs";
|
|
9
|
+
import fsp from "fs/promises";
|
|
10
|
+
import path from "path";
|
|
11
|
+
import process from "process";
|
|
12
|
+
import prompts from "prompts";
|
|
13
|
+
import c2 from "picocolors";
|
|
14
|
+
import parse from "parse-gitignore";
|
|
15
|
+
|
|
16
|
+
// src/cli/constants.ts
|
|
17
|
+
import c from "picocolors";
|
|
18
|
+
|
|
19
|
+
// package.json
|
|
20
|
+
var version = "1.1.0";
|
|
21
|
+
var devDependencies = {
|
|
22
|
+
"@antfu/ni": "^0.21.10",
|
|
23
|
+
"@stylistic/eslint-plugin-migrate": "^1.4.0",
|
|
24
|
+
"@types/eslint": "^8.44.7",
|
|
25
|
+
"@types/fs-extra": "^11.0.4",
|
|
26
|
+
"@types/node": "^20.9.4",
|
|
27
|
+
"@types/prompts": "^2.4.9",
|
|
28
|
+
"@types/yargs": "^17.0.32",
|
|
29
|
+
"@unocss/eslint-plugin": "^0.57.7",
|
|
30
|
+
bumpp: "^9.2.0",
|
|
31
|
+
eslint: "^8.54.0",
|
|
32
|
+
"eslint-flat-config-viewer": "^0.1.3",
|
|
33
|
+
"eslint-plugin-react": "^7.33.2",
|
|
34
|
+
"eslint-plugin-react-hooks": "^4.6.0",
|
|
35
|
+
"eslint-plugin-react-refresh": "^0.4.4",
|
|
36
|
+
esno: "^4.0.0",
|
|
37
|
+
execa: "^8.0.1",
|
|
38
|
+
"fast-glob": "^3.3.2",
|
|
39
|
+
"fs-extra": "^11.1.1",
|
|
40
|
+
"lint-staged": "^15.1.0",
|
|
41
|
+
rimraf: "^5.0.5",
|
|
42
|
+
"simple-git-hooks": "^2.9.0",
|
|
43
|
+
tsup: "^8.0.1",
|
|
44
|
+
typescript: "^5.3.2",
|
|
45
|
+
vitest: "^0.34.6"
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// src/cli/constants.ts
|
|
49
|
+
var ARROW = c.cyan("\u2192");
|
|
50
|
+
var CHECK = c.green("\u2714");
|
|
51
|
+
var CROSS = c.red("\u2718");
|
|
52
|
+
var WARN = c.yellow("\u2139");
|
|
53
|
+
var eslintVersion = devDependencies.eslint;
|
|
54
|
+
var vscodeSettingsString = `
|
|
55
|
+
// Enable the ESlint flat config support
|
|
56
|
+
"eslint.experimental.useFlatConfig": true,
|
|
57
|
+
|
|
58
|
+
// Disable the default formatter, use eslint instead
|
|
59
|
+
"prettier.enable": false,
|
|
60
|
+
"editor.formatOnSave": false,
|
|
61
|
+
|
|
62
|
+
// Auto fix
|
|
63
|
+
"editor.codeActionsOnSave": {
|
|
64
|
+
"source.fixAll.eslint": "explicit",
|
|
65
|
+
"source.organizeImports": "never"
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
// Silent the stylistic rules in you IDE, but still auto fix them
|
|
69
|
+
"eslint.rules.customizations": [
|
|
70
|
+
{ "rule": "style/*", "severity": "off" },
|
|
71
|
+
{ "rule": "*-indent", "severity": "off" },
|
|
72
|
+
{ "rule": "*-spacing", "severity": "off" },
|
|
73
|
+
{ "rule": "*-spaces", "severity": "off" },
|
|
74
|
+
{ "rule": "*-order", "severity": "off" },
|
|
75
|
+
{ "rule": "*-dangle", "severity": "off" },
|
|
76
|
+
{ "rule": "*-newline", "severity": "off" },
|
|
77
|
+
{ "rule": "*quotes", "severity": "off" },
|
|
78
|
+
{ "rule": "*semi", "severity": "off" }
|
|
79
|
+
],
|
|
80
|
+
|
|
81
|
+
// Enable eslint for all supported languages
|
|
82
|
+
"eslint.validate": [
|
|
83
|
+
"javascript",
|
|
84
|
+
"javascriptreact",
|
|
85
|
+
"typescript",
|
|
86
|
+
"typescriptreact",
|
|
87
|
+
"vue",
|
|
88
|
+
"html",
|
|
89
|
+
"markdown",
|
|
90
|
+
"json",
|
|
91
|
+
"jsonc",
|
|
92
|
+
"yaml"
|
|
93
|
+
]
|
|
94
|
+
`;
|
|
95
|
+
|
|
96
|
+
// src/cli/utils.ts
|
|
97
|
+
import { execSync } from "child_process";
|
|
98
|
+
function isGitClean() {
|
|
99
|
+
try {
|
|
100
|
+
execSync("git diff-index --quiet HEAD --");
|
|
101
|
+
return true;
|
|
102
|
+
} catch (error) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// src/cli/run.ts
|
|
108
|
+
async function run(options = {}) {
|
|
109
|
+
const SKIP_PROMPT = !!process.env.SKIP_PROMPT || options.yes;
|
|
110
|
+
const SKIP_GIT_CHECK = !!process.env.SKIP_GIT_CHECK;
|
|
111
|
+
const cwd = process.cwd();
|
|
112
|
+
const pathFlatConfig = path.join(cwd, "eslint.config.js");
|
|
113
|
+
const pathPackageJSON = path.join(cwd, "package.json");
|
|
114
|
+
const pathESLintIngore = path.join(cwd, ".eslintignore");
|
|
115
|
+
if (fs.existsSync(pathFlatConfig)) {
|
|
116
|
+
console.log(c2.yellow(`${WARN} eslint.config.js already exists, migration wizard exited.`));
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (!SKIP_GIT_CHECK && !isGitClean())
|
|
120
|
+
throw new Error("There are uncommitted changes in the current repository, please commit them and try again");
|
|
121
|
+
console.log(c2.cyan(`${ARROW} bumping @coderwyd/eslint-config to v${version}`));
|
|
122
|
+
const pkgContent = await fsp.readFile(pathPackageJSON, "utf-8");
|
|
123
|
+
const pkg = JSON.parse(pkgContent);
|
|
124
|
+
pkg.devDependencies ?? (pkg.devDependencies = {});
|
|
125
|
+
pkg.devDependencies["@coderwyd/eslint-config"] = `^${version}`;
|
|
126
|
+
if (!pkg.devDependencies.eslint)
|
|
127
|
+
pkg.devDependencies.eslint = eslintVersion;
|
|
128
|
+
await fsp.writeFile(pathPackageJSON, JSON.stringify(pkg, null, 2));
|
|
129
|
+
console.log(c2.green(`${CHECK} changes wrote to package.json`));
|
|
130
|
+
const eslintIgnores = [];
|
|
131
|
+
if (fs.existsSync(pathESLintIngore)) {
|
|
132
|
+
console.log(c2.cyan(`${ARROW} migrating existing .eslintignore`));
|
|
133
|
+
const content = await fsp.readFile(pathESLintIngore, "utf-8");
|
|
134
|
+
const parsed = parse(content);
|
|
135
|
+
const globs = parsed.globs();
|
|
136
|
+
for (const glob of globs) {
|
|
137
|
+
if (glob.type === "ignore")
|
|
138
|
+
eslintIgnores.push(...glob.patterns);
|
|
139
|
+
else if (glob.type === "unignore")
|
|
140
|
+
eslintIgnores.push(...glob.patterns.map((pattern) => `!${pattern}`));
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
let eslintConfigContent = "";
|
|
144
|
+
const coderwydConfig = `${eslintIgnores.length ? `ignores: ${JSON.stringify(eslintIgnores)}` : ""}`;
|
|
145
|
+
if (pkg.type === "module") {
|
|
146
|
+
eslintConfigContent = `
|
|
147
|
+
import coderwyd from '@coderwyd/eslint-config'
|
|
148
|
+
|
|
149
|
+
export default coderwyd({
|
|
150
|
+
${coderwydConfig}
|
|
151
|
+
})
|
|
152
|
+
`.trimStart();
|
|
153
|
+
} else {
|
|
154
|
+
eslintConfigContent = `
|
|
155
|
+
const coderwyd = require('@coderwyd/eslint-config').default
|
|
156
|
+
|
|
157
|
+
module.exports = coderwyd({
|
|
158
|
+
${coderwydConfig}
|
|
159
|
+
})
|
|
160
|
+
`.trimStart();
|
|
161
|
+
}
|
|
162
|
+
await fsp.writeFile(pathFlatConfig, eslintConfigContent);
|
|
163
|
+
console.log(c2.green(`${CHECK} created eslint.config.js`));
|
|
164
|
+
const files = fs.readdirSync(cwd);
|
|
165
|
+
const legacyConfig = [];
|
|
166
|
+
files.forEach((file) => {
|
|
167
|
+
if (file.includes("eslint") || file.includes("prettier"))
|
|
168
|
+
legacyConfig.push(file);
|
|
169
|
+
});
|
|
170
|
+
if (legacyConfig.length) {
|
|
171
|
+
console.log(`${WARN} you can now remove those files manually:`);
|
|
172
|
+
console.log(` ${c2.dim(legacyConfig.join(", "))}`);
|
|
173
|
+
}
|
|
174
|
+
let promptResult = {
|
|
175
|
+
updateVscodeSettings: true
|
|
176
|
+
};
|
|
177
|
+
if (!SKIP_PROMPT) {
|
|
178
|
+
try {
|
|
179
|
+
promptResult = await prompts({
|
|
180
|
+
initial: true,
|
|
181
|
+
message: "Update .vscode/settings.json for better VS Code experience?",
|
|
182
|
+
name: "updateVscodeSettings",
|
|
183
|
+
type: "confirm"
|
|
184
|
+
}, {
|
|
185
|
+
onCancel: () => {
|
|
186
|
+
throw new Error(`Cancelled`);
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
} catch (cancelled) {
|
|
190
|
+
console.log(cancelled.message);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (promptResult?.updateVscodeSettings ?? true) {
|
|
195
|
+
const dotVscodePath = path.join(cwd, ".vscode");
|
|
196
|
+
const settingsPath = path.join(dotVscodePath, "settings.json");
|
|
197
|
+
if (!fs.existsSync(dotVscodePath))
|
|
198
|
+
await fsp.mkdir(dotVscodePath, { recursive: true });
|
|
199
|
+
if (!fs.existsSync(settingsPath)) {
|
|
200
|
+
await fsp.writeFile(settingsPath, `{${vscodeSettingsString}}
|
|
201
|
+
`, "utf-8");
|
|
202
|
+
console.log(c2.green(`${CHECK} created .vscode/settings.json`));
|
|
203
|
+
} else {
|
|
204
|
+
let settingsContent = await fsp.readFile(settingsPath, "utf8");
|
|
205
|
+
settingsContent = settingsContent.trim().replace(/\s*}$/, "");
|
|
206
|
+
settingsContent += settingsContent.endsWith(",") || settingsContent.endsWith("{") ? "" : ",";
|
|
207
|
+
settingsContent += `${vscodeSettingsString}}
|
|
208
|
+
`;
|
|
209
|
+
await fsp.writeFile(settingsPath, settingsContent, "utf-8");
|
|
210
|
+
console.log(c2.green(`${CHECK} updated .vscode/settings.json`));
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
console.log(c2.green(`${CHECK} migration completed`));
|
|
214
|
+
console.log(`Now you can update the dependencies and run ${c2.blue("eslint . --fix")}
|
|
215
|
+
`);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// src/cli/index.ts
|
|
219
|
+
function header() {
|
|
220
|
+
console.log(`
|
|
221
|
+
${c3.green(`@coderwyd/eslint-config `)}${c3.dim(`v${version}`)}`);
|
|
222
|
+
}
|
|
223
|
+
var instance = yargs(hideBin(process2.argv)).scriptName("@coderwyd/eslint-config").usage("").command(
|
|
224
|
+
"*",
|
|
225
|
+
"Run the initialization or migration",
|
|
226
|
+
(args) => args.option("yes", { alias: "y", description: "Skip prompts and use default values", type: "boolean" }).help(),
|
|
227
|
+
async (args) => {
|
|
228
|
+
header();
|
|
229
|
+
console.log();
|
|
230
|
+
try {
|
|
231
|
+
await run(args);
|
|
232
|
+
} catch (error) {
|
|
233
|
+
console.error(c3.inverse(c3.red(" Failed to migrate ")));
|
|
234
|
+
console.error(c3.red(`${CROSS} ${String(error)}`));
|
|
235
|
+
process2.exit(1);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
).showHelpOnFail(false).alias("h", "help").version("version", version).alias("v", "version");
|
|
239
|
+
instance.help().argv;
|