Skip to content

Commit

Permalink
feat(lint): uniformize ESLint commands
Browse files Browse the repository at this point in the history
We override the CLI arguments passed at runtime to simplify our setup in a way
that all the extra config (cache, max warnings) also come from a single point,
without needing to update the run command of a lot of packages
  • Loading branch information
ferferga committed Nov 4, 2024
1 parent 05a26fa commit 356aacc
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 25 deletions.
9 changes: 8 additions & 1 deletion frontend/eslint.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,12 @@ export default [
...getTSVueConfig(true, import.meta.dirname),
...unocss,
...getNodeFiles(),
...getWorkerFiles()
...getWorkerFiles(),
{
name: '(@jellyfin-vue/frontend) Ignored files',
ignores: [
'types/global/routes.d.ts',
'types/global/components.d.ts'
]
}
] satisfies Linter.Config[];
4 changes: 2 additions & 2 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"scripts": {
"analyze:bundle": "vite build --mode analyze:bundle",
"analyze:cycles": "vite build --mode analyze:cycles",
"lint": "eslint . --max-warnings=0 --flag unstable_ts_config",
"lint:fix": "eslint . --fix --max-warnings=0 --flag unstable_ts_config",
"lint": "eslint . --flag unstable_ts_config",
"lint:fix": "eslint . --fix --flag unstable_ts_config",
"lint:inspect": "eslint-config-inspector",
"build": "vite build",
"check": "npm run lint && npm run check:types",
Expand Down
6 changes: 3 additions & 3 deletions packages/configs/eslint.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Linter } from 'eslint';
import { getBaseConfig } from './eslint/rules/base';
import { getTSVueConfig } from './eslint/rules/typescript-vue';
import { getBaseConfig, getTSVueConfig, getNodeFiles, tsFiles } from './eslint/';

export default [
...getBaseConfig('@jellyfin-vue/configs'),
...getTSVueConfig(false, import.meta.dirname)
...getTSVueConfig(false, import.meta.dirname),
...getNodeFiles(tsFiles)
] satisfies Linter.Config[];
62 changes: 48 additions & 14 deletions packages/configs/eslint/rules/base.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { join, basename } from 'node:path';
import { spawnSync } from 'node:child_process';
import type { Linter } from 'eslint';
/*
* import { join } from 'node:path';
* import { findUpSync } from 'find-up-simple';
*/
import { findUpSync } from 'find-up-simple';
import unicorn from 'eslint-plugin-unicorn';
// @ts-expect-error - No types available
import js from '@eslint/js';
Expand All @@ -13,22 +12,57 @@ import gitignore from 'eslint-config-flat-gitignore';
import fileProgress from 'eslint-plugin-file-progress';
import { eqeqeqConfig } from '../shared';

const CI_environment = process.env.CI ? 0 : 1;
const CI_environment = Boolean(process.env.CI);

/**
* Gets ESLint's minimal configuration for the monorepo
*
* @param packageName - The name of the current package
* @param forceCache - Whether to enable ESLint caching for this run (default `true`)
* @param warningAsErrors - All warnings are treated as errors (default `true`)
*/
export function getBaseConfig(packageName: string): Linter.Config[] {
export function getBaseConfig(packageName: string, forceCache = !CI_environment, warningAsErrors = true): Linter.Config[] {
const cliOverrides = forceCache || warningAsErrors;

/**
* Workaround for implementing https://github.com/eslint/eslint/issues/19015
* Stops the current process if the necessary flags are provided, and spawn a new one with the appropiate flags
* inheriting it.
*
* This is necessary to have predictable ESLint runs across the board, without having to worry about specifying the
* correct flags for each monorepo package.
* We check for eslint directly to avoid messing up with other packages reading this file, like @eslint/config-inspector.
*/
if (cliOverrides && basename(process.argv[1]) === 'eslint') {
const newArgs = process.argv.slice(1);

if (forceCache && !(newArgs.includes('--cache') && newArgs.includes('--cache-location'))) {
const cacheLocation = join(findUpSync('node_modules', { type: 'directory' }) ?? '', '.cache/eslint', packageName.replace('/', '_'));

newArgs.push('--cache', '--cache-location', cacheLocation);
console.log('[@jellyfin-vue/configs/eslint] Force enabling caching for this run');
}

if (warningAsErrors && !newArgs.some(arg => arg.includes('--max-warnings'))) {
newArgs.push('--max-warnings=0');
console.log('[@jellyfin-vue/configs/eslint] Force enabling warnings for this run');
}

const argsHaveChanged = new Set(newArgs).difference(new Set(process.argv.slice(1))).size > 0;

if (argsHaveChanged) {
console.log();

const result = spawnSync(process.argv[0], newArgs, {
stdio: 'inherit',
maxBuffer: Number.MAX_SAFE_INTEGER
});

process.exit(result.status ?? 0);
}
}

return [
/*
* TODO: Wait for https://github.com/eslint/eslint/issues/19015
* {
* cache: true,
* cacheLocation: join(findUpSync('node_modules', { type: 'directory' }) ?? '', '.cache/eslint', packageName.replace('/', '_')),
* },
*/
{ ...js.configs.recommended,
name: '(@jellyfin-vue/configs/eslint/base - eslint) Extended config from plugin'
},
Expand Down Expand Up @@ -140,7 +174,7 @@ export function getBaseConfig(packageName: string): Linter.Config[] {
'file-progress': fileProgress
},
rules: {
'file-progress/activate': CI_environment
'file-progress/activate': CI_environment ? 0 : 1
}
}
];
Expand Down
3 changes: 2 additions & 1 deletion packages/configs/eslint/rules/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export function getNodeFiles(files = ['*.config.*', 'scripts/**/*.ts']): Linter.
devDependencies: true
}
],
'import-x/no-nodejs-modules': 'off'
'import-x/no-nodejs-modules': 'off',
'unicorn/no-process-exit': 'off'
}
}
];
Expand Down
4 changes: 2 additions & 2 deletions packages/configs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"./unocss": "./unocss.ts"
},
"scripts": {
"lint": "eslint . --max-warnings=0 --flag unstable_ts_config",
"lint:fix": "eslint . --fix --max-warnings=0 --flag unstable_ts_config",
"lint": "eslint . --flag unstable_ts_config",
"lint:fix": "eslint . --fix --flag unstable_ts_config",
"lint:inspect": "eslint-config-inspector",
"check:types": "vue-tsc"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/vite-plugins/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"exports": "./index.ts",
"type": "module",
"scripts": {
"lint": "eslint . --max-warnings=0 --flag unstable_ts_config",
"lint:fix": "eslint . --fix --max-warnings=0 --flag unstable_ts_config",
"lint": "eslint . --flag unstable_ts_config",
"lint:fix": "eslint . --fix --flag unstable_ts_config",
"lint:inspect": "eslint-config-inspector",
"check:types": "vue-tsc"
},
Expand Down

0 comments on commit 356aacc

Please sign in to comment.