roc
Version:
Build modern web applications easily
218 lines (183 loc) • 8.51 kB
JavaScript
;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.runHook = runHook;
exports.runHookDirectly = runHookDirectly;
exports.registerHooks = registerHooks;
exports.getHooks = getHooks;
exports.setHooks = setHooks;
var _chalk = require('chalk');
var _chalk2 = _interopRequireDefault(_chalk);
var _style = require('../helpers/style');
var _validation = require('../validation');
var _verbose = require('../helpers/verbose');
var _getSuggestions = require('../helpers/get-suggestions');
var _getSuggestions2 = _interopRequireDefault(_getSuggestions);
var _actions = require('./actions');
var _configuration = require('../configuration');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
// This needs to be global, same case as with configuration
global.roc = global.roc || {};
global.roc.hooks = global.roc.hooks || {};
/**
* Used to invoke a hook that have been initialized using registerHooks.
*
* @example
* // First function takes the extension name, the second takes the hook name and possible
* // arguments and the third will take a callback that will receive a single value.
* // The callback is expected if "hasCallback" was set to true.
* (extensionName) => (hookName, ...arguments) => (potentialCallback)
*
* @param {string} extensionName - The extension that the hook belongs to.
*
* @returns {function} - Will return a function that takes in the name of the hook and potential arguments.
*/
function runHook(extensionName) {
const hooks = global.roc.hooks[extensionName];
if (!hooks) {
console.log((0, _style.feedbackMessage)((0, _style.errorLabel)('Error', 'Hook problem'), 'The given extension is not registered.\n\n' + (0, _getSuggestions2.default)([extensionName], Object.keys(global.roc.hooks))));
/* eslint-disable no-process-exit */
process.exit(1);
/* eslint-enable */
}
return function (name) {
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
if (!hooks[name]) {
console.log((0, _style.feedbackMessage)((0, _style.errorLabel)('Error', 'Hook problem'), 'The given hook is not registered.\n\n' + (0, _getSuggestions2.default)([name], Object.keys(hooks))));
/* eslint-disable no-process-exit */
process.exit(1);
/* eslint-enable */
}
if (hooks[name].hasCallback) {
return callback => runHookDirectly(_extends({}, hooks[name], {
extension: extensionName,
name
}), args, callback);
}
return runHookDirectly(_extends({}, hooks[name], {
extension: extensionName,
name
}), args);
};
}
/**
* Used to invoke a hook directly without needing to initialize using registerHooks.
*
* @example
* // First function takes the extension name, the second takes the hook name and possible
* // arguments and the third will take a callback that will receive a single value.
* // The callback is expected if "hasCallback" was set to true.
* (extensionName) => (hookName, ...arguments) => (potentialCallback)
*
* @param {Object} hookMeta - Meta data related to a hook.
* @param {Object[]} args - Arguments to send to an action.
* @param {function} callback - Callback to be used for the response of the action.
*
* @returns {function} - Will return a function that takes in the name of the hook and potential arguments.
*/
function runHookDirectly(_ref) {
let extension = _ref.extension;
let name = _ref.name;
let description = _ref.description;
let returns = _ref.returns;
let argumentsDefinitions = _ref.arguments;
let initialValue = _ref.initialValue;
let args = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1];
let callback = arguments[2];
// Validate args
if (argumentsDefinitions) {
args.forEach((value, i) => {
if (value !== undefined) {
const validationResult = (0, _validation.isValid)(value, argumentsDefinitions[i].validation);
if (validationResult !== true) {
try {
(0, _validation.throwError)(argumentsDefinitions[i].name, validationResult, value, 'argument');
} catch (err) {
console.log((0, _style.feedbackMessage)((0, _style.errorLabel)('Error', 'Hook problem'), 'A argument was not valid.\n\n' + err.message));
/* eslint-disable no-process-exit */
process.exit(1);
/* eslint-enable */
}
}
}
});
}
let previousValue = initialValue;
(0, _actions.getActions)().forEach(_ref2 => {
let actionExtensionName = _ref2.name;
let actions = _ref2.actions;
Object.keys(actions).map(key => {
const action = actions[key];
// Only run if no connection is made to a hook/extension or if they match
if ((!action.extension || action.extension === extension) && (!action.hook || action.hook === name)) {
const createAction = action.action({
extension,
hook: name,
previousValue,
description,
settings: (0, _configuration.getSettings)(),
verbose: (0, _verbose.isVerbose)()
});
if (createAction) {
const performAction = createAction.apply(undefined, _toConsumableArray(args));
if (performAction) {
previousValue = performAction();
if ((0, _verbose.isVerbose)()) {
console.log(`${ _chalk2.default.magenta.bold('Hook') } - ` + `Running hook defined in ${ _chalk2.default.underline(extension) } ` + `named ${ _chalk2.default.underline(name) } ` + `with ${ _chalk2.default.underline(key) } added from ${ _chalk2.default.underline(actionExtensionName) }`);
}
if (returns) {
const validationResult = (0, _validation.isValid)(previousValue, returns);
if (validationResult !== true) {
try {
(0, _validation.throwError)(key, validationResult, previousValue, 'return value of');
} catch (err) {
console.log((0, _style.feedbackMessage)((0, _style.errorLabel)('Error', 'Hook problem'), 'A return value was not valid.\n\n' + err.message));
/* eslint-disable no-process-exit */
process.exit(1);
/* eslint-enable */
}
}
}
if (callback) {
callback(previousValue);
}
}
}
}
});
});
return previousValue;
}
/**
* Register hooks with Roc.
*
* @param {Object} hooks - Object with hooks.
* @param {string} name - Name of the extension that the hooks belongs to.
*/
function registerHooks(hooks, name) {
global.roc.hooks = _extends({}, global.roc.hooks, {
[name]: hooks
});
}
/**
* Gets the registered hooks.
*
* @returns {Object} - The registered hooks as an object where the key will be the extension they belong to.
*/
function getHooks() {
return global.roc.hooks;
}
/**
* Sets the registered hooks.
*
* @param {Object} hooks - The hooks as an object where the key will be the extension they belong to.
*/
function setHooks(hooks) {
global.roc.hooks = hooks;
}
//# sourceMappingURL=index.js.map