|
1 | 1 | # API Reference |
2 | 2 |
|
| 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 | + |
3 | 84 | ## cd() |
4 | 85 |
|
5 | 86 | Changes the current working directory. |
@@ -133,10 +214,55 @@ The [which](https://github.com/npm/node-which) package. |
133 | 214 | const node = await which('node') |
134 | 215 | ``` |
135 | 216 |
|
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 |
137 | 257 |
|
138 | 258 | The [minimist](https://www.npmjs.com/package/minimist) package. |
139 | 259 |
|
| 260 | +```js |
| 261 | +const argv = minimist(process.argv.slice(2), {}) |
| 262 | +``` |
| 263 | + |
| 264 | +## argv |
| 265 | + |
140 | 266 | A minimist-parsed version of the `process.argv` as `argv`. |
141 | 267 |
|
142 | 268 | ```js |
|
0 commit comments