Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: clarify within(), cd() and syncProcessCwd relations #878

Merged
merged 1 commit into from
Aug 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion api.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ trailing newlines from `ProcessOutput` enabling common idioms like:
cd(await $`mktemp -d`)
```

> ⚠️ `cd` invokes `process.chdir()` internally, so it does affect the global context. To keep `process.cwd()` in sync with separate `$` calls enable [syncProcessCwd()](#syncprocesscwd) hook.

## fetch()

A wrapper around the [node-fetch-native](https://www.npmjs.com/package/node-fetch-native)
Expand Down Expand Up @@ -148,16 +150,20 @@ Creates a new async context.

```js
await $`pwd` // => /home/path
$.foo = 'bar'

within(async () => {
cd('/tmp')
$.cwd = '/tmp'
$.foo = 'baz'

setTimeout(async () => {
await $`pwd` // => /tmp
$.foo // baz
}, 1000)
})

await $`pwd` // => /home/path
$.foo // still 'bar'
```

```js
Expand All @@ -172,6 +178,19 @@ const version = await within(async () => {
echo(version) // => v16.20.0
```

## syncProcessCwd()

Keeps the `process.cwd()` in sync with the internal `$` current working directory if it is changed via [cd()](#cd).

```ts
import {syncProcessCwd} from 'zx'

syncProcessCwd()
syncProcessCwd(false) // pass false to disable the hook
```

> This feature is disabled by default because of performance overhead.

## retry()

Retries a callback for a few times. Will return after the first
Expand Down