UNPKG

@conio/serverless-plugin-ifelse

Version:

A Serverless Plugin to write If Else conditions in serverless YAML file

219 lines (210 loc) 7.63 kB
class serverlessPluginIfElse { /** * * @param {*} serverless * @param {*} options */ constructor(serverless, options = {}) { this.serverless = serverless; this.options = options; this.hooks = { "before:package:initialize": this.applyConditions.bind(this), "before:remove:remove": this.applyConditions.bind(this), "before:offline:start:init": this.applyConditions.bind(this), "before:offline:start": this.applyConditions.bind(this), "before:openapi:generate:serverless": this.applyConditions.bind(this), }; this.pluginName = "serverless-plugin-ifelse"; } /** * */ applyConditions() { let params = this.serverless.service.custom.serverlessIfElse; if (!this.isvalidObject(params)) { return; } params.forEach((item) => { if (item.If) { try { if (eval(item.If)) { this.conditionMatchLog(item.If, true); this.setItemValues(item.Set); this.removeItems(item.Exclude); } else { this.conditionMatchLog(item.If, false); this.setItemValues(item.ElseSet); this.removeItems(item.ElseExclude); } } catch (e) { this.evaluateErrorLog(item.If, e); } } if (this.isvalidObject(item.ExcludeIf)) { Object.keys(item.ExcludeIf).forEach((exludeKey) => { try { if (eval(item.ExcludeIf[exludeKey])) { this.conditionMatchLog(item.ExcludeIf[exludeKey], true); this.changeKey(exludeKey); } } catch (e) { this.evaluateErrorLog(item.ExcludeIf[exludeKey], e); } }); } if (item.IterExcludeIf) { item.IterExcludeIf.forEach((element) => { Object.keys(this.serverless.service[element.key]).forEach((obj) => { var expr = element.expression var realItem = element.key + "." + obj var domainElement = "this.serverless.service." + realItem // console.log(realItem); // console.log(domainElement); var changeItem = realItem if (element.subkey) { changeItem = realItem + "." + element.subkey } expr = expr.replace(/<item>/g, domainElement) try { if (eval(expr)) { this.conditionMatchLog(expr, true); this.changeKey(changeItem); } } catch (e) { console.error("Error:", e) } }); }); } }); } /** * * @param {*} item */ isvalidObject(item) { return item && typeof item == "object"; } /** * * @param {*} items */ setItemValues(items) { if (!this.isvalidObject(items)) { return; } Object.keys(items).forEach((key) => { this.changeKey(key, "set", items[key]); }); } /** * * @param {*} item */ removeItems(item) { if (!item) { return; } if (typeof item == "object") { item.forEach((key) => { this.changeKey(key); }); } else { this.changeKey(item); } } /** * * @param {*} keyPath */ changeKey(keyPath, type = "remove", newValue = null) { // console.log(keyPath) let path = keyPath.split("."); if (path.length <= 1) { return; } let item = this.serverless.service; let i = 0; for (i; i < path.length - 1; i++) { item = item[path[i]]; if (!item) { return; } } // console.log("AAAAAA", path[i], item, item instanceof Array) if (item instanceof Array) { let j = 0; for (j; j < item.length; j++) { if (item[j] == undefined) { continue; } // console.log("BBBBB", path[i], typeof path[i], path[i] instanceof String, item[j], typeof item[j], item[j] instanceof Object) if (path[i] in item[j]) { if (type == "remove") { this.serverless.cli.log(this.pluginName + " - Excluding: " + keyPath); delete item[j]; } else if (type == "set") { item[j] = newValue; if (typeof newValue == "object") { newValue = JSON.stringify(newValue); } this.serverless.cli.log(this.pluginName + " - Value Changed for : " + keyPath + " to: " + newValue); } } else { if (type == "set") { item.push(newValue); if (typeof newValue == "object") { newValue = JSON.stringify(newValue); } this.serverless.cli.log(this.pluginName + " - Value Changed for : " + keyPath + " to: " + newValue); } } } } else { if (path[i] in item) { if (type == "remove") { this.serverless.cli.log(this.pluginName + " - Excluding: " + keyPath); delete item[path[i]]; } else if (type == "set") { item[path[i]] = newValue; if (typeof newValue == "object") { newValue = JSON.stringify(newValue); } this.serverless.cli.log(this.pluginName + " - Value Changed for : " + keyPath + " to: " + newValue); } // If item not exists => add it to path } else { if (type == "set") { item[path[i]] = newValue; if (typeof newValue == "object") { newValue = JSON.stringify(newValue); } this.serverless.cli.log(this.pluginName + " - Value Changed for : " + keyPath + " to: " + newValue); } } } } /** * * @param {*} condition * @param {*} matched */ conditionMatchLog(condition, matched = true) { if (this.options.v || this.options.verbose) { this.serverless.cli.log(this.pluginName + " - (" + condition + ") Condition" + ((!matched) ? " not" : "") + " true."); } } /** * * @param {*} condition * @param {*} e */ evaluateErrorLog(condition, e) { this.serverless.cli.log(this.pluginName + " - cannot evaluate condition " + condition + " : " + e); } } module.exports = serverlessPluginIfElse;