tswatch/dist_ts/tswatch.classes.watcher.js
2024-01-28 18:23:32 +01:00

90 lines
6.5 KiB
JavaScript

import * as plugins from './tswatch.plugins.js';
import { logger } from './tswatch.logging.js';
/**
* A watcher keeps track of one child execution
*/
export class Watcher {
constructor(optionsArg) {
/**
* used to execute shell commands
*/
this.smartshellInstance = new plugins.smartshell.Smartshell({
executor: 'bash',
});
this.smartchokWatcher = new plugins.smartchok.Smartchok([]);
this.options = optionsArg;
}
/**
* start the file
*/
async start() {
await this.setupCleanup();
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();
});
await this.updateCurrentExecution();
}
/**
* updates the current execution
*/
async updateCurrentExecution() {
if (this.options.commandToExecute) {
if (this.currentExecution) {
logger.log('ok', `reexecuting ${this.options.commandToExecute}`);
this.currentExecution.kill();
}
else {
logger.log('ok', `executing ${this.options.commandToExecute} for the first time`);
}
this.currentExecution = await this.smartshellInstance.execStreaming(this.options.commandToExecute);
}
else {
console.log('no executionCommand set');
}
if (this.options.functionToCall) {
this.options.functionToCall();
}
else {
console.log('no functionToCall set.');
}
}
/**
* this method sets up a clean exit strategy
*/
async setupCleanup() {
process.on('exit', () => {
console.log('');
console.log('now exiting!');
this.stop();
process.exit(0);
});
process.on('SIGINT', () => {
console.log('');
console.log('ok! got SIGINT We are exiting! Just cleaning up to exit neatly :)');
this.stop();
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!`);
this.stop();
process.exit(0);
});
}
}
/**
* stops the watcher
*/
async stop() {
await this.smartchokWatcher.stop();
if (this.currentExecution && !this.currentExecution.childProcess.killed) {
this.currentExecution.kill();
}
}
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZX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