-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
903f929
commit 7b8a80e
Showing
4 changed files
with
127 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* @fileoverview Main CLI that is run via the eslint command. | ||
* @author Nicholas C. Zakas | ||
* @fileoverview Main CLI that is run via the `npm init @eslint/config` command. | ||
* @author 唯然<weiran.zsd@outlook.com> | ||
*/ | ||
|
||
/* eslint no-console:off -- CLI */ | ||
import { initializeConfig } from "../lib/init/config-initializer.js"; | ||
initializeConfig(); | ||
import { ConfigGenerator } from "../lib/config-generator.js"; | ||
|
||
const generator = new ConfigGenerator(); | ||
|
||
generator.prompt(); | ||
generator.calc(); | ||
generator.output(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* @fileoverview to generate config files. | ||
* @author 唯然<weiran.zsd@outlook.com> | ||
*/ | ||
import process from "process"; | ||
import path from "path"; | ||
import { writeFile } from "fs/promises"; | ||
import { isPackageTypeModule } from "./utils/npm-utils.js"; | ||
|
||
/** | ||
* | ||
*/ | ||
export class ConfigGenerator { | ||
constructor(options) { | ||
this.cwd = options.cwd || process.cwd(); | ||
this.answers = options.answers || {}; | ||
this.result = { | ||
devDependencies: [], | ||
configPath: "", | ||
configContent: "" | ||
}; | ||
} | ||
|
||
prompt() { | ||
|
||
// TODO: ask users to input | ||
this.answers = { | ||
purpose: "syntax", | ||
module: "esm", | ||
framework: "vue", | ||
typescript: true | ||
}; | ||
} | ||
|
||
calc() { | ||
const isModule = isPackageTypeModule(); | ||
|
||
this.result.configPath = path.join(this.cwd, isModule ? "eslint.config.js" : "eslint.config.mjs"); | ||
|
||
let importContent = ""; | ||
let exportContent = ""; | ||
|
||
// TODO: we may need to use "@eslint/eslintrc" as these plugins have not support flat configs yet? | ||
if (this.answers.typescript) { | ||
this.result.devDependencies.push("@typescript-eslint/eslint-plugin"); | ||
importContent += "import PluginTs from '@typescript-eslint/eslint-plugin';\n"; | ||
exportContent += "PluginTs.configs.recommended,"; | ||
} | ||
|
||
if (this.answers.framework === "vue") { | ||
this.result.devDependencies.push("eslint-plugin-vue"); | ||
importContent += "import PluginVue from 'eslint-plugin-vue';\n"; | ||
exportContent += "PluginVue.configs.recommended,"; | ||
} | ||
|
||
if (this.answers.framework === "react") { | ||
this.result.devDependencies.push("eslint-plugin-react"); | ||
importContent += "import PluginReact from 'eslint-plugin-react';\n"; | ||
exportContent += "PluginReact.configs.recommended,"; | ||
} | ||
|
||
this.result.configContent = `${importContent}export default [${exportContent}];`; | ||
} | ||
|
||
async output() { | ||
await writeFile(this.result.configPath, this.result.configContent); | ||
|
||
// TODO: install this.result.devDependencies | ||
// TODO: is peerDependencies still needed? | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* @fileoverview Handle logging for ESLint | ||
* @author Gyandeep Singh | ||
*/ | ||
|
||
|
||
/* eslint no-console: "off" -- Logging util */ | ||
|
||
/** | ||
* Cover for console.log | ||
* @param {...any} args The elements to log. | ||
* @returns {void} | ||
*/ | ||
export function info(...args) { | ||
console.log(...args); | ||
} | ||
|
||
/** | ||
* Cover for console.error | ||
* @param {...any} args The elements to log. | ||
* @returns {void} | ||
*/ | ||
export function error(...args) { | ||
console.error(...args); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters