Skip to content

Commit

Permalink
[Watch & Run] Add missing hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Giovannini committed Jul 11, 2022
1 parent b9ccabd commit 2399672
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions packages/vite-plugin-watch-and-run/src/index.ts
Expand Up @@ -26,10 +26,14 @@ export type Options = {
name?: string | null
}

export type WatchKind = 'ADD' | 'CHANGE' | 'DELETE'
const kindWithPath = ['add', 'addDir', 'change', 'delete', 'unlink', 'unlinkDir'] as const
export type KindWithPath = typeof kindWithPath[number]
const kindWithoutPath = ['all', 'error', 'raw', 'ready'] as const
export type KindWithoutPath = typeof kindWithoutPath[number]
export type WatchKind = KindWithPath | KindWithoutPath

export type StateDetail = {
kind: ('ADD' | 'CHANGE' | 'DELETE')[]
kind: WatchKind[]
run: string
delay: number
isRunning: boolean
Expand All @@ -43,8 +47,7 @@ function checkConf(params: Options[]) {

const paramsChecked: Record<string, StateDetail> = {}

for (let i = 0; i < params.length; i++) {
const param = params[i]
params.forEach(param => {
if (!param.watch) {
throw new Error('plugin watch-and-run, `watch` is missing.')
}
Expand All @@ -53,21 +56,23 @@ function checkConf(params: Options[]) {
}

paramsChecked[param.watch] = {
kind: param.watchKind ?? ['ADD', 'CHANGE', 'DELETE'],
kind: param.watchKind ?? ['add', 'change', 'delete'],
run: param.run,
delay: param.delay ?? 300,
isRunning: false,
name: param.name,
}
}
})

return paramsChecked
}

async function shouldRun(absolutePath: string, watchKind: WatchKind, watchAndRunConf: Record<string, StateDetail>) {
for (const globToWatch in watchAndRunConf) {
const param = watchAndRunConf[globToWatch]
if (!param.isRunning && param.kind.includes(watchKind) && micromatch.isMatch(absolutePath, globToWatch)) {
function shouldRun(absolutePath: string | null, watchKind: WatchKind, watchAndRunConf: Record<string, StateDetail>) {
for (const [globToWatch, param] of Object.entries(watchAndRunConf)) {
const isWatched = param.kind.includes(watchKind)
const isPathMatching = absolutePath && micromatch.isMatch(absolutePath, globToWatch)
const isWatchKindWithoutPath = kindWithoutPath.includes(watchKind as KindWithoutPath)
if (!param.isRunning && isWatched && (isPathMatching || isWatchKindWithoutPath)) {
return {
shouldRun: true,
globToWatch,
Expand All @@ -86,14 +91,14 @@ function formatLog(str: string, name?: string) {
return `${name ? logMagneta(`[${name}]`) : ''} ${str}`
}

async function watcher(absolutePath: string, watchKind: WatchKind, watchAndRunConf: Record<string, StateDetail>) {
const shouldRunInfo = await shouldRun(absolutePath, watchKind, watchAndRunConf)
async function watcher(absolutePath: string | null, watchKind: WatchKind, watchAndRunConf: Record<string, StateDetail>) {
const shouldRunInfo = shouldRun(absolutePath, watchKind, watchAndRunConf)
if (shouldRunInfo.shouldRun) {
watchAndRunConf[shouldRunInfo.globToWatch].isRunning = true

log.info(
`${logGreen('✔')} Thx to ${logGreen(shouldRunInfo.globToWatch)}, ` +
`triggered by ${logCyan(watchKind)} ${logGreen(absolutePath)}, ` +
`triggered by ${logCyan(watchKind)} ${absolutePath && logGreen(absolutePath)}, ` +
`we will wait ${logCyan(shouldRunInfo.param.delay + 'ms')} and run ${logGreen(shouldRunInfo.param.run)}.`
)

Expand Down Expand Up @@ -139,19 +144,15 @@ export default function watchAndRun(params: Options[]) {
watchAndRunConf,

configureServer(server) {
const watcherAdd = async absolutePath => {
watcher(absolutePath, 'ADD', watchAndRunConf)
}
const watcherChange = async absolutePath => {
watcher(absolutePath, 'CHANGE', watchAndRunConf)
}
const watcherDelete = async absolutePath => {
watcher(absolutePath, 'DELETE', watchAndRunConf)
}
kindWithPath.forEach((kind: KindWithPath) => {
const _watcher = async (absolutePath: string) => watcher(absolutePath, kind, watchAndRunConf)
server.watcher.on(kind, _watcher)
})

server.watcher.on('add', watcherAdd)
server.watcher.on('change', watcherChange)
server.watcher.on('delete', watcherDelete)
kindWithPath.forEach((kind: KindWithPath) => {
const _watcher = watcher(null, kind, watchAndRunConf)
server.watcher.on(kind, _watcher)
})
},
}
}

0 comments on commit 2399672

Please sign in to comment.