Skip to content
/ egzek Public

opinionated lighweight wrapper around child_process.execSync

Notifications You must be signed in to change notification settings

hasparus/egzek

Repository files navigation

egzek

Build, Test and Release npm semantic-release Gitmoji

An opinionated wrapper around child_process.execSync.

It runs each line consecutively and prints it for convenience.


Installation

yarn add --dev egzek

Example

import { exec } from "egzek";

exec(`
  echo "commands are run sequentially" > ${filepath}
  cat ${filepath}
  rm ${filepath}
`);

Output:

 ➡ echo "commands are run sequentially" > /test-1.txt

 ➡ cat /test-1.txt
"commands are run sequentially"

 ➡ rm /test-1.txt

Why?

  • Bash is a hell to maintain. Node is a great alternative for utility scripts
  • I wanted something smaller and simpler than executive and execa.
    • small
      • 7.36kB unpacked
      • 479B gzipped, 428B Brotli.
      • 831B of uncompressed JavaScript code, 1.2kB of types.
    • simple
      • one exported function and one exported type
      • almost the same public API as child_process.execSync
        • common defaults for stdio and encoding options

Public API

import { ExecSyncOptions, StdioOptions } from "child_process";
export interface ExecOptions extends Omit<ExecSyncOptions, "encoding"> {
  /**
   * A directory in which the commands are run.
   */
  dir?: string;
  /**
   * The encoding used for all stdio inputs and outputs.
   * @default "utf-8"
   * @see https://nodejs.org/api/child_process.html#child_process_child_process_execsync_command_options
   */
  encoding?: BufferEncoding;
  /**
   * Child's stdio configuration.
   * @default "inherit"
   */
  stdio?: StdioOptions;
}
export declare namespace ExecOptions {
  interface Pipe extends ExecOptions {
    stdio: "pipe" | [any, "pipe", any?];
  }
  interface Inherit extends ExecOptions {
    stdio?: "inherit" | [any, "inherit", any?];
  }
}
/**
 * @example
 *  exec(`
 *    echo "commands are run sequentially" > ${filepath}
 *    cat ${filepath}
 *  `);
 *
 * @returns array of strings when options.stdio is `pipe`, otherwise undefined
 */
export declare function exec(
  commands: string,
  options: ExecOptions.Pipe
): string[];
export declare function exec(
  commands: string,
  options?: ExecOptions.Inherit
): undefined;

Types from @types/node

About

opinionated lighweight wrapper around child_process.execSync

Resources

Stars

Watchers

Forks

Packages

No packages published