2018-10-28 00:48:43 +00:00
|
|
|
import * as plugins from './tswatch.plugins';
|
|
|
|
|
2018-10-28 18:28:08 +00:00
|
|
|
export interface ITsWatchConstructorOptions {
|
|
|
|
filePathToWatch: string;
|
|
|
|
commandToExecute: string;
|
|
|
|
timeout?: number;
|
|
|
|
}
|
|
|
|
|
2018-10-28 00:48:43 +00:00
|
|
|
/**
|
|
|
|
* handles the management of watching for foes
|
|
|
|
*/
|
|
|
|
export class TsWatch {
|
|
|
|
private smartshellInstance = new plugins.smartshell.Smartshell({
|
|
|
|
executor: 'bash'
|
|
|
|
});
|
|
|
|
private currentExecution: plugins.smartshell.IExecResultStreaming;
|
2019-05-08 09:14:57 +00:00
|
|
|
private watcher = new plugins.smartchok.Smartchok([], {});
|
2018-10-28 18:28:08 +00:00
|
|
|
private options: ITsWatchConstructorOptions;
|
2018-10-28 00:48:43 +00:00
|
|
|
|
2018-10-28 18:28:08 +00:00
|
|
|
constructor(optionsArg: ITsWatchConstructorOptions) {
|
|
|
|
this.options = optionsArg;
|
2018-10-28 00:48:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* start the file
|
|
|
|
*/
|
|
|
|
public async start() {
|
|
|
|
this.setupCleanup();
|
2018-10-28 18:28:08 +00:00
|
|
|
console.log(`Looking at ${this.options.filePathToWatch} for changes`);
|
2019-05-08 09:14:57 +00:00
|
|
|
this.watcher.add([this.options.filePathToWatch]); // __dirname refers to the directory of this very file
|
|
|
|
const changeObservable = await this.watcher.getObservableFor('change');
|
|
|
|
changeObservable.subscribe(() => {
|
2018-10-28 00:48:43 +00:00
|
|
|
this.updateCurrentExecution();
|
2019-05-08 09:16:24 +00:00
|
|
|
});
|
2018-10-28 00:48:43 +00:00
|
|
|
this.updateCurrentExecution();
|
|
|
|
}
|
|
|
|
|
|
|
|
private async updateCurrentExecution() {
|
|
|
|
if (this.currentExecution) {
|
|
|
|
process.kill(-this.currentExecution.childProcess.pid);
|
|
|
|
}
|
2019-05-08 09:16:24 +00:00
|
|
|
this.currentExecution = await this.smartshellInstance.execStreaming(
|
|
|
|
this.options.commandToExecute
|
|
|
|
);
|
2018-10-28 00:48:43 +00:00
|
|
|
this.currentExecution = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this method sets up a clean exit strategy
|
|
|
|
*/
|
2018-10-28 18:28:08 +00:00
|
|
|
private async setupCleanup() {
|
2018-10-28 00:48:43 +00:00
|
|
|
const cleanup = () => {
|
|
|
|
if (this.currentExecution) {
|
|
|
|
process.kill(-this.currentExecution.childProcess.pid);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
process.on('exit', () => {
|
|
|
|
console.log('');
|
|
|
|
console.log('now exiting!');
|
|
|
|
cleanup();
|
|
|
|
process.exit(0);
|
|
|
|
});
|
|
|
|
process.on('SIGINT', () => {
|
|
|
|
console.log('');
|
|
|
|
console.log('ok! got SIGINT We are exiting! Just cleaning up to exit neatly :)');
|
|
|
|
cleanup();
|
|
|
|
process.exit(0);
|
|
|
|
});
|
2018-10-28 18:28:08 +00:00
|
|
|
|
|
|
|
// handle timeout
|
|
|
|
if (this.options.timeout) {
|
|
|
|
plugins.smartdelay.delayFor(this.options.timeout).then(() => {
|
|
|
|
console.log(`timed out afer ${this.options.timeout} milliseconds! exiting!`);
|
|
|
|
cleanup();
|
|
|
|
process.exit(0);
|
|
|
|
});
|
|
|
|
}
|
2018-10-28 00:48:43 +00:00
|
|
|
}
|
|
|
|
}
|