利用node.js開發(fā)cli的完整步驟
CLI介紹
命令行界面(英語:command-line interface,縮寫:CLI),是在圖形用戶界面得到普及之前使用最為廣泛的用戶界面,它通常不支持鼠標,用戶通過鍵盤輸入指令,計算機接收到指令后,予以執(zhí)行。
目前前端開發(fā)中,CLI是常用的工具。前端三大框架Vue、React、Angular都有對應的CLI,包括現(xiàn)在最流行的前端工程化的打包工具Webpack,也有對應的webpack-cli。
在現(xiàn)代的前端開發(fā)中,CLI提高了開發(fā)的效率。讓相應的前端開發(fā)者免去了大量的重復性操作,節(jié)省了大量的時間。CLI可以完成的功能,包括但不限于初始化生成對應的項目模板、執(zhí)行特定的腳本文件、在項目中創(chuàng)建新的模塊 。下面來介紹一下前端工程師如何使用node.js來完成一個CLI。
創(chuàng)建項目
打開終端,創(chuàng)建moka-cli的目錄
mkdir moka-cli
進入目錄,初始化項目
cd moka-cli npm init -y
根目錄下新建bin目錄,并新建index.js文件,此時目錄結(jié)構(gòu)如下
|-- moka-cli |-- package.json |-- bin |-- index.js
開發(fā)cli需要借助commander、inquirer、chalk三個庫
npm install commander inquirer chalk --save
修改package.json文件,使用es moudle,并新增moka命令
package.json
{
"name": "moka-cli",
"version": "1.0.0",
"description": "",
"main": "index.js",
+ "type": "module",
+ "bin": {
+ "moka": "./bin/index.js"
+ },
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"chalk": "^4.1.0",
"commander": "^6.2.1",
"inquirer": "^7.3.3"
}
}
接下來需要在/bin/index.js中編程,在第一行添加如下代碼,這行代碼是告訴系統(tǒng)要用node來執(zhí)行這個文件。
#!/usr/bin/env node
再添加下面代碼
#!/usr/bin/env node
console.log('moka-cli run!')
然后在項目根目錄下全局安裝項目
npm install -g
到這里,一個簡單的CLI就完成了,可以打開終端,在任意目錄下輸入moka,控制臺就會打印出對應的消息。
$ moka moka-cli run!
第三方庫介紹
moka-cli希望實現(xiàn)可以創(chuàng)建前端開發(fā)模板的功能,可以使用moka命令來創(chuàng)建一個vue或react項目。
先來介紹一下使用到到第三方的庫
commander
用來實現(xiàn)命令的庫,在moka-cli中會用來實現(xiàn)create命令,實現(xiàn)用下面的命令行來創(chuàng)建一個vue-demo的項目
moka create vue vue-demo
inquirer
實現(xiàn)用戶和終端交互的庫,在moka-cli中使用create指令的時候會詢問是否覆蓋已有項目
$ moka create vue moka-demo ? Template named moka-demo is already existed, are you sure to overwrite? (Y/n)
chalk
可以在終端界面顯示出多種顏色的文本和背景
核心功能
在根目錄創(chuàng)建actions和templates目錄,目錄結(jié)構(gòu)如下
|-- moka-cli |-- package-lock.json |-- package.json + |-- actions + | |-- create.js |-- bin | |-- index.js + |-- templates + |-- react + | |-- app.js + | |-- index.js + | |-- src + | |-- index.js + |-- vue + |-- app.js + |-- index.js + |-- src + |-- index.js
templats下面用來存放通過CLI要生成的項目的模板,在這里先隨便創(chuàng)建幾個文件展示一下。
修改/bin/index.js文件
#!/usr/bin/env node
import create from '../actions/create.js';
import commander from 'commander';
const { program } = commander;
program.version('0.0.1');
program
.command('create <template> [name]')
.description('新建項目')
.action(create);
program.parse(process.argv);
修改./actions/create.js文件
import fs from 'fs';
import path from 'path';
import chalk from 'chalk';
import inquirer from 'inquirer';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const TEMPLATES = ['vue', 'react'];
const targetPath = process.cwd();
function createTemplate(template, name) {
const templatePath = path.join(__dirname, '../', `templates/${template}`);
function readAndCopyFile(parentPath, tempPath) {
let files = fs.readdirSync(parentPath);
files.forEach((file) => {
let curPath = `${parentPath}/${file}`;
let stat = fs.statSync(curPath);
let filePath = `${targetPath}/${tempPath}/${file}`;
if (stat.isDirectory()) {
fs.mkdirSync(filePath);
readAndCopyFile(`${parentPath}/${file}`, `${tempPath}/${file}`);
} else {
const contents = fs.readFileSync(curPath, 'utf8');
fs.writeFileSync(filePath, contents, 'utf8');
}
});
}
readAndCopyFile(templatePath, name);
}
function deleteTemplate(path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function (file, index) {
var curPath = path + '/' + file;
if (fs.lstatSync(curPath).isDirectory()) {
// recurse
deleteTemplate(curPath);
} else {
// delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}
export default function create(template, name) {
if (!TEMPLATES.includes(template)) {
console.log(chalk.red(`No ${template} Template`));
return;
}
const projectPath = path.resolve(targetPath, name);
if (fs.existsSync(projectPath)) {
inquirer
.prompt([
{
name: 'template-overwrite',
type: 'confirm',
message: `Template named ${name} is already existed, are you sure to overwrite?`,
validate: function (input) {
if (input.lowerCase !== 'y' && input.lowerCase !== 'n') {
return 'Please input y/n !';
} else {
return true;
}
},
},
])
.then((answers) => {
// 如果確定覆蓋
if (answers['template-overwrite']) {
// 刪除文件夾
deleteTemplate(projectPath);
console.log(chalk.yellow(`Template already existed , removing!`));
//創(chuàng)建新模塊文件夾
fs.mkdirSync(projectPath);
createTemplate(template, name);
console.log(
chalk.green(`${template} template has been created successfully!`)
);
}
})
.catch((err) => {
console.log(chalk.red(err));
});
} else {
fs.mkdirSync(projectPath);
createTemplate(template, name);
console.log(
chalk.green(`${template} template has been created successfully!`)
);
}
}
最后在項目根目錄運行以下命令,重新安裝一下moka-cli
npm install -g
隨便找一個路徑,運行moka create <template> [name]命令來生成項目
moka create react react-demo
效果如下,會在該目錄下生成一個react-demo的文件夾,里面存放的就是moka-cli項目中/templates/react下的所有
$ moka create react react-demo react template has been created successfully!
如果在該目錄下繼續(xù)創(chuàng)建一個同名的項目,就會提示是否覆蓋,輸入y后繼續(xù)執(zhí)行
$ moka create react react-demo ? Template named react-demo is already existed, are you sure to overwrite? Yes Template already existed , removing! react template has been created successfully!
create命令的核心邏輯是通過node的fs模塊來復制/templates下的文件,然后將其放到指定的路徑下,具體實現(xiàn)直接看代碼就可以來。
總結(jié)
CLI是提示前端開發(fā)效率的重要工具,如果有長期維護的項目,開發(fā)一個CLI來完成一些重復性的工作,是一個不錯的選擇。
moka-cli還沒有上傳github,等過段時間完善一些/templates下的項目模板,我會在文章里補充項目地址。
到此這篇關(guān)于利用node.js開發(fā)cli的文章就介紹到這了,更多相關(guān)node.js開發(fā)cli內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
electron demo項目npm install安裝失敗的解決方法
下面小編就為大家分享一篇electron demo項目npm install安裝失敗的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02
Node.js入門教程:在windows和Linux上安裝配置Node.js圖文教程
這篇文章主要介紹了Node.js入門教程:在windows和Linux上安裝配置Node.js的方法,本文圖文并茂,步驟明細,是學習安裝node.js的絕佳教程,需要的朋友可以參考下2014-08-08
Node.js發(fā)出請求走Proxyman代理調(diào)試tip詳解
這篇文章主要為大家介紹了Node.js發(fā)出請求走Proxyman代理調(diào)試tip詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08
node.js 使用 net 模塊模擬 websocket 握手進行數(shù)據(jù)傳遞操作示例
這篇文章主要介紹了node.js 使用 net 模塊模擬 websocket 握手進行數(shù)據(jù)傳遞操作,結(jié)合實例形式分析了node.js基于net模塊模擬 websocket握手相關(guān)原理及進行數(shù)據(jù)傳遞具體操作技巧,需要的朋友可以參考下2020-02-02

