Skip to content

Commit

Permalink
new eslint config installer for flat config
Browse files Browse the repository at this point in the history
  • Loading branch information
ahkhanjani committed Oct 27, 2024
1 parent 154cc06 commit d0a5674
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 105 deletions.
58 changes: 44 additions & 14 deletions cli/src/installers/eslint.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,62 @@
import path from "path";
import fs from "fs-extra";
import { format } from "prettier";

import { _initialConfig } from "~/../template/extras/config/_eslint.js";
import { type Installer } from "~/installers/index.js";

export const dynamicEslintInstaller: Installer = ({ projectDir, packages }) => {
const usingDrizzle = !!packages?.drizzle?.inUse;

const eslintConfig = getEslintConfig({ usingDrizzle });
const imports = getImports(usingDrizzle);

// Convert config from _eslint.config.json to .eslintrc.cjs
const eslintrcFileContents = [
'/** @type {import("eslint").Linter.Config} */',
`const config = ${JSON.stringify(eslintConfig, null, 2)}`,
"module.exports = config;",
const eslintConfig = getEslintConfig(usingDrizzle);
const stringifiedConfig = JSON.stringify(eslintConfig, null, 2).replace(
/"%%|%%"/g,
""
);

// Convert config from _eslint.js to eslint.config.js
const eslintConfigFileContents = [
...imports,
"",
"export default tseslint.config(",
stringifiedConfig,
");",
].join("\n");

const eslintConfigDest = path.join(projectDir, ".eslintrc.cjs");
fs.writeFileSync(eslintConfigDest, eslintrcFileContents, "utf-8");
format(eslintConfigFileContents, { parser: "typescript" })
.then((content) => {
const eslintConfigDest = path.join(projectDir, "eslint.config.js");
fs.writeFileSync(eslintConfigDest, content, "utf-8");
})
.catch((error) => {
console.error("Invalid 'eslint.config.js'.", error);
});
};

const getEslintConfig = ({ usingDrizzle }: { usingDrizzle: boolean }) => {
function getImports(usingDrizzle: boolean) {
const imports = [
'import nextPlugin from "@next/eslint-plugin-next"',
'import tseslint from "typescript-eslint"',
];

if (usingDrizzle) {
imports.unshift('import drizzlePlugin from "eslint-plugin-drizzle"');
}

return imports;
}

function getEslintConfig(usingDrizzle: boolean) {
const eslintConfig = _initialConfig;

if (usingDrizzle) {
eslintConfig.plugins = [...(eslintConfig.plugins ?? []), "drizzle"];
Object.assign(eslintConfig.plugins, {
drizzle: "%%drizzlePlugin%%",
});

eslintConfig.rules = {
...eslintConfig.rules,
Object.assign(eslintConfig.rules, {
"drizzle/enforce-delete-with-where": [
"error",
{ drizzleObjectName: ["db", "ctx.db"] },
Expand All @@ -36,7 +65,8 @@ const getEslintConfig = ({ usingDrizzle }: { usingDrizzle: boolean }) => {
"error",
{ drizzleObjectName: ["db", "ctx.db"] },
],
};
});
}

return eslintConfig;
};
}
2 changes: 1 addition & 1 deletion cli/template/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
"zod": "^3.23.3"
},
"devDependencies": {
"@next/eslint-plugin-next": "^15.0.1",
"@types/node": "^20.17.1",
"@types/react": "npm:types-react@rc",
"@types/react-dom": "npm:types-react-dom@rc",
"eslint": "^9.13.0",
"eslint-config-next": "^15.0.1",
"typescript": "^5.6.3",
"typescript-eslint": "^8.11.0"
},
Expand Down
18 changes: 11 additions & 7 deletions cli/template/extras/config/_eslint.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
/** @type {import("eslint").Linter.Config} */
export const _initialConfig = {
parser: "@typescript-eslint/parser",
parserOptions: { project: true },
plugins: ["@typescript-eslint"],
plugins: {
"@next/next": "%%nextPlugin%%",
"@typescript-eslint": "%%tseslint.plugin%%",
},
extends: [
"next/core-web-vitals",
"plugin:@typescript-eslint/recommended-type-checked",
"plugin:@typescript-eslint/stylistic-type-checked",
"%%...nextPlugin.configs['core-web-vitals'].rules%%",
"%%...tseslint.configs.recommendedTypeChecked%%",
"%%...tseslint.configs.stylisticTypeChecked%%",
],
languageOptions: {
parser: "%%tseslint.parser%%",
parserOptions: { project: true },
},
rules: {
"@typescript-eslint/array-type": "off",
"@typescript-eslint/consistent-type-definitions": "off",
Expand Down
123 changes: 40 additions & 83 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d0a5674

Please sign in to comment.