Skip to content

Commit 4de3d33

Browse files
committed
refactor: fix lint formatter usage
1 parent 65c365e commit 4de3d33

File tree

10 files changed

+277
-42
lines changed

10 files changed

+277
-42
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
- name: Install dependencies
5050
run: npm ci
5151
- name: Lint affected projects
52-
run: npx nx affected:lint --parallel=3
52+
run: ESLINT_FORMATTER_PROJECTS_DIR=packages npx nx affected:lint --parallel=3
5353

5454
unit-test:
5555
strategy:

code-pushup.preset.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,13 @@ export const eslintCoreConfigNx = async (
177177
eslintrc: `packages/${projectName}/eslint.config.js`,
178178
patterns: ['.'],
179179
})
180-
: await eslintPlugin(await eslintConfigFromAllNxProjects()),
180+
: await eslintPlugin(await eslintConfigFromAllNxProjects(), {
181+
artifacts: {
182+
generateArtifactsCommand:
183+
'ESLINT_FORMATTER_PROJECTS_DIR=packages NX_TUI=false npx nx run-many -t lint --include="packages/**"',
184+
artifactsPaths: ['packages/**/.eslint/eslint-report.json'],
185+
},
186+
}),
181187
],
182188
categories: eslintCategories,
183189
});

nx.json

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,15 @@
9797
"cache": true
9898
},
9999
"lint": {
100+
"dependsOn": ["eslint-formatter-multiple-formats:build"],
100101
"inputs": ["lint-eslint-inputs"],
101102
"executor": "@nx/eslint:lint",
102103
"outputs": ["{options.outputFile}"],
103104
"cache": true,
104105
"options": {
105106
"errorOnUnmatchedPattern": false,
106107
"maxWarnings": 0,
108+
"format": "./tools/eslint-formatter-multiple-formats/dist/index.js",
107109
"lintFilePatterns": [
108110
"{projectRoot}/**/*.ts",
109111
"{projectRoot}/package.json"
@@ -127,16 +129,12 @@
127129
}
128130
},
129131
"lint-formatter": {
130-
"inputs": ["default", "{workspaceRoot}/eslint.config.?(c)js"],
131-
"outputs": ["{projectRoot}/.eslint/eslint-report*.json"],
132-
"cache": true,
132+
"dependsOn": ["eslint-formatter-multiple-formats:build"],
133+
"inputs": ["lint-eslint-inputs"],
133134
"executor": "nx:run-commands",
134135
"options": {
135-
"command": "nx lint {projectName}",
136-
"args": ["--format=./dist/eslint-multi-format/index.js"],
137-
"env": {
138-
"ESLINT_FORMATTER_CONFIG": "{\"outputDir\":\"{projectRoot}/.eslint\"}"
139-
}
136+
"command": "./tools/scripts/lint-formatter.sh {projectName} {projectRoot}",
137+
"cwd": "{workspaceRoot}"
140138
}
141139
},
142140
"nxv-pkg-install": {

packages/utils/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"build": {},
88
"lint": {},
99
"lint-report": {},
10+
"lint-formatter": {},
1011
"perf": {
1112
"command": "npx tsx --tsconfig=../tsconfig.perf.json",
1213
"options": {

tools/eslint-formatter-multiple-formats/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
"access": "public"
3333
},
3434
"dependencies": {
35-
"eslint-formatter-stylish": "^8.40.0",
36-
"@code-pushup/utils": "0.77.0"
35+
"ansis": "^3.3.0"
3736
},
3837
"peerDependencies": {
3938
"eslint": "^8.0.0 || ^9.0.0"

tools/eslint-formatter-multiple-formats/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"executor": "@nx/vite:build",
1313
"outputs": ["{options.outputPath}"],
1414
"options": {
15-
"outputPath": "dist/eslint-multi-format"
15+
"outputPath": "{projectRoot}/dist"
1616
}
1717
}
1818
}

tools/eslint-formatter-multiple-formats/src/lib/multiple-formats.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
import type { ESLint } from 'eslint';
22
import path from 'node:path';
3-
import type { EslintFormat, FormatterConfig } from './types.js';
3+
import * as process from 'node:process';
4+
import type { FormatterConfig } from './types.js';
5+
import type { EslintFormat } from './utils.js';
46
import {
57
formatTerminalOutput,
68
findConfigFromEnv as getConfigFromEnv,
79
persistEslintReports,
810
} from './utils.js';
911

10-
export const DEFAULT_OUTPUT_DIR = path.join(process.cwd(), '.eslint');
12+
export const DEFAULT_OUTPUT_DIR = '.eslint';
1113
export const DEFAULT_FILENAME = 'eslint-report';
1214
export const DEFAULT_FORMATS = ['json'] as EslintFormat[];
1315
export const DEFAULT_TERMINAL = 'stylish' as EslintFormat;
1416

15-
export const DEFAULT_CONFIG: Required<FormatterConfig> = {
17+
export const DEFAULT_CONFIG: Required<
18+
Pick<
19+
FormatterConfig,
20+
'outputDir' | 'filename' | 'formats' | 'terminal' | 'verbose'
21+
>
22+
> = {
1623
outputDir: DEFAULT_OUTPUT_DIR,
1724
filename: DEFAULT_FILENAME,
1825
formats: DEFAULT_FORMATS,
@@ -48,19 +55,29 @@ export default function multipleFormats(
4855
results: ESLint.LintResult[],
4956
_args?: unknown,
5057
): string {
51-
const config = { ...DEFAULT_CONFIG, ...getConfigFromEnv(process.env) };
58+
const config = {
59+
...DEFAULT_CONFIG,
60+
...getConfigFromEnv(process.env),
61+
} satisfies FormatterConfig;
5262

5363
const {
5464
outputDir = DEFAULT_OUTPUT_DIR,
55-
filename = DEFAULT_FILENAME,
56-
formats = DEFAULT_FORMATS,
65+
projectsDir,
66+
projectName = process.env['NX_TASK_TARGET_PROJECT'],
67+
filename,
68+
formats,
5769
terminal,
5870
verbose = false,
5971
} = config;
6072

73+
const filalOutputDir =
74+
typeof projectName === 'string' && typeof projectsDir === 'string'
75+
? path.join(projectsDir ?? '', projectName ?? '', outputDir)
76+
: outputDir;
77+
6178
try {
6279
persistEslintReports(formats, results, {
63-
outputDir,
80+
outputDir: filalOutputDir,
6481
filename,
6582
verbose,
6683
});
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
export type EslintFormat = 'stylish' | 'json' | string;
1+
import type { EslintFormat, PersistConfig } from './utils.js';
22

3-
export type FormatterConfig = {
4-
outputDir?: string;
5-
filename?: string;
3+
export type FormatterConfig = Omit<PersistConfig, 'format'> & {
4+
projectsDir?: string; // e.g. 'apps' or 'packages' to make paths relative to these folders
5+
projectName?: string; // e.g. 'utils' or 'models' also auto-derived for Nx environment variables
66
formats?: EslintFormat[];
77
terminal?: EslintFormat;
8-
verbose?: boolean;
98
};

0 commit comments

Comments
 (0)