Skip to content

Instantly share code, notes, and snippets.

@rbuckton
Last active July 17, 2024 18:01
Show Gist options
  • Save rbuckton/06226fd85380b2b2559a964b238a58f6 to your computer and use it in GitHub Desktop.
Save rbuckton/06226fd85380b2b2559a964b238a58f6 to your computer and use it in GitHub Desktop.
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));
@rbuckton
Copy link
Author

rbuckton commented Jul 17, 2024

Outputs:

Setting up real/link folders...
Creating recursive watchers...
Creating 'real/sub/folder/test.txt'...
  [    +4ms] real: sub\folder\test.txt rename
  [    +7ms] link: sub\folder\test.txt rename
  [    +9ms] real: sub\folder\test.txt change
  [   +10ms] real: sub\folder change
  [   +13ms] link: sub\folder\test.txt change
  [   +15ms] link: sub\folder change
Deleting 'real/sub/folder/test.txt'...
  [    +0ms] real: sub\folder\test.txt rename
  [    +1ms] link: sub\folder\test.txt rename
  [  +505ms] real: sub\folder change
  [  +507ms] link: sub\folder change
Creating 'link/sub/folder/test.txt'...
  [    +2ms] real: sub\folder\test.txt rename
  [    +4ms] link: sub\folder\test.txt rename
  [    +6ms] real: sub\folder\test.txt change
  [    +8ms] real: sub\folder change
  [   +10ms] link: sub\folder\test.txt change
  [   +12ms] link: sub\folder change
Deleting 'link/sub/folder/test.txt'...
  [    +2ms] real: sub\folder\test.txt rename
  [    +3ms] link: sub\folder\test.txt rename
Observing the link directory...
  [    +3ms] real: sub\folder change
  [    +9ms] link: sub\folder change
Closing watchers...
  • OS Name: Microsoft Windows 11 Enterprise
  • Version: 10.0.22631 Build 22631
  • Developer Mode: Enabled
  • File System: NTFS, Basic Disk, BitLocker Encrypted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment