UNPKG

@tuoyuan/cli

Version:

拓源网络脚手架

116 lines (107 loc) 3.41 kB
#! /usr/bin/env node import inquirer from "inquirer"; import { getInputConfig, getGitConfig } from "../config/config.js"; import { createByName,printText,updateVersion} from "../utils/index.js"; import {program} from "commander" import figlet from "figlet"; import chalk from 'chalk' import { execa } from "execa"; import ora from "ora"; import { createRequire } from 'module'; const require = createRequire(import.meta.url); const { version } = require('../package.json'); // tools // 创建模板 需要名字和模板 function createTemplate() { const prompt = inquirer.createPromptModule(); // 使用 then 方法 const questions = getInputConfig(); // 获取相关配置 prompt(questions).then((answers) => { createByName(answers.name,answers.project); }).catch(()=>{ printText("已取消","warning") }) } // 创建模板 需要名字 function createProjectName(name) { const prompt = inquirer.createPromptModule(); const questions = [getInputConfig()[1]]; // 获取相关配置 prompt(questions).then((answers) => { createByName(name,answers.project); }).catch(()=>{ printText("已取消","warning") }) } /************监听help | create 命令 ************************/ program .on('--help', () => { // 使用 figlet 绘制 Logo console.log('\r\n' + figlet.textSync('TuoYuan')); console.log(chalk.green(`\r\n ©2024 拓源网络\r\n`)) }) .description() /***************************** create 命令 *******************/ program .command('create [projectName]') .description('创建项目 | -t 项目模板') .option('-t <projectName>', '创建模板') .action((name, options) => { if(options.t!== undefined){ if(getGitConfig().map(res=>res.name).includes(options.t)){ createByName(name,options.t) } else{ printText(options.t+"模板不存在","error") } } // 没有参数,选择模板 else{ name===undefined?createTemplate():createProjectName(name) } }) /***************************** test测试命令 ***************************/ program.command('build') .description('打包项目 | 同时更新版本号') .action(() => { const {version,error}=updateVersion() if(!error){ const spinner = ora('正在打包...\n').start(); execa('pnpm', ['run', 'build']).then(()=>{ spinner.succeed('打包成功'); }).catch(()=>{ spinner.fail('打包失败'); }) } }) /***************************** test测试命令 ***************************/ program.command('test') .description('测试命令') .action(() => { // 当前路径 printText(process.cwd(),"info") }) /***************************** 升级命令 ***************************/ program.command('update') .description('升级脚手架') .action( () => { const spinner = ora('正在升级...\n').start(); execa('npm', ['update','-g', '@tuoyuan/cli']).then(()=>{ spinner.succeed('升级成功'); }).catch(()=>{ spinner.fail('升级失败') }) }); // ...... 其他命令 program // 配置版本号信息 .usage('<command> [option]') program.version(version, "-v, --version"); program .command('*') .description('') .action(() => { printText("找不到相应的命令","error") }) program.parse(process.argv);