node学习--自定义脚手架(六):完成具体指令逻辑

一、使用download-git-repo,下载远程仓库代码到本地。

具体功能可参考download-git-repo - npm

根据用户选择的不同选项,下载不同的模版代码。

二、使用ora,实现下载代码时的等待功能。

const ora = require("ora")
const spinner = ora().start()
spinner.text = `${answer.framWork}模版下载中...`
setTimeout(()=>{
    spinner.succeed('Done!')
},3000)

此处,我用setTimeout模拟代码下载过程。

效果如下:

三、使用chalk,实现命令行打印信息的文字样式。

const chalk = require("chalk")
console.log(chalk.green('you will do next:'))

效果如下:

四、完整代码如下:

 const inquirer = require('inquirer')
 const config = require('../../config')
 const ora = require("ora")
 const chalk = require("chalk")

 const createAction = (project,args)=>{  // action 用来写这个指令具体的操作逻辑  回调中的参数是<project> [other...]
    // 命令行的执行逻辑代码
    // 需求:询问用户,让用户选择需要的node框架
    inquirer.default.prompt([
        {
            type:'list', // 题目类型  list input ...
            choices:config.frameworkList, // 选项
            name:'framWork', // answer对象接收的字段名称
            message:'请选择您需要的框架?' // 问题
        }
    ]).then(answer=>{ // 通过then方法接受处理用户的选择回答
        console.log(answer)
        // 下载对应的代码---使用download-git-repo
        // 使用ora---实现下载等待功能
        // 使用chalk---实现命令行提示文字的样式
        const spinner = ora().start()
        spinner.text = `${answer.framWork}模版下载中...`
        setTimeout(()=>{
            spinner.succeed('Done!')
            console.log(chalk.green('you will do next:'))
            console.log(`cd ${project}`)
            console.log(`npm install`)
            console.log(`npm run dev`)
        },3000)
    })
    
}
 module.exports = createAction

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值