drive-detector
Version:
Detect drives and changes in mounted drives.
53 lines • 1.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const events_1 = require("events");
const DriveList = require("drivelist");
const equal = require("fast-deep-equal");
class DriveDetector extends events_1.EventEmitter {
constructor() {
super(...arguments);
this.drives = [];
}
start(intervalMilliseconds = 5000) {
this.interval = setInterval(this.checkDrives.bind(this), intervalMilliseconds);
this.checkDrives();
}
stop() {
clearInterval(this.interval);
this.drives = [];
}
list() {
return new Promise((resolve, reject) => {
DriveList.list((err, drives) => {
if (err) {
return reject(err);
}
resolve(drives);
});
});
}
checkDrives() {
this.list()
.then((detectedDrives) => {
if (this.drives.length === 0) {
this.emit('started', detectedDrives);
}
else {
this.identifyMountedDrives(this.drives, detectedDrives)
.forEach(drive => this.emit('mounted', drive));
this.identifyUnmountedDrives(this.drives, detectedDrives)
.forEach(drive => this.emit('unmounted', drive));
}
this.drives = detectedDrives;
})
.catch(err => this.emit('error', err));
}
identifyMountedDrives(currentList, newList) {
return newList.filter(drive => !currentList.some(currentDrive => equal(drive, currentDrive)));
}
identifyUnmountedDrives(currentList, newList) {
return currentList.filter(currentDrive => !newList.some(drive => equal(drive, currentDrive)));
}
}
exports.default = DriveDetector;
//# sourceMappingURL=drivedetector.js.map