Home  >  Article  >  Web Front-end  >  Learn about Nodejs and command line programs together

Learn about Nodejs and command line programs together

青灯夜游
青灯夜游forward
2021-05-27 11:37:342190browse

This article will introduce you to Nodejs and command line programs. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Learn about Nodejs and command line programs together

#Creation is speechless but has feelings. Every time when the cold comes, spring comes. Thousands of red and purple arrangements are waiting for the first sound of new thunder. —— Qing. Zhang Weiping's "New Thunder"

Origin

Many programs rooted in the Unix system environment implement the design of the Unix system Philosophy as a pursuit. Douglas McIlroy, the inventor of the Unix system pipeline mechanism, summarized the Unix philosophy into three points:

  • Focus on doing one thing and doing it to the extreme.

  • Programs work together.

  • For general interfaces, such as text data streams.

As Unix/Linux systems become more and more influential on servers and the development of various cross-platform solutions, this philosophy has also been brought to various platforms. Several years ago, when I first came into contact with NodeJS and its package management solution NPM, I felt that its officially advocated style was very consistent with the Unix system philosophy. In recent years, as NodeJS continues to develop in the fields of server-side and front-end construction, the ideas of NodeJS are rapidly penetrating into these fields.

In fact, NodeJS itself is also an important tool for developing command line programs. This article will introduce several commonly used NodeJS-related command line programs, then introduce several commonly used components in the development command line, and finally introduce how to publish npm packages and packages with scope.

[Recommended learning: "nodejs Tutorial"]

How the command line works

The command line can be simple It is defined as a user interaction interface and interaction method based on text flow. Command line programs often run in different ways by passing command line parameters. And because all commands are issued based on text, it also provides convenience for metaprogramming.

Command line programs can be compiled and executed, or they can be interpreted and executed. The compiled command line program will be executed directly in machine code. For most interpreted command line programs, running them often requires the help of a command line interpreter.

The command line program mentioned in this article specifically refers to the command line program that requires an interpreter.

Can be used as a command line interpreter, which actually includes common interpreters that everyone has heard of, such as bash, zsh, perl, python, ruby, tcl, etc., and of course NodeJS.

Open a command line program. The standard way of writing is to write the path of the interpreter program on the first line, such as:

#!/usr/local/opt/python/bin/python3.6

Here#! becomes the shebang, generally Located at the very beginning of the file. In Unix systems, the part following the line where #! is treated as an interpreter directive. At the same time, the path to the file will be appended to the interpreter as a parameter. In the above example, if the file is /usr/local/bin/pip, the effect of running /usr/local/bin/pip directly is equivalent to /usr/ local/opt/python/bin/python3.6 /usr/local/bin/pip.

In this way, the user does not need to care about the interpreter program or the language in which the code is written, and can just run the corresponding command line program itself. This is also the meaning of the existence of shebang. However, due to system settings, students using Windows may not be able to enjoy this convenience, and generally need to manually specify the path to the interpreter. However, they can be run with a double click :-).

You can try to use a text editing tool to open a script written in NodeJS, such as: webpack, and you will find that the first line is #!/usr/bin/env node . This sentence is not a direct NodeJS parser. Here, /usr/bin/env is a program whose purpose is to find the address of the interpreter with the corresponding name from the system's PATH. At this time, the interpreter can be installed in various paths and can be found as long as it is registered in the system PATH.

Maybe you have encountered this problem. When running some NodeJS programs, an error message will appear:

/usr/bin/env: node: No such file or directory

At this time, you can troubleshoot the problem by checking whether there is a file path called node in the system PATH, whether certain versions of NodeJS are named node, etc.

NodeJS related: easy-to-use command line tools

With NodeJS now becoming the main language for front-end workflow, babeland webpack has basically become an important tool for front-end development, testing, and publishing. At the same time, there are a series of peripheral toolkits and plug-ins around babel and webpack to assist developers in completing all aspects of daily development.

At the same time, the currently most popular front-end frameworks Angular, react, and vue (in alphabetical order) each have their own scaffolding and development assistance tools. Such as ng-cli, create-react-app and vue-cli etc. There are also scaffolding tools like Poi that take in both React and Vue.

Each of the above parts can be explained independently. Interested readers can refer to the official websites of the above tools for more information.

Let’s talk about several other NodeJS-related software packages.

Multiple versions coexist n/nvm

In most cases, we only need to face a single NodeJS version. When the time is right, upgrade the NodeJS version to a higher version.

However, the author once encountered a situation where a project that had been in disrepair for a long time needed to be re-maintained. At this time, you need to switch the NodeJS version to the old version. At the same time, we don’t want to abandon the new version of NodeJS environment that most projects run on.

In this case, you can use n or nvm. The figure below shows the process of using n to download and switch to a new version.

Learn about Nodejs and command line programs together

In addition to downloading, n also provides a list method to switch between multiple versions, and a method to delete a certain version. Readers can use n -h after installation to view all available parameters.

n is written in bash. However, it provides an entrance to the npm warehouse installation. You can use the traditional npm installation method for global installation. The premise is that you must have a NodeJS environment that can run.

npm install -g n

Or in an environment without NodeJS, you can use the n-install script. To install just run: curl -L https://git.io/n-install | bash.

If you are a Windows user, you can install wsl under Windows 10 to obtain the Linux script running environment. An issue in the official warehouse has an operation instruction.

For users with Windows 10 or below, you can consider using Cygwin.

In addition to n, there is also a management tool called nvm, which is also written in bash script. Installation can also be done using an installation script. For example: curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash or wget -qO- https://raw.githubusercontent .com/creationix/nvm/v0.34.0/install.sh | bash. v0.34.0 here is the version number, which may change with version iterations.

Readers who use windows, in addition to the above wsl and Cygwin, may consider using the nvm-windows version written in Golang.

As far as the latest version is concerned, both n and nvm will try to handle public dependent libraries, but the processing methods are different.

n and nvm will download this version of NodeJS to the local when using a certain version for the first time. The difference is: n will try to replace the system path with the new version. Key paths such as bin, lib, include, share package. nvm keeps a copy of each version and points the NodeJS system path to the sandbox address maintained by .nvm.

In terms of processing, nvm appears to be more lightweight and efficient, but it requires modifying the system's PATH. This step will be completed automatically by the nvm script. n, there is no need to invade the system path, but the operating system path is required every time you modify it. At this time, it is best to use sudo n to avoid being refused to copy to the system path due to insufficient permissions.

Since nvm will modify the PATH address, if nvm and n are installed by default at the same time, n will not operate properly. One solution is to avoid simultaneous installation. In addition, you can manually modify PATH so that the default NodeJS path precedes the nvm system path. For example, modify the PATH fragment to:

/usr/local/bin:/Users /leon/.nvm/versions/node/v10.6.0/bin:

Execution auxiliary nodemon/npx

nodemon is an executor, the meaning is , if the version changes or the program changes, there is no need to restart. This is very useful during development.

nodemon can also specify the running port, such as:

nodemon ./server.js localhost 8080

In addition to controlling the NodeJS package, nodemon You can also control non-NodeJS scripts, such as: nodemon --exec "python -v" ./app.py, which will monitor the content of app.py and call ## at the beginning and when changes occur. #python -vPerform parsing. Of course, if your app.py specifies the shebang, you don't need to specify the parsing function.

Learn about Nodejs and command line programs together

nodemon has many flexible configurations. Through these configurations, you can implement functions such as environment variable settings, delayed startup, command execution, monitoring of customized extensions, graceful restart, event monitoring, etc. . The method is to provide the relevant configuration

nodemon.json in the directory where these configurations are required, or you can specify it through the nodemonConfig field in package.json.

这里是官方提供的一份配置文件的样例,供读者参考。

再来说说npx。什么是npx呢?简单说,就是找到并运行一个包,并且“用完即走”。

这里有两层意思:

  • 找到。从哪里找:先是当前的依赖,然后是PATH,还找不到就到网上找来安装。

  • 用完即走。即使从网上安装的,运行完就会删掉,不会留下运行的包。 读者可以试着运行下:npx github:piuccio/cowsay "awesome npx"体验下。

这实在是居家旅行、开发调试的利器。比如我要在当前目录下开一个http服务,可以直接运行:npx http-server

Learn about Nodejs and command line programs together

之后就可以直接在浏览器访问这个地址进行调试了。

另外,如果你需要临时用一个老版本的node来运行某个脚本,也可以祭出npx,这个node会被临时安装、临时使用、用完即走。

npx -p node@6 npm init

切换NodeJS注册表 nrm/yrm

nrm/yrm维护了一个列表,包括npm主站和其他镜像。可以使用nrm/yrm use 快速切换,以达到最快的下载速度。nrm维护的是npm的注册表,yrm维护的是yarn注册表。

Learn about Nodejs and command line programs together

辅助编写NodeJS包

除了直接用大神们写好的命令之外,我们也可以按照自己的需求定制自己需要的NodeJS包。我们知道,命令行其实也是一种人机交互,因此,交互上有很多可以借鉴的效果。编写者只需将包倒入就可以使用这些交互效果。这里笔者给大家推荐几个包

命令行参数读取 commander

命令行的一个特点就是根据参数的不同调整运行策略。然而处理命令行输入以及验证是一个非常繁琐的事情。为此,TJ大神曾经创立了commander包。最基础的用法如下:

var program = require('commander');

program
  .version('0.1.0')
  .option('-p, --peppers', 'Add peppers')
  .option('-P, --pineapple', 'Add pineapple')
  .option('-b, --bbq-sauce', 'Add bbq sauce')
  .option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble')
  .parse(process.argv);

console.log('you ordered a pizza with:');
if (program.peppers) console.log('  - peppers');
if (program.pineapple) console.log('  - pineapple');
if (program.bbqSauce) console.log('  - bbq');
console.log('  - %s cheese', program.cheese);

默认地,commander会自动创建-h的帮助文件,即利用每一个option的输入产生帮助文案。

Learn about Nodejs and command line programs together

用户的每一个输入,都会放置在program对应option长名的字段的驼峰形式上,如果没有提供长名,则放在短名字段上。上例中,如使用: testcommander -p 111 -P 222 -b 333则依次存储在programpepperspineapplebbqSauce上。

同时,commander提供多种验证方式,如正则表达式:

program.option('-s --size <size>', 'Pizza size', /^(large|medium|small)$/i, 'medium')</size>

则指定只能输入特定的值。

同时,commander提供一个方案,允许用户设置子命令。commander称之为Git风格的子命令。

var program = require(&#39;commander&#39;);

program
  .version(&#39;0.1.0&#39;)
  .command(&#39;install [name]&#39;, &#39;install one or more packages&#39;)
  .command(&#39;search [query]&#39;, &#39;search with optional query&#39;)
  .command(&#39;list&#39;, &#39;list packages installed&#39;, {isDefault: true})
  .parse(process.argv);

这个例子中,假设命令行名字为pm,则当用户输入pm-installpm-searchpm-list时候,commander会尝试在入口文件的同一级目录找到installsearchlist,并交给这个文件去执行。

进度条 progress

在编写web程序时候,大家经常会展示一个进度条。用以缓解用户在等待时候的焦虑。其实在命令行程序中也会有这种交互方式。比如wget就会在下载过程中给出进度提示。

在NodeJS中也有这样的效果可以使用。这就是progress包。下面的代码,运行结果是下载CentOS安装盘。在下载之中,会实时打印进度

const ProgressBar = require("progress")
const request = require("request")
const progress = require("request-progress")
const fs = require("fs")

const download = (url, headers, target, totalSize) => {
    let percent = 0

    const bar = new ProgressBar(&#39;下载中: ├:bar┤ 完成:percent 预估完成时间:eta秒 用时:elapseds&#39;, {
        total: 100,
        complete: "█",
        incomplete: "─",
        width: 60
    })

    let opt = {
        headers,
        url: url
    }

    return new Promise((resolve, reject) => {
        progress(request.get(opt))
            .on(&#39;progress&#39;, function (state) {
                let progressFix = ((state.percent) * 100).toFixed(2)
                delta = progressFix - percent
                bar.tick(delta)
                percent = progressFix
            })
            .on("error", () => {
                return reject()
            })
            .on(&#39;end&#39;,  () => {
                bar.tick(100 - percent)
                console.log(&#39;\n&#39;)
                return resolve(target)
            })
            .pipe(fs.createWriteStream(target));
    })
}

const foo = {
    getHeaders: () => {
        const headers = {
            &#39;Accept&#39;: &#39;text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8&#39;,
            &#39;Accept-Charset&#39;: &#39;UTF-8,*;q=0.5&#39;,
            &#39;Accept-Encoding&#39;: &#39;gzip,deflate,sdch&#39;,
            &#39;Accept-Language&#39;: &#39;en-US,en;q=0.8&#39;,
            &#39;User-Agent&#39;: &#39;Mozilla/5.0 (X11; Linux x86_64; rv:13.0) Gecko/20100101 Firefox/13.0&#39;
        }

        return Object.assign({}, headers)
    },

    download: function (url, target, totalSize){
        let headers = this.getHeaders()
        headers = Object.assign(headers)

        download(url, headers, target, totalSize)
    }
}


foo.download("http://mirrors.cmich.edu/centos/7.6.1810/isos/x86_64/CentOS-7-x86_64-DVD-1810.iso",
    "CentOS-7-x86_64-DVD-1810.iso", 4508876.8
    )

运行的结果如图:

Learn about Nodejs and command line programs together

这个包的核心就是根据内置和自定义的token在命令行打印出相应的字符,用以完成交互。

交互着色 chalk

chalk是一个命令行交互的着色工具。在命令行支持的情况下,可以支持最多16位色域(前提是命令行终端可以支持)。一般可以配合console.log使用,如:

const chalk = require(&#39;chalk&#39;);
const log = console.log;

// Combine styled and normal strings
log(chalk.blue(&#39;Hello&#39;) + &#39; World&#39; + chalk.red(&#39;!&#39;));

笔者曾经做过一个在命令行下显示图片的程序,就是利用的chalk和console.log进行的配合。

Learn about Nodejs and command line programs together

交互式问答 inquirer

在需要不断的同用户进行交互式问答,并根据用户的输入进行验证和路径选择,这个时候inquirer是非常趁手的工具。它内置了单选、多选、问答等多种交互方式。大家可以感受下:

Learn about Nodejs and command line programs together

Learn about Nodejs and command line programs together

Learn about Nodejs and command line programs together

Learn about Nodejs and command line programs together

1Learn about Nodejs and command line programs together

1Learn about Nodejs and command line programs together

甚至可以通过插件实现suggest

Learn about Nodejs and command line programs together

vue框架的脚手架vue-cli是一个使用inquire的绝佳案例,读者可以通过阅读源码,感受下大神出神入化的使用。

小图标 ora

ora打印出一个优雅的文本小图标,用于在各种情况下给出用户优雅而清晰的提示。用法很简单:

const ora = require(&#39;ora&#39;);

const spinner = ora(&#39;Loading unicorns&#39;).start();

setTimeout(() => {
	spinner.color = &#39;yellow&#39;;
	spinner.text = &#39;Loading rainbows&#39;;
}, 1000);

Learn about Nodejs and command line programs together

命令行玩浏览器 puppeteer

puppeteer是谷歌开发的无头浏览器,使得命令行亦可操作浏览器,并能根据浏览器的执行结果进行进一步操控。因为puppeteer源自官方,所以之前类似项目PhantomJS的开发者决定不再更新PhantomJS。

目前puppeteer已经广泛用于前端测试,端对端测试,以及爬虫。

鉴于篇幅无法展开介绍,读者可以参考其官方文档。同时,奇舞周刊中黄小璐老师的的这篇文章以及李光钊老师的这篇文章都曾经介绍过puppeteer的使用。

发布NodeJS包

写好的NodeJS包需要发布出去,才能给大家使用。npm publish就是为了这个需求而产生的。为了发布你需要在npm上注册用户,并登录,然后发布就好了。npm的详情页面以及各个镜像会在一段时间内自动更新。

如果你的NodeJS包,是使用尚未广泛支持的语法写成的。那么需要在package.json的script字段加入prepublish命令,调用babel等预编译器处理,使得程序可以有更多的兼容性。

对于希望用户在全局使用的命令,要注意在根目录写好入口,一般是在package.json中的bin字段,指定入口文件。在安装时,如果是全局安装,npm将会使用符号链接把这些文件链接到prefix/bin,如果是本地安装,会链接到./node_modules/.bin/。

除了通常的包,还有一种是带有scope的包,vue-cli的3.0版本就是@vue开头的。这个scope是组织的名字。每一个带有scope的包有公有和私有之分,私有的需要付费给npm。

目前npm的读写权限策略如下:

1Learn about Nodejs and command line programs together

如果是个人,可以考虑增加公有的命名空间。如果是企业付费用户,你在发布相关包之前,需要申请成为这个scope的member。

对公有scope,首先将包的name改为@scope名字/包名,同时,在发布时,使用npm publish --access public即可。

小结

本文简述了命令行的意义和优势,介绍了解释型命令行的运行机制,同时介绍了几个NodeJS相关的命令行工具,推荐了几款撰写命令行程序常用的包,最后,概述了发布包和使用scope的发布情况。希望给大家的NodeJS命令行相关开发和技术选型,提供一些有用的帮助。

原文地址:https://juejin.cn/post/6844903821672448008

作者:刘观宇

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of Learn about Nodejs and command line programs together. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete