- Notifications
You must be signed in to change notification settings - Fork11
Observable and Promise versions of child_process.spawn
License
anaisbetts/spawn-rx
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Linux/OSX | Windows |
---|---|
spawn-rx
is a package that adds an Observable as well as a Promise version ofthechild_process.spawn
API, and fixes some deficiencies inspawn
that comeup especially on Windows. For example:
spawn
searches PATH on POSIX platforms but will not on Windows, you need toprovide an exact path. spawn-rx makes Windows act like other platforms.On Windows,
{detached: true}
doesn't actually create a process group properly.spawn-rx
provides aspawnDetached
method that allows you to spawn a detachedprocess and kill the entire process group if needed.POSIX platforms allow you to directly execute scripts that have a shebang atthe top of the file, whereas Windows can only natively
spawn
EXE files, whichmakes executing npm binaries annoying.spawn-rx
automatically rewrites yourcmd
andargs
parameters for CMD scripts, PowerShell scripts, and node.jsfiles.
spawn-as-promise:
// Will run down path to find C:\Windows\System32\wmic.exe, whereas normal// 'spawn' would require an absolute path.spawnPromise('wmic',[]).then((result)=>console.log(result));
Handle failed processes as errors:
try{awaitspawnPromise('exit',['-1']);}catch(e){console.log("Processes that return non-zero exit codes throw")}
Kill running process trees:
letdisp=spawnDetached('takesALongTime',[]).subscribe();awaitPromise.delay(1000);// Kill the process and its children by unsubscribing.disp.dispose();
Stream process output:
spawn('ls',['-r']).subscribe((x)=>console.log(x),(e)=>console.log("Process exited with an error"));
Execute scripts:
// Executes ./node_modules/.bin/uuid.cmd on Windows if invoked via `npm run`letresult=awaitspawnPromise('uuid');
Jobber is a Windows executable that will execute a command in a process group,and if signaled via a named pipe, will terminate that process group. It's usedin the implementation ofspawnDetached
. Jobber was written by me, and its licenseis the same asspawn-rx
, it is not a third-party dependency.
By default spawn will merge stdout and stderr into the returned observable.You can exclude one or the other by passingignore
in thestdio
option of spawn.
Alternatively if you call it with{ split: true }
option, the observable outputwill be an object{ source: 'stdout', text: '...' }
so you can distinguishthe outputs.
If you provide anObservable<string>
inopts.stdin
, it'll be subscribed uponand fed into the child process stdin. Its completion will terminate stdin stream.
/** * Spawns a process attached as a child of the current process. * *@param {string} exe The executable to run *@param {string[]} params The parameters to pass to the child *@param {SpawnOptions & SpawnRxExtras} opts Options to pass to spawn. * *@return {Observable<OutputLine>} Returns an Observable that when subscribed * to, will create a child process. The * process output will be streamed to this * Observable, and if unsubscribed from, the * process will be terminated early. If the * process terminates with a non-zero value, * the Observable will terminate with onError. */functionspawn(exe:string,params:string[],opts:SpawnOptions&SpawnRxExtras&{split:true}):Observable<OutputLine>;functionspawn(exe:string,params:string[],opts?:SpawnOptions&SpawnRxExtras&{split:false|undefined}):Observable<string>;/** * Spawns a process but detached from the current process. The process is put * into its own Process Group. * *@param {string} exe The executable to run *@param {string[]} params The parameters to pass to the child *@param {SpawnOptions & SpawnRxExtras} opts Options to pass to spawn. * *@return {Observable<OutputLine>} Returns an Observable that when subscribed * to, will create a detached process. The * process output will be streamed to this * Observable, and if unsubscribed from, the * process will be terminated early. If the * process terminates with a non-zero value, * the Observable will terminate with onError. */functionspawnDetached(exe:string,params:string[],opts:SpawnOptions&SpawnRxExtras&{split:true}):Observable<OutputLine>;functionspawnDetached(exe:string,params:string[],opts?:SpawnOptions&SpawnRxExtras&{split:false|undefined}):Observable<string>;/** * Spawns a process as a child process. * *@param {string} exe The executable to run *@param {string[]} params The parameters to pass to the child *@param {SpawnOptions & SpawnRxExtras} opts Options to pass to spawn. * *@return {Promise<[string, string]>} Returns a Promise that represents a child * process. The value returned is the process * output. If the process terminates with a * non-zero value, the Promise will resolve with * an Error. */functionspawnPromise(exe:string,params:string[],opts:SpawnOptions&SpawnRxExtras&{split:true}):Promise<[string,string]>;functionspawnPromise(exe:string,params:string[],opts?:SpawnOptions&SpawnRxExtras):Promise<string>;/** * Spawns a process but detached from the current process. The process is put * into its own Process Group. * *@param {string} exe The executable to run *@param {string[]} params The parameters to pass to the child *@param {SpawnOptions & SpawnRxExtras} opts Options to pass to spawn. * *@return {Promise<[string, string]>} Returns a Promise that represents a detached * process. The value returned is the process * output. If the process terminates with a * non-zero value, the Promise will resolve with * an Error. */functionspawnDetachedPromise(exe:string,params:string[],opts:SpawnOptions&SpawnRxExtras&{split:true}):Promise<[string,string]>;functionspawnDetachedPromise(exe:string,params:string[],opts?:SpawnOptions&SpawnRxExtras):Promise<string>;/** * Finds the actual executable and parameters to run on Windows. This method * mimics the POSIX behavior of being able to run scripts as executables by * replacing the passed-in executable with the script runner, for PowerShell, * CMD, and node scripts. * * This method also does the work of running down PATH, which spawn on Windows * also doesn't do, unlike on POSIX. * *@param {string} exe The executable to run *@param {string[]} args The arguments to run * *@return {Object} The cmd and args to run *@property {string} cmd The command to pass to spawn *@property {string[]} args The arguments to pass to spawn */functionfindActualExecutable(exe:string,args:string[]):{cmd:string;args:string[];};
About
Observable and Promise versions of child_process.spawn
Resources
License
Code of conduct
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors7
Uh oh!
There was an error while loading.Please reload this page.