1.0.0 • Published 1 year ago
@xscale/exec-sh v1.0.0
exec-sh: A simple utility for executing shell commands in Node.js
This package provides a convenient way to execute shell commands from your Node.js applications. It handles capturing standard output (stdout) and standard error (stderr) while ensuring proper shell handling for both Windows and POSIX environments.
Installation
yarn add exec-sh
Usage
The execSh
function takes two arguments:
- command (string | string[]): The shell command(s) to execute. You can provide a single string or an array of strings for multiple commands.
- options (SpawnOptionsWithoutStdio, optional): An optional object containing additional options to be passed to the
child_process.spawn
function.
The function returns a promise that resolves to an object with the following properties:
- stdout (string, optional): The captured standard output of the command.
- stderr (string, optional): The captured standard error of the command.
- error (Error, optional): An error object if the command execution fails.
Example:
import { execSh } from 'exec-sh'
async function main() {
try {
const result = await execSh('ls -l /tmp')
console.log(result.stdout)
} catch (error) {
console.error(error)
}
}
main()
This example executes the ls -l /tmp
command and logs its standard output to the console. In case of any errors during execution, it will catch and log the error object.
Platform Compatibility
exec-sh
automatically detects the platform and uses the appropriate shell for execution.
- Windows:
cmd
with/C
argument - POSIX:
sh
with-c
argument
License
This package is licensed under the MIT License. See the LICENSE file for details.
1.0.0
1 year ago