Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic JavaScript Runner #2

Open
metaskills opened this issue Nov 17, 2023 · 0 comments
Open

Basic JavaScript Runner #2

metaskills opened this issue Nov 17, 2023 · 0 comments

Comments

@metaskills
Copy link
Member

Here is a simple handler I cooked up in a node project.

import env from "./env.js";
env.rollbar;
import { execSync } from "child_process";

export const handler = async (event) => {
  const cwd = process.env.LAMBDA_TASK_ROOT || process.cwd();
  const command = event?.X_LAMBDA_CONSOLE?.run;
  if (command) {
    try {
      const options = {
        cwd,
        env: process.env,
        stdio: ["ignore", "pipe", "pipe"], // stdin, stdout, stderr
        encoding: "utf-8",
      };
      const stdout = execSync(command, options);
      return { statusCode: 0, headers: {}, body: stdout };
    } catch (error) {
      const stderr = error.stderr.toString();
      return { statusCode: 1, headers: {}, body: error.stderr.toString() };
    }
  } else {
    return {
      statusCode: 1,
      headers: {},
      body: `Command not found\n`,
    };
  }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant