Skip to content

Commit

Permalink
feat: ctx.monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
zaaack committed Jul 26, 2021
1 parent a0d9d83 commit 7e841bc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,11 @@ task('build', async ctx => {
ctx.exec('tsc'),
ctx.exec('typedoc'),
])
// restart process when file changes
ctx.monitor('./src', async () => {
await ctx.run('build:server')
return ctx.exec('node ./dist')
})
})


Expand Down
25 changes: 25 additions & 0 deletions src/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,31 @@ export class ShellContext {
}
return this
}
/**
* restart processes when file changes
*/
monitor(dir: string, run: (ctx: ShellContext) => execa.ExecaChildProcess | Promise<execa.ExecaChildProcess>, {
throttle
}: {
throttle?: number
}) {
let p: execa.ExecaChildProcess | null = null
fs.watchDir(dir, { throttle }, async (event, file) => {
while (p && !p.killed) {
p && p.kill()
await sleep(500)
}
let ret = run(this)
if ('kill' in ret) {
p = ret
} else {
await ret.then(r => {
p=r
return
})
}
})
}
/**
* reset env to default
*/
Expand Down

0 comments on commit 7e841bc

Please sign in to comment.