dx icon indicating copy to clipboard operation
dx copied to clipboard

support "await $`command`.exitCode" without try/catch

Open alsuren opened this issue 4 years ago • 1 comments

The zx docs give this example:

if (await $`[[ -d path ]]`.exitCode == 0) {
  ...
}

https://github.com/google/zx#nothrow

This is implemented by making the return value of $`` be a ProcessPromise<ProcessOutput> rather than a normal promise.

This is described as

class ProcessPromise<T> extends Promise<T> {
  readonly stdin: Writable
  readonly stdout: Readable
  readonly stderr: Readable
  readonly exitCode: Promise<number>
  pipe(dest): ProcessPromise<T>
  kill(signal = 'SIGTERM'): Promise<void>
}

-- https://github.com/google/zx#processpromise

Adding something like ProcessPromise would also allow piping into/out of processes. This should probably happen later though.

alsuren avatar Sep 09 '21 11:09 alsuren

I have add nothow() function as following

if( (await nothrow($`lll -al`)).exitCode > 0 ) { 
   console.log("Failed")
}

I'm not sure to adopt ProcessOutput as result for $, and now the result is stdout string, and it's behavior is like sub shell output=$(ls -al).

in zx, the result is ProcessOutput and not string.

    const result: ProcessOutput = await $`ls -al`

I have change return type of $o to ProcessOutput, and you can use $o to check exitCode as following:

if( (await $o`lll -al`).exitCode > 0 ) {
   console.log("Failed")
}

More feedback is welcome.

linux-china avatar Sep 26 '21 03:09 linux-china