Commit 6178fc8

Anton Golub <antongolub@antongolub.com>
2024-08-25 18:52:58
docs: clarify `within()`, `cd()` and `syncProcessCwd` relations (#878)
1 parent 4ab61e8
Changed files (1)
api.md
@@ -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)
@@ -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
@@ -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