Code Monkey home page Code Monkey logo

Comments (1)

austinrivas avatar austinrivas commented on June 2, 2024

it might just be easiest to call the hasura pro cli directly from ts using shelljs?

//#region Global Imports
import * as shell from 'shelljs';
//#endregion Global Imports

//#region Local Imports
import {
  ShellExecResponse,
  ShellExecSuccess,
  ShellExecError,
  ShellExecTimeoutError,
  ShellExecNotFoundError,
} from './Responses';
import { ShellExecOptions } from '@Modules/shell-utils/Interfaces';
//#endregion Local Imports

export const SHELL_DEFAULT_SETTINGS: ShellExecOptions = {
  silent: true,
  simple: true,
};

/*eslint complexity: ["error", 9]*/
export const handleShellResponse = (
  code: number | null,
  stdout: string | null,
  stderr: string | null,
  command: string,
  settings: ShellExecOptions,
  resolve: (value?: ShellExecSuccess) => void,
  reject: (value?: ShellExecError) => void,
): void => {
  switch (code) {
    case 0: // success
      return resolve(
        new ShellExecSuccess(code, stdout, stderr, command, settings),
      );
    // If there is an error then reject
    case null: // timeout
      return reject(
        new ShellExecTimeoutError(code, stdout, stderr, command, settings),
      );
    case 127: // not-found
      return reject(
        new ShellExecNotFoundError(code, stdout, stderr, command, settings),
      );
    case 1: // Catch-all
    case 2: // Misuse of shell builtins (according to Bash documentation)
    case 126: // Command invoked cannot execute
    case 128: // Invalid argument to exit
    case 130: // Script terminated by Control-C
    default:
      return reject(
        new ShellExecError(code, stdout, stderr, command, settings),
      );
  }
};

export const shellExec = async (
  command: string,
  settings: ShellExecOptions = SHELL_DEFAULT_SETTINGS,
): Promise<ShellExecResponse> => {
  return new Promise((resolve, reject) => {
    shell.exec(
      command,
      settings,
      (code: number | null, stdout: string | null, stderr: string | null) => {
        handleShellResponse(
          code,
          stdout,
          stderr,
          command,
          settings,
          resolve,
          reject,
        );
      },
    );
  });
};

from create-full-stack.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.