diff --git a/src/configs.ts b/src/configs.ts index 0a47be3..fa970df 100644 --- a/src/configs.ts +++ b/src/configs.ts @@ -8,7 +8,21 @@ import type { FlatConfigItem, MatchedFile, Payload, RuleInfo } from '../shared/t import { isIgnoreOnlyConfig, matchFile } from '../shared/configs' import { MARK_CHECK, MARK_INFO, configFilenames } from './constants' -export interface ReadConfigOptions { +export interface ReadConfigOptions extends ResolveConfigPathOptions { + /** + * Glob file paths matched by the configs + * + * @default true + */ + globMatchedFiles?: boolean + /** + * Change current working directory to basePath + * @default true + */ + chdir?: boolean +} + +export interface ResolveConfigPathOptions { /** * Current working directory */ @@ -22,36 +36,20 @@ export interface ReadConfigOptions { * Override base path. When not provided, will use directory of discovered config file. */ userBasePath?: string - /** - * Glob file paths matched by the configs - * - * @default true - */ - globMatchedFiles?: boolean - /** - * Change current working directory to basePath - * @default true - */ - chdir?: boolean } /** - * Search and read the ESLint config file, processed into inspector payload with module dependencies - * - * Accpet an options object to specify the working directory path and overrides. + * Search and read the ESLint config file. * - * It uses `bundle-requires` load the config file and find it's dependencies. - * It always get the latest version of the config file (no ESM cache). + * Accept an options object to specify the working directory path and overrides. */ -export async function readConfig( - { +export async function resolveConfigPath(options: ResolveConfigPathOptions) { + let { cwd, userConfigPath, userBasePath, - chdir = true, - globMatchedFiles: globFiles = true, - }: ReadConfigOptions, -): Promise<{ configs: FlatConfigItem[], payload: Payload, dependencies: string[] }> { + } = options + if (userBasePath) userBasePath = resolve(cwd, userBasePath) @@ -67,7 +65,29 @@ export async function readConfig( ? cwd // When user explicit provide config path, use current working directory as root : dirname(configPath) // Otherwise, use config file's directory as root ) + return { + basePath, + configPath, + } +} + +/** + * Search and read the ESLint config file, processed into inspector payload with module dependencies + * + * Accept an options object to specify the working directory path and overrides. + * + * It uses `bundle-requires` load the config file and find it's dependencies. + * It always get the latest version of the config file (no ESM cache). + */ +export async function readConfig( + options: ReadConfigOptions, +): Promise<{ configs: FlatConfigItem[], payload: Payload, dependencies: string[] }> { + const { + chdir = true, + globMatchedFiles: globFiles = true, + } = options + const { basePath, configPath } = await resolveConfigPath(options) if (chdir && basePath !== process.cwd()) process.chdir(basePath) diff --git a/src/ws.ts b/src/ws.ts index 5689b0a..bb85216 100644 --- a/src/ws.ts +++ b/src/ws.ts @@ -2,7 +2,8 @@ import chokidar from 'chokidar' import type { WebSocket } from 'ws' import { WebSocketServer } from 'ws' import { getPort } from 'get-port-please' -import { type ReadConfigOptions, readConfig } from './configs' +import type { ReadConfigOptions } from './configs' +import { readConfig, resolveConfigPath } from './configs' import { MARK_CHECK } from './constants' import type { Payload } from '~~/shared/types' @@ -26,9 +27,10 @@ export async function createWsServer(options: CreateWsServerOptions) { ws.on('close', () => wsClients.delete(ws)) }) + const { basePath } = await resolveConfigPath(options) const watcher = chokidar.watch([], { ignoreInitial: true, - cwd: options.cwd, + cwd: basePath, disableGlobbing: true, })