Hello, I am Qiu Feng.
Today, we are going to discuss an awesome project — ZX, which has grown by 15,000 stars in one month and become the no.1 star project in 2021.
What exactly is Zx?
We can see from the official website, a tool to make scripting easier. (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. The
zx
package provides useful wrappers aroundchild_process
, escapes arguments and gives sensible defaults.
Translation:
Bash is great for scripting, but people often choose a more convenient way to script, such as using a programming language like JavaScript. However, node.js requires a lot of extra operations before it can be used, such as packing, fetching libraries, etc. But ZX provides more convenience and simplifies the encapsulation of child_process so that some commands can be invoked directly.
Reading the summary and description, we can see that while Bash is great, it’s not as simple as Node.js. While Node.js is easy to write, there are some tricky things to do before using it. However, ZX does not have the disadvantages of the above two ways, which can simplify complexity and provide simple and convenient operation.
Before we dive further into ZX, let’s take a look at some of the concepts mentioned so far that will help us write better scripts.
Shell, Shell script, Bash, zx, Node
First of all, let’s talk about what is Shell. Shell means Shell in Chinese, and it refers to the Shell connected with the operating kernel.
In the narrow sense, Shell refers to software on the command line, mostly referring to Bash (Bash is called Bourne Again Shell, which is the default Shell of the Linux standard. It is based on Bourne Shell and absorbs C Shell and Korn Shell). The broad Shell includes a graphical interface.
So a Shell is a big concept that includes command-line tools like Bash, and scripts written using these tools are called Shell scripts; While Node is a programming language, you can write JS files to execute some commands. Zx is a tool developed based on Node, so you can also write scripts to execute commands.
The relationship between them is described in a diagram, and the concept of the title is highlighted in red.
What can a script do?
The simplest things are repetitive things, dealing with data formats, data import and export, as well as various simple common widgets, environment configuration and so on.
Here are some concrete examples:
Download the video
www.jianshu.com/p/0a013fa5a…
Download music
Binaryify. Making. IO/NeteaseClou…
Statistical word
Geek-docs.com/shell/shell…
Automatic check-in
Github.com/RWoxiN/Qian…
.
There are too many functions to enumerate, anyway, you can help you simplify the operation, you can help you realize the operation.
Who can use it?
Scripts can help not only developers but also non-developers.
For example, many people like to write articles on their personal blog, then you can use WordPress to quickly build a blog, and then we use a script to install WordPress with one click, the following Shell script for example:
Gist.github.com/dessibelle/…
Zx, Node, Shell(Bash) function evaluation
I talked about some of the concepts of scripting and what scripting can help us do. So, given that scripts are so powerful and there are so many different types of scripts, why is zX so popular right away?
Let’s take the actual function as an example to experience, respectively using zx, Node, Shell(Bash, hereinafter called Bash) three scripts to write a batch compression of audio and video scripts.
There are four main steps to implementing an audio feature
1. Traverse the current directory
2. Check the current file type
3. Execute the compressed audio video script
First let’s look at three scripts to iterate through the current directory:
Bash
#! bin/bash for file in `(ls)`; do ... doneCopy the code
Node
import fs from 'fs';
const dirs = fs.readdirSync('./'));
for (let i in dirs) {
...
}
Copy the code
zx
const dirs = (await $`ls`).stdout.split('\n')
for (let i in dirs) {
...
}
Copy the code
As you can see, Bash is similar to Zx, except that ZX saves on the code for marshalling compared to Node.
Advantage: zx = Bash > Node
Secondly, we will look at the writing method of three scripts to judge the current file type:
Bash
if test -f $file then filename=$(basename $file); if [ "${file##*.}"x = "mp4"x ]; then fi if [ "${file##*.}"x = "mp3"x ]; then fi fiCopy the code
The Node, zx
if (dirs[i] && ! fs.statSync(source).isDirectory()) { if (source.endsWith(".mp4")) { } if (source.endsWith(".mp3")) { } }Copy the code
Writing the code in Shell is generally very concise, but for those who don’t use it often, there are some problems, such as if statement format is very strict, comparison is judged in a special way, and string manipulation is troublesome.
Advantage Node = zx > Bash
Finally, execute the compressed audio video script:
Bash
. ffmpeg -i $file -r 30 -c copy -c:v libx264 -vf scale=720:-2 "${filename%%.*}-30-720".mp4; .Copy the code
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`]); .Copy the code
zx
$`ffmpeg -i ${file} -r 30 -c copy -c:v libx264 -vf scale=720:-2 ${file.replace(".mp4","")}-30-720.mp4; `;Copy the code
Zx can be as streamlined as Shell, with some built-in Node packages that reduce the overall amount of code. Node needs to write some extra code, such as executing the command run and so on.
Bash = zx > Node
To fit degree | Code complexity | |
---|---|---|
Shell | difficult | concise |
Node | simple | tedious |
zx | simple | concise |
Zx experience is very good, can be summed up in four words, “simple and easy to use”, so you have zX heart?