Skip to content

Commit

Permalink
feat: introduce experimental withTimeout helper
Browse files Browse the repository at this point in the history
closes #349
  • Loading branch information
antongolub committed Mar 18, 2022
1 parent 20ea621 commit f586d06
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -408,6 +408,16 @@ await $`long-running command`
stop()
```
#### `withTimeout()`
Runs and sets a timeout for a cmd.
```js
import {withTimeout} from 'zx/experimental'

await withTimeout(100, 'SIGTERM')`sleep 9999`
```
### FAQ
#### Passing env variables
Expand Down
9 changes: 4 additions & 5 deletions src/experimental.d.ts
Expand Up @@ -12,18 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {ProcessOutput} from './index'
import {ZxTemplate} from './index'

interface Echo {
(pieces: TemplateStringsArray, ...args: any[]): void
(...args: any[]): void
}
export const echo: Echo

interface Retry {
(pieces: TemplateStringsArray, ...args: any[]): Promise<ProcessOutput>
}
export const retry: (count?: number, delay?: number) => Retry
export const retry: (count?: number, delay?: number) => ZxTemplate

export const withTimeout: (delay?: number, signal?: string | number) => ZxTemplate

type StopSpinner = () => void
export function startSpinner(title: string): StopSpinner
10 changes: 10 additions & 0 deletions src/experimental.mjs
Expand Up @@ -25,6 +25,16 @@ export const retry = (count = 5, delay = 0) => async (cmd, ...args) => {
}
}

// Runs and sets a timeout for a cmd
export const withTimeout = (timeout, signal) => async (cmd, ...args) => {
let p = $(cmd, ...args)
if (!timeout) return p

let timer = setTimeout(() => p.kill(signal), timeout)

return p.finally(() => clearTimeout(timer))
}

// A console.log() alternative which can take ProcessOutput.
export function echo(pieces, ...args) {
if (Array.isArray(pieces) && pieces.every(isString) && pieces.length - 1 === args.length) {
Expand Down
4 changes: 3 additions & 1 deletion src/index.d.ts
Expand Up @@ -23,9 +23,11 @@ import * as _yaml from 'yaml'
import _fetch from 'node-fetch'
import {ParsedArgs} from 'minimist'

interface $ {
export interface ZxTemplate {
(pieces: TemplateStringsArray, ...args: any[]): ProcessPromise<ProcessOutput>
}

interface $ extends ZxTemplate {
verbose: boolean
shell: string
prefix: string
Expand Down
15 changes: 14 additions & 1 deletion test.mjs
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

import {strict as assert} from 'assert'
import {retry} from './src/experimental.mjs'
import {retry, withTimeout} from './src/experimental.mjs'

let всегоТестов = 0

Expand Down Expand Up @@ -257,6 +257,19 @@ if (test('Retry works')) {
assert(Date.now() >= now + 50 * (5 - 1))
}

if (test('withTimeout works')) {
let exitCode = 0
let signal
try {
await withTimeout(100, 'SIGKILL')`sleep 9999`
} catch (p) {
exitCode = p.exitCode
signal = p.signal
}
assert.equal(exitCode, null)
assert.equal(signal, 'SIGKILL')
}

let version
if (test('require() is working in ESM')) {
let data = require('./package.json')
Expand Down

0 comments on commit f586d06

Please sign in to comment.