diff --git a/.gitignore b/.gitignore index dd7858f6..a3759e41 100644 --- a/.gitignore +++ b/.gitignore @@ -60,4 +60,5 @@ Cargo.lock /.cache .netlify *.tsbuildinfo -/fixtures-tmp \ No newline at end of file +/fixtures-tmp +mizdra-eslint-interactive-*.tgz diff --git a/bin/eslint-interactive.js b/bin/eslint-interactive.js index d74628bf..839bf7dc 100755 --- a/bin/eslint-interactive.js +++ b/bin/eslint-interactive.js @@ -1,4 +1,4 @@ -#!/usr/bin/env -S node --enable-source-maps --unhandled-rejections=strict +#!/usr/bin/env -S node --enable-source-maps --unhandled-rejections=strict --experimental-import-meta-resolve import { run } from '../dist/index.js'; diff --git a/e2e-test/global-installation/.gitignore b/e2e-test/global-installation/.gitignore index 6b618b37..6903b57a 100644 --- a/e2e-test/global-installation/.gitignore +++ b/e2e-test/global-installation/.gitignore @@ -1,2 +1 @@ -/mizdra-eslint-interactive-*.tgz /package-lock.json \ No newline at end of file diff --git a/e2e-test/global-installation/test.sh b/e2e-test/global-installation/test.sh index 123a2919..ad53c9a8 100755 --- a/e2e-test/global-installation/test.sh +++ b/e2e-test/global-installation/test.sh @@ -7,9 +7,6 @@ npm install -g $(ls mizdra-eslint-interactive-*.tgz) ## install peerDeps manually npm install -g eslint@$(cat ../../node_modules/eslint/package.json | jq -r .version) -## workaround for https://github.com/mizdra/eslint-interactive/issues/133 -npm install source-map-support - # Test cases diff --git a/src/core.ts b/src/core.ts index 8461a547..6c1f9e28 100644 --- a/src/core.ts +++ b/src/core.ts @@ -1,6 +1,9 @@ import { tmpdir } from 'os'; import { join } from 'path'; +import { fileURLToPath } from 'url'; import { ESLint } from 'eslint'; +// eslint-disable-next-line @typescript-eslint/no-require-imports +import isInstalledGlobally = require('is-installed-globally'); import { format } from './formatter/index.js'; import { eslintInteractivePlugin, @@ -77,7 +80,23 @@ export class Core { */ async formatResultDetails(results: ESLint.LintResult[], ruleIds: (string | null)[]): Promise { const eslint = new ESLint(this.baseOptions); - const formatter = await eslint.loadFormatter(this.config.formatterName ?? 'codeframe'); + const formatterName = this.config.formatterName ?? 'codeframe'; + + // When eslint-interactive is installed globally, eslint-formatter-codeframe will also be installed globally. + // On the other hand, `eslint.loadFormatter` cannot load the globally installed formatter by name. So here it loads them by path. + const resolvedFormatterNameOrPath = + isInstalledGlobally && formatterName === 'codeframe' + ? fileURLToPath( + // @ts-expect-error + await import.meta.resolve( + 'eslint-formatter-codeframe', + // @ts-expect-error + await import.meta.resolve('@mizdra/eslint-interactive'), + ), + ) + : formatterName; + + const formatter = await eslint.loadFormatter(resolvedFormatterNameOrPath); return formatter.format(filterResultsByRuleId(results, ruleIds)); }