tswatch/dist_ts/tswatch.classes.watcher.js

90 lines
6.5 KiB
JavaScript
Raw Normal View History

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
});
2024-01-28 17:23:32 +00:00
this.smartchokWatcher = new plugins.smartchok.Smartchok([]);
2020-05-22 07:25:34 +00:00
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
}
}
}
2024-01-28 17:23:32 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHN3YXRjaC5jbGFzc2VzLndhdGNoZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy90c3dhdGNoLmNsYXNzZXMud2F0Y2hlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEtBQUssT0FBTyxNQUFNLHNCQUFzQixDQUFDO0FBQ2hELE9BQU8sRUFBRSxNQUFNLEVBQUUsTUFBTSxzQkFBc0IsQ0FBQztBQVM5Qzs7R0FFRztBQUNILE1BQU0sT0FBTyxPQUFPO0lBWWxCLFlBQVksVUFBc0M7UUFYbEQ7O1dBRUc7UUFDSyx1QkFBa0IsR0FBRyxJQUFJLE9BQU8sQ0FBQyxVQUFVLENBQUMsVUFBVSxDQUFDO1lBQzdELFFBQVEsRUFBRSxNQUFNO1NBQ2pCLENBQUMsQ0FBQztRQUdLLHFCQUFnQixHQUFHLElBQUksT0FBTyxDQUFDLFNBQVMsQ0FBQyxTQUFTLENBQUMsRUFBRSxDQUFDLENBQUM7UUFJN0QsSUFBSSxDQUFDLE9BQU8sR0FBRyx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