2.0.0 • Published 8 months ago

@zhangyunjie/shell v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

shell node version Build Status npm version

Simple exec of shell commands in node

const { shell } = require('@zhangyunjie/shell')

shell('touch somefile.js')
shell('http-server', { async: true })

shell(cmd, options)

Runs given command as a child process. Returns output of executed command.

const { shell } = require('@zhangyunjie/shell')

Options:

interface IShellOptions {
  // current working directory
  cwd?: string

  // environment key-value pairs
  env?: NodeJS.ProcessEnv

  // timeout after which execution will be cancelled
  timeout?: number

  // default: false, if true it runs command asynchronously and returns a Promise
  async?: boolean

  // if true, it will send output directly to parent process (stdio="inherit"), it won't return the output though
  // usefull if default piping strips too much colours when printing to the terminal
  // if enabled, transform option won't work
  nopipe?: boolean

  // if true, it won't print anything to the terminal but it will still return the output as a string
  silent?: boolean

  // function which allows to transform the output, line by line
  // usefull for adding prefixes to async commands output
  transform?: (output: string) => string
}

prefixTransform(prefix)

Transform function which can be used for transform option.

Example:

const { shell, prefixTransform } = require('@pawelgalazka/shell')

shell('echo "test"', { transform: prefixTransform('[prefix]') })
$ node ./script.js
echo "test"
[prefix] test