Skip to content

Commit

Permalink
feat!: support flat config
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed Jan 10, 2024
1 parent 903f929 commit 7b8a80e
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 7 deletions.
14 changes: 9 additions & 5 deletions bin/create-config.js
@@ -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();
71 changes: 71 additions & 0 deletions lib/config-generator.js
@@ -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";

Check failure on line 7 in lib/config-generator.js

View workflow job for this annotation

GitHub Actions / Lint

The 'fs/promises' is not supported until Node.js 14.0.0. The configured version range is '^12.22.0 || ^14.17.0 || >=16.0.0'
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?
}
}
25 changes: 25 additions & 0 deletions lib/utils/logging.js
@@ -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);
}
24 changes: 22 additions & 2 deletions lib/init/npm-utils.js → lib/utils/npm-utils.js
Expand Up @@ -12,7 +12,7 @@ import fs from "fs";
import spawn from "cross-spawn";

import path from "path";
import * as log from "../shared/logging.js";
import * as log from "./logging.js";

//------------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -155,6 +155,25 @@ function checkPackageJson(startDir) {
return !!findPackageJson(startDir);
}

/**
* check if the package.type === "module"
* @returns {boolean} return true if the package.type === "module"
*/
function isPackageTypeModule() {
const pkgJSONPath = findPackageJson();

if (pkgJSONPath) {
const pkgJSONContents = JSON.parse(fs.readFileSync(pkgJSONPath, "utf8"));

if (pkgJSONContents.type === "module") {
return true;
}
}

return false;
}


//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------
Expand All @@ -165,5 +184,6 @@ export {
findPackageJson,
checkDeps,
checkDevDeps,
checkPackageJson
checkPackageJson,
isPackageTypeModule
};

0 comments on commit 7b8a80e

Please sign in to comment.