Skip to content

Commit 3058c24

Browse files
committed
feat: add support for running watcher once
1 parent bc0c49d commit 3058c24

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

packages/runtime/src/helpers/watcher.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const updateWatchedFiles = async (watcher: FSWatcher, base: string, isFirstRun =
6565
console.log('...done')
6666
}
6767

68-
const start = async (base: string) => {
68+
const start = async (base: string, once = false) => {
6969
const watcher = watch(['middleware.js', 'middleware.ts', 'src/middleware.js', 'src/middleware.ts'], {
7070
// Try and ensure renames just emit one event
7171
atomic: true,
@@ -88,11 +88,15 @@ const start = async (base: string) => {
8888
console.log(`File ${path} has been removed`)
8989
updateWatchedFiles(watcher, base)
9090
})
91-
.on('ready', () => {
91+
.on('ready', async () => {
9292
console.log('Initial scan complete. Ready for changes')
9393
// This only happens on the first scan
94-
updateWatchedFiles(watcher, base, true)
94+
await updateWatchedFiles(watcher, base, true)
95+
if (once) {
96+
watcher.close()
97+
}
9598
})
99+
96100
await new Promise((resolve) => {
97101
watcher.on('close', resolve)
98102
})
@@ -101,7 +105,7 @@ const start = async (base: string) => {
101105
}
102106
}
103107

104-
start(process.argv[2]).catch((error) => {
108+
start(process.argv[2], process.argv[3] === '--once').catch((error) => {
105109
console.error(error)
106110
// eslint-disable-next-line n/no-process-exit
107111
process.exit(1)

0 commit comments

Comments
 (0)