File: watch-folder.js

package info (click to toggle)
node-watchpack 2.4.0%2B~cs2.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 412 kB
  • sloc: javascript: 3,958; makefile: 2
file content (40 lines) | stat: -rw-r--r-- 982 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"use strict";

var path = require("path");
var Watchpack = require("../");

var folder = path.join(__dirname, "folder");

function startWatcher(name, files, folders) {
	var w = new Watchpack({
		aggregateTimeout: 3000
	});

	w.on("change", function(file, mtime) {
		console.log(name, "change", path.relative(folder, file), mtime);
	});

	w.on("aggregated", function(changes) {
		var times = w.getTimes();
		console.log(name, "aggregated", Array.from(changes, function(file) {
			return path.relative(folder, file);
		}), Object.keys(times).reduce(function(obj, file) {
			obj[path.relative(folder, file)] = times[file];
			return obj
		}, {}));
	});

	var startTime = Date.now() - 10000;
	console.log(name, startTime);
	w.watch(files, folders, startTime);
}

startWatcher("folder", [], [folder]);
startWatcher("sub+files", [
	path.join(folder, "a.txt"),
	path.join(folder, "b.txt"),
	path.join(folder, "c.txt"),
	path.join(folder, "d.txt"),
], [
	path.join(folder, "subfolder")
]);