simplr-gulp
Version:
Fully functional gulpfile.js implementation. Tailored for Single Page Application. Written in TypeScript.
104 lines (103 loc) • 4.34 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const gulp = require("gulp");
const logger_1 = require("../utils/logger");
const helpers_1 = require("../utils/helpers");
class TasksHandler {
constructor(config) {
this._className = helpers_1.GetClassName(this.constructor);
this._moduleName = `TasksHandler.${this._className}`;
this.configuration = config(this.initConfiguration);
this.constructedTasks = this.registerTasks(this.configuration.Tasks);
this.constructedTasksHandler = this.loadTasksHandlers(this.configuration.TasksHandlers);
if (this.configuration.HandlerAsTask) {
this.registerMainTask();
}
}
get TaskName() {
return this.configuration.Name;
}
;
get initConfiguration() {
return {
Name: "",
TasksSufix: "",
Tasks: [],
TasksHandlers: [],
TasksAsync: true,
WithProduction: false,
HandlerAsTask: true
};
}
registerTasks(tasks) {
let constructedTasks = {};
if (tasks == null || tasks.length === 0) {
logger_1.LoggerInstance.warn(`(${this._moduleName}) The tasks list is empty.`);
return constructedTasks;
}
tasks.forEach(task => {
let constructedTask = new task();
let fullName = this.generateName(constructedTask.Name);
if (constructedTasks[fullName] != null) {
logger_1.LoggerInstance.warn(`(${this._moduleName}) Task "${fullName}" already exist.`);
}
else {
constructedTasks[fullName] = constructedTask;
this.registerTaskFunction(fullName, false, constructedTask);
if (this.configuration.WithProduction) {
this.registerTaskFunction(`${fullName}:Production`, true, constructedTask);
}
}
});
return constructedTasks;
}
registerTaskFunction(name, production, constructedTask) {
let func = constructedTask.TaskFunction.bind(this, production);
func.displayName = name;
if (constructedTask.Description != null && !production) {
func.description = "# " + constructedTask.Description;
}
gulp.task(name, func);
}
loadTasksHandlers(tasksHandlers) {
let constructedTasksHander = {};
if (tasksHandlers == null || tasksHandlers.length === 0) {
return constructedTasksHander;
}
tasksHandlers.forEach(handler => {
let taskHandler = new handler();
let fullName = taskHandler.TaskName;
if (constructedTasksHander[fullName] != null) {
logger_1.LoggerInstance.warn(`(${this._moduleName}) Tasks handler "${fullName}" already exist.`);
}
else {
constructedTasksHander[fullName] = taskHandler;
}
});
return constructedTasksHander;
}
registerMainTask() {
if (this.configuration.Name == null || this.configuration.Name.length === 0) {
return;
}
let method = this.configuration.TasksAsync ? gulp.parallel : gulp.series;
let tasksList = Object.keys(this.constructedTasks).concat(Object.keys(this.constructedTasksHandler));
gulp.task(this.configuration.Name, method(tasksList));
if (this.configuration.WithProduction) {
let tasksListProduction = tasksList.map(x => { return `${x}:Production`; });
gulp.task(`${this.configuration.Name}:Production`, method(tasksListProduction));
}
}
generateName(taskName) {
let name = taskName;
if (this.configuration.Name != null && this.configuration.Name.length > 0) {
if (name.slice(0, this.configuration.Name.length + 1) !== `${this.configuration.Name}.`) {
name = `${this.configuration.Name}.${name}`;
}
}
if (this.configuration.TasksSufix != null && this.configuration.TasksSufix.length > 0) {
name = `${name}.${this.configuration.TasksSufix}`;
}
return name;
}
}
exports.TasksHandler = TasksHandler;