Commit 2411c37

Anton Medvedev <anton@medv.io>
2021-05-25 21:16:38
Update examples
1 parent dd5cac4
examples/basics.mjs
@@ -16,13 +16,33 @@
 
 await $`ls -1 | wc -l`
 
+await Promise.all([
+  $`sleep 1; echo 1`,
+  $`sleep 2; echo 2`,
+  $`sleep 3; echo 3`,
+])
+
 let branch = await $`git branch --show-current`
 await $`printf ${branch} | wc` // The new line trimmed from stdout.
 
 let foo = `hi; echo 'oops'`
 await $`echo ${foo} | wc` // Vars properly quoted.
 
-// We can use import and require together.
-let path = await import('path')
-let {name} = require(path.join(__dirname, '..', 'package.json'))
-console.log(chalk.black.bgCyanBright(name))
+let path = await import('path') // We can use import,
+let {name} = require(path.join(__dirname, '..', 'package.json')) // and require.
+
+console.log(chalk.black.bgCyanBright(name)) // The chalk is available without import.
+
+try {
+  await $`exit 42` // ProcessOutput will be thrown on non-zero exit codes.
+} catch (p) {
+  console.log(p.exitCode)
+}
+
+let {exitCode} = await nothrow($`exit 42`) // Change behavior to no-throw.
+exitCode = await $`exit 42`.exitCode // Or wait on exitCode itself.
+
+// Use combinations of pipe() and nothrow():
+await $`find ./examples -type f -print0`
+  .pipe(nothrow($`xargs -0 grep ${'missing' + 'part'}`))
+  .pipe($`wc -l`)
examples/cjs.js
@@ -1,23 +0,0 @@
-#!/usr/bin/env zx
-
-// Copyright 2021 Google LLC
-// 
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-// 
-//     https://www.apache.org/licenses/LICENSE-2.0
-// 
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-// In CommonJS module we can't use top-level await,
-// so wrap in in async function.
-
-void async function () {
-  await $`echo "Hello, CommonJS!"`
-}()
-  .catch(console.error)
examples/interactive.mjs
@@ -14,6 +14,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-let out = fs.createWriteStream('log.txt')
-process.stdin.pipe(out)
-await $`npm init`.pipe(out)
+let out = fs.createWriteStream('log.txt') // Record program output to this file.
+
+process.stdin.pipe(out) // Record user input as well.
+await $`npm init`.pipe(out) // Process will be interactive.
examples/index.md → examples/markdown.md
@@ -1,14 +1,14 @@
 # Markdown Scripts
 
 It's possible to write scripts using markdown. Only code blocks will be executed
-by zx. Try to run `zx examples/index.md`.
+by zx. Try to run `zx examples/markdown.md`.
 
 ```js
 await $`whoami`
 await $`ls -la ${__dirname}`
 ```
 
-The `__filename` will be pointed to **index.md**:
+The `__filename` will be pointed to **markdown.md**:
 
 ```js
 console.log(chalk.yellowBright(__filename))
examples/parallel.mjs
@@ -1,21 +0,0 @@
-#!/usr/bin/env zx
-
-// Copyright 2021 Google LLC
-// 
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-// 
-//     https://www.apache.org/licenses/LICENSE-2.0
-// 
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-await Promise.all([
-  $`sleep 1; echo 1`,
-  $`sleep 2; echo 2`,
-  $`sleep 3; echo 3`,
-])
examples/pipelines.md
@@ -40,3 +40,11 @@ let greeting = await $`printf "hello"`
 
 console.log(greeting.stdout)
 ```
+
+Use combinations of `pipe()` and [`nothrow()`](https://github.com/google/zx#nothrow):
+
+```js
+await $`find ./examples -type f -print0`
+  .pipe(nothrow($`xargs -0 grep ${'missing' + 'part'}`))
+  .pipe($`wc -l`)
+```
examples/typescript.ts
@@ -14,9 +14,16 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-/// <reference types=".." />
+import {ProcessOutput, ProcessPromise} from '..'
 
 void async function () {
-  await $`pwd`
+  let p: ProcessPromise<ProcessOutput> = $`cat`
+  p.pipe(process.stderr)
+
+  p.stdin.write('Hello, World!\n')
+  p.stdin.end()
+
+  let out: ProcessOutput = await p
+  console.log(chalk.red(out.exitCode))
 }()
 
README.md
@@ -316,10 +316,10 @@ module.
 ### Markdown scripts
 
 The `zx` can execute scripts written in markdown 
-([examples/index.md](examples/index.md)):
+([examples/index.md](examples/markdown.md)):
 
 ```bash
-zx examples/index.md
+zx examples/markdown.md
 ```
 
 ### TypeScript scripts
test.mjs
@@ -84,7 +84,7 @@ import {strict as assert} from 'assert'
 }
 
 { // Markdown scripts are working
-  await $`node zx.mjs examples/index.md`
+  await $`node zx.mjs examples/markdown.md`
 }
 
 { // Pipes are working