simplr-gulp
Version:
Fully functional gulpfile.js implementation. Tailored for Single Page Application. Written in TypeScript.
77 lines (76 loc) • 2.81 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const task_base_1 = require("../tasks/task-base");
const gulp = require("gulp");
const logger_1 = require("../utils/logger");
class WatchTaskBase extends task_base_1.TaskBase {
constructor() {
super(...arguments);
this.UseWatchTaskFunctionOnly = false;
this.Description = "Watch source files and start tasks";
this.TaskFunction = (production, done) => {
let taskName = `${this.TaskNamePrefix}.${this.Name}`;
if (production) {
taskName = this.addTasksProductionSuffix(taskName);
}
let completeTask = () => {
this.emit("end");
done();
};
if (this.UseWatchTaskFunctionOnly) {
if (this.WatchTaskFunction == null) {
logger_1.LoggerInstance.withType(`in class ${this._className}`)
.error(`Cannot use "UseWatchTaskFunctionOnly" without "WatchTaskFunction" function.`);
completeTask();
return;
}
let maybePromise = this.WatchTaskFunction(production, done);
if (maybePromise != null && maybePromise.then != null) {
maybePromise
.then(completeTask)
.catch((error) => {
logger_1.LoggerInstance.withType(taskName).error(error);
completeTask();
});
}
}
else {
return gulp.parallel(this.getStarterFunction(taskName), taskName)(completeTask);
}
};
this.listeners = {};
this.uniqId = 0;
}
getStarterFunction(taskName) {
let func = (done) => {
this.emit("start");
done();
};
func.displayName = taskName + ".Starter";
return func;
}
addTasksProductionSuffix(text) {
return text + ":Production";
}
get UniqueId() {
return this.uniqId++;
}
On(eventName, callback) {
let id = this.UniqueId;
this.listeners[id] = { Callback: callback, Event: eventName };
return { remove: this.removeListener.bind(this, id) };
}
emit(eventName, ...params) {
Object.keys(this.listeners).forEach(key => {
let listener = this.listeners[key];
if (listener.Event === eventName) {
listener.Callback(params);
}
});
}
removeListener(id) {
if (this.listeners[id] != null) {
delete this.listeners[id];
}
}
}
exports.WatchTaskBase = WatchTaskBase;