Skip to content

Commit

Permalink
feat(cli): support full reload
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Apr 11, 2021
1 parent 3a7e6b2 commit 44881a7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/koishi/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type LogLevel = number | LogLevelConfig

export interface WatchConfig extends WatchOptions {
root?: string
fullReload?: boolean
}

export interface AppConfig extends AppOptions {
Expand Down
18 changes: 12 additions & 6 deletions packages/koishi/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { App, BotOptions, version } from 'koishi-core'
import { resolve, dirname } from 'path'
import { resolve, dirname, relative } from 'path'
import { coerce, Logger, noop, LogLevelConfig } from 'koishi-utils'
import { performance } from 'perf_hooks'
import { watch } from 'chokidar'
Expand Down Expand Up @@ -196,19 +196,25 @@ function loadDependencies(filename: string, ignored: MapOrSet<string>) {
function createWatcher() {
if (process.env.KOISHI_WATCH_ROOT === undefined && !config.watch) return

const { root = '', ignored = [] } = config.watch || {}
const watchRoot = process.env.KOISHI_WATCH_ROOT ?? root
const { root = '', ignored = [], fullReload } = config.watch || {}
const watchRoot = resolve(process.cwd(), process.env.KOISHI_WATCH_ROOT ?? root)
const externals = loadDependencies(__filename, pluginMap)
const watcher = watch(resolve(process.cwd(), watchRoot), {
const watcher = watch(watchRoot, {
...config.watch,
ignored: ['**/node_modules/**', '**/.git/**', ...ignored],
})

const logger = new Logger('watcher')

watcher.on('change', async (path) => {
if (!require.cache[path] || externals.has(path)) return
logger.debug('change detected:', path)
if (!require.cache[path]) return
logger.info('change detected:', relative(watchRoot, path))

if (externals.has(path)) {
if (!fullReload) return
logger.info('trigger full reload')
process.exit(114)
}

/** files that should be reloaded */
const accepted = new Set<string>()
Expand Down

0 comments on commit 44881a7

Please sign in to comment.