Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Observable and Promise versions of child_process.spawn

License

NotificationsYou must be signed in to change notification settings

anaisbetts/spawn-rx

Repository files navigation

Linux/OSXWindows
Build StatusBuild status

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 nativelyspawn EXE files, whichmakes executing npm binaries annoying.spawn-rx automatically rewrites yourcmd andargs parameters for CMD scripts, PowerShell scripts, and node.jsfiles.

Examples

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');

What's Jobber?

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.

Spawn output

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.

Stdin support

If you provide anObservable<string> inopts.stdin, it'll be subscribed uponand fed into the child process stdin. Its completion will terminate stdin stream.

Methods

/** * 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

Stars

Watchers

Forks

Packages

No packages published

Contributors7


[8]ページ先頭

©2009-2025 Movatter.jp