Skip to content

Commit

Permalink
--wip-- [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed Jan 11, 2024
1 parent 7b8a80e commit 449f0e1
Show file tree
Hide file tree
Showing 49 changed files with 542 additions and 1,501 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
end_of_line = lf
insert_final_newline = true
43 changes: 29 additions & 14 deletions lib/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
* @author 唯然<[email protected]>
*/
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) {
constructor(options = {}) {
this.cwd = options.cwd || process.cwd();
this.answers = options.answers || {};
this.result = {
devDependencies: [],
configPath: "",
devDependencies: ["eslint"],
configFilename: "eslint.config.js",
configContent: ""
};
}
Expand All @@ -33,37 +32,53 @@ export class ConfigGenerator {
}

calc() {
const isModule = isPackageTypeModule();
const isModule = isPackageTypeModule(this.cwd);

this.result.configPath = path.join(this.cwd, isModule ? "eslint.config.js" : "eslint.config.mjs");
this.result.configFilename = isModule ? "eslint.config.js" : "eslint.config.mjs";

let importContent = "";
let exportContent = "";

if (this.answers.purpose === "syntax") {

// ?
} else if (this.answers.purpose === "problem") {
this.result.devDependencies.push("@eslint/js");
importContent += `import pluginJs from "@eslint/js";\n`;
exportContent += " pluginJs.configs.recommended,\n";
} else if (this.answers.purpose === "style") {

// TODO: style
}

if (this.answers.module === "commonjs") {
exportContent = ` {languageOption:{sourceType: "commonjs"}},\n${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,";
importContent += `import pluginTs from "@typescript-eslint/eslint-plugin";\n`;
exportContent += " pluginTs.configs.recommended,\n";
}

if (this.answers.framework === "vue") {
this.result.devDependencies.push("eslint-plugin-vue");
importContent += "import PluginVue from 'eslint-plugin-vue';\n";
exportContent += "PluginVue.configs.recommended,";
importContent += `import pluginVue from "eslint-plugin-vue";\n`;
exportContent += " pluginVue.configs.recommended,\n";
}

if (this.answers.framework === "react") {
this.result.devDependencies.push("eslint-plugin-react");
importContent += "import PluginReact from 'eslint-plugin-react';\n";
exportContent += "PluginReact.configs.recommended,";
importContent += `import pluginReact from "eslint-plugin-react";\n`;
exportContent += " pluginReact.configs.recommended,\n";
}

this.result.configContent = `${importContent}export default [${exportContent}];`;
this.result.configContent = `${importContent}export default [\n${exportContent}];`;
}

async output() {
await writeFile(this.result.configPath, this.result.configContent);
await writeFile(this.result.configFilename, this.result.configContent);

// TODO: install this.result.devDependencies
// TODO: is peerDependencies still needed?
Expand Down
134 changes: 0 additions & 134 deletions lib/init/config-file.js

This file was deleted.

Loading

0 comments on commit 449f0e1

Please sign in to comment.