2018-02-28 23:08:08 +00:00
|
|
|
import * as plugins from './smartchok.plugins';
|
2018-10-10 15:06:40 +00:00
|
|
|
import { Stringmap } from '@pushrocks/lik';
|
|
|
|
import { Observablemap } from '@pushrocks/smartrx';
|
2017-06-30 16:05:55 +00:00
|
|
|
|
2018-02-28 23:08:08 +00:00
|
|
|
export type TSmartchokStatus = 'idle' | 'starting' | 'watching';
|
|
|
|
export type TFsEvent =
|
|
|
|
| 'add'
|
|
|
|
| 'addDir'
|
|
|
|
| 'change'
|
|
|
|
| 'error'
|
|
|
|
| 'unlink'
|
|
|
|
| 'unlinkDir'
|
|
|
|
| 'ready'
|
|
|
|
| 'raw';
|
2017-06-30 16:05:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Smartchok allows easy wathcing of files
|
|
|
|
*/
|
|
|
|
export class Smartchok {
|
2018-02-28 23:08:08 +00:00
|
|
|
watchStringmap = new Stringmap();
|
|
|
|
chokidarOptions: plugins.chokidar.WatchOptions;
|
|
|
|
status: TSmartchokStatus = 'idle';
|
|
|
|
private watcher;
|
2018-10-10 15:06:40 +00:00
|
|
|
private watchingDeferred = plugins.smartpromise.defer<void>(); // used to run things when watcher is initialized
|
2018-02-28 23:08:08 +00:00
|
|
|
private eventObservablemap = new plugins.smartrx.Observablemap(); // register one observable per event
|
2017-06-30 16:05:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* constructor of class smartchok
|
|
|
|
*/
|
2018-02-28 23:08:08 +00:00
|
|
|
constructor(watchArrayArg: string[], optionsArg: plugins.chokidar.WatchOptions = {}) {
|
|
|
|
this.watchStringmap.addStringArray(watchArrayArg);
|
|
|
|
this.chokidarOptions = optionsArg;
|
2017-06-30 16:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* adds files to the list of watched files
|
|
|
|
*/
|
|
|
|
add(pathArrayArg: string[]) {
|
2018-02-28 23:08:08 +00:00
|
|
|
this.watchStringmap.addStringArray(pathArrayArg);
|
2017-06-30 16:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* removes files from the list of watched files
|
|
|
|
*/
|
2018-02-28 23:08:08 +00:00
|
|
|
remove(pathArg: string) {
|
|
|
|
this.watchStringmap.removeString(pathArg);
|
2017-06-30 16:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets an observable for a certain event
|
|
|
|
*/
|
2018-02-28 23:08:08 +00:00
|
|
|
getObservableFor(fsEvent: TFsEvent): Promise<plugins.smartrx.rxjs.Observable<any>> {
|
2018-10-10 15:06:40 +00:00
|
|
|
let done = plugins.smartpromise.defer<plugins.smartrx.rxjs.Observable<any>>();
|
2017-06-30 16:05:55 +00:00
|
|
|
this.watchingDeferred.promise.then(() => {
|
2018-02-28 23:08:08 +00:00
|
|
|
let eventObservable = this.eventObservablemap.getObservableForEmitterEvent(
|
|
|
|
this.watcher,
|
|
|
|
fsEvent
|
|
|
|
);
|
|
|
|
done.resolve(eventObservable);
|
|
|
|
});
|
|
|
|
return done.promise;
|
2017-06-30 16:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* starts the watcher
|
|
|
|
* @returns Promise<void>
|
|
|
|
*/
|
2017-06-30 18:32:35 +00:00
|
|
|
start(): Promise<void> {
|
2018-10-10 15:06:40 +00:00
|
|
|
let done = plugins.smartpromise.defer<void>();
|
2018-02-28 23:08:08 +00:00
|
|
|
this.status = 'starting';
|
|
|
|
this.watcher = plugins.chokidar.watch(
|
|
|
|
this.watchStringmap.getStringArray(),
|
|
|
|
this.chokidarOptions
|
|
|
|
);
|
2017-06-30 16:05:55 +00:00
|
|
|
this.watcher.on('ready', () => {
|
2018-02-28 23:08:08 +00:00
|
|
|
this.status = 'watching';
|
|
|
|
this.watchingDeferred.resolve();
|
|
|
|
done.resolve();
|
|
|
|
});
|
|
|
|
return done.promise;
|
2017-06-30 16:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* stop the watcher process if watching
|
|
|
|
*/
|
|
|
|
stop() {
|
|
|
|
let closeWatcher = () => {
|
2018-02-28 23:08:08 +00:00
|
|
|
this.watcher.close();
|
|
|
|
};
|
2017-06-30 16:05:55 +00:00
|
|
|
if (this.status === 'watching') {
|
2018-02-28 23:08:08 +00:00
|
|
|
console.log('closing while watching');
|
|
|
|
closeWatcher();
|
2017-06-30 16:05:55 +00:00
|
|
|
} else if (this.status === 'starting') {
|
2018-02-28 23:08:08 +00:00
|
|
|
this.watchingDeferred.promise.then(() => {
|
|
|
|
closeWatcher();
|
|
|
|
});
|
2017-06-30 16:05:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|