溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

基于node的cli工具怎么開發(fā)使用

發(fā)布時(shí)間:2023-03-31 14:35:58 來(lái)源:億速云 閱讀:112 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“基于node的cli工具怎么開發(fā)使用”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

背景

公司內(nèi)部有維護(hù)adminh6兩套基礎(chǔ)模版,但是每次新項(xiàng)目需要打開gitlabgit地址 clone,稍微有點(diǎn)麻煩,所以集成一個(gè)工具,方便使用。

效果預(yù)覽

基于node的cli工具怎么開發(fā)使用

插件開發(fā)

推薦使用nrm管理npm

  • 創(chuàng)建用戶

npm adduser --registry http://127.0.0.1:4873/
  • 發(fā)布

npm publish --registry http://127.0.0.1:4873/

使用

  • 安裝

npm install web-cli -g --registry http://127.0.0.1:4873/
  • 使用

web-cli create

實(shí)現(xiàn)原理

使用到的工具

  • chalk: 修改終端顏色

  • commander: 添加版本命令提示

  • inquirer: 命令行交互

  • minimis: 命令行解析

  • ora: 終端旋轉(zhuǎn)器

  • shelljs: 執(zhí)行shell命令

  • rimraf:刪除文件夾

package.json

{
  "name": "web-cli",
  "version": "0.0.5",
  "description": "前端腳手架",
  "main": "dist/src/service.js",
  "author": {
    "name": "taosiqi",
    "email":"thinlf97@gmail.com"
  },
  "keywords": [
    "rs",
    "cli"
  ],
  "bin": {
    "web-cli": "dist/src/bin/web-cli.js" //會(huì)注冊(cè)成全局可執(zhí)行文件
  },
  "scripts": {
    "dev": "tsc -w -p .",
    "build": "rimraf dist && tsc -p .",
    "test": "echo "Error: no test specified" && exit 1",
    "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s"
  },
}

web-cli.ts

#!/usr/bin/env node
// 第一行不可少,其作用是它告訴系統(tǒng)這個(gè)腳本需要用node解釋器來(lái)執(zhí)行。
// 這個(gè)文件主要是用來(lái)處理接受參數(shù)的入口文件,
const Service = require('../service') //引入我們的入口文件
const service = new Service() //實(shí)例化Service
const rawArgv = process.argv.slice(2)
const args = require('minimist')(rawArgv) //解析命令行參數(shù)
const command = args._[0]
// 執(zhí)行初始化
service.run(command, args, rawArgv)

service.ts

import program from 'commander'
import packageInfo from '../package.json'
import { create } from "./commands/create";
module.exports = class Service {
    constructor() {
        setupDefaultCommands() //設(shè)置默認(rèn)命令
    }
    run(_id, _args = {}, rawArgv = []) {
        program.parse(rawArgv, { from: 'user' })  //執(zhí)行相應(yīng)的命令
    }
}
// 設(shè)置默認(rèn)命令
const setupDefaultCommands = () => {
    program.version(packageInfo.version, '-v, --version', '輸出當(dāng)前版本號(hào)')
    program.helpOption('-h, --help', '獲取幫助')
    program.command('create').description('新建項(xiàng)目').alias('c').action(async () => {
        await create()
    })
    program.addHelpCommand(false)
}

create.ts

import shelljs from 'shelljs'
import inquirer from 'inquirer'
import * as fs from 'fs'
import rimraf from 'rimraf'
import log from '../utils/log'
const templateType = {
    type: 'list',
    message: '請(qǐng)選擇模版類型',
    name: 'type',
    choices: [
        {
            name: 'h6',
            value: 'vue3-h6-starter'
        },
        {
            name: 'admin',
            value: 'vue3-admin-starter'
        }
    ]
}
const templateName = [
    {
        type: 'input',
        message: '請(qǐng)輸入項(xiàng)目名稱:',
        name: 'name',
        validate: (val) => {
            return val !== ''
        }
    }
]
export const create = async () => {
    // 選擇模版類型
    let { type } = await inquirer.prompt([templateType])
    // 項(xiàng)目名稱
    let { name } = await inquirer.prompt(templateName)
    // 拼接git地址,自行替換「」字段
    const url = `git clone http://gitlab-ci-token:「token」@「ip/域名」/pinxin/${type}.git --depth 1`
    // 執(zhí)行clone
    await shelljs.exec(url)
    // 重命名
    await fs.renameSync(`./${type}`, `./${name}`)
    // 刪除無(wú)關(guān)文件
    await rimraf(`./${name}/.git`)
    await rimraf(`./${name}/.idea`)
    await rimraf(`./${name}/.vscode`)
    log.succeed('創(chuàng)建成功')
    log.info(`cd ${name}`)
    log.info(`pnpm install`)
}

“基于node的cli工具怎么開發(fā)使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI