tswatch/ts/tswatch.classes.watcher.ts

106 lines
2.9 KiB
TypeScript
Raw Permalink Normal View History

2022-03-14 20:17:36 +00:00
import * as plugins from './tswatch.plugins.js';
import { logger } from './tswatch.logging.js';
2019-05-08 22:08:40 +00:00
export interface IWatcherConstructorOptions {
filePathToWatch: string;
2022-03-18 06:13:20 +00:00
commandToExecute?: string;
functionToCall?: () => Promise<any>;
2019-05-08 22:08:40 +00:00
timeout?: number;
}
/**
* A watcher keeps track of one child execution
*/
export class Watcher {
2019-10-12 14:54:03 +00:00
/**
* used to execute shell commands
*/
2019-05-08 22:08:40 +00:00
private smartshellInstance = new plugins.smartshell.Smartshell({
2020-07-04 10:45:56 +00:00
executor: 'bash',
2019-05-08 22:08:40 +00:00
});
2019-10-12 14:54:03 +00:00
2019-05-08 22:08:40 +00:00
private currentExecution: plugins.smartshell.IExecResultStreaming;
private smartchokWatcher = new plugins.smartchok.Smartchok([], {});
private options: IWatcherConstructorOptions;
constructor(optionsArg: IWatcherConstructorOptions) {
this.options = optionsArg;
}
/**
* start the file
*/
public async start() {
2019-10-12 13:07:44 +00:00
await this.setupCleanup();
2019-05-08 22:08:40 +00:00
console.log(`Looking at ${this.options.filePathToWatch} for changes`);
this.smartchokWatcher.add([this.options.filePathToWatch]); // __dirname refers to the directory of this very file
await this.smartchokWatcher.start();
const changeObservable = await this.smartchokWatcher.getObservableFor('change');
changeObservable.subscribe(() => {
this.updateCurrentExecution();
});
2019-10-14 12:53:55 +00:00
await this.updateCurrentExecution();
2019-05-08 22:08:40 +00:00
}
/**
* updates the current execution
*/
private async updateCurrentExecution() {
2022-03-18 06:13:20 +00:00
if (this.options.commandToExecute) {
2019-10-12 14:54:03 +00:00
if (this.currentExecution) {
logger.log('ok', `reexecuting ${this.options.commandToExecute}`);
2020-05-22 07:19:39 +00:00
this.currentExecution.kill();
2019-10-12 14:54:03 +00:00
} else {
logger.log('ok', `executing ${this.options.commandToExecute} for the first time`);
}
this.currentExecution = await this.smartshellInstance.execStreaming(
this.options.commandToExecute
);
2019-05-08 22:08:40 +00:00
} else {
2022-03-18 06:13:20 +00:00
console.log('no executionCommand set');
}
if (this.options.functionToCall) {
this.options.functionToCall();
} else {
console.log('no functionToCall set.')
2019-05-08 22:08:40 +00:00
}
}
/**
* this method sets up a clean exit strategy
*/
private async setupCleanup() {
process.on('exit', () => {
console.log('');
console.log('now exiting!');
2021-07-26 19:42:51 +00:00
this.stop();
2019-05-08 22:08:40 +00:00
process.exit(0);
});
process.on('SIGINT', () => {
console.log('');
console.log('ok! got SIGINT We are exiting! Just cleaning up to exit neatly :)');
2021-07-26 19:42:51 +00:00
this.stop();
2019-05-08 22:08:40 +00:00
process.exit(0);
});
// handle timeout
if (this.options.timeout) {
plugins.smartdelay.delayFor(this.options.timeout).then(() => {
console.log(`timed out afer ${this.options.timeout} milliseconds! exiting!`);
2021-07-26 19:42:51 +00:00
this.stop();
2019-05-08 22:08:40 +00:00
process.exit(0);
});
}
}
2019-10-12 13:07:44 +00:00
2019-05-08 22:08:40 +00:00
/**
* stops the watcher
*/
public async stop() {
await this.smartchokWatcher.stop();
2019-05-27 13:38:07 +00:00
if (this.currentExecution && !this.currentExecution.childProcess.killed) {
2021-07-26 19:42:51 +00:00
this.currentExecution.kill();
2019-05-08 22:08:40 +00:00
}
}
}