自定义脚手架

2021-12-27  本文已影响0人  O蚂蚁O

一、涉及到的node

1、chalk:控制台输出颜色
const chalk = require('chalk');

console.log(chalk.green(val));

2、ora:控制台loading效果

const ora = require('ora')

const spinner = ora('Project loading...')
spinner.start() // 开始
spinner.fail() // 失败
spinner.succeed() // 成功

3、inquirer:问答对话

const Inquirer = require('inquirer')

const question = [
  {
    type: "input",
    message: "请输入项目名称:",
    name: "projectName",
    default: "lh-pc"
  },
  {
    type: "input",
    message: "请输入作者:",
    name: "author",
    default: 'dev'
  },
  {
    type: "list",
    message: "请选择你的模版:",
    choices: templateKeys,
    name: "templateKey",
    default: "pc-ts",
    validate: (val) => {
      console.log(chalk.green(val));
      if (val) {
        return true;
      } else {
        console.log(chalk.yellow('请选择一个模版'))
        return false
      }
    }
  }
]
// 结果
Inquirer.prompt(question).then((answers) => {
  
})

4、download-git-repo: 克隆下载git仓库

const download = require('download-git-repo')

 download(`direct:${git地址}`, '项目名',{clone:true}, err=>{
    console.log(chalk.red(err))
    if(err) {
        
        console.log(chalk.red(`Project loading failed`))
        return
    }
  })

我的配置--server.js

const chalk = require('chalk');
const ora = require('ora') // loading效果
const Inquirer = require('inquirer')
const download = require('download-git-repo')
const path = require('path')
const template = require('./template.json')
const templateKeys = Object.keys(template)
const question = [
  {
    type: "input",
    message: "请输入项目名称:",
    name: "projectName",
    default: "lh-pc"
  },
  {
    type: "input",
    message: "请输入作者:",
    name: "author",
    default: 'dev'
  },
  {
    type: "list",
    message: "请选择你的模版:",
    choices: templateKeys,
    name: "templateKey",
    default: "pc-ts",
    validate: (val) => {
      console.log(chalk.green(val));
      if (val) {
        return true;
      } else {
        console.log(chalk.yellow('请选择一个模版'))
        return false
      }
    }
  }
]

Inquirer.prompt(question).then((answers) => {
  console.log('answers', chalk.yellow(JSON.stringify(answers)))
  const spinner = ora('Project loading...')
  spinner.start()
  const { projectName,templateKey } = answers
  const tempUrl = template[templateKey]
  download(`direct:${tempUrl}`,projectName,{clone:true}, err=>{
    console.log(chalk.red(err))
    if(err) {
        spinner.fail()
        console.log(chalk.red(`Project loading failed`))
        return
    }
    spinner.succeed()
  })
})

package

{
   "name": "my-node",
   "version": "1.0.0",
   "author": "liruijiang",
   "description": "node",
   "main": "server.js",
   "scripts": {
      "start": "node server.js"
   },
   "keywords": [],
   "license": "ISC",
   "devDependencies": {
      "chalk": "^4.1.0",
      "commander": "^7.2.0",
      "download-git-repo": "^3.0.2",
      "handlebars": "^4.7.7",
      "inquirer": "^8.0.0",
      "log-symbols": "^4.1.0",
      "metalsmith": "^2.3.0",
      "ora": "^5.4.0",
      "rimraf": "^3.0.2"
   }
}
上一篇下一篇

猜你喜欢

热点阅读