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 7, 2022
1 parent 42c8e76 commit 17faac6
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions packages/vite-plugin-watch-and-run/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,22 @@ export type Options = {
name?: string | null
}

export type WatchKind = 'ADD' | 'CHANGE' | 'DELETE'
const watchKind = [
'ADD',
'ADD_DIR',
'ALL',
'CHANGE',
'DELETE',
'ERROR',
'RAW',
'READY',
'UNLINK',
'UNLINK_DIR',
] as const
export type WatchKind = typeof watchKind[number]

export type StateDetail = {
kind: ('ADD' | 'CHANGE' | 'DELETE')[]
kind: WatchKind[]
run: string
delay: number
isRunning: boolean
Expand Down Expand Up @@ -128,6 +140,12 @@ async function watcher(absolutePath: string, watchKind: WatchKind, watchAndRunCo
}

const log = new Log('KitQL vite-plugin-watch-and-run')
const toCamel = (s: string): string => s
.toLowerCase()
.replace(/([-_][a-z])/ig, (_) => _.toUpperCase()
.replace('-', '')
.replace('_', '')
)

export default function watchAndRun(params: Options[]) {
// check params, throw Errors if not valid and return a new object representing the state of the plugin
Expand All @@ -139,19 +157,9 @@ 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)
}

server.watcher.on('add', watcherAdd)
server.watcher.on('change', watcherChange)
server.watcher.on('delete', watcherDelete)
watchKind.forEach(kind => {
server.watcher.on(toCamel(kind), (absolutePath: string) => watcher(absolutePath, kind, watchAndRunConf))
})
},
}
}

0 comments on commit 17faac6

Please sign in to comment.