@sinclair/hammer
Version:
Build Tool for Browser and Node Applications
117 lines (110 loc) • 4.82 kB
JavaScript
;
/*--------------------------------------------------------------------------
MIT License
Copyright (c) Hammer 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
---------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.watch = exports.Watcher = void 0;
const index_1 = require("../async/index");
const path = require("path");
const fs = require("fs");
class Watcher {
watchers;
receiver;
sender;
debounce;
constructor(sourcePaths) {
const [sender, receiver] = (0, index_1.channel)();
this.debounce = new index_1.Debounce(100);
this.watchers = new Map();
this.sender = sender;
this.receiver = receiver;
this.add(sourcePaths);
}
// --------------------------------------------------------------
// Iterator
// --------------------------------------------------------------
async *[Symbol.asyncIterator]() {
for await (const path of this.receiver) {
yield path;
}
}
// --------------------------------------------------------------
// Methods
// --------------------------------------------------------------
/** Adds additional sourcePaths to watch. If the sourcePath already exists in this watchers set, then no action will be taken. */
add(sourcePaths) {
for (const sourcePath of sourcePaths) {
if (this.watchers.has(sourcePath))
continue;
if (!fs.existsSync(sourcePath))
continue;
this.watchers.set(sourcePath, [...this.createWatchers(sourcePath)]);
}
}
/** Disposes of this watcher and terminates all internal watchers. */
dispose() {
this.sender.end();
for (const [sourcePath, watchers] of this.watchers) {
this.watchers.delete(sourcePath);
for (const watcher of watchers) {
watcher.close();
}
}
}
// --------------------------------------------------------------
// Events
// --------------------------------------------------------------
onChange(sourcePath) {
this.debounce.run(() => this.sender.send(sourcePath));
}
// --------------------------------------------------------------
// Watchers
// --------------------------------------------------------------
*createLinuxDirectoryWatchers(directory, sourcePath) {
const stat = fs.statSync(directory);
yield fs.watch(directory, (event) => this.onChange(sourcePath));
for (const filepath of fs.readdirSync(directory)) {
const next = path.join(directory, filepath);
const stat = fs.statSync(next);
if (stat.isDirectory())
yield* this.createLinuxDirectoryWatchers(next, sourcePath);
}
}
*createLinuxWatchers(sourcePath) {
const stat = fs.statSync(sourcePath);
if (stat.isDirectory()) {
yield* this.createLinuxDirectoryWatchers(sourcePath, sourcePath);
}
else if (stat.isFile()) {
yield fs.watch(sourcePath, (event) => this.onChange(sourcePath));
}
}
*createWindowsWatchers(sourcePath) {
const stat = fs.statSync(sourcePath);
yield stat.isDirectory() ? fs.watch(sourcePath, { recursive: true }, (event) => this.onChange(sourcePath)) : fs.watch(sourcePath, (event) => this.onChange(sourcePath));
}
*createWatchers(sourcePath) {
yield* /^win/.test(process.platform) ? this.createWindowsWatchers(sourcePath) : this.createLinuxWatchers(sourcePath);
}
}
exports.Watcher = Watcher;
function watch(sourcePaths) {
return new Watcher(sourcePaths);
}
exports.watch = watch;