107 lines
7.1 KiB
JavaScript
107 lines
7.1 KiB
JavaScript
"use strict";
|
|
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;
|
|
});
|
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.Watcher = void 0;
|
|
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({
|
|
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 (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!');
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
exports.Watcher = Watcher;
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHN3YXRjaC5jbGFzc2VzLndhdGNoZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy90c3dhdGNoLmNsYXNzZXMud2F0Y2hlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7O0FBQUEsMkRBQTZDO0FB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