Skip to content

Commit f4ab4e7

Browse files
committed
--wip-- [skip ci]
1 parent 8e3dfb9 commit f4ab4e7

File tree

4 files changed

+108
-30
lines changed

4 files changed

+108
-30
lines changed

bin/create-config.js

100644100755
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ import { ConfigGenerator } from "../lib/config-generator.js";
99

1010
const generator = new ConfigGenerator();
1111

12-
generator.prompt();
12+
await generator.prompt();
1313
generator.calc();
14-
generator.output();
14+
await generator.output();

eslint.config.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import eslintConfigESLint from "eslint-config-eslint";
2-
import globals from "globals";
32

43
export default [
54
{
@@ -8,13 +7,5 @@ export default [
87
"tests/fixtures/"
98
]
109
},
11-
...eslintConfigESLint,
12-
{
13-
files: ["tests/**"],
14-
languageOptions: {
15-
globals: {
16-
...globals.mocha
17-
}
18-
}
19-
}
10+
...eslintConfigESLint
2011
];

lib/config-generator.js

Lines changed: 104 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import path from "path";
77
import { spawnSync } from "child_process";
88
import { writeFile } from "fs/promises";
99
import enquirer from "enquirer";
10-
import { isPackageTypeModule, installSyncSaveDev } from "./utils/npm-utils.js";
10+
import { isPackageTypeModule, installSyncSaveDev, checkPackageJson } from "./utils/npm-utils.js";
1111
import * as log from "./utils/logging.js";
1212

1313
/**
@@ -36,16 +36,94 @@ export class ConfigGenerator {
3636
* Prompt the user for input.
3737
* @returns {void}
3838
*/
39-
prompt() {
40-
41-
// TODO: ask users to input
42-
this.answers = {
43-
purpose: "syntax",
44-
module: "esm",
45-
framework: "vue",
46-
lang: "js",
47-
env: ["browser", "node"]
48-
};
39+
async prompt() {
40+
const packageJsonExists = checkPackageJson(this.cwd);
41+
42+
if (!packageJsonExists) {
43+
throw new Error("A package.json file is necessary to initialize ESLint. Run `npm init` to create a package.json file and try again.");
44+
}
45+
46+
const questions = [
47+
{
48+
type: "select",
49+
name: "purpose",
50+
message: "How would you like to use ESLint?",
51+
initial: 1,
52+
choices: [
53+
{ message: "To check syntax only", name: "syntax" },
54+
{ message: "To check syntax and find problems", name: "problems" },
55+
{ message: "To check syntax, find problems, and enforce code style", name: "style" }
56+
]
57+
},
58+
{
59+
type: "select",
60+
name: "moduleType",
61+
message: "What type of modules does your project use?",
62+
initial: 0,
63+
choices: [
64+
{ message: "JavaScript modules (import/export)", name: "esm" },
65+
{ message: "CommonJS (require/exports)", name: "commonjs" },
66+
{ message: "None of these", name: "none" }
67+
]
68+
},
69+
{
70+
type: "select",
71+
name: "framework",
72+
message: "Which framework does your project use?",
73+
initial: 0,
74+
choices: [
75+
{ message: "React", name: "react" },
76+
{ message: "Vue.js", name: "vue" },
77+
{ message: "None of these", name: "none" }
78+
]
79+
},
80+
{
81+
type: "select",
82+
name: "language",
83+
message: "Does your project use TypeScript?",
84+
choices: [
85+
{ message: "No", name: "javascript" },
86+
{ message: "Yes", name: "typescript" }
87+
],
88+
initial: 0
89+
},
90+
{
91+
type: "multiselect",
92+
name: "env",
93+
message: "Where does your code run?",
94+
hint: "(Press <space> to select, <a> to toggle all, <i> to invert selection)",
95+
initial: 0,
96+
choices: [
97+
{ message: "Browser", name: "browser" },
98+
{ message: "Node", name: "node" }
99+
]
100+
}
101+
];
102+
103+
const answers = await enquirer.prompt(questions);
104+
105+
Object.assign(this.answers, answers);
106+
107+
if (this.answers.purpose === "style") {
108+
109+
const jsStyleGuides = [
110+
{ message: "Airbnb: https://github.com/airbnb/javascript", name: "eslint-config-airbnb" },
111+
{ message: "Standard: https://github.com/standard/standard", name: "staeslint-config-standard" },
112+
{ message: "XO: https://github.com/xojs/eslint-config-xo", name: "eslint-config-xo" }
113+
];
114+
const tsStyleGuides = [
115+
{ message: "Standard: https://github.com/standard/eslint-config-standard-with-typescript", name: "eslint-config-standard-with-typescript" },
116+
{ message: "XO: https://github.com/xojs/eslint-config-xo-typescript", name: "eslint-config-xo-typescript" }
117+
];
118+
const styleguideAnswer = await enquirer.prompt({
119+
type: "select",
120+
name: "styleguide",
121+
message: "Which style guide do you want to follow?",
122+
choices: this.answers.language === "javascript" ? jsStyleGuides : tsStyleGuides
123+
});
124+
125+
Object.assign(this.answers, styleguideAnswer);
126+
}
49127
}
50128

51129
// eslint-disable-next-line jsdoc/require-throws -- ts is not supported yet
@@ -112,7 +190,21 @@ export class ConfigGenerator {
112190
exportContent += " pluginReactConfig,\n";
113191
}
114192

115-
this.result.configContent = `${importContent}export default [\n { ${languageOptionsContent}},\n${exportContent}];`;
193+
if (this.answers.styleguide) {
194+
this.result.devDependencies.push(this.answers.styleguide, "@eslint/js");
195+
importContent += "import pluginJs from \"@eslint/js\";\n";
196+
197+
// importContent += `import styleguide from "${this.answers.styleguide}";\n`;
198+
importContent += "import path from \"path\";\nimport { fileURLToPath } from \"url\";\n";
199+
importContent += "// mimic CommonJS variables -- not needed if using CommonJS\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = path.dirname(__filename);\n\n";
200+
importContent += "const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: pluginJs.configs.recommended});\n";
201+
202+
const extendedName = this.answers.styleguide.replace("eslint-config-", "");
203+
204+
exportContent += ` compat.extends("${extendedName}"),\n`;
205+
}
206+
207+
this.result.configContent = `${importContent}export default [].concat(\n { ${languageOptionsContent}},\n${exportContent});`;
116208
}
117209

118210
/**

package.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@
3939
"test:snapshots": "vitest run snapshots",
4040
"test:snapshots:update": "vitest -u run snapshots"
4141
},
42-
"mocha": {
43-
"loader": "esmock",
44-
"ui": "bdd",
45-
"timeout": 10000
46-
},
4742
"dependencies": {
4843
"@eslint/eslintrc": "^1.0.3",
4944
"cross-spawn": "^7.0.2",
@@ -58,7 +53,7 @@
5853
"c8": "^7.10.0",
5954
"chai": "^4.3.4",
6055
"cross-env": "^7.0.3",
61-
"eslint": "^8.48.0",
56+
"eslint": "^8.56.0",
6257
"eslint-config-eslint": "^9.0.0",
6358
"eslint-release": "^3.2.0",
6459
"esmock": "^2.5.8",

0 commit comments

Comments
 (0)