1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| const fs = require("fs"); const shell = require("shelljs"); const symbols = require("log-symbols"); const inquirer = require("inquirer"); const gitConfig = require("../config/gitConfig"); const ora = require("ora"); const chalk = require("chalk"); const clone = require("../utils/clone");
const questions = [ { type: "input", message: "请输入模板名称", name: "name", validate(val) { if (!val) return "模板名不能为空!"; if (val.match(/[^A-Za-z0-9\u4e00-\u9fa5_-]/g)) return "模板名称包含非法字符,请重新输入"; if (fs.existsSync(val)) return "该文件夹已存在"; return true; }, }, { type: "list", message: "请选择项目类型", choices: ["simple-html", "vue-cli-h5"], name: "type", }, ];
const initAction = async (option) => { if (!shell.which("git")) { console.log(symbols.error, "对不起, git命令不可用!"); shell.exit(1); } const answers = await inquirer.prompt(questions); let confirm = await inquirer.prompt([ { type: "confirm", message: "是否创建?", default: "Y", name: "isConfirm", }, ]); if (!confirm.isConfirm) return false;
const { type, name } = answers; let branch = gitConfig[type].branch; if (option.dev) branch = "develop"; const remote = gitConfig[type].remote; await clone(`direct:${remote}#${branch}`, name, { clone: true }); const deleteDire = [ ".git", ".gitignore", "README.md", "README.en.md", "LICENSE", ]; const pwd = shell.pwd(); deleteDire.map((item) => shell.rm("-rf", pwd + `/${name}/${item}`)); shell.cd(`./${name}`); const installSpinner = ora("正在安装依赖...").start(); if (shell.exec("yarn install").code !== 0) { console.log(symbols.warning, chalk.yellow("自动安装失败, 请手动安装")); installSpinner.fail(); shell.exit(1); } installSpinner.succeed(chalk.green("依赖安装成功")); console.log(chalk.gray("\n $ ") + chalk.blue(`cd ./${name}`)); console.log(chalk.gray(" $ ") + chalk.blue(`yarn serve`)); };
module.exports = initAction;
|