@telefonica/child-process-manager
v2.0.1
Published
Async child process manager providing access to exit code and stdout/stderr
Downloads
37
Readme
child-process-manager
Async child process manager providing access to exit code and stdout/stderr logs.
Table of Contents
Description
This library enables to create a child process using cross-spawn, which provides compatibility across different operating systems, and manage it asynchronously. It mainly provides:
- Enables to manage child processes as promises. The promise is resolved with the exit code and logs when the process finishes.
- Method to kill the child process at any time. It returns a promise that is resolved when the process is killed.
- Method to get the logs of the child process at any moment. It returns an array with the stdout/stderr of the process until that moment.
It is useful to execute shell commands from tests and check their output, for example.
Installation
Install the package using npm:
npm install @telefonica/child-process-managerExample
Import the library, create a child process and wait for it to finish. It will return the exit code and the array of logs in the resolved object.
import { ChildProcessManager } from '@telefonica/child-process-manager';
const childProcess = new ChildProcessManager(["echo", 'Hello world!']);
const { logs, exitCode } = await childProcess.run();
console.log(logs); // ["Hello world!"]
console.log(exitCode); // 0API
Constructor
ChildProcessManager(command, options?, spawnOptions?): ChildProcessManager: Creates a new child process manager with the given command and options.command-string[]: Command to be executed, and its arguments.options-object: Object containing next properties:env-object: Environment key-value pairs to be added to the child process.silent-boolean: If true, the child process will not output anything to the console (but it will still be stored in the logs). Default isfalse.cwd-string: Current working directory of the child process. Default isprocess.cwd().
spawnOptions-object: Options to be passed directly to thecross-spawnlibrary when creating the child process.
Methods
run(): Promise<{ logs: string[], exitCode: number }>: Runs the child process and returns a promise that resolves with the logs and exit code when the process finishes.kill(): Promise<{ logs: string[], exitCode: number | null }>: Kills the child process and returns a promise that resolves when the process is killed. In case the process did not start yet, it will returnnullas the exit code.
Properties
logs: string[]: Array containing the logs of the child process. It is updated in real-time as the process outputs data.exitCode: number | null: Exit code of the child process. It isnullif the process did not finish yet.exitPromise: Promise that resolves when the child process finishes. It is resolved with the logs and exit code. It will beundefinedif the process did not start yet.
Contributing
Please read our Contributing Guidelines for details on how to contribute to this project before submitting a pull request.
License
This project is licensed under the Apache-2.0 License - see the LICENSE file for details.
