Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/artdecocode/spawncommand
Spawn a ChildProcess with a promise property resolved on exit with stdout, stderr and code.
https://github.com/artdecocode/spawncommand
async child-process fork javascript node nodejs promise spawn
Last synced: 5 days ago
JSON representation
Spawn a ChildProcess with a promise property resolved on exit with stdout, stderr and code.
- Host: GitHub
- URL: https://github.com/artdecocode/spawncommand
- Owner: artdecocode
- License: mit
- Created: 2017-02-04T03:27:52.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-04-25T17:26:03.000Z (over 5 years ago)
- Last Synced: 2024-09-15T02:13:25.603Z (2 months ago)
- Topics: async, child-process, fork, javascript, node, nodejs, promise, spawn
- Language: JavaScript
- Homepage: https://nodejs.tools
- Size: 369 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# spawnCommand
[![npm version](https://badge.fury.io/js/spawncommand.svg)](https://npmjs.org/package/spawncommand)
_SpawnCommand_ will run the `spawn` or `fork` methods from the `child_process` module, and add a `promise` property to the returned process instance. The promise will be resolved on process exit with an object consisting of `code`, `stdout` and `stderr` properties.
```
yarn add spawncommand
```## Table Of Contents
- [Table Of Contents](#table-of-contents)
- [API](#api)
* [Types](#types)
* [`_spawncommand.ChildProcessWithPromise`](#type-_spawncommandchildprocesswithpromise)
* [`_spawncommand.PromiseResult`](#type-_spawncommandpromiseresult)
* [`spawn(module: string, args: string[], options?: SpawnOptions): ChildProcessWithPromise`](#spawnmodule-stringargs-stringoptions-spawnoptions-childprocesswithpromise)
* [`fork(module: string, args: string[], options?: ForkOptions): ChildProcessWithPromise`](#forkmodule-stringargs-stringoptions-forkoptions-childprocesswithpromise)
- [Copyright](#copyright)## API
_SpawnCommand_ can be used by importing the default `spawn` and named `fork` exports.
```js
import spawn, { fork } from 'spawncommand'
```### Types
The package's main type is _ChildProcessWithPromise_ which enriches the standard _ChildProcess_ with a `promise` property.
[`import('child_process').ChildProcess`](https://nodejs.org/api/child_process.html#child_process_class_childprocess) __`child_process.ChildProcess`__
__`_spawncommand.ChildProcessWithPromise`__: A child process with an extra `promise` property.
| Name | Type | Description |
| ----------------- | ----------------------------------------------------- | ------------------------------------------ |
| __promise*__ | !Promise<!_spawncommand.PromiseResult> | A promise resolved when the process exits. |
| __spawnCommand*__ | string | The spawn arguments joined by whitespace. |__`_spawncommand.PromiseResult`__
| Name | Type | Description |
| ----------- | --------------- | ---------------------------------------------- |
| __stdout*__ | string | The accumulated result of the `stdout` stream. |
| __stderr*__ | string | The accumulated result of the `stderr` stream. |
| __code*__ | number | The code with which the process exited. |### `spawn(`
`module: string,`
`args: string[],`
`options?: SpawnOptions,`
`): ChildProcessWithPromise`Spawns a command and returns a _ChildProcess_ instance with the `promise` property resolved on exit. The promise will be rejected if an error was encountered when trying to spawn the process.
`import('child_process').SpawnOptions` __`child_process.SpawnOptions`__
```js
import spawn from 'spawncommand'(async () => {
const { promise } = spawn('echo', ['hello world'])
const { stderr, stdout, code } = await promise
console.log(JSON.stringify({
stderr, stdout, code,
}, null, 2))
})()
```
```json5
{
"stderr": "",
"stdout": "hello world\n",
"code": 0
}
```The returned object is a `ChildProcess` and all of its properties can be accessed in the standard way.
```js
import spawnCommand from 'spawncommand'(async () => {
const { stdout, promise } = spawnCommand('echo', ['hello world'])stdout.pipe(process.stdout)
await promise
})()
```
```
hello world
```### `fork(`
`module: string,`
`args: string[],`
`options?: ForkOptions,`
`): ChildProcessWithPromise`Forks a Node.js module and adds a `promise` property to the returned _ChildProcess_.
`import('child_process').ForkOptions` __`child_process.ForkOptions`__
```js
import { fork } from 'spawncommand'(async () => {
const { promise } = fork('node_modules/.bin/alanode',
['example/spawn.js'], {
stdio: 'pipe',
})
const { stdout } = await promise
console.log(stdout)
})()
```
```json5
{
"stderr": "",
"stdout": "hello world\n",
"code": 0
}
```The `pipe` option needs to be set in order to gather the output of the `stderr` and `stdout` streams (or an array for older versions of Node.js when [this does not work][2]).
## Copyright
© Art Deco 2019
Tech Nation Visa Sucks
[2]: https://github.com/nodejs/node/pull/10866