"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
const { execSync } = require("child_process");
const { processStringSync } = require("@devlego/string-templates");
const automationUtils = require("../automationUtils");
const environment = require("../../environment");
exports.definition = {
    name: "Bash Scripting",
    tagline: "Execute a bash command",
    icon: "JourneyEvent",
    description: "Run a bash script",
    type: "ACTION",
    internal: true,
    stepId: "EXECUTE_BASH",
    inputs: {},
    schema: {
        inputs: {
            properties: {
                code: {
                    type: "string",
                    customType: "code",
                    title: "Code",
                },
            },
            required: ["code"],
        },
        outputs: {
            properties: {
                stdout: {
                    type: "string",
                    description: "Standard output of your bash command or script",
                },
                success: {
                    type: "boolean",
                    description: "Whether the command was successful",
                },
            },
        },
        required: ["stdout"],
    },
};
exports.run = function ({ inputs, context }) {
    return __awaiter(this, void 0, void 0, function* () {
        if (inputs.code == null) {
            return {
                stdout: "Budibase bash automation failed: Invalid inputs",
            };
        }
        try {
            const command = processStringSync(inputs.code, context);
            let stdout, success = true;
            try {
                stdout = execSync(command, {
                    timeout: environment.QUERY_THREAD_TIMEOUT || 500,
                }).toString();
            }
            catch (err) {
                stdout = err.message;
                success = false;
            }
            return {
                stdout,
                success,
            };
        }
        catch (err) {
            return {
                success: false,
                response: automationUtils.getError(err),
            };
        }
    });
};
