大家好,我是秋风。
明天来探讨一个牛逼的我的项目 ——zx ,1个月增长15000 star, 成为了2021年度明星我的项目排行榜第一。
zx 到底是什么呢?
咱们能够从官网的介绍看到,一个能更不便地写脚本的工具。(A tool for writing better scripts)
Bash is great, but when it comes to writing scripts, people usually choose a more convenient programming language. JavaScript is a perfect choice, but standard Node.js library requires additional hassle before using. Thezx
package provides useful wrappers aroundchild_process
, escapes arguments and gives sensible defaults.
翻译:
Bash 用来写脚本十分棒,然而大家通常还是会去抉择一种更不便形式去编写脚本,例如应用像 JavaScript 这种编程语言。然而 Node.js 在应用之前须要很多额定的操作,比方装包、引库等。然而zx 提供更多便捷的性能并且还对 child_process 进行了简化封装,从而可能间接调用一些命令。
通过浏览摘要和形容,咱们能够晓得尽管 Bash 很棒,然而没有 Node.js 简略。尽管 Node.js 编写起来简略,然而在应用前还是有一些麻烦的操作。而zx 没有以上两种形式的毛病,可能化繁为简,提供简略又不便操作。
在持续深刻理解 zx 前,咱们先来屡分明目前提到的一些概念,理解这些概念有助于咱们更好地去写脚本。
Shell、Shell脚本、Bash、zx、Node
首先来说说什么是Shell,Shell的中文意思是贝壳,是指与操作内核连贯的外壳。
广义的Shell指的是命令行方面的软件,大多指Bash(Bash全称为 Bourne Again SHell ,是linux规范的默认Shell,它基于Bourne Shell,排汇了C Shell和Korn Shell );狭义的Shell则包含图形界面。
因而 Shell 是一个大概念,蕴含了 Bash 等这些命令行工具,而利用这些工具写的脚本叫做Shell 脚本;而 Node 属于编程语言,能够编写 js 文件来执行一些命令, zx 是基于 Node 开发的工具,因而也能通过编写脚本来执行命令。
他们之间的关系我用一张图进行了形容,题目的概念用红色字样进行了减轻。
脚本能够做那些事件?
最为简略的就是反复的事件、解决数据格式,数据导入导出以及各种简略罕用小工具的制作,环境配置等等。
举一些具体的例子就是:
下载视频
https://www.jianshu.com/p/0a0...
下载音乐
https://binaryify.github.io/N...
统计字数
https://geek-docs.com/shell/s...
主动签到
https://github.com/RWoxiN/Qia...
...
性能太多了列举不过去,反正你会的操作能帮你简化,你不会的操作能帮你实现。
哪些人能够应用?
脚本不仅仅能够帮忙开发人员还能帮忙非开发人员。
例如很多人都喜爱在集体博客下面写文章,这时就能够用WordPress 疾速搭建一个博客,而后咱们就用脚本一键来装置WordPress,上面以 Shell 脚本为例:
https://gist.github.com/dessi...
zx、Node、Shell(Bash) 性能评测
下面聊了脚本的一些概念以及脚本能帮忙咱们做什么。那么既然脚本这么弱小,且脚本品种也十分多,为什么 zx 一经推出就这么收欢送呢?
咱们就以理论的性能为例来体验一下,别离应用了zx、Node、Shell(Bash,以下都称作Bash )三种脚本写一个批量压缩音视频的脚本。
实现一个音频性能次要分成四个步骤
1.遍历当前目录
2.判断以后文件类型
3.执行压缩音频视脚本
首先咱们先来看遍历当前目录三种脚本的写法:
Bash
#!bin/bashfor file in `(ls)`;do ...done
Node
import fs from 'fs';const dirs = fs.readdirSync('./'));for (let i in dirs) { ... }
zx
const dirs = (await $`ls`).stdout.split('\n')for (let i in dirs) { ...}
能够看到 Bash 和 zx 差不多,然而 zx 比Node 省去了引包的代码。
劣势:zx = Bash > Node
其次咱们再来看判断以后文件类型三种脚本的写法:
Bash
if test -f $filethen filename=$(basename $file); if [ "${file##*.}"x = "mp4"x ];then fi if [ "${file##*.}"x = "mp3"x ]; then fifi
Node、zx
if (dirs[i] && !fs.statSync(source).isDirectory()) { if (source.endsWith(".mp4")) { } if (source.endsWith(".mp3")) { }}
用Shell 来写整体上代码都十分的精炼,然而对于不常常应用的人来说,经常会遇到一些问题,例如 if 语句格局十分严格、判断比拟的形式比拟非凡、字符串操作都比拟麻烦。
劣势 Node = zx > Bash
最初再来执行压缩音频视脚本:
Bash
...ffmpeg -i $file -r 30 -c copy -c:v libx264 -vf scale=720:-2 "${filename%%.*}-30-720".mp4;...
Node
const { spawn } = require('child_process');function run(command) { return new Promise((rev, rej) => { console.log(command); const cmd = spawn(command.slice(0, 1)[0], command.slice(1)); cmd.stdout.on('data', (data) => { console.log(`stdout: ${data}`); }); cmd.stderr.on('data', (data) => { console.error(`stderr: ${data}`); }); cmd.on('close', (code) => { console.log(`child process exited with code ${code}`); rev(); }); })}...await run(["ffmpeg", "-i", source ,"-r","30","-c", "copy","-c:v", "libx264", "-vf", "scale=720:-2", `${dirs[i].replace('.mp4', '')}-30-720.mp4`]);...
zx
$`ffmpeg -i ${file} -r 30 -c copy -c:v libx264 -vf scale=720:-2 ${file.replace(".mp4","")}-30-720.mp4;`;
用 zx 能够做到和 Shell 一样的精简,利用内置的一些 Node 包使得整体的代码量大大降落。Node须要写一些额定的代码,例如执行命令run等等。
劣势 Bash = zx > Node
上手水平 | 代码复杂度 | |
---|---|---|
Shell | 难 | 简洁 |
Node | 简略 | 繁琐 |
zx | 简略 | 简洁 |
zx 上手体验十分好,能够说用四个字来概括, “简洁易用”,至此你是否对 zx 心动了呢?