Upgrade to ESLint 9
This commit is contained in:
94
src/environment.ts
Normal file
94
src/environment.ts
Normal file
@ -0,0 +1,94 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
|
||||
import type { Middleware } from './middleware';
|
||||
import { reactQuery, storybook, vitest } from './presets/misc';
|
||||
import { react, reactRefresh } from './presets/react';
|
||||
|
||||
const jsdoc = () => import('./presets/jsdoc');
|
||||
const tailwind = () => import('./presets/tailwind');
|
||||
const testingLibrary = () => import('./presets/testing-library');
|
||||
|
||||
const middlewares = {
|
||||
react,
|
||||
reactRefresh,
|
||||
tailwind,
|
||||
storybook,
|
||||
reactQuery,
|
||||
testingLibrary,
|
||||
jsdoc,
|
||||
vitest,
|
||||
} satisfies {
|
||||
[key: string]: Middleware;
|
||||
};
|
||||
|
||||
export const envs: {
|
||||
dependency: string;
|
||||
eslintPlugin?: string;
|
||||
middleware: keyof typeof middlewares;
|
||||
}[] = [
|
||||
{
|
||||
dependency: 'react',
|
||||
middleware: 'react',
|
||||
},
|
||||
{
|
||||
dependency: '@vitejs/plugin-react',
|
||||
middleware: 'reactRefresh',
|
||||
},
|
||||
{
|
||||
dependency: 'tailwindcss',
|
||||
eslintPlugin: 'eslint-plugin-tailwindcss',
|
||||
middleware: 'tailwind',
|
||||
},
|
||||
{
|
||||
dependency: 'storybook',
|
||||
eslintPlugin: 'eslint-plugin-storybook',
|
||||
middleware: 'storybook',
|
||||
},
|
||||
{
|
||||
dependency: '@tanstack/react-query',
|
||||
eslintPlugin: '@tanstack/eslint-plugin-query',
|
||||
middleware: 'reactQuery',
|
||||
},
|
||||
{
|
||||
dependency: '@testing-library/react',
|
||||
eslintPlugin: 'eslint-plugin-testing-library',
|
||||
middleware: 'testingLibrary',
|
||||
},
|
||||
{
|
||||
dependency: 'vitest',
|
||||
eslintPlugin: 'eslint-plugin-vitest',
|
||||
middleware: 'vitest',
|
||||
},
|
||||
];
|
||||
|
||||
export function getProjectDependencies() {
|
||||
const rootDir = process.cwd();
|
||||
|
||||
const pkgJsonPath = path.resolve(rootDir, 'package.json');
|
||||
const pkgJson = fs.existsSync(pkgJsonPath)
|
||||
? (JSON.parse(fs.readFileSync(pkgJsonPath, 'utf8')) as {
|
||||
dependencies?: Record<string, string>;
|
||||
devDependencies?: Record<string, string>;
|
||||
peerDependencies?: Record<string, string>;
|
||||
})
|
||||
: {};
|
||||
|
||||
return new Set(
|
||||
Object.keys({
|
||||
...pkgJson.dependencies,
|
||||
...pkgJson.devDependencies,
|
||||
...pkgJson.peerDependencies,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export function* checkEnv(): Generator<Middleware> {
|
||||
const deps = getProjectDependencies();
|
||||
|
||||
for (const { dependency, eslintPlugin, middleware } of envs) {
|
||||
if (deps.has(dependency) && (!eslintPlugin || deps.has(eslintPlugin))) {
|
||||
yield middlewares[middleware];
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user