2022-03-14 21:04:11 +00:00
|
|
|
import * as plugins from './tswatch.plugins.js';
|
|
|
|
import { logger } from './tswatch.logging.js';
|
2020-05-22 07:25:34 +00:00
|
|
|
/**
|
|
|
|
* A watcher keeps track of one child execution
|
|
|
|
*/
|
2022-03-14 21:04:11 +00:00
|
|
|
export class Watcher {
|
2020-05-22 07:25:34 +00:00
|
|
|
constructor(optionsArg) {
|
|
|
|
/**
|
|
|
|
* used to execute shell commands
|
|
|
|
*/
|
|
|
|
this.smartshellInstance = new plugins.smartshell.Smartshell({
|
2021-03-08 00:26:44 +00:00
|
|
|
executor: 'bash',
|
2020-05-22 07:25:34 +00:00
|
|
|
});
|
|
|
|
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() {
|
2022-03-18 15:36:13 +00:00
|
|
|
if (this.options.commandToExecute) {
|
2020-05-22 07:25:34 +00:00
|
|
|
if (this.currentExecution) {
|
2022-03-14 21:04:11 +00:00
|
|
|
logger.log('ok', `reexecuting ${this.options.commandToExecute}`);
|
2020-05-22 07:25:34 +00:00
|
|
|
this.currentExecution.kill();
|
|
|
|
}
|
|
|
|
else {
|
2022-03-14 21:04:11 +00:00
|
|
|
logger.log('ok', `executing ${this.options.commandToExecute} for the first time`);
|
2020-05-22 07:25:34 +00:00
|
|
|
}
|
|
|
|
this.currentExecution = await this.smartshellInstance.execStreaming(this.options.commandToExecute);
|
|
|
|
}
|
|
|
|
else {
|
2022-03-18 15:36:13 +00:00
|
|
|
console.log('no executionCommand set');
|
|
|
|
}
|
|
|
|
if (this.options.functionToCall) {
|
|
|
|
this.options.functionToCall();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log('no functionToCall set.');
|
2020-05-22 07:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* this method sets up a clean exit strategy
|
|
|
|
*/
|
|
|
|
async setupCleanup() {
|
|
|
|
process.on('exit', () => {
|
|
|
|
console.log('');
|
|
|
|
console.log('now exiting!');
|
2021-07-26 19:42:51 +00:00
|
|
|
this.stop();
|
2020-05-22 07:25:34 +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();
|
2020-05-22 07:25:34 +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();
|
2020-05-22 07:25:34 +00:00
|
|
|
process.exit(0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* stops the watcher
|
|
|
|
*/
|
|
|
|
async stop() {
|
|
|
|
await this.smartchokWatcher.stop();
|
|
|
|
if (this.currentExecution && !this.currentExecution.childProcess.killed) {
|
2021-07-26 19:42:51 +00:00
|
|
|
this.currentExecution.kill();
|
2020-05-22 07:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-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