zx 包提供了围绕 child_process 的有用包装器,转义参数并提供合理的默认值。
安装
npm i -g zx
使用
脚本需要包含以下文件头:
#!/usr/bin/env zx
为脚本添加执行权限再运行
$ chmod +x ./script.mjs$ ./script.mjs# 或者使用这个命令$ zx ./script.mjs
常用命令
cd 进入目录
cd('/tmp')// 将输出 /tmpawait $`pwd`
fetch
fetch() 方法是对 node-fetch 包的包装:
let resp = await fetch('http://wttr.in')if (resp.ok) {console.log(await resp.text())}
question
question() 方法是对 readline 包的包装
let username = await question('What is your username? ')let token = await question('Choose env variable: ', {choices: Object.keys(process.env)})
chalk
对于 chalk 包,不需要导入就可以直接用
console.log(chalk.blue('Hello world!'))
