@the-goat/core
Version:
    || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var commander_1 = __importDefault(require("commander"));
var notifier_1 = __importDefault(require("./notifier"));
var GoatEvents_1 = __importDefault(require("./events/GoatEvents"));
var writeConfig_1 = __importDefault(require("./schemas/writeConfig"));
var config_1 = require("./config/config");
var schema_1 = __importDefault(require("./validators/schema"));
var watch_1 = __importDefault(require("./events/watch"));
/**
* Class defining all Goat tasks
*/
var GoatTask = /** @class */ (function () {
function GoatTask(build) {
this.name = build.name;
this.key = build.command;
this.description = build.description;
this.schema = build.schema;
this.method = build.method;
this.watch = build.watch;
this.path = process.cwd();
this.init = build.init;
this.events = new GoatEvents_1.default();
this.options = build.options || [];
this.configuration = this.getConfiguration();
this.command = this.buildCommand();
}
/**
* Build the commander command object
*/
GoatTask.prototype.buildCommand = function () {
var _this = this;
var command = new commander_1.default.Command(this.key)
.allowUnknownOption(true)
.command(this.key)
.description(this.description)
// DO NOT SIMPLIFY: this-binding required
.action(function (config) { return _this.action(config); });
if (this.watch) {
command.option('-w, --watch', 'Watch for file changes');
}
if (this.options) {
this.options.forEach(function (option) {
if (!option.allowOnOnce) {
return;
}
command.option(option.flags, option.label);
});
}
return command;
};
/**
* Method to be executed by running Goat command.
*/
GoatTask.prototype.action = function (config) {
if (config.watch) {
watch_1.default(this.events);
return this.watchBase(config, this.events);
}
return this.actionBase(config);
};
/**
* Goat configuration object of the current project
*/
GoatTask.prototype.getConfiguration = function () {
var configuration = config_1.getConfig();
if (!configuration) {
throw new Error('Missing Goat configuraton');
}
var isValid = config_1.validateConfig(configuration);
// Validate used config
if (!isValid || (this.schema && !schema_1.default(configuration, this.schema))) {
notifier_1.default.error('The configuration is not correct');
writeConfig_1.default(this.schema);
}
return configuration;
};
/**
* Forms the base of all Goat actions,
*/
GoatTask.prototype.actionBase = function (config) {
notifier_1.default.log(notifier_1.default.emoji('goat') + " Running " + (this.name || 'task') + " in " + process.cwd() + "\n");
// const result = ;
// result.then((callback) => {
// if (typeof callback === 'function') {
// callback();
// }
// });
return this.method(__assign(__assign({}, this), { options: config }));
};
/**
* Base function for watch tasks
*/
GoatTask.prototype.watchBase = function (config, events) {
this.events = events;
if (!this.watch) {
notifier_1.default.log('This command has no watch option');
return;
}
this.watch(__assign(__assign({}, this), { options: config }));
};
/**
* Build command
*/
GoatTask.prototype.getCommand = function () {
return this.command;
};
return GoatTask;
}());
exports.default = GoatTask;
//# sourceMappingURL=GoatTask.js.map