UNPKG

sls-dev-tools

Version:

The Dev Tools for the Serverless World

111 lines (89 loc) 3.04 kB
"use strict"; var _fieldWithTitle = require("../components/fieldWithTitle"); var _box = require("../components/box"); var _modalLayout = require("../components/modalLayout"); var _modalTitle = require("../components/modalTitle"); var _invoke = require("../services/invoke"); const blessed = require("blessed"); const wrapPayload = payload => JSON.stringify({ body: payload }); const lambdaInvokeModal = (screen, application, functionName, awsLambdaApi, previousLambdaPayload) => { const lambdaInvokeLayout = new _modalLayout.ModalLayout(screen, 112, 20, true); const preset = [functionName, previousLambdaPayload || "{}"]; const fields = ["FunctionName", "Payload"]; const numTextboxes = 2; let currentTextbox = 1; const textboxes = []; const unselectTextbox = index => { textboxes[index].style.border.fg = "green"; if (index === 2) { textboxes[index].style.fg = "green"; } }; const selectTextbox = index => { textboxes[index].style.border.fg = "yellow"; if (index === 2) { textboxes[index].style.fg = "yellow"; } }; const storePayload = () => { application.previousLambdaPayload[functionName] = textboxes[1].getValue(); }; const closeModal = () => { // Store the payload storePayload(); application.setIsModalOpen(false); application.returnFocus(); lambdaInvokeLayout.destroy(); }; new _modalTitle.ModalTitle(lambdaInvokeLayout, 110, "Lambda Invoke"); for (let i = 0; i < numTextboxes; i += 1) { const textboxWithTitle = (0, _fieldWithTitle.generateFieldWithTitle)(blessed, lambdaInvokeLayout, fields[i], preset[i], 110); const { textbox } = textboxWithTitle; textbox.on("cancel", () => { closeModal(); }); textboxes.push(textbox); } const submit = new _box.Box(lambdaInvokeLayout, 110, 4, "Submit"); textboxes.push(submit); new _box.Box(lambdaInvokeLayout, 110, 6, // eslint-disable-next-line quotes 'Arrow keys to select field | ENTER to toggle edit mode \n Note: payload wrapped {"body": {...}} \nENTER on Submit to inject event | ESC to close'); lambdaInvokeLayout.focus(); selectTextbox(currentTextbox); lambdaInvokeLayout.key(["enter"], () => { // Inject event or select field for entry if (currentTextbox === 2) { (0, _invoke.invokeLambda)(awsLambdaApi, functionName, wrapPayload(textboxes[1].getValue())); closeModal(); } else { textboxes[currentTextbox].focus(); } }); lambdaInvokeLayout.key(["up"], () => { unselectTextbox(currentTextbox); currentTextbox -= 1; if (currentTextbox === -1) { currentTextbox = 2; } selectTextbox(currentTextbox); }); lambdaInvokeLayout.key(["down"], () => { unselectTextbox(currentTextbox); currentTextbox += 1; if (currentTextbox === 3) { currentTextbox = 1; } selectTextbox(currentTextbox); }); lambdaInvokeLayout.key(["escape"], () => { // Discard modal closeModal(); }); }; module.exports = { lambdaInvokeModal };