Skip to content

Commit bfaf2ff

Browse files
authored
Docs v8.1.0 (#808)
1 parent 330426c commit bfaf2ff

11 files changed

+454
-21
lines changed

.vitepress/config.mts

+5-2
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ export default defineConfig({
3737
text: 'Docs',
3838
items: [
3939
{text: 'Getting Started', link: '/getting-started'},
40-
{text: 'Process Promise', link: '/process-promise'},
40+
{text: 'Setup', link: '/setup'},
4141
{text: 'API Reference', link: '/api'},
42-
{text: 'Configuration', link: '/configuration'},
4342
{text: 'CLI Usage', link: '/cli'},
43+
{text: 'Configuration', link: '/configuration'},
44+
{text: 'Process Promise', link: '/process-promise'},
45+
{text: 'Contribution Guide', link: '/contribution'},
46+
{text: 'Migration from v7', link: '/migration-from-v7'},
4447
],
4548
},
4649
{

api.md

+127-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,86 @@
11
# API Reference
22

3+
## $.sync
4+
Zx provides both synchronous and asynchronous command executions, returns [`ProcessOutput`](./process-output) or [`ProcessPromise`](./process-promise) respectively.
5+
6+
```js
7+
const list = await $`ls -la`
8+
const dir = $.sync`pwd`
9+
```
10+
11+
## $({...})
12+
13+
`$` object holds the default zx [configuration](./configuration), which is used for all execution. To specify a custom preset use `$` as factory:
14+
15+
```js
16+
const $$ = $({
17+
verbose: false,
18+
env: {NODE_ENV: 'production'},
19+
})
20+
21+
const env = await $$`node -e 'console.log(process.env.NODE_ENV)'`
22+
const pwd = $$.sync`pwd`
23+
const hello = $({quiet: true})`echo "Hello!"`
24+
```
25+
26+
### $({input})
27+
28+
The input option passes the specified `stdin` to the command.
29+
30+
```js
31+
const p1 = $({ input: 'foo' })`cat`
32+
const p2 = $({ input: Readable.from('bar') })`cat`
33+
const p3 = $({ input: Buffer.from('baz') })`cat`
34+
const p4 = $({ input: p3 })`cat`
35+
const p5 = $({ input: await p3 })`cat`
36+
```
37+
38+
### $({signal})
39+
40+
The signal option makes the process abortable.
41+
42+
```js
43+
const {signal} = new AbortController()
44+
const p = $({ signal })`sleep 9999`
45+
46+
setTimeout(() => signal.abort('reason'), 1000)
47+
```
48+
49+
### $({timeout})
50+
51+
The timeout option makes the process autokillable after the specified delay.
52+
53+
```js
54+
const p = $({timeout: '1s'})`sleep 999`
55+
```
56+
57+
The full options list:
58+
```ts
59+
interface Options {
60+
cwd: string
61+
ac: AbortController
62+
signal: AbortSignal
63+
input: string | Buffer | Readable | ProcessOutput | ProcessPromise
64+
timeout: Duration
65+
timeoutSignal: string
66+
stdio: StdioOptions
67+
verbose: boolean
68+
sync: boolean
69+
env: NodeJS.ProcessEnv
70+
shell: string | boolean
71+
nothrow: boolean
72+
prefix: string
73+
postfix: string
74+
quote: typeof quote
75+
quiet: boolean
76+
detached: boolean
77+
spawn: typeof spawn
78+
spawnSync: typeof spawnSync
79+
log: typeof log
80+
kill: typeof kill
81+
}
82+
```
83+
384
## cd()
485

586
Changes the current working directory.
@@ -133,10 +214,55 @@ The [which](https://github.com/npm/node-which) package.
133214
const node = await which('node')
134215
```
135216

136-
## argv
217+
## ps()
218+
219+
The [@webpod/ps](https://github.com/webpod/ps) package to provide a cross-platform way to list processes.
220+
221+
```js
222+
const all = await ps.lookup()
223+
const nodejs = await ps.lookup({ command: 'node' })
224+
const children = await ps.tree({ pid: 123 })
225+
const fulltree = await ps.tree({ pid: 123, recursive: true })
226+
```
227+
228+
## kill()
229+
230+
A process killer.
231+
232+
```js
233+
await kill(123)
234+
await kill(123, 'SIGKILL')
235+
```
236+
237+
## tmpdir()
238+
239+
Creates a temporary directory.
240+
241+
```js
242+
t1 = tmpdir() // /os/based/tmp/zx-1ra1iofojgg/
243+
t2 = tmpdir('foo') // /os/based/tmp/zx-1ra1iofojgg/foo/
244+
```
245+
246+
## tmpfile()
247+
248+
Temp file factory.
249+
250+
```js
251+
f1 = tmpfile() // /os/based/tmp/zx-1ra1iofojgg
252+
f2 = tmpfile('f.txt') // /os/based/tmp/zx-1ra1iofojgg/foo.txt
253+
f3 = tmpfile('f.txt', 'string or buffer')
254+
```
255+
256+
## minimist
137257

138258
The [minimist](https://www.npmjs.com/package/minimist) package.
139259

260+
```js
261+
const argv = minimist(process.argv.slice(2), {})
262+
```
263+
264+
## argv
265+
140266
A minimist-parsed version of the `process.argv` as `argv`.
141267

142268
```js

cli.md

+40
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,46 @@ the import.
6767
import sh from 'tinysh' // @^1
6868
```
6969

70+
## --quiet
71+
72+
Suppress any outputs.
73+
74+
## --verbose
75+
76+
Enable verbose mode.
77+
78+
## --shell
79+
80+
Specify a custom shell binary.
81+
82+
```bash
83+
zx --shell=/bin/bash script.mjs
84+
```
85+
86+
## --prefix & --postfix
87+
88+
Attach a command to the beginning or the end of every command.
89+
90+
```bash
91+
zx --prefix='echo foo;' --postfix='; echo bar' script.mjs
92+
```
93+
94+
## --cwd
95+
96+
Set the current working directory.
97+
98+
```bash
99+
zx --cwd=/foo/bar script.mjs
100+
```
101+
102+
## --version
103+
104+
Print the current version of `zx`.
105+
106+
## --help
107+
108+
Print help.
109+
70110
## __filename & __dirname
71111

72112
In [ESM](https://nodejs.org/api/esm.html) modules, Node.js does not provide

configuration.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Or use a CLI argument: `--shell=/bin/bash`
1414

1515
Specifies a `spawn` api. Defaults to `require('child_process').spawn`.
1616

17+
To override a sync API implementation, set `$.spawnSync` correspondingly.
18+
1719
## $.prefix
1820

1921
Specifies the command that will be prefixed to all commands run.
@@ -22,19 +24,33 @@ Default is `set -euo pipefail;`.
2224

2325
Or use a CLI argument: `--prefix='set -e;'`
2426

27+
## $.postfix
28+
29+
Like a `$.prefix`, but for the end of the command.
30+
31+
```js
32+
$.postfix = '; exit $LastExitCode' // for PowerShell compatibility
33+
```
34+
2535
## $.quote
2636

2737
Specifies a function for escaping special characters during
2838
command substitution.
2939

3040
## $.verbose
3141

32-
Specifies verbosity. Default is `true`.
42+
Specifies verbosity. Default is `false`.
3343

3444
In verbose mode, `zx` prints all executed commands alongside with their
3545
outputs.
3646

37-
Or use the CLI argument `--quiet` to set `$.verbose = false`.
47+
Or use the CLI argument: `--verbose` to set `true`.
48+
49+
## $.quiet
50+
51+
Suppresses all output. Default is `false`.
52+
53+
Via CLI argument: `--quiet` sets `$.quiet = true`.
3854

3955
## $.env
4056

contribution.md

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Contribution Guide
2+
3+
zx is a fully [open-source project](https://github.com/google/zx), which is developing by the community for the community.
4+
We welcome contributions of any kind, including but not limited to:
5+
* Bug reports
6+
* Feature requests
7+
* Code contributions
8+
* Documentation improvements
9+
* Discussions
10+
11+
## Community Guidelines
12+
13+
This project follows [Google's Open Source Community Guidelines](https://opensource.google/conduct/).
14+
In short: all contributors are treated with respect and fairness.
15+
16+
## Contributor License Agreement
17+
18+
Contributions to this project must be accompanied by a Contributor License
19+
Agreement. You (or your employer) retain the copyright to your contribution;
20+
this simply gives us permission to use and redistribute your contributions as
21+
part of the project. Head over to <https://cla.developers.google.com/> to see
22+
your current agreements on file or to sign a new one.
23+
24+
You generally only need to submit a CLA once, so if you've already submitted one
25+
(even if it was for a different project), you probably don't need to do it
26+
again.
27+
28+
## How to Contribute
29+
Before proposing changes, look for similar ones in the project's [issues](https://github.com/google/zx/issues) and [pull requests](https://github.com/google/zx/pulls). If you can't decide, create a new [discussion](https://github.com/google/zx/discussions) topic, and we will help you figure it out. When ready to move on:
30+
* Prepare your development environment.
31+
* Ensure you have Node.js 20+ installed.
32+
* Bash is essential for running zx scripts. Linux and macOS users usually have it installed by default. Consider using [Windows Subsystem for Linux](https://docs.microsoft.com/en-us/windows/wsl/install) or [Git Bash](https://git-scm.com/downloads) if you are on Windows.
33+
* Fork [the repository](https://github.com/google/zx).
34+
* Create a new branch.
35+
* Make your changes.
36+
* If you are adding a new feature, please include additional tests. The coverage threshold is 98%.
37+
* Create a [conventional-commits](https://www.conventionalcommits.org/en/v1.0.0/) compliant messages.
38+
* Ensure that everything is working:
39+
* `npm run fmt` to format your code.
40+
* `npm run cov` to run the tests.
41+
* Push the changes to your fork.
42+
* Create a pull request.
43+
* Describe your changes in detail.
44+
* Reference any related issues if applicable.
45+
46+
## Code Reviews
47+
48+
All submissions, including submissions by project members, require review. We use GitHub pull requests for this purpose. Consult [GitHub Help](https://help.github.com/articles/about-pull-requests/) for more information on using pull requests.
49+
50+
## License
51+
52+
The project is licensed under the [Apache-2.0](https://github.com/google/zx?tab=Apache-2.0-1-ov-file#readme)

getting-started.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ gives sensible defaults.
3535
npm install zx
3636
```
3737

38+
```bash [bun]
39+
bun install zx
40+
```
41+
3842
```bash [deno]
3943
deno install -A npm:zx
4044
```
@@ -82,7 +86,12 @@ import 'zx/globals'
8286
### ``$`command` ``
8387

8488
Executes a given command using the `spawn` func
85-
and returns [`ProcessPromise`](process-promise.md).
89+
and returns [`ProcessPromise`](process-promise.md). It supports both sync and async modes.
90+
91+
```js
92+
const list = await $`ls -la`
93+
const dir = $.sync`pwd`
94+
```
8695

8796
Everything passed through `${...}` will be automatically escaped and quoted.
8897

migration-from-v7.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Migration from v7 to v8
2+
3+
[v8.0.0 release](https://github.com/google/zx/releases/tag/8.0.0) brought many features, improvements and fixes, but also has introduced a few breaking changes.
4+
5+
1. `$.verbose` is set to `false` by default, but errors are still printed to `stderr`. Set `$.quiet = true` to suppress any output.
6+
```js
7+
$.verbose = true // everything works like in v7
8+
9+
$.quiet = true // to completely turn off logging
10+
```
11+
12+
2. `ssh` API was dropped. Install [webpod](https://github.com/webpod/webpod) package instead.
13+
```js
14+
// import {ssh} from 'zx' ↓
15+
import {ssh} from 'webpod'
16+
17+
const remote = ssh('user@host')
18+
await remote`echo foo`
19+
```
20+
21+
3. zx is not looking for `PowerShell` anymore, on Windows by default. If you still need it, use the `usePowerShell` helper to enable:
22+
23+
```js
24+
import { usePowerShell, useBash } from 'zx'
25+
26+
usePowerShell() // to enable powershell
27+
useBash() // switch to bash, the default
28+
```
29+
30+
To look for modern [PowerShell v7+](https://github.com/google/zx/pull/790), invoke `usePwsh()` helper instead:
31+
32+
```js
33+
import { usePwsh } from 'zx'
34+
35+
usePwsh()
36+
```
37+
38+
4. Process cwd synchronization between `$` invocations is now disabled by default. This functionality is provided via an async hook and can now be controlled directly.
39+
40+
```js
41+
import { syncProcessCwd } from 'zx'
42+
43+
syncProcessCwd() // restores legacy v7 behavior
44+
```

0 commit comments

Comments
 (0)