exec-utils
A modern, promise-based utility package for Node.js that provides enhanced child process execution with robust error handling, timeouts, and input streaming support.
Description
exec-utils wraps Node.js child process functions with additional features that make command execution more reliable and convenient:
- Promise-based API - Clean async/await pattern for process execution
- Timeout support - Automatically kill long-running processes
- AbortSignal integration - Cancel operations from outside
- Input streaming - Easily pipe data to child processes
- Unified error handling - Consistent error format with exit codes
- Buffer size limits - Prevent memory issues with large outputs
- Configurable encoding - Control output encoding
- Typescript support - Full type definitions included
Installation
NPM
npm i exec-utils
GitHub
npm i https://github.com/lacherogwu/exec-utils
Requirements
Usage
Basic Usage
import { spawn, exec } from 'exec-utils';
const { data } = await spawn('echo', ['Hello', 'World']);
console.log(data);
const { data: execData } = await exec('echo "Hello World"');
console.log(execData);
With Timeout
const { error } = await spawn('sleep', ['10'], { timeout: 5000 });
if (error) {
console.log(error.message);
console.log(error.code);
}
With Input
const { data } = await spawn('grep', ['good'], {
input: 'no errors here\nthis line has an error\nall good',
});
console.log(data);
const { data: jsonData } = await spawn('jq', ['.name'], {
input: JSON.stringify({ name: 'John', age: 30 }),
});
console.log(jsonData.trim());
With AbortController
const controller = new AbortController();
const { signal } = controller;
const processPromise = spawn('sleep', ['10'], { signal });
setTimeout(() => {
controller.abort();
}, 2000);
const { error } = await processPromise;
if (error) {
console.log(error.message);
console.log(error.code);
}
Error Handling
const { error, data } = await spawn('ls', ['non-existent-directory']);
if (error) {
console.error(`Command failed with code ${error.code}`);
console.error(error.message);
} else {
console.log(data);
}
API
spawn(command: string, args: string[], options?: SpawnOptions): Promise<CommandResult>
Executes a command with arguments.
- Parameters:
command
: Command to execute
args
: Array of arguments
options
: Optional configuration object
- Returns:
- Promise resolving to a
CommandResult
object
exec(command: string, options?: ExecOptions): Promise<CommandResult>
Executes a shell command.
- Parameters:
command
: Shell command to execute
options
: Optional configuration object
- Returns:
- Promise resolving to a
CommandResult
object
Options
interface SpawnOptions {
timeout?: number;
maxBuffer?: number;
encoding?: BufferEncoding;
signal?: AbortSignal;
input?: string | Buffer | NodeJS.ReadableStream;
}
interface ExecOptions {
}
Result Object
type CommandResult =
| {
data: string;
dataAsBuffer: Buffer;
error: null;
process: ChildProcess;
}
| {
data: null;
dataAsBuffer: null;
error: ExecUtilsError;
process: ChildProcess;
};
License
MIT