Last active
February 20, 2024 21:13
-
-
Save jimmywarting/76e5752655deba2d25b63313f47e9e78 to your computer and use it in GitHub Desktop.
Simple dependency free file auto-reload (NodeJS)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { watch } from 'node:fs/promises' | |
import { Worker } from 'node:worker_threads' | |
let worker = new Worker('./app.js') | |
async function reloadOnChange (dir) { | |
const watcher = watch(dir, { recursive: true }) | |
for await (const change of watcher) { | |
if (change.filename.endsWith('.js')) { | |
worker.terminate() | |
worker = new Worker('./app.js') | |
} | |
} | |
} | |
// All the folder to watch for | |
['./src', './lib', './test'].map(reloadOnChange) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment