1.5.9 • Published 1 year ago

@momsfriendlydevco/exec v1.5.9

Weekly downloads
1
License
MIT
Repository
github
Last release
1 year ago

@momsfriendlydevco/exec

Tiny wrapper around child_process.spawn() / exec() which provides some additional functionality.

Differences from spawn / exec:

  • Always returns a promise for running processes. That promise can return output if buffer is enabled
  • Can easily prefix output to STDOUT / STDERR or both
  • Can accept a single array of command arguments rather than treating the program + args separately
  • Can accept a single string command which is correctly transformed internally
  • Optionally can buffer and provide all output when resolving / rejecting
  • Supports piping
  • Auto-trimming out output
  • Hashbangs supported
  • Can natively accept JSON and fail if not given valid input
  • Support for aliases within commands (and pipes)
var exec = require('@momsfriendlydevco/exec');

exec('echo "Hello world"')
	.then(()=> /* ... */)

exec(['docker', 'build', '--tag=momsfriendlydevco/test', '.'], {
	prefix: '[docker]',
})
	.then(()=> /* ... */)

Debugging

This module uses the Debug NPM package and responds to exec.

To see verbose debugging output simply set DEBUG=exec or any valid glob expression.

> DEBUG=exec node someNodeFile.js

API

This module exposes a function (which returns a promise) as well as a few utility functions. The function takes an array (or string) of arguments where the executable is the first item within that array. An additional options object can be passed.

exec(cmd, <cmd+args|args>, options)

Supported options:

OptionTypeDefaultDescription
bufferbooleanundefinedSet both bufferStdout + bufferStderr at once
bufferStdoutbooleanfalseWhen resolving the promise provide the output from the command as the value of the resolved promise
bufferStderrbooleanfalseAs with bufferStdout but also include STDERR stream data
logboolean or functionundefinedSet both logStdout + logStderr at once
logStdoutboolean or functionconsole.logLogging function to use when outputting STDOUT, set to falsy to disable
logStderrboolean or functionconsole.logSimilar to logStdout but with the STDERR stream
prefixstring or functionundefinedSets both prefixStdout + prefixStderr at once
prefixStdoutstring or functionundefinedSets a string prefix for any STDOUT output, if a function it is called as (msg) and uses the return value, effectively acting as a wrapper
prefixStderrstring or functionundefinedSimilar functionality to prefixStdout but with STDERR streams
rejectErrorstring, boolean or function'Non-zero exit code'If a string use this to signal errors, boolean false returns the error code in the catch() block, if a function that function is called as (code) to return the string to return
reformatbooleanundefinedSet both reformatStdout + reformatStderr at once
reformatStdoutbooleanfalseWhen accepting data from Stdout and prefixStdout is enabled, also split newline input so each newline is prefixed
reformatStderrbooleanfalseAs with reformatStdout but also reformat STDERR stream data
resolveCodesarray[0]Array of numeric error codes to accept as a valid response
jsonbooleanfalseAttempt to convert the contents of the output buffer (contents dictated by buffer* into JSON before returning), implies bufferStdout
jsonInvalidTruncatenumber30When parsing invalid JSON truncate the error output to this many bytes (i.e. not UTF-8 friendly)
jsonInvalidTruncateSuffixstring"…"Suffix to append when truncating invalid JSON input
pipeboolean or string'auto'Use the shell to execute, if 'auto' pipes are detected automatically
shellstring'/bin/sh'A STDIN processing shell, used when pipe is true or auto detected
hashbangbooleantrueIf specified the file is opened and hashbangReadLength bytes examined for a hashbang, if one is found the command is prefixed with it
hashbangReadLengthnumber100How many bytes to explore at the start of files for the hashbang
trimbooleantrueAutomatically trim output to remove trailing newlines and spaces
trimRegExpRegExp/[\n\s]+$/mThe regular expression used when trimming
envobject{}Environment variables to pass to the shell
cwdstringundeinfedThe current working directory to execute the process within
uidnumberundefinedThe UID who owns the process
gidnumberundefinedThe GID who owns the process
aliasobject{}Object list of command aliases
stdinstream.Readable, "inherit", buffer or stringundefinedEither connect the stream to the proess STDIN or feed the given input into STDIN if its a string or buffer

NOTES:

  • Setting log or log{Stdout,Stderr} automatically implies the respective logStdout / logStderr function to be true
  • Setting stdin="inherit" is the equivelent of setting stdin=process.stdin to connect the outer process STDIN pipe to the innner process

exec.defaults

An object containing the default options for exec() which can be globally changed.

exec.split(cmd)

Take a command line and split it into a child_process.spawn() compatible array.

exec.join(args)

Take a disected child_process.spawn() comaptible array and convert it into a single line, runnable shell command.

1.5.9

1 year ago

1.5.8

3 years ago

1.5.7

3 years ago

1.5.6

3 years ago

1.5.5

3 years ago

1.5.4

3 years ago

1.5.3

3 years ago

1.5.2

4 years ago

1.5.1

4 years ago

1.5.0

5 years ago

1.4.3

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago