process-promises
Node child processes as promises
Installation
npm install process-promises --save
exec
Runs a command in a shell and buffers the output.
Usage - TypeScript
import { exec } from 'process-promises';
exec('node ./node_modules/gulp/bin/gulp.js default')
.on('process', process => console.log('Pid: ', process.pid))
.then(result => {
console.log('stdout: ', result.stdout);
console.log('stderr: ', result.stderr);
})
.fail(err => {
console.error('ERROR: ', err);
});
Usage - JavaScript
var exec = require('process-promises').exec;
exec('node ./node_modules/gulp/bin/gulp.js default')
.on('process', function(process) {
console.log('Pid: ', process.pid);
})
.then(function (result) {
console.log('stdout: ', result.stdout);
console.log('stderr: ', result.stderr);
})
.fail(function (err) {
console.error('ERROR: ', err);
});
Syntax
function exec(command: string, options?: ExecOptions): PromiseWithEvents<ExecResult>;
interface ExecOptions {
cwd?: string;
env?: any;
encoding?: string;
shell?: string;
timeout?: number;
maxBuffer?: number;
killSignal?: string;
}
interface ExecResult {
stdout: string;
stderr: string;
}
execFile
This is similar to exec() except it does not execute a subshell but rather the specified
file directly. This makes it slightly leaner than exec().
Usage - TypeScript
import { execFile } from 'process-promises';
execFile(process.execPath, ['./node_modules/gulp/bin/gulp.js', 'default'])
.on('process', process => console.log('Pid: ', process.pid))
.then(result => {
console.log('stdout: ', result.stdout);
console.log('stderr: ', result.stderr);
})
.fail(err => {
console.error('ERROR: ', err);
});
Usage - JavaScript
var execFile = require('process-promises').execFile;
execFile(process.execPath, ['./node_modules/gulp/bin/gulp.js', 'default'])
.on('process', function(process) {
console.log('Pid: ', process.pid);
})
.then(function (result) {
console.log('stdout: ', result.stdout);
console.log('stderr: ', result.stderr);
})
.fail(function (err) {
console.error('ERROR: ', err);
});
Syntax
function execFile(file: string, options?: ExecFileOptions): PromiseWithEvents<ExecResult>;
function execFile(file: string, args?: string[], options?: ExecFileOptions): PromiseWithEvents<ExecResult>;
interface ExecFileOptions {
cwd?: string;
env?: any;
encoding?: string;
timeout?: number;
maxBuffer?: number;
killSignal?: string;
}
interface ExecResult {
stdout: string;
stderr: string;
}
spawn
Launches a new process with the given command, with command line arguments in args. If
omitted, args defaults to an empty Array.
Usage - TypeScript
import { spawn } from 'process-promises';
spawn(process.execPath, ['./node_modules/gulp/bin/gulp.js', 'default'])
.on('process', process => console.log('Pid: ', process.pid))
.on('stdout', line => console.log('stdout: ', line))
.on('stderr', line => console.log('stderr: ', line))
.then(result => {
console.log('Exit code: ' + result.exitCode);
})
.fail(err => {
console.error('ERROR: ', err);
});
Usage - JavaScript
var execFile = require('process-promises').execFile;
spawn(process.execPath, ['./node_modules/gulp/bin/gulp.js', 'default'])
.on('process', function(process) {
console.log('Pid: ', process.pid);
})
.on('stdout', function(line) {
console.log('stdout: ', line);
})
.on('stderr', function(line) {
console.log('stdout: ', line);
})
.then(function (result) {
console.log('Exit code: ' + result.exitCode);
})
.fail(function (err) {
console.error('ERROR: ', err);
});
Syntax
function spawn(command: string, options?: SpawnOptions): PromiseWithEvents<SpawnResult>;
function spawn(command: string, args?: string[], options?: SpawnOptions): PromiseWithEvents<SpawnResult>;
interface SpawnOptions {
cwd?: string;
env?: any;
stdio?: string | [
string | NodeJS.WritableStream | number,
string | NodeJS.ReadableStream | number,
string | NodeJS.ReadableStream | number
];
detached?: boolean;
}
interface SpawnResult {
exitCode: number;
}
fork
Launches a new node process with the given module, with command line arguments in args. If
omitted, args defaults to an empty Array.
The module is resolved using the node require.resolve() algorithm
This function calls spawn - not child_process.fork
Usage - TypeScript
import { fork } from 'process-promises';
fork('gulp/bin/gulp.js', ['default'])
.on('process', process => console.log('Pid: ', process.pid))
.on('stdout', line => console.log('stdout: ', line))
.on('stderr', line => console.log('stderr: ', line))
.then(result => {
console.log('Exit code: ' + result.exitCode);
})
.fail(err => {
console.error('ERROR: ', err);
});
Usage - JavaScript
var execFile = require('process-promises').execFile;
fork('gulp/bin/gulp.js', ['default'])
.on('process', function(process) {
console.log('Pid: ', process.pid);
})
.on('stdout', function(line) {
console.log('stdout: ', line);
})
.on('stderr', function(line) {
console.log('stdout: ', line);
})
.then(function (result) {
console.log('Exit code: ' + result.exitCode);
})
.fail(function (err) {
console.error('ERROR: ', err);
});
Syntax
function fork(modulePath: string, options?: SpawnOptions): PromiseWithEvents<SpawnResult>;
function fork(modulePath: string, args?: string[], options?: SpawnOptions): PromiseWithEvents<SpawnResult>;
interface SpawnOptions {
cwd?: string;
env?: any;
stdio?: string | [
string | NodeJS.WritableStream | number,
string | NodeJS.ReadableStream | number,
string | NodeJS.ReadableStream | number
];
detached?: boolean;
}
interface SpawnResult {
exitCode: number;
}