Skip to content

Commit 6178fc8

Browse files
authored
docs: clarify within(), cd() and syncProcessCwd relations (#878)
1 parent 4ab61e8 commit 6178fc8

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

api.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ trailing newlines from `ProcessOutput` enabling common idioms like:
9797
cd(await $`mktemp -d`)
9898
```
9999

100+
> ⚠️ `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.
101+
100102
## fetch()
101103

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

149151
```js
150152
await $`pwd` // => /home/path
153+
$.foo = 'bar'
151154

152155
within(async () => {
153-
cd('/tmp')
156+
$.cwd = '/tmp'
157+
$.foo = 'baz'
154158

155159
setTimeout(async () => {
156160
await $`pwd` // => /tmp
161+
$.foo // baz
157162
}, 1000)
158163
})
159164

160165
await $`pwd` // => /home/path
166+
$.foo // still 'bar'
161167
```
162168

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

181+
## syncProcessCwd()
182+
183+
Keeps the `process.cwd()` in sync with the internal `$` current working directory if it is changed via [cd()](#cd).
184+
185+
```ts
186+
import {syncProcessCwd} from 'zx'
187+
188+
syncProcessCwd()
189+
syncProcessCwd(false) // pass false to disable the hook
190+
```
191+
192+
> This feature is disabled by default because of performance overhead.
193+
175194
## retry()
176195

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

0 commit comments

Comments
 (0)