@@ -33,6 +33,12 @@ npm i -g zx
33
33
34
34
** Requirement** : Node version >= 16.0.0
35
35
36
+ ## Goods
37
+
38
+ [ $] ( #command- ) · [ cd()] ( #cd ) · [ fetch()] ( #fetch ) · [ question()] ( #question ) · [ sleep()] ( #sleep ) · [ nothrow()] ( #nothrow ) · [ quiet()] ( #quiet ) ·
39
+ [ chalk] ( #chalk-package ) · [ yaml] ( #yaml-package ) · [ fs] ( #fs-package ) · [ globby] ( #globby-package ) · [ os] ( #os-package ) · [ path] ( #path-package ) · [ minimist] ( #minimist-package ) · [ which] ( #which-package ) ·
40
+ [ __ filename] ( #__filename--__dirname ) · [ __ dirname] ( #__filename--__dirname )
41
+
36
42
## Documentation
37
43
38
44
Write your scripts in a file with ` .mjs ` extension in order to
@@ -103,7 +109,7 @@ try {
103
109
}
104
110
```
105
111
106
- #### ` ProcessPromise `
112
+ ### ` ProcessPromise `
107
113
108
114
``` ts
109
115
class ProcessPromise <T > extends Promise <T > {
@@ -124,7 +130,7 @@ await $`cat file.txt`.pipe(process.stdout)
124
130
125
131
Read more about [ pipelines] ( docs/pipelines.md ) .
126
132
127
- #### ` ProcessOutput `
133
+ ### ` ProcessOutput `
128
134
129
135
``` ts
130
136
class ProcessOutput {
@@ -136,9 +142,9 @@ class ProcessOutput {
136
142
}
137
143
```
138
144
139
- ### Functions
145
+ ## Functions
140
146
141
- #### ` cd() `
147
+ ### ` cd() `
142
148
143
149
Changes the current working directory.
144
150
@@ -147,7 +153,7 @@ cd('/tmp')
147
153
await $` pwd` // outputs /tmp
148
154
```
149
155
150
- #### ` fetch() `
156
+ ### ` fetch() `
151
157
152
158
A wrapper around the [ node-fetch] ( https://www.npmjs.com/package/node-fetch ) package.
153
159
@@ -158,7 +164,7 @@ if (resp.ok) {
158
164
}
159
165
```
160
166
161
- #### ` question() `
167
+ ### ` question() `
162
168
163
169
A wrapper around the [ readline] ( https://nodejs.org/api/readline.html ) package.
164
170
@@ -178,15 +184,15 @@ function question(query?: string, options?: QuestionOptions): Promise<string>
178
184
type QuestionOptions = { choices: string [] }
179
185
` ` `
180
186
181
- #### ` sleep ()`
187
+ ### ` sleep ()`
182
188
183
189
A wrapper around the ` setTimeout ` function.
184
190
185
191
` ` ` js
186
192
await sleep (1000 )
187
193
` ` `
188
194
189
- #### ` nothrow ()`
195
+ ### ` nothrow ()`
190
196
191
197
Changes behavior of ` $ ` to not throw an exception on non-zero exit codes.
192
198
@@ -219,7 +225,7 @@ if ((await nothrow($`[[ -d path ]]`)).exitCode == 0) {
219
225
...
220
226
}
221
227
` ` `
222
- #### ` quiet() `
228
+ ### ` quiet() `
223
229
224
230
Changes behavior of ` $ ` to disable verbose output .
225
231
@@ -234,35 +240,35 @@ await quiet($`grep something from-file`)
234
240
// Command and output will not be displayed.
235
241
` ` `
236
242
237
- ### Packages
243
+ ## Packages
238
244
239
245
Following packages are available without importing inside scripts .
240
246
241
- #### ` chalk ` package
247
+ ### ` chalk ` package
242
248
243
249
The [chalk ](https :// www.npmjs.com/package/chalk) package.
244
250
245
251
` ` ` js
246
252
console.log(chalk.blue('Hello world!'))
247
253
` ` `
248
254
249
- #### ` yaml ` package
255
+ ### ` yaml ` package
250
256
251
257
The [yaml ](https :// www.npmjs.com/package/yaml) package.
252
258
253
259
` ` ` js
254
260
console.log(YAML.parse('foo: bar').foo)
255
261
` ` `
256
262
257
- #### ` fs ` package
263
+ ### ` fs ` package
258
264
259
265
The [fs - extra ](https :// www.npmjs.com/package/fs-extra) package.
260
266
261
267
` ` ` js
262
268
let content = await fs.readFile('./package.json')
263
269
` ` `
264
270
265
- #### ` globby ` package
271
+ ### ` globby ` package
266
272
267
273
The [globby ](https :// github.com/sindresorhus/globby) package.
268
274
@@ -278,29 +284,29 @@ Also, globby available via the `glob` shortcut:
278
284
await $ ` svgo $ {await glob(' *.svg' )}`
279
285
` ` `
280
286
281
- #### ` os ` package
287
+ ### ` os ` package
282
288
283
289
The [os ](https :// nodejs.org/api/os.html) package.
284
290
285
291
` ` ` js
286
292
await $ ` cd $ {os.homedir()} && mkdir example `
287
293
` ` `
288
294
289
- #### ` path ` package
295
+ ### ` path ` package
290
296
291
297
The [path ](https :// nodejs.org/api/path.html) package.
292
298
293
299
` ` ` js
294
300
await $ ` mkdir $ {path.join(basedir , ' output' )}`
295
301
` ` `
296
302
297
- #### ` minimist ` package
303
+ ### ` minimist ` package
298
304
299
305
The [minimist](https:// www.npmjs.com/package/minimist) package.
300
306
301
307
Available as global const ` argv ` .
302
308
303
- #### ` which `
309
+ ### ` which ` package
304
310
305
311
The [which ](https :// github.com/npm/node-which) package.
306
312
@@ -310,9 +316,9 @@ let node = await which('node')
310
316
let node = which.sync('node')
311
317
` ` `
312
318
313
- ### Configuration
319
+ ## Configuration
314
320
315
- #### ` $.shell `
321
+ ### ` $.shell `
316
322
317
323
Specifies what shell is used . Default is ` which bash ` .
318
324
@@ -322,29 +328,29 @@ $.shell = '/usr/bin/bash'
322
328
323
329
Or use a CLI argument : ` --shell=/bin/bash `
324
330
325
- #### ` $.spawn `
331
+ ### ` $.spawn `
326
332
327
333
Specifies a ` spawn ` api . Defaults to ` require('child_process').spawn ` .
328
334
329
- #### ` $.maxBuffer `
335
+ ### ` $.maxBuffer `
330
336
331
337
Specifies the largest number of bytes allowed on stdout or stderr .
332
338
Defaults to ` 200 * 1024 * 1024 ` (200 MiB ).
333
339
334
- #### ` $.prefix `
340
+ ### ` $.prefix `
335
341
336
342
Specifies the command that will be prefixed to all commands run .
337
343
338
344
Default is ` set -euo pipefail; ` .
339
345
340
346
Or use a CLI argument : ` --prefix='set -e;' `
341
347
342
- #### ` $.quote `
348
+ ### ` $.quote `
343
349
344
350
Specifies a function for escaping special characters during
345
351
command substitution.
346
352
347
- #### `$.verbose`
353
+ ### `$.verbose`
348
354
349
355
Specifies verbosity. Default is `true`.
350
356
@@ -353,15 +359,15 @@ outputs.
353
359
354
360
Or use a CLI argument `--quiet` to set `$.verbose = false`.
355
361
356
- ### Polyfills
362
+ ## Polyfills
357
363
358
- #### `__filename` & `__dirname`
364
+ ### `__filename` & `__dirname`
359
365
360
366
In [ESM](https : // nodejs.org/api/esm.html) modules, Node.js does not provide
361
367
` __filename ` and ` __dirname ` globals . As such globals are really handy in scripts ,
362
368
` zx ` provides these for use in ` .mjs ` files (when using the ` zx ` executable ).
363
369
364
- #### `require()`
370
+ ### `require()`
365
371
366
372
In [ESM](https : // nodejs.org/api/modules.html#modules_module_createrequire_filename)
367
373
modules , the ` require() ` function is not defined.
@@ -372,13 +378,13 @@ files (when using `zx` executable).
372
378
let {version } = require (' ./package.json' )
373
379
` ` `
374
380
375
- ### Experimental
381
+ ## Experimental
376
382
377
383
The zx also provides a few experimental functions. Please leave a feedback about
378
384
those features in [the discussion](https://github.com/google/zx/discussions/299).
379
385
To enable new features via CLI pass ` -- experimental ` flag.
380
386
381
- #### ` retry ()`
387
+ ### ` retry ()`
382
388
383
389
Retries a command a few times. Will return after the first
384
390
successful attempt, or will throw after specifies attempts count.
@@ -392,7 +398,7 @@ let {stdout} = await retry(5)`curl localhost`
392
398
let {stdout } = await retry (3 , 500 )` npm whoami `
393
399
` ` `
394
400
395
- #### ` echo ()`
401
+ ### ` echo ()`
396
402
397
403
A ` console .log ()` alternative which can take [ProcessOutput](#processoutput).
398
404
@@ -406,7 +412,7 @@ echo`Current branch is ${branch}.`
406
412
echo (' Current branch is' , branch )
407
413
` ` `
408
414
409
- #### ` startSpinner ()`
415
+ ### ` startSpinner ()`
410
416
411
417
Starts a simple CLI spinner, and returns ` stop ()` function.
412
418
@@ -418,7 +424,7 @@ await $`long-running command`
418
424
stop ()
419
425
` ` `
420
426
421
- #### ` withTimeout ()`
427
+ ### ` withTimeout ()`
422
428
423
429
Runs and sets a timeout for a cmd.
424
430
@@ -428,16 +434,16 @@ import {withTimeout} from 'zx/experimental'
428
434
await withTimeout (100 , ' SIGTERM' )` sleep 9999 `
429
435
` ` `
430
436
431
- ### FAQ
437
+ ## FAQ
432
438
433
- #### Passing env variables
439
+ ### Passing env variables
434
440
435
441
` ` ` js
436
442
process .env .FOO = ' bar'
437
443
await $ ` echo $FOO `
438
444
` ` `
439
445
440
- #### Passing array of values
446
+ ### Passing array of values
441
447
442
448
If array of values passed as argument to ` $ ` , items of the array will be escaped
443
449
individually and concatenated via space.
@@ -448,7 +454,7 @@ let files = [...]
448
454
await $ ` tar cz ${files } `
449
455
` ` `
450
456
451
- #### Importing from other scripts
457
+ ### Importing from other scripts
452
458
453
459
It is possible to make use of ` $ ` and other functions via explicit imports:
454
460
@@ -458,13 +464,13 @@ import {$} from 'zx'
458
464
await $ ` date `
459
465
` ` `
460
466
461
- #### Scripts without extensions
467
+ ### Scripts without extensions
462
468
463
469
If script does not have a file extension (like ` .git / hooks / pre - commit ` ), zx
464
470
assumes that it is an [ESM](https://nodejs.org/api/modules.html#modules_module_createrequire_filename)
465
471
module.
466
472
467
- #### Markdown scripts
473
+ ### Markdown scripts
468
474
469
475
The ` zx ` can execute scripts written in markdown
470
476
([docs/markdown.md](docs/markdown.md)):
@@ -473,7 +479,7 @@ The `zx` can execute scripts written in markdown
473
479
zx docs / markdown .md
474
480
` ` `
475
481
476
- #### TypeScript scripts
482
+ ### TypeScript scripts
477
483
478
484
` ` ` ts
479
485
import {$ } from ' zx'
@@ -511,7 +517,7 @@ in `tsconfig.json`.
511
517
` ` `
512
518
513
519
514
- #### Executing remote scripts
520
+ ### Executing remote scripts
515
521
516
522
If the argument to the ` zx ` executable starts with ` https :// `, the file will be
517
523
downloaded and executed .
@@ -524,7 +530,7 @@ zx https://medv.io/example-script.mjs
524
530
zx https://medv.io/game-of-life.mjs
525
531
` ` `
526
532
527
- #### Executing scripts from stdin
533
+ ### Executing scripts from stdin
528
534
529
535
The ` zx ` supports executing scripts from stdin .
530
536
@@ -534,7 +540,7 @@ await $`pwd`
534
540
EOF
535
541
` ` `
536
542
537
- #### Attaching .bash_profile / .zshrc
543
+ ### Attaching .bash_profile / .zshrc
538
544
539
545
By default ` child_process ` does not include aliases and bash functions .
540
546
But you are still able to do it by hand . Just attach necessary directives to ` $.prefix ` .
0 commit comments