Last active
July 17, 2024 18:01
-
-
Save rbuckton/06226fd85380b2b2559a964b238a58f6 to your computer and use it in GitHub Desktop.
This file contains 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
const fs = require("fs"); | |
const path = require("path"); | |
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms)); | |
async function main() { | |
console.log("Setting up real/link folders..."); | |
try { fs.mkdirSync(path.join(__dirname, "real/sub/folder"), { recursive: true }); } catch { } | |
try { fs.unlinkSync(path.join(__dirname, "real/sub/folder/test.txt")); } catch { } | |
try { fs.symlinkSync(path.join(__dirname, "real"), path.join(__dirname, "link"), "junction"); } catch { } | |
await delay(1_000); | |
console.log("Creating recursive watchers..."); | |
let last = 0; | |
const watcher1 = fs.watch(path.join(__dirname, "real"), { persistent: true, recursive: true }, (e, f) => { | |
const now = Date.now(); | |
const delta = now - last; | |
console.log(` [${`+${delta.toString()}`.padStart(6, " ")}ms] real:`, f, e); | |
}); | |
const watcher2 = fs.watch(path.join(__dirname, "link"), { persistent: true, recursive: true }, (e, f) => { | |
const now = Date.now(); | |
const delta = now - last; | |
console.log(` [${`+${delta.toString()}`.padStart(6, " ")}ms] link:`, f, e); | |
}); | |
await delay(1_000); | |
console.log("Creating 'real/sub/folder/test.txt'..."); | |
last = Date.now(); | |
fs.writeFileSync(path.join(__dirname, "real/sub/folder/test.txt"), "test", { encoding: "utf8" }); | |
await delay(2_000); | |
console.log("Deleting 'real/sub/folder/test.txt'..."); | |
last = Date.now(); | |
fs.unlinkSync(path.join(__dirname, "real/sub/folder/test.txt")); | |
await delay(2_000); | |
console.log("Creating 'link/sub/folder/test.txt'..."); | |
last = Date.now(); | |
fs.writeFileSync(path.join(__dirname, "link/sub/folder/test.txt"), "test", { encoding: "utf8" }); | |
await delay(2_000); | |
console.log("Deleting 'link/sub/folder/test.txt'..."); | |
last = Date.now(); | |
fs.unlinkSync(path.join(__dirname, "link/sub/folder/test.txt")); | |
await delay(5_000); | |
console.log("Observing the link directory..."); | |
process.cwd(path.join(__dirname, "link/sub/folder/")); | |
fs.readdirSync(path.join(__dirname, "link/sub/folder/")); | |
last = Date.now(); | |
await delay(10_000); | |
console.log("Closing watchers..."); | |
watcher1.close(); | |
watcher2.close(); | |
await delay(1_000); | |
} | |
main().catch(e => console.error(e)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Outputs: