Hello everyone, I am Qiufeng.
Today, let's discuss a great project - zx, which has grown by 15,000 stars in one month, and has become the number one star project list in 2021.
What the hell is zx?
We can see from the introduction of the official website, a tool that can write scripts more conveniently. (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.
translate:
Bash is great for scripting, but people usually choose a more convenient way to write scripts, such as using a programming language like JavaScript. But Node.js requires a lot of additional operations before use, such as packaging, importing libraries, etc. But zx provides more convenient functions and also simplifies the encapsulation of child_process, so that some commands can be called directly.
By reading the abstract and description, we can see that while Bash is great, it is not as simple as Node.js. Although Node.js is simple to write, there are still some cumbersome operations before using it. And zx does not have the shortcomings of the above two methods, it can simplify the complexity and provide simple and convenient operation.
Before continuing to understand zx in depth, let's clear some of the concepts mentioned so far. Understanding these concepts will help us to write scripts better.
Shell, Shell Script, Bash, zx, Node
First of all, what is Shell, the Chinese meaning of Shell is shell, which refers to the shell connected to the operating kernel.
Shell in a narrow sense refers to command-line software, mostly 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 generalized Shell includes graphics interface.
Therefore, Shell is a big concept, including Bash and other command-line tools, and scripts written with 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, Therefore, commands can also be executed by writing scripts.
The relationship between them is described with a picture, and the concept of the title is emphasized with red words.
What can scripts do?
The simplest is to repeat things, process data formats, import and export data, and make various simple and commonly used gadgets, environment configuration, and so on.
Some specific examples are:
download video
https://www.jianshu.com/p/0a013fa5a250
download music
https://binaryify.github.io/NeteaseCloudMusicApi/#/
Word Count
Automatic sign-in
https://github.com/RWoxiN/Qiandao
...
There are too many functions to list. Anyway, the operations you know can help you simplify, and the operations you don't can help you achieve.
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 blogs. At this time, you can use WordPress to quickly build a blog, and then we use the script to install WordPress with one click. Here is an example of a shell script:
https://gist.github.com/dessibelle/2666478
zx, Node, Shell (Bash) function evaluation
The above talked about some concepts of scripting and what scripting can help us do. So since the script is so powerful and there are many types of scripts, why is zx so popular once it is launched?
Let's take the actual function as an example to experience it. We use three scripts: zx, Node, and Shell (Bash, hereinafter referred to as Bash) to write a batch compressing audio and video script .
Implementing an audio function is mainly divided into four steps
1. Traverse the current directory
2. Determine the current file type
3. Execute compressed audio and video scripts
First, let's look at the three scripts traversing the current directory
Bash
#!bin/bash
for 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) {
...
}
It can be seen that Bash is similar to zx, but zx saves the code for importing packages than Node.
Advantage: zx = Bash > Node
Next, let's look at the writing judge the current file type 161e505f2af2a9:
Bash
if test -f $file
then
filename=$(basename $file);
if [ "${file##*.}"x = "mp4"x ];then
fi
if [ "${file##*.}"x = "mp3"x ]; then
fi
fi
Node、zx
if (dirs[i] && !fs.statSync(source).isDirectory()) {
if (source.endsWith(".mp4")) {
}
if (source.endsWith(".mp3")) {
}
}
Using Shell to write the overall code is very refined, but for people who don't use it often, they often encounter some problems, such as the format of the if statement is very strict, the way of judgment and comparison is special, and the string operations are more troublesome.
Advantage Node = zx > Bash
Finally, execute the compressed audio and video script :
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;`;
With zx, you can achieve the same simplification as Shell, and use some built-in Node packages to greatly reduce the overall code size. Node needs to write some extra code, such as executing the command run and so on.
Advantage Bash = zx > Node
Getting started | code complexity | |
---|---|---|
Shell | difficult | concise |
Node | Simple | cumbersome |
zx | Simple | concise |
The experience of getting started with zx is very good. It can be summed up in four words, "concise and easy to use". Are you excited about zx now?
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。