Skip to content

常见问题

¥FAQ

传递环境变量

¥Passing env variables

js
process.env.FOO = 'bar'
await $`echo $FOO`

传递值数组

¥Passing array of values

当将值数组作为参数传递给 $ 时,数组中的项目将单独转义并通过空格连接。

¥When passing an array of values as an argument to $, items of the array will be escaped individually and concatenated via space.

示例:

¥Example:

js
const files = [...]
await $`tar cz ${files}`

导入到其他脚本中

¥Importing into other scripts

可以通过显式导入来使用 $ 和其他函数:

¥It is possible to make use of $ and other functions via explicit imports:

js
#!/usr/bin/env node
import {$} from 'zx'

await $`date`

附加配置文件

¥Attaching a profile

默认情况下,child_process 不包含别名和 bash 函数。但你仍然可以手动完成。只需将必要的指令附加到 $.prefix

¥By default child_process does not include aliases and bash functions. But you are still able to do it by hand. Just attach necessary directives to the $.prefix.

js
$.prefix += 'export NVM_DIR=$HOME/.nvm; source $NVM_DIR/nvm.sh; '
await $`nvm -v`

使用 GitHub Actions

¥Using GitHub Actions

默认的 GitHub Action 运行器附带安装了 npx

¥The default GitHub Action runner comes with npx installed.

yaml
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Build
        env:
          FORCE_COLOR: 3
        run: |
          npx zx <<'EOF'
          await $`...`
          EOF

Canary / Beta / RC 版本

¥Canary / Beta / RC builds

不耐烦的早期采用者可以尝试实验性的 zx 版本。但请记住:这些构建在任何意义上都是 ⚠️️__beta__ 。

¥Impatient early adopters can try the experimental zx versions. But keep in mind: these builds are ⚠️️__beta__ in every sense.

bash
npm i zx@dev
npx zx@dev --install --quiet <<< 'import _ from "lodash" /* 4.17.15 */; console.log(_.VERSION)'

google/zx 中文网 - 粤ICP备13048890号