smartchok/dist/index.js

83 lines
5.5 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const plugins = require("./smartchok.plugins");
const lik_1 = require("lik");
/**
* Smartchok allows easy wathcing of files
*/
class Smartchok {
/**
* constructor of class smartchok
*/
constructor(watchArrayArg, optionsArg = {}) {
this.watchStringmap = new lik_1.Stringmap();
this.status = 'idle';
this.watchingDeferred = plugins.q.defer(); // used to run things when watcher is initialized
this.eventObservablemap = new plugins.lik.Observablemap(); // register one observable per event
this.watchStringmap.addStringArray(watchArrayArg);
this.chokidarOptions = optionsArg;
}
/**
* adds files to the list of watched files
*/
add(pathArrayArg) {
this.watchStringmap.addStringArray(pathArrayArg);
if (this.status !== 'idle') {
this.watchingDeferred.promise.then(() => {
this.watcher.add(pathArrayArg);
});
}
}
/**
* removes files from the list of watched files
*/
remove(pathArg) {
this.watchStringmap.removeString(''); // TODO
this.watchingDeferred.promise.then(() => {
this.watcher.unwatch(pathArg);
});
}
/**
* gets an observable for a certain event
*/
getObservableFor(fsEvent) {
let done = plugins.q.defer();
this.watchingDeferred.promise.then(() => {
let eventObservable = this.eventObservablemap.getObservableForEmitterEvent(this.watcher, fsEvent);
done.resolve(eventObservable);
});
return done.promise;
}
/**
* starts the watcher
* @returns Promise<void>
*/
start() {
let done = plugins.q.defer();
this.status = 'starting';
this.watcher = plugins.chokidar.watch(this.watchStringmap.getStringArray(), this.chokidarOptions);
this.watcher.on('ready', () => {
this.status = 'watching';
this.watchingDeferred.resolve();
done.resolve();
});
return done.promise;
}
/**
* stop the watcher process if watching
*/
stop() {
let closeWatcher = () => {
this.watcher.close();
};
if (this.status === 'watching') {
console.log('closing while watching');
closeWatcher();
}
else if (this.status === 'starting') {
this.watchingDeferred.promise.then(() => { closeWatcher(); });
}
}
}
exports.Smartchok = Smartchok;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6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