https://github.com/ksxnodemodules/advanced-spawn-async
Advanced isomorphic asynchronous spawn function
https://github.com/ksxnodemodules/advanced-spawn-async
async asynchronous child-process isomorphic javascript nodejs spawn typescript
Last synced: 25 days ago
JSON representation
Advanced isomorphic asynchronous spawn function
- Host: GitHub
- URL: https://github.com/ksxnodemodules/advanced-spawn-async
- Owner: ksxnodemodules
- License: mit
- Created: 2018-09-25T11:47:47.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-09T13:00:37.000Z (over 3 years ago)
- Last Synced: 2024-04-25T04:02:50.486Z (about 2 years ago)
- Topics: async, asynchronous, child-process, isomorphic, javascript, nodejs, spawn, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/advanced-spawn-async
- Size: 262 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
# advanced-spawn-async
Advanced isomorphic asynchronous spawn function
## Requirements
* Node.js ≥ 8.9.0
## Usage
### Basic Usage
Usage without custom `spawn` function (i.e. Use built-in `child_process.spawn`).
```javascript
import spawn from 'advanced-spawn-async'
const {
process, // ChildProcess
onclose, // Promise<{ command, args, options, stdout, stderr, output, status, signal, process }>
onexit // Promise<{ command, args, options, stdout, stderr, output, status, signal, process }>
} = spawn('node')
process.stdin.write('console.info("stdout")\n')
process.stdin.write('console.error("stderr")\n')
process.stdin.end('require("process").exit(0)\n')
onclose.then(({ stdout, stderr, output }) => {
console.log('CLOSE', { stdout, stderr, output })
})
onexit.then(({ stdout, stderr, output }) => {
console.log('EXIT', { stdout, stderr, output })
})
```
**Sample Output:**
```javascript
EXIT {
stdout: 'stdout\n',
stderr: 'stderr\n',
output: 'stdout\nstderr\n'
}
CLOSE {
stdout: 'stdout\n',
stderr: 'stderr\n',
output: 'stdout\nstderr\n'
}
```
### Custom Spawn Function
User provide custom spawn function.
```javascript
import { core } from 'advanced-spawn-async'
import spawn from 'cross-spawn'
const { process, onclose, onexit } = core(spawn, 'node')
// The rest is like the above example
```
### Provide Arguments and Options
```javascript
import spawn from 'advanced-spawn-async'
const { onclose } = spawn('echo', ['hello'], { stdio: 'inherit', event: 'close' })
onclose.then(status => console.log({status}))
```
### Non-Zero Status Code
When process returns non-zero code.
```javascript
import spawn from 'advanced-spawn-async'
const { process, onclose } = spawn('node', [], { event: 'close' })
process.stdin.end('process.exit(123)\n')
onclose.catch(error => console.log(error.info))
```
## License
[MIT](https://git.io/fAAID) © [Hoàng Văn Khải](https://github.com/KSXGitHub)