diff --git a/src/cli/main.ts b/src/cli/main.ts index b925dc65..6ba4382d 100644 --- a/src/cli/main.ts +++ b/src/cli/main.ts @@ -88,6 +88,10 @@ import { findTSLintConfiguration } from "../input/findTSLintConfiguration"; import { findTypeScriptConfiguration } from "../input/findTypeScriptConfiguration"; import { importer, ImporterDependencies } from "../input/importer"; import { mergeLintConfigurations } from "../input/mergeLintConfigurations"; +import { + findExistingESLintConfiguration, + FindExistingESLintConfigurationDependencies, +} from "../input/findExistingESLintConfiguration"; const convertFileCommentsDependencies: ConvertFileCommentsDependencies = { converters: ruleConverters, @@ -133,6 +137,10 @@ const findOriginalConfigurationsDependencies: FindOriginalConfigurationsDependen mergeLintConfigurations, }; +const findExistingESLintConfigurationDependencies: FindExistingESLintConfigurationDependencies = { + globAsync, +}; + const collectCommentFileNamesDependencies: CollectCommentFileNamesDependencies = { findTypeScriptConfiguration: bind(findTypeScriptConfiguration, findConfigurationDependencies), }; @@ -216,6 +224,10 @@ const runCliDependencies: RunCliDependencies = { findOriginalConfigurations, findOriginalConfigurationsDependencies, ), + findExistingESLintConfiguration: bind( + findExistingESLintConfiguration, + findExistingESLintConfigurationDependencies, + ), logger: processLogger, }; diff --git a/src/cli/runCli.ts b/src/cli/runCli.ts index 42ab7b76..629a1c7b 100644 --- a/src/cli/runCli.ts +++ b/src/cli/runCli.ts @@ -6,6 +6,7 @@ import { version } from "../../package.json"; import { Logger } from "../adapters/logger"; import { SansDependencies } from "../binding"; import { Converter } from "../converters/types"; +import { findExistingESLintConfiguration } from "../input/findExistingESLintConfiguration"; import { AllOriginalConfigurations, findOriginalConfigurations, @@ -15,6 +16,7 @@ import { ResultStatus, ResultWithStatus, TSLintToESLintSettings } from "../types export type RunCliDependencies = { converters: Converter[]; + findExistingESLintConfiguration: SansDependencies; findOriginalConfigurations: SansDependencies; logger: Logger; }; @@ -41,7 +43,6 @@ export const runCli = async ( .option("-V, --version", "output the package version"); const parsedArgv = { - config: "./.eslintrc.js", ...command.parse(rawArgv).opts(), } as TSLintToESLintSettings; @@ -51,6 +52,21 @@ export const runCli = async ( return ResultStatus.Succeeded; } + // 2-B. Lookup the eslint config file `.eslintrc.*` (js, json, yaml) / package.json file + const eslintConfiguration = await dependencies.findExistingESLintConfiguration(); + if (eslintConfiguration.status !== ResultStatus.Succeeded) { + logErrorResult(eslintConfiguration, dependencies.logger); + } else { + if (Array.isArray(eslintConfiguration.data)) { + // Pick the first one, it should take into account the order of + // preference based on official ESLint Docs. + parsedArgv.config = eslintConfiguration.data[0]; + } else { + // TODO: We need to keep the current package.json data + parsedArgv.config = "package.json"; + } + } + // 3. Any existing linter and TypeScript configurations are read from disk. const originalConfigurations = await dependencies.findOriginalConfigurations(parsedArgv); if (originalConfigurations.status !== ResultStatus.Succeeded) { diff --git a/src/input/findExistingESLintConfiguration.ts b/src/input/findExistingESLintConfiguration.ts new file mode 100644 index 00000000..7ad8de30 --- /dev/null +++ b/src/input/findExistingESLintConfiguration.ts @@ -0,0 +1,40 @@ +import { fsFileSystem } from "../adapters/fsFileSystem"; +import { GlobAsync } from "../adapters/globAsync"; +import { ResultStatus, ResultWithDataStatus } from "../types"; +import { ESLintConfiguration } from "./findESLintConfiguration"; + +export type FindExistingESLintConfigurationDependencies = { + globAsync: GlobAsync; +}; + +export const findExistingESLintConfiguration = async ( + dependencies: FindExistingESLintConfigurationDependencies, +): Promise> => { + const globResult = await dependencies.globAsync(".eslintrc.{js,json,yaml}"); + const packageContent = await fsFileSystem.readFile("package.json"); + if (!(globResult instanceof Error)) { + return { + data: globResult, + status: ResultStatus.Succeeded, + }; + } + if (globResult instanceof Error && packageContent instanceof Error) { + return { + errors: [globResult, packageContent], + status: ResultStatus.Failed, + }; + } + if (globResult instanceof Error && typeof packageContent === "string") { + const parsedPackage: Record = JSON.parse(packageContent); + if (parsedPackage.hasOwnProperty("eslintConfig")) { + return { + data: parsedPackage.eslintConfig as ESLintConfiguration, + status: ResultStatus.Succeeded, + }; + } + } + return { + errors: [new Error("Could not find an existing ESLint Config")], + status: ResultStatus.Failed, + }; +};