UNPKG

@hz-lib/hz-build-cli

Version:

hz-build-cli 脚手架

79 lines (68 loc) 2.21 kB
/* * @Author: your name * @Date: 2021-10-21 09:33:18 * @LastEditTime: 2021-10-22 14:06:32 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: \cli\hz-cli\bin\create.js */ const path = require('path') const fs = require('fs-extra') const inquirer = require('inquirer') const Generator = require('./Generator') const chalk = require('chalk') const { wrapLoading } = require('./util') let startTime, endTime; module.exports = async function (name, options) { startTime = new Date().getTime() // 执行创建命令 // 当前命令行选择的目录 const cwd = process.cwd(); // 需要创建的目录地址 const targetAir = path.join(cwd, name) // 目录是否已经存在? if (fs.existsSync(targetAir)) { // 是否为强制创建? if (options.force) { await fs.remove(targetAir) } else { // TODO:询问用户是否确定要覆盖 // 询问用户是否确定要覆盖 let { action } = await inquirer.prompt([ { name: 'action', type: 'list', message: '目标目录已存在,请选择操作:', choices: [ { name: '覆盖', value: 'overwrite' },{ name: '取消', value: false } ] } ]) if (!action) { return; } else if (action === 'overwrite') { // 移除已存在的目录 console.log(`\r\nRemoving...`) await wrapLoading(fs.remove,'移除已存在的目标目录',targetAir); } } } // 创建项目 const generator = new Generator(name, targetAir); // 开始创建项目 await generator.create() endTime = new Date().getTime(); usageTime = (endTime - startTime) / 1000 console.log(`> 项目已经创建成功,用时${chalk.cyan(usageTime)}s,请输入以下命令继续...`); // 5)模板使用提示 console.log(`\r\nSuccessfully created project ${chalk.cyan(name)}`) console.log(`\r\n cd ${chalk.cyan(name)}`) console.log(' npm run server\r\n') return true; }