Skip to content

Commit 81a3940

Browse files
authored
docs: describe $.defaults and shell setup helpers, mention asyncIterator, align headers formatting (google#1091)
1 parent a94559d commit 81a3940

File tree

4 files changed

+147
-61
lines changed

4 files changed

+147
-61
lines changed

docs/api.md

+71-29
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# API Reference
22

3-
## $.sync
3+
## `$.sync`
44
Zx provides both synchronous and asynchronous command executions, returns [`ProcessOutput`](./process-output) or [`ProcessPromise`](./process-promise) respectively.
55

66
```js
77
const list = await $`ls -la`
88
const dir = $.sync`pwd`
99
```
1010

11-
## $({...})
11+
## `$({...})`
1212

1313
`$` object holds the default zx [configuration](./configuration), which is used for all execution. To specify a custom preset use `$` as factory:
1414

@@ -36,7 +36,7 @@ const $3 = $({ sync: true })({ nothrow: true })
3636
assert.equal($3`exit 3`.exitCode, 3)
3737
```
3838

39-
### $({input})
39+
### `$({input})`
4040

4141
The input option passes the specified `stdin` to the command.
4242

@@ -48,7 +48,7 @@ const p4 = $({ input: p3 })`cat`
4848
const p5 = $({ input: await p3 })`cat`
4949
```
5050

51-
### $({signal})
51+
### `$({signal})`
5252

5353
The signal option makes the process abortable.
5454

@@ -59,7 +59,7 @@ const p = $({ signal })`sleep 9999`
5959
setTimeout(() => signal.abort('reason'), 1000)
6060
```
6161

62-
### $({timeout})
62+
### `$({timeout})`
6363

6464
The timeout option makes the process autokillable after the specified delay.
6565

@@ -97,8 +97,9 @@ interface Options {
9797
halt: boolean
9898
}
9999
```
100+
See also [Configuration](./configuration).
100101

101-
## cd()
102+
## `cd()`
102103

103104
Changes the current working directory.
104105

@@ -116,7 +117,7 @@ cd(await $`mktemp -d`)
116117

117118
> ⚠️ `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.
118119
119-
## fetch()
120+
## `fetch()`
120121

121122
A wrapper around the [node-fetch-native](https://www.npmjs.com/package/node-fetch-native)
122123
package.
@@ -125,23 +126,23 @@ package.
125126
const resp = await fetch('https://medv.io')
126127
```
127128

128-
## question()
129+
## `question()`
129130

130131
A wrapper around the [readline](https://nodejs.org/api/readline.html) package.
131132

132133
```js
133134
const bear = await question('What kind of bear is best? ')
134135
```
135136

136-
## sleep()
137+
## `sleep()`
137138

138139
A wrapper around the `setTimeout` function.
139140

140141
```js
141142
await sleep(1000)
142143
```
143144

144-
## echo()
145+
## `echo()`
145146

146147
A `console.log()` alternative which can take [ProcessOutput](#processoutput).
147148

@@ -153,15 +154,15 @@ echo`Current branch is ${branch}.`
153154
echo('Current branch is', branch)
154155
```
155156

156-
## stdin()
157+
## `stdin()`
157158

158159
Returns the stdin as a string.
159160

160161
```js
161162
const content = JSON.parse(await stdin())
162163
```
163164

164-
## within()
165+
## `within()`
165166

166167
Creates a new async context.
167168

@@ -195,7 +196,7 @@ const version = await within(async () => {
195196
echo(version) // => v16.20.0
196197
```
197198

198-
## syncProcessCwd()
199+
## `syncProcessCwd()`
199200

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

@@ -208,7 +209,7 @@ syncProcessCwd(false) // pass false to disable the hook
208209

209210
> This feature is disabled by default because of performance overhead.
210211
211-
## retry()
212+
## `retry()`
212213

213214
Retries a callback for a few times. Will return after the first
214215
successful attempt, or will throw after specifies attempts count.
@@ -223,7 +224,7 @@ const p = await retry(20, '1s', () => $`curl https://medv.io`)
223224
const p = await retry(30, expBackoff(), () => $`curl https://medv.io`)
224225
```
225226

226-
## spinner()
227+
## `spinner()`
227228

228229
Starts a simple CLI spinner.
229230

@@ -236,15 +237,15 @@ await spinner('working...', () => $`sleep 99`)
236237

237238
And it's disabled for `CI` by default.
238239

239-
## glob()
240+
## `glob()`
240241

241242
The [globby](https://github.com/sindresorhus/globby) package.
242243

243244
```js
244245
const packages = await glob(['package.json', 'packages/*/package.json'])
245246
```
246247

247-
## which()
248+
## `which()`
248249

249250
The [which](https://github.com/npm/node-which) package.
250251

@@ -258,7 +259,7 @@ If nothrow option is used, returns null if not found.
258259
const pathOrNull = await which('node', { nothrow: true })
259260
```
260261

261-
## ps()
262+
## `ps`
262263

263264
The [@webpod/ps](https://github.com/webpod/ps) package to provide a cross-platform way to list processes.
264265

@@ -269,7 +270,7 @@ const children = await ps.tree({ pid: 123 })
269270
const fulltree = await ps.tree({ pid: 123, recursive: true })
270271
```
271272

272-
## kill()
273+
## `kill()`
273274

274275
A process killer.
275276

@@ -278,7 +279,7 @@ await kill(123)
278279
await kill(123, 'SIGKILL')
279280
```
280281

281-
## tmpdir()
282+
## `tmpdir()`
282283

283284
Creates a temporary directory.
284285

@@ -287,7 +288,7 @@ t1 = tmpdir() // /os/based/tmp/zx-1ra1iofojgg/
287288
t2 = tmpdir('foo') // /os/based/tmp/zx-1ra1iofojgg/foo/
288289
```
289290

290-
## tmpfile()
291+
## `tmpfile()`
291292

292293
Temp file factory.
293294

@@ -298,15 +299,15 @@ f3 = tmpfile('f3.txt', 'string or buffer')
298299
f4 = tmpfile('f4.sh', 'echo "foo"', 0o744) // executable
299300
```
300301

301-
## minimist
302+
## `minimist`
302303

303304
The [minimist](https://www.npmjs.com/package/minimist) package.
304305

305306
```js
306307
const argv = minimist(process.argv.slice(2), {})
307308
```
308309

309-
## argv
310+
## `argv`
310311

311312
A minimist-parsed version of the `process.argv` as `argv`.
312313

@@ -330,47 +331,48 @@ const myCustomArgv = minimist(process.argv.slice(2), {
330331
})
331332
```
332333

333-
## chalk
334+
## `chalk`
334335

335336
The [chalk](https://www.npmjs.com/package/chalk) package.
336337

337338
```js
338339
console.log(chalk.blue('Hello world!'))
339340
```
340341

341-
## fs
342+
## `fs`
342343

343344
The [fs-extra](https://www.npmjs.com/package/fs-extra) package.
344345

345346
```js
346347
const {version} = await fs.readJson('./package.json')
347348
```
348349

349-
## os
350+
## `os`
350351

351352
The [os](https://nodejs.org/api/os.html) package.
352353

353354
```js
354355
await $`cd ${os.homedir()} && mkdir example`
355356
```
356357

357-
## path
358+
## `path`
358359

359360
The [path](https://nodejs.org/api/path.html) package.
360361

361362
```js
362363
await $`mkdir ${path.join(basedir, 'output')}`
363364
```
364365

365-
## yaml
366+
## `yaml`
366367

367368
The [yaml](https://www.npmjs.com/package/yaml) package.
368369

369370
```js
370371
console.log(YAML.parse('foo: bar').foo)
371372
```
372373

373-
## dotenv
374+
## `dotenv`
375+
374376
The [envapi](https://www.npmjs.com/package/envapi) package.
375377
An API to interact with environment vars in [dotenv](https://www.npmjs.com/package/dotenv) format.
376378

@@ -388,3 +390,43 @@ await $({ env })`echo $FOO`.stdout // BAR
388390
dotenv.config('.env')
389391
process.env.FOO // BAR
390392
```
393+
394+
## `quote()`
395+
396+
Default bash quoting function.
397+
398+
```js
399+
quote("$FOO") // "$'$FOO'"
400+
```
401+
402+
## `quotePowerShell()`
403+
404+
PowerShell specific quoting.
405+
406+
```js
407+
quotePowerShell("$FOO") // "'$FOO'"
408+
```
409+
410+
## `useBash()`
411+
412+
Enables bash preset: sets `$.shell` to `bash` and `$.quote` to `quote`.
413+
414+
```js
415+
useBash()
416+
```
417+
418+
## `usePowerShell()`
419+
420+
Switches to PowerShell. Applies the `quotePowerShell` for quoting.
421+
422+
```js
423+
usePowerShell()
424+
```
425+
426+
## `usePwsh()`
427+
428+
Sets pwsh (PowerShell v7+) as `$.shell` default.
429+
430+
```js
431+
usePwsh()
432+
```

0 commit comments

Comments
 (0)