my-opendir-watcher
Version:
A custom watcher that emits 'opendir' events when directories are opened
27 lines (22 loc) • 554 B
JavaScript
const fs = require('fs');
const EventEmitter = require('events');
const path = require('path');
class MyWatcher extends EventEmitter {
constructor() {
super();
}
openDirectory(dirPath) {
this.emit('opendir', dirPath);
try {
const files = fs.readdirSync(dirPath, { withFileTypes: true });
return files.map((f) => ({
name: f.name,
isDirectory: f.isDirectory(),
}));
} catch (err) {
this.emit('error', err);
return [];
}
}
}
module.exports = MyWatcher;