tswatch/dist_ts/tswatch.classes.watcher.js

107 lines
7.1 KiB
JavaScript
Raw Permalink Normal View History

2020-05-22 07:25:34 +00:00
"use strict";
2021-03-08 00:26:44 +00:00
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
2020-05-22 07:25:34 +00:00
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
2021-08-18 16:55:43 +00:00
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
2021-03-08 00:26:44 +00:00
__setModuleDefault(result, mod);
2020-05-22 07:25:34 +00:00
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
2021-03-08 00:26:44 +00:00
exports.Watcher = void 0;
2020-05-22 07:25:34 +00:00
const plugins = __importStar(require("./tswatch.plugins"));
const tswatch_logging_1 = require("./tswatch.logging");
/**
* A watcher keeps track of one child execution
*/
class Watcher {
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() {
if (typeof this.options.commandToExecute === 'string') {
if (this.currentExecution) {
tswatch_logging_1.logger.log('ok', `reexecuting ${this.options.commandToExecute}`);
this.currentExecution.kill();
}
else {
tswatch_logging_1.logger.log('ok', `executing ${this.options.commandToExecute} for the first time`);
}
this.currentExecution = await this.smartshellInstance.execStreaming(this.options.commandToExecute);
}
else {
console.log('cannot run execution task');
}
}
/**
* 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
}
}
}
exports.Watcher = Watcher;
2021-07-26 19:42:51 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHN3YXRjaC5jbGFzc2VzLndhdGNoZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy90c3dhdGNoLmNsYXNzZXMud2F0Y2hlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7O0FBQUEsMkRBQTZDO0FBQzdDLHVEQUEyQztBQVUzQzs7R0FFRztBQUNILE1BQWEsT0FBTztJQVlsQixZQUFZLFVB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