当前位置: 首页 > news >正文

【VUECLI】node.js打造自己的前端cli脚手架工具

都用过vue技术栈的cli工具vue-cli,react的cra脚手架,还有vite是全部技术栈都可以使用工具。方便了我们的项目工程的创建,构建过程。

这里借鉴下我们使用vuecli的过程,初步实现一个自己的脚手架工具

初始化项目

npm init -y

在这里插入图片描述

{"name": "demo02","version": "1.0.0","description": "","main": "index.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1"},"bin": {"mycli": "./bin/index.js"},"keywords": [],"author": "","license": "ISC","devDependencies": {"chalk": "^4.0.0","commander": "^14.0.1","download-git-repo": "^3.0.2","inquirer": "^8.2.4","ora": "^5.3.0"}
}

创建如下的工程结构
在这里插入图片描述

入口文件

  • bin/index.js

注意在文件的开头使用#!/usr/bin/env node 表明这个文件是可执行的文件

#!/usr/bin/env node 
const { program } = require('commander');
const myHelp = require('../lib/core/help');
const myCommander = require('../lib/core/commander');myHelp(program)
myCommander(program)
program.parse(process.argv); // 表示使用 Commander 来处理命令行参数

在 package.json文件中加入如下的配置

"bin": {"mycli": "./bin/index.js"
}

在项目根目录下面,npm link,将mycli挂载到全局,以后在其他地方都可以执行mycli命令

在这里插入图片描述

  • action.js
const inquirer = require('inquirer');
const config = require("../../config");
const download = require('download-git-repo')
const ora = require('ora')
const chalk = require('chalk')
const myAction = (project, args) => { // 命令行的执行逻辑代码inquirer.prompt([{type: 'input',name: 'name',message: 'What is your project name?'},{type:'list',name: 'framework',message: 'Which framework do you use?',choices: config.framework}]).then(answers => {console.log(answers)const spinner = ora({ color: 'yellow' }).start() // 创建实例并启动加载指示器spinner.text = '代码正在下载……' // 下载过程中在命令行展示的 loading 信息download(`direct:${config.frameworkUrls[answers.framework]}`, answers.name, { clone: true }, err => {if (!err) {spinner.succeed('代码下载成功')// 下载成功后,提示使用者j接下来可执行的操作spinner.succeed('代码下载成功')console.log(chalk.green.bold('Done!'), chalk.yellow('you run:\n\n'));console.log(chalk.blue.bold('cd ') + chalk.yellow(answers.name));console.log(chalk.blue.bold('         npm install\n'));console.log(chalk.blue.bold('         npm run dev '));} else {spinner.fail(chalk.red.bold('代码下载失败'))}})})
}
module.exports = myAction
  • commader.js
const myAction = require("./action");const myCommander = function (program) {program.command("clone <source> [destination]").description("clone a repository into a newly created directory").action((source, destination) => {console.log("clone command called");});program.command("create <appname> [other]").alias("cte").description("create a new project").action(myAction);
};module.exports = myCommander;
  • help.js
const myHelp = function(program){program.version('0.0.1')program.option('-f --framework <framework>', '设置框架')
}
module.exports = myHelp
  • config.js
module.exports = {framework: ['express', 'vue', 'react'],frameworkUrls: {express: 'https://github.com/expressjs/express.git',vue: 'https://gitee.com/youlaiorg/vue3-element-admin.git',react: 'https://github.com/facebook/react.git'}
}
  • 获取版本号
    在这里插入图片描述
  • 创建项目
    在这里插入图片描述
    在当前文件夹下面就多出了个新项目
    在这里插入图片描述
  • 项目创建失败
    在这里插入图片描述

inquirer报错prompt不是函数的问题

主要是版本的不适配导致的,整个项目使用的是cjs,所以需要版本降级,参考这里的解决方法https://github.com/bridgewwater/git-tidier/issues/100

如果使用的是es规范,在安装的时候直接使用最新的工具就可以了。

http://www.dtcms.com/a/422643.html

相关文章:

  • 磁共振成像原理(理论)15:空间信息编码 (Spatial Information Encoding) -频率编码相位编码
  • 磐石网站seo手机nfc网站开发
  • 命名视图学习笔记
  • CentOS7安装OpenStack云计算平台框架
  • PyCharm中搭建PyTorch和YOLOv10开发环境
  • 无人机开源项目
  • 查看网站建设时间如何免费做网站优化
  • UNIX下C语言编程与实践8-UNIX 静态库原理与创建:ar 命令的使用与静态库调用全流程
  • MATLAB用到的符号计算数学引擎
  • 精读C++20设计模式——结构型设计模式:组合模式
  • 做网站费用怎么入账新洲城乡建设局网站
  • Windows系统安装arm麒麟系统
  • ARMv8的异常处理
  • IA-静态路由
  • 圆柱电池气动点焊机的全场景应用|深圳比斯特自动化
  • 学习嵌入式的第四十四天——ARM——I2C
  • 网站建设维护岗位职责怎样做机械租赁的网站
  • 安卓开发---耳机的按键设置的UI实例
  • 苍穹外卖-菜品新增、删除
  • 如何找到到自己研究领域的经典论文?
  • Kubernetes 健康探针详解
  • 出售东西的网站怎么做建设网站运营成本
  • 【Qt开发】输入类控件(三)-> QComboBox
  • 什么是专用对讲机?遨游专用对讲机有什么优势?
  • 港大和字节携手打造WorldWeaver:以统一建模方案整合感知条件,为长视频生成领域带来质量与一致性双重飞跃。
  • 第三方软件测试公司:【Gatling基于Scala的开源高性能负载测试工具】
  • Social-Auto-Upload - 多平台社交媒体视频自动化上传工具
  • 十一、Hadoop 三种部署模式对比表 组件介绍
  • 十、Hadoop 核心目录功能说明表
  • Leetcode+Java+图论+岛屿问题