[TOC]
# 模板引擎
[art-template](https://aui.github.io/art-template/) 是一個簡約、超快的模板引擎。
它采用作用域預聲明的技術來優化模板渲染速度,從而獲得接近 JavaScript 極限的運行性能,并且同時支持 NodeJS 和瀏覽器。[在線速度測試](https://aui.github.io/art-template/rendering-test/)。
[EJS](https://ejs.co/)
[handlebars](http://handlebarsjs.com/)
[jade](http://jade-lang.com/)
# loT
## Node-RED
官網:https://github.com/node-red
# 系統
## systeminformation
官網:https://hub.fastgit.org/sebhildebrandt/systeminformation
# Web開發
## Ramda
Ramda is a practical functional library for JavaScript programmers. The library focuses on immutability and side-effect free functions. Ramda functions are also automatically curried.
~~~
$ npm i ramda
~~~
## cheerio
官網:https://github.com/cheeriojs/cheerio
jQuery 核心功能的 服務器端實現
## Osmosis
官網:https://github.com/rchipka/node-osmosis
Osmosis 是一個 HTML/XML 解析器和網頁數據采集工具。它是用 Node.js 編寫的,用 CSS3/xpath 選擇器和輕量級 HTTP 包裝器打包。它與 Cheerio、jQuery 和 jsdom 相比,它沒有顯著的依賴項。
## Nightmare
官網:https://github.com/segmentio/nightmare
一個來自 Segment 的高級瀏覽器的自動化庫。
## request-promise
官網:https://github.com/request/request-promise
Request-Promise 是 npm 庫中的一個變體,它通過自動化的瀏覽器提供了一個更快的解決方案。當內容不是動態呈現時,可以使用此網頁數據采集工具。如果要處理的網站有一個認證系統,它可以是一個更先進的解決方案。如果我們把它和 Puppeteer 相比,它的用法正好相反。
> 目前已被不贊成使用,可替代:https://github.com/request/request/issues/3143
## monk
官網:https://automattic.github.io/monk/
說明:一個很小的層,為Node.JS中的MongoDB使用提供了簡單而實質性的可用性改進。
## joi
官網:[https://npm.io/package/joi](https://npm.io/package/joi)
說明:Object schema description language and validator for JavaScript objects.
## morgan
HTTP request logger middleware for node.js
# 文件操作
## rimraf
官網:https://github.com/isaacs/rimraf#readme
說明:以包的形式包裝`rm -rf`命令,用來刪除文件和文件夾的,不管文件夾是否為空,都可刪除.
安裝:
~~~
npm i -D rimraf
~~~
使用:
~~~
const rimraf = require('rimraf');
rimraf('./test.txt', function (err) { // 刪除當前目錄下的 test.txt
console.log(err);
});
~~~
## chokidar
官網:https://github.com/paulmillr/chokidar
說明:
使用:
## node-watch
官網:https://github.com/yuanchuan/node-watch
說明:
使用:
# 輔助 npm
## npm-check-updates
官網:https://github.com/tjunnone/npm-check-updates
說明:npm 包版本更新工具,可以將`package.json` 或 `bower.json`中的依賴項升級到最新版。
使用:升級項目的包文件
~~~
$ ncu -u
express 4.12.x → 4.13.x
~~~
## Concurrently
官網:https://github.com/kimmobrunfeldt/concurrently#readme
說明:在前端工程化的時候,需要同時啟動**并行多個命令**。但是本身的 `&` 有時會意外出現問題,所以需要一個更好的解決方法。
安裝:
~~~
npm i -D concurrently
~~~
使用:
首先通過 安裝開發依賴。然后按照以下格式將其添加到腳本中:
~~~
copy{
"start": "concurrently \"command1 arg\" \"command2 arg\""
}
~~~
> 前端工程化并行解決方案-concurrently](https://zhuanlan.zhihu.com/p/65564606)
## npm-run-all
官網:http://github.com/mysticatea/npm-run-all
說明:一個CLI工具,可以并行或連續運行多個npm腳本。
> npm 本身也可以并行運行命令:參考《Web 前端工程化-node.js-`npm run`》部分。
使用:
為了使事情更加一致,我們可以使用一個名為`npm-run-all`的包。它提供了額外的命令,更具體地說就是,用`run-s`來運行串聯任務, 用 `run-p` 來運行并行任務,它將正確處理所有的子進程。
并行:
```
"scripts": {
"lint": "run-p eslint csslint htmllint" // "npm-run-all --parallel eslint csslint htmllint"
}
```
串行:
```
"scripts": {
"build": "run-s babel jest"
}
```
## ntl
官網:https://github.com/ruyadorno/ntl#readme
使用:script 的任務管理器
~~~
ntl
~~~
## json
官網:https://github.com/trentm/json
說明:`package.json` 是一個常規的 `json` 文件,因此可以使用工具庫 [json](http://trentm.com/json/) 從命令行進行編輯。 這在修改 `package.json` 提供另外一種新的方式,允許 w 你 q 創建超出默認值的快捷方式。
使用:
全局安裝:
~~~
npm i -g json
json --version
~~~
然后,可以使用它來使用 `-I` 進行就地編輯。 例如,要添加值為 “bar” 的新腳本 “foo”,這樣寫:
~~~shell
json -I -f package.json -e 'this.scripts.foo="bar"'
~~~
# 環境變量
## cross-env
* Windows 臨時配置
~~~shell
# node中常用的到的環境變量是NODE_ENV,首先查看是否存在
set NODE_ENV
# 如果不存在則添加環境變量
set NODE_ENV=production
# 環境變量追加值 set 變量名=%變量名%;變量內容
set path=%path%;C:\web;C:\Tools
# 某些時候需要刪除環境變量
set NODE_ENV=
~~~
* linux 臨時配置
~~~shell
# node中常用的到的環境變量是NODE_ENV,首先查看是否存在
echo $NODE_ENV
# 如果不存在則添加環境變量
export NODE_ENV=production
# 環境變量追加值
export path=$path:/home/download:/usr/local/
# 某些時候需要刪除環境變量
unset NODE_ENV
# 某些時候需要顯示所有的環境變量
ls env
~~~
問題是:`set`?只在?windows?下管用,在?Linux?下要使用?`export`,所有才有了?`cross-env`
### 使用方法
* 安裝
`npm?i ?-S?cross-env`
* 在?`NODE_ENV=xxxxxxx`?前面添加?`cross-env`?就可以了。(Linux方式)
~~~
{
...
"scripts": {
"demo2": "cross-var echo $npm_package_name",
"demo1": "cross-env NODE_ENV=test node src/index.js"
}
...
}
~~~
### vue?3.0?項目?`.env`?文件配置全局環境變量
首先根目錄下創建.
```
.env?或者.env.production?文件(生產環境),
.env.development?文件(開發環境)
```
變量命名格式:
```
VUE_APP_NAME?=?''
```
`VUE_APP_`?是規定的命名格式,`NAME`?是自定義的變量名(這點和?CRA?很像?[https://create-react-app.dev/docs/adding-custom-environment-variables](https://create-react-app.dev/docs/adding-custom-environment-variables))
通過:
```
process.env.variableName
```
獲取環境變量
```
data():{
??return{
????url:process.env.VUE_APP_URL
??}
}
```
## dotenv
官網:https://github.com/motdotla/dotenv
一個可以使得 Node.js **從文件中**加載環境變量的庫,使用 `dotenv`,我們只需要將程序的環境變量配置寫在 `.env` 文件中。
> create-react-app 就使用了這種方式,可以參考:[添加自定義環境變量](https://www.html.cn/create-react-app/docs/adding-custom-environment-variables/)
> ~~~
> * .env :默認。
> * .env.local :本地覆蓋。除 test 之外的所有環境都加載此文件。
> * .env.development , .env.test , .env.production :設置特定環境。
> * .env.development.local , .env.test.local , .env.production.local:設置特定環境的本地覆蓋。
> ~~~
~~~
# .env 默認文件
DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3
~~~
然后,在 Node.js 程序啟動時運行:
~~~
require('dotenv').config()
~~~
接著,我們就可以在接下來的程序中方便地使用環境變量了:
~~~
const db = require('db')
db.connect({
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASS
})
~~~
# 下載操作
## degit
官網:https://github.com/Rich-Harris/degit
說明:git 倉庫代碼下載
~~~shell
npx degit sveltejs/template my-svelte-project
~~~
## download-git-repo
官網:https://github.com/flippidippi/download-git-repo
說明:git 倉庫代碼下載
# 交互工具庫
| 包名 | 描述 |
| --- | --- |
| sade | |
| [sirv-cli](https://github.com/lukeed/sirv) | Quickly start a server to preview the assets of*any*directory! |
| [Enquirer](https://github.com/enquirer/enquirer) | Stylish CLI prompts that are user-friendly, intuitive and easy to create. |
| [validate-npm-package-name](https://www.npmjs.com/package/validate-npm-package-name) | 校驗包名 |
| [chalk](https://github.com/chalk/chalk) | 命令行輸出樣式美化 |
| [ora](https://github.com/sindresorhus/ora) | 命令行有趣的進度輸出 |
| [didyoumean](https://www.npmjs.com/package/didyoumean) | 腳本命令匹配 |
| [ncp](https://www.npmjs.com/package/ncp) | 異步的拷貝文件,包含空文件夾 |
| [log-symbols](https://www.npmjs.com/package/log-symbols) | 打印日志的特殊標志 |
| [recursive-readdir](https://www.npmjs.com/package/recursive-readdir) | 遞歸地列出目錄和子目錄下的所有文件,不包含目錄本身。 |
| [global-prefix](https://www.npmjs.com/package/global-prefix) | 獲取 npm 全局安裝的前綴 |
| [exec-sh](https://www.npmjs.com/package/exec-sh) | 執行 shell 命令轉發所有 stdio 流,比 shelljs exec 命令好用 |
| [wml](https://www.npmjs.com/package/wml) | wml 偵聽某個文件夾中的更改(使用 watchman),然后將更改的文件復制到另一個文件夾中。 |
| [cmd-open](https://blog.ihaiu.com/cmd-open/) | 擴展 Windows 命令 open |
| [shx](https://github.com/shelljs/shx) | shx 是對 ShellJS Unix 命令的包裝,為 npm 包腳本中的簡單的類 Unix 的跨平臺命令提供了一個簡單的解決方案。 |
> [nodejs 交互工具庫 -- hash-sum, deepmerge 和 yaml-front-matter](https://segmentfault.com/a/1190000037656240)
## Inquirer
https://github.com/SBoudrias/Inquirer.js
命令行交互輸入插件
### 參數詳解
* `type`: 表示提問的類型,包括:`input`, `confirm`, `list`, `rawlist`, `expand`, `checkbox`, `password`, `editor`;
* `name`: 存儲當前問題回答的變量;
* `message`: 問題的描述;
* `default`: 默認值;
* `choices`: 列表選項,在某些 type 下可用,并且包含一個分隔符 (separator);
* `validate`: 對用戶的回答進行校驗;
* `filter`: 對用戶的回答進行過濾處理,返回處理后的值;
* `transformer`: 對用戶回答的顯示效果進行處理 (如:修改回答的字體或背景顏色),但不會影響最終的答案的內容;
* `when`: 根據前面問題的回答,判斷當前問題是否需要被回答;
* `pageSize`: 修改某些 `type` 類型下的渲染行數;
* `prefix`: 修改 `message` 默認前綴;
* `suffix`: 修改 `message` 默認后綴。
## fs-extra
官網:https://www.npmjs.com/package/fs-extra
說明:fs 的替代品,提供了比 原生 fs 更多的方法
## semver
官網:https://www.npmjs.com/package/semver
說明:語義化日志控制
## oclif
官網:https://github.com/oclif/oclif
說明:簡單快速的命令行 App 開發框架
## yargs
官網:https://github.com/yargs/yargs
說明:模塊能夠解決如何處理命令行參數。
安裝
~~~
npm i -S yargs
~~~
使用:
yargs 模塊提供了 `argv` 對象,用來讀取命令行參數
~~~
#!/usr/bin/env node
let argv = require('yargs').argv;
console.log('hello ',argv.name);
~~~
運行:
~~~
hello --name=zxmf
hello --name zxmf
~~~
process.argv
~~~
[ '/usr/local/bin/node', '/usr/local/bin/hello4', '--name=zxmf' ]
~~~
Argv
~~~
{
name: 'zxmf',
}
~~~
## execa
官網:[execa](https://hub.fastgit.org/sindresorhus/execa)
說明:比 [`child_process`](https://nodejs.org/api/child_process.html) 更友好的進程執行庫
## shelljs
官網:https://hub.fastgit.org/shelljs/shelljs
說明:這個庫能夠讓我們在`js`文件中執行 shell 命令。
安裝:
~~~
npm?i?-D shelljs
~~~
使用:
```
//局部模式
var shell = require('shelljs');
//全局模式下,就不需要用shell開頭了。
//require('shelljs/global');
if (shell.exec('npm run build').code !== 0) {//執行npm run build 命令
shell.echo('Error: Git commit failed');
shell.exit(1);
}
//由于我的用另外一個倉庫存放dist目錄,所以這里要將文件增量復制到目標目錄。并切換到對應目錄。
shell.cp ('-r', './dist/*', '../../Rychou');
shell.cd('../../Rychou');
shell.exec('git add .');
shell.exec("git commit -m 'autocommit'")
shell.exec('git push')
```
## Commander
官網:http://github.com/tj/commander.js
使用:
* `command`: 定義命令行指令,后面可跟上一個 name,用空格隔開,如 `.command( 'app [name]')`
* `alias`: 定義一個更短的命令行指令
* `description`: 描述,它會在 help 里面展示
* `option`: 定義參數。它接受四個參數
* 在第一個參數中,它可輸入短名字 `-a` 和長名字 `–app`,使用 `|` 或者 `,` 分隔,在命令行里使用時,這兩個是等價的,區別是后者可以在程序里通過回調獲取到
* 第二個為描述,會在 `help` 信息里展示出來
* 第三個參數為回調函數,他接收的參數為一個 `string`,有時候我們需要一個命令行創建多個模塊,就需要一個回調來處理
* 第四個參數為默認值
* `action`: 注冊一個 `callback` 函數,這里需注意目前回調不支持 let 聲明變量
* `parse`: 解析命令行
> [Commander.js 助力命令行程序開發](http://www.uedlinker.com/2018/08/13/commander-jszhu-li-qian-duan-tong-xue-ming-ling-xing-cheng-xu-kai-fa/)
## terminal-kit
官網:https://github.com/cronvel/terminal-kit