Async child process manager providing access to exit code and stdout/stderr
npm install @telefonica/child-process-manager  
 
Async child process manager providing access to exit code and stdout/stderr logs.
- Description
- Installation
- Example
- API
- Contributing
- License
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.
Install the package using npm:
``bash`
npm install @telefonica/child-process-manager
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.
`js title="Example"
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); // 0
`
* __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 is false.cwd
* - string: Current working directory of the child process. Default is process.cwd().spawnOptions
* - object: Options to be passed directly to the cross-spawn library when creating the child process.
$3
* __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 return null as the exit code.
* __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 is null if 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 be undefined` if the process did not start yet.
Please read our Contributing Guidelines for details on how to contribute to this project before submitting a pull request.
This project is licensed under the Apache-2.0 License - see the LICENSE file for details.