• 分享

Node.js 写 bash 脚本终极方案!

avatar avatar
1790阅读 0评论 1喜欢
![640.webp](https://files.codelife.cc/blog/images/2021/07/1626658936574.webp) ### 前言 最近在学习`bash`脚本语法,但是如果对bash语法不是熟手的话,感觉非常容易出错,比如说:显示未定义的变量`shell`中变量没有定义,仍然是可以使用的,但是它的结果可能不是你所预期的。举个例子: ```sh #!/bin/bash # 这里是判断变量var是否等于字符串abc,但是var这个变量并没有声明 if [ "$var" = "abc" ] then # 如果if判断里是true就在控制台打印 “ not abc” echo " not abc" else # 如果if判断里是false就在控制台打印 “ abc” echo " abc " fi ``` 结果是打印了abc,但问题是,这个脚本应该报错啊,变量并没有赋值算是错误吧。 为了弥补这些错误,我们学会在脚本开头加入:`set -u` 这句命令的意思是脚本在头部加上它,遇到不存在的变量就会报错,并停止执行。 再次运行就会提示:`test.sh: 3: test.sh: num: parameter not set` 再想象一下,你本来想删除:`rm -rf $dir/*`然后dir是空的时候,变成了什么?`rm -rf`是删除命令,`$dir`是空的话,相当于执行 `rm -rf /*`,这是删除所有文件和文件夹。。。然后,你的系统就没了,这就是传说中的删库跑路吗~~~~ 如果是`node`或者浏览器环境,我们直接`var === 'abc'` 肯定是会报错的,也就是说很多javascript编程经验无法复用到`bash`来,如果能复用的话,该多好啊。 后来就开始探索,如果用`node`脚本代替`bash`该多好啊,经过一天折腾逐渐发现一个神器,`Google`旗下的`zx`库,先别着急,我先不介绍这个库,我们先看看目前主流用`node`如何编写`bash`脚本,就知道为啥它是神器了。 #### node执行bash脚本: 勉强解决方案:child_process API 例如 `child_process`的API里面`exec`命令 ```js const { exec } = require("child_process"); exec("ls -la", (error, stdout, stderr) => { if (error) { console.log(`error: ${error.message}`); return; } if (stderr) { console.log(`stderr: ${stderr}`); return; } console.log(`stdout: ${stdout}`); }); ``` 这里需要注意的是,首先`exec`是异步的,但是我们`bash`脚本命令很多都是同步的。 而且注意:`error`对象不同于`stderr`. `error`当`child_process`模块无法执行命令时,该对象不为空。例如,查找一个文件找不到该文件,则error对象不为空。但是,如果命令成功运行并将消息写入标准错误流,则该`stderr`对象不会为空。 当然我们可以使用同步的`exec`命令,`execSync` ```js // 引入 exec 命令 from child_process 模块 const { execSync } = require("child_process"); // 同步创建了一个hello的文件夹 execSync("mkdir hello"); ``` 再简单介绍一下child_process的其它能够执行bash命令的api * spawn: 启动一个子进程来执行命令 * exec:启动一个子进程来执行命令,与spawn不同的是,它有一个回调函数能知道子进程的情况 * execFile:启动一子进程来执行可执行文件 * fork:与spawn类似,不同点是它需要指定子进程需要需执行的javascript文件 exec跟ececFile不同的是,exec适合执行命令,eexecFile适合执行文件。 #### node执行bash脚本: 进阶方案 shelljs ```js const shell = require('shelljs'); # 删除文件命令 shell.rm('-rf', 'out/Release'); // 拷贝文件命令 shell.cp('-R', 'stuff/', 'out/Release'); # 切换到lib目录,并且列出目录下到.js结尾到文件,并替换文件内容(sed -i 是替换文字命令) shell.cd('lib'); shell.ls('*.js').forEach(function (file) { shell.sed('-i', 'BUILD_VERSION', 'v0.1.2', file); shell.sed('-i', /^.*REMOVE_THIS_LINE.*$/, '', file); shell.sed('-i', /.*REPLACE_LINE_WITH_MACRO.*\n/, shell.cat('macro.js'), file); }); shell.cd('..'); # 除非另有说明,否则同步执行给定的命令。 在同步模式下,这将返回一个 ShellString #(与 ShellJS v0.6.x 兼容,它返回一个形式为 { code:..., stdout:..., stderr:... } 的对象)。 # 否则,这将返回子进程对象,并且回调接收参数(代码、标准输出、标准错误)。 if (shell.exec('git commit -am "Auto-commit"').code !== 0) { shell.echo('Error: Git commit failed'); shell.exit(1); } ``` 从上面代码上看来,shelljs真的已经算是非常棒的nodejs写bash脚本的方案了,如果你们那边的node环境不能随便升级,我觉得shelljs确实够用了。 接着我们看看今天的主角zx,start已经17.4k了。 ### zx库 官方网址:[www.npmjs.com/package/zx](https://link.juejin.cn/?target=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2Fzx "https://www.npmjs.com/package/zx") 我们先看看怎么用 ```js #!/usr/bin/env zx await $`cat package.json | grep name` let branch = await $`git branch --show-current` await $`dep deploy --branch=${branch}` await Promise.all([ $`sleep 1; echo 1`, $`sleep 2; echo 2`, $`sleep 3; echo 3`, ]) let name = 'foo bar' await $`mkdir /tmp/${name} ``` 各位看官觉得咋样,是不是就是在写linux命令而已,bash语法可以忽略很多,直接上js就行,而且它的优点还不止这些,有一些特点挺有意思的: 1、支持ts,自动编译.ts为.mjs文件,.mjs文件是node高版本自带的支持es6 module的文件结尾,也就是这个文件直接import模块就行,不用其它工具转义 2、自带支持管道操作pipe方法 3、自带fetch库,可以进行网络请求,自带chalk库,可以打印有颜色的字体,自带错误处理nothrow方法,如果bash命令出错,可以包裹在这个方法里忽略错误 #### 完整中文文档(在下翻译水平一般,请见谅) ```js #!/usr/bin/env zx await $`cat package.json | grep name` let branch = await $`git branch --show-current` await $`dep deploy --branch=${branch}` await Promise.all([ $`sleep 1; echo 1`, $`sleep 2; echo 2`, $`sleep 3; echo 3`, ]) let name = 'foo bar' await $`mkdir /tmp/${name} ``` Bash 很棒,但是在编写脚本时,人们通常会选择更方便的编程语言。 JavaScript 是一个完美的选择,但标准的 Node.js 库在使用之前需要额外的做一些事情。 zx 基于 child_process ,转义参数并提供合理的默认值。 安装 ```sh npm i -g zx ``` 需要的环境 ```sh Node.js >= 14.8.0 ``` 将脚本写入扩展名为 `.mjs` 的文件中,以便能够在顶层使用`await`。 将以下 `shebang`添加到 `zx` 脚本的开头: ```js #!/usr/bin/env zx 现在您将能够像这样运行您的脚本: chmod +x ./script.mjs ./script.mjs ``` 或者通过 `zx`可执行文件: ```js zx ./script.mjs ``` 所有函数`($、cd、fetch 等)`都可以直接使用,无需任何导入。 #### $`command` 使用 `child_process` 包中的 spawn 函数执行给定的字符串, 并返回 ProcessPromise. ```js let count = parseInt(await $`ls -1 | wc -l`) console.log(`Files count: ${count}`) ``` 例如,要并行上传文件: 如果执行的程序返回非零退出代码,`ProcessOutput` 将被抛出 ```js try { await $`exit 1` } catch (p) { console.log(`Exit code: ${p.exitCode}`) console.log(`Error: ${p.stderr}`) } ``` ProcessPromise,以下是promise typescript的接口定义 ```js class ProcessPromise<T> extends Promise<T> { readonly stdin: Writable readonly stdout: Readable readonly stderr: Readable readonly exitCode: Promise<number> pipe(dest): ProcessPromise<T> } ``` 阅读更多的关于管道的信息 [github.com/google/zx/b…](https://link.juejin.cn/?target=https%3A%2F%2Fgithub.com%2Fgoogle%2Fzx%2Fblob%2FHEAD%2Fexamples%2Fpipelines.md "https://github.com/google/zx/blob/HEAD/examples/pipelines.md") `ProcessOutput`的`typescript`接口定义 ```js class ProcessOutput { readonly stdout: string readonly stderr: string readonly exitCode: number toString(): string } ``` 函数: #### cd() 更改当前工作目录 ```js cd('/tmp') await $`pwd` // outputs /tmp cd('/tmp') await $`pwd` // outputs /tmp ``` #### fetch() node-fetch 包。 ```js let resp = await fetch('http://wttr.in') if (resp.ok) { console.log(await resp.text()) } ``` #### question() readline包 ```js let bear = await question('What kind of bear is best? ') let token = await question('Choose env variable: ', { choices: Object.keys(process.env) }) ``` 在第二个参数中,可以指定选项卡自动完成的选项数组 以下是接口定义 ```js function question(query?: string, options?: QuestionOptions): Promise<string> type QuestionOptions = { choices: string[] } ``` #### sleep() 基于setTimeout 函数 ```js await sleep(1000) ``` #### nothrow() 将 $ 的行为更改, 如果退出码不是0,不跑出异常. ts接口定义 ```js function nothrow<P>(p: P): P ``` ```js await nothrow($`grep something from-file`) // 在管道内: await $`find ./examples -type f -print0` .pipe(nothrow($`xargs -0 grep something`)) .pipe($`wc -l`) ``` 以下的包,无需导入,直接使用 #### chalk ```js await nothrow($`grep something from-file`) // 在管道内: await $`find ./examples -type f -print0` .pipe(nothrow($`xargs -0 grep something`)) .pipe($`wc -l`) ``` #### fs 类似于如下的使用方式 ```js import {promises as fs} from 'fs' let content = await fs.readFile('./package.json') ``` #### os ```js await $`cd ${os.homedir()} && mkdir example` ``` 配置: #### $.shell 指定要用的bash. ```js $.shell = '/usr/bin/bash' ``` #### $.quote 指定用于在命令替换期间转义特殊字符的函数 默认用的是 shq 包. 注意: `__filename &amp; __dirname`这两个变量是在`commonjs`中的。我们用的是`.mjs`结尾的`es6` 模块。 在`ESM`模块中,`Node.js` 不提供`__filename`和` __dirname` 全局变量。 由于此类全局变量在脚本中非常方便,因此` zx` 提供了这些以在 `.mjs` 文件中使用(当使用 `zx` 可执行文件时) `require`也是`commonjs`中的导入模块方法, 在 `ESM` 模块中,没有定义 `require()` 函数。`zx`提供了 `require()` 函数,因此它可以与 `.mjs` 文件中的导入一起使用(当使用 `zx `可执行文件时) #### 传递环境变量 ```js process.env.FOO = 'bar' await $`echo $FOO` ``` #### 传递数组 如果值数组作为参数传递给 $,数组的项目将被单独转义并通过空格连接Example: ```js let files = [1,2,3] await $`tar cz ${files}` ``` 可以通过显式导入来使用 $ 和其他函数 ```js #!/usr/bin/env node import {$} from 'zx' await $`date` ``` zx 可以将 .ts 脚本编译为 .mjs 并执行它们 ```ts zx examples/typescript.ts ```
avatar avatar
请登录后留言

赶快写下您的第一条评论吧