Skip to content

Commit

Permalink
fix: start watcher with basePath (#39)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
lvjiaxuan and antfu committed Apr 10, 2024
1 parent 67192ab commit e259020
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 25 deletions.
66 changes: 43 additions & 23 deletions src/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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)

Expand All @@ -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)

Expand Down
6 changes: 4 additions & 2 deletions src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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,
})

Expand Down

0 comments on commit e259020

Please sign in to comment.