Package detail

@bconnorwhite/exec

bconnorwhite281MIT6.0.12

Execute commands while keeping flags easily configurable as an object

exec, spawn, run, execute

readme

@bconnorwhite/exec

npm typescript Coveralls Status GitHub stars Twitter Follow


Execute commands while keeping flags easily configurable as an object.

  • Run one or multiple commands in parallel or series
  • Easily define arguments and flags
  • Easily extract JSON output
  • Inject environment variables
  • Set silent to block CLI output

Installation

yarn add @bconnorwhite/exec
npm install @bconnorwhite/exec

API


exec

Usage

import { exec } from "@bconnorwhite/exec";

// Simple usage:
exec("echo", "hello");

// Explicit usage:
exec({
  command: "babel",
  args: [
    "./src",
    { // Objects are used for flags
      "out-dir": "./build",
      "config-file": "./babel.config.json",
      "w": true // single character flags will be set using a single dash
    }
  ]
});

// Equivalent of:
// babel ./src --out-dir ./build --config-file ./babel.config.json -w

Types

function exec(command: string, args: Args, { env, silent }: Options): Promise<ExecResult>;
function exec({ command, args, env, silent, cwd }: Executable): Promise<ExecResult>;

type Executable = {
  command: string;
  args?: Args;
  cwd?: string;
  env?: NodeJS.ProcessEnv;
  silent?: boolean;
}

type Args = Arg | Arg[];

type Arg = string | Flags;

type Flags = {
  [flag: string]: string | number | boolean  | string[] | undefined;
}

type ExecResult = {
  output: string;
  error: string;
  textOutput: string; // output stripped on ANSI colors
  textError: string; // error stripped on ANSI colors
  jsonOutput: () => JSONObject | JSONArray | undefined; // First JSON object or array in output
  jsonError: () => JSONObject | JSONArray | undefined; // First JSON object or array in error
}


execSync

Usage

import { execSync } from "@bconnorwhite/exec";

// Simple usage:
execSync("echo", "hello");

// Object usage:
execSync({
  command: "babel",
  args: [
    "./src",
    { // Objects are used for flags
      "out-dir": "./build",
      "config-file": "./babel.config.json",
      "w": true // single character flags will be set using a single dash
    }
  ]
});

// Equivalent of:
// babel ./src --out-dir ./build --config-file ./babel.config.json -w

Types

function execSync(command: string, args: Args, { env, silent }: Options): ExecResult;
function execSync({ command, args, env, silent }: Executable): ExecResult;


execAll

Usage

import { execAll } from "@bconnorwhite/exec";

execAll([{
  command: "babel",
  args: [
    "./src",
    { // Objects are used for flags
      "out-dir": "./build",
      "config-file": "./babel.config.json",
      "w": true // single character flags will be set using a single dash
    }
  ]
}, {
  command: "tsc",
  args: {
    "emitDeclarationOnly": true
  }
}], {
  env: {
    NODE_ENV: "development"
  },
  parallel: false
});
// Equivalent of:
// NODE_ENV=development babel ./src --out-dir ./build --config-file ./babel.config.json --watch && tsc --emitDeclarationOnly

Types

function execAll(
  executables: Executable[],
  options: ExecAllOptions
): Promise<ExecResult[]>;

type ExecAllOptions = {
  cwd?: string;
  env?: NodeJS.ProcessEnv; // default, will not override individual commands
  silent?: boolean; // default, will not override individual commands
  parallel?: boolean;
}


executableToString

Usage

import { executableToString } from "@bconnorwhite/exec";

executableToString({
  command: "foo",
  args: [
    "a",
    "b",
    {
      c: true,
      d: "ok",
      long: true
    }
  ]
});
// "foo a b -c -d ok --long"

Types

function executableToString(command: Executable): string;

type Executable = {
  command: string;
  args?: string | string[];
  flags?: Flags;
  env?: NodeJS.ProcessEnv;
}


Dependenciesdependencies


Dev DependenciesDavid


License license

MIT

changelog

6.0.12 (2023-01-01)

Bug Fixes

  • include missing parameter in types (c2b64fe)

6.0.11 (2022-01-17)

Bug Fixes

6.0.10 (2021-08-14)

6.0.9 (2021-07-12)

Bug Fixes

  • destructure undefined env issue (2158e79)

6.0.8 (2021-06-26)

Bug Fixes

6.0.7 (2021-04-01)

Bug Fixes

  • bumped terminating-newline (471cc7e)

6.0.6 (2020-09-25)

Bug Fixes

  • extra space after command with no args (59096f4)
  • undefined windowSize fallback (488ce19)
  • use extract-first-json for execSync (e7e35a2)

6.0.5 (2020-09-21)

Bug Fixes

  • set cli width despite stdio pipe (ce897fd)

6.0.4 (2020-09-21)

Bug Fixes

  • set textError from sterr not stdout (2a984de)

6.0.3 (2020-09-21)

Bug Fixes

  • export Arg, Args, Flag, Flags (54086a8)

6.0.2 (2020-09-20)

Bug Fixes

6.0.1 (2020-09-20)

Bug Fixes

  • switch to as-typed-array (7191594)

6.0.0 (2020-09-20)

5.3.2 (2020-09-15)

5.3.1 (2020-09-10)

5.3.0 (2020-09-06)

5.2.1 (2020-09-05)

5.2.0 (2020-09-05)

5.1.0 (2020-08-22)

5.0.0 (2020-08-22)

4.0.0 (2020-08-12)

3.1.2 (2020-08-12)

3.1.1 (2020-08-12)

3.1.0 (2020-08-12)

3.0.3 (2020-08-09)

3.0.2 (2020-08-09)

3.0.1 (2020-08-09)

3.0.0 (2020-08-09)

2.3.0 (2020-08-09)

2.2.0 (2020-08-09)

2.1.0 (2020-08-09)

2.0.0 (2020-08-09)

1.1.2 (2020-07-26)