gchcg-cli
Version:
1 lines • 4.68 kB
JavaScript
;module.exports=core;const path=require("path"),semver=require("semver"),colors=require("colors/safe"),userHome=require("user-home"),fs=require("fs"),pathExists=require("path-exists").sync,commander=require("commander"),log=require("./log"),exec=require("./exec"),constant=require("./const"),pkg=require("../package.json"),program=new commander.Command;async function core(){try{await prepare(),registerCommand()}catch(e){log.error(e.message)}}function registerCommand(){program.name(Object.keys(pkg.bin)[0]).usage("<command> [options]").description("工厂化重构项目脚手架,支持项目初始化、发布、推送").version(pkg.version),program.command("init [projectName]").description("初始化项目,初次使用会检查更新token").option("-r, --refreshToken","强制更新远程仓库token").option("-f, --force","是否强制初始化项目").action(exec),program.command("publish").description("发布项目。\n 1. 检测是否为最新开发环境(dev/*.*.*)\n 1.1 如果是则和push方法一样,正常提交git\n 1.2 否则会根据tag判断当前是否有最新开发分支\n 1.2.1 如果有则切换最新的开发分支\n 1.2.2 没有最新分支会提示选择升级版本号,创建并切换到开发分支\n 2. 如果选择发布到正式环境,会自动合并代码到master,生成tag\n").option("-r, --refreshToken","强制更新远程仓库token").option("-p, --prod","是否发布到正式环境,自动合并代码到master,生成tag").option("-d, --delete","publish --prod 时,是否为删除本地dev分支和远程dev分支").action(exec),program.command("push").description("推送项目,选择提交类型,输入 commit 信息,自动 commit 和 push").option("-s, --skip","跳过git add,直接commit").action(exec),program.command("commit").description("ai 生成 commit 信息").option("-u, --baseUrl","设置 api base url,默认 https://api.deepseek.com/v1,本地如 http://localhost:3000/v1").option("-t, --setToken","设置token,本地 localhost 地址 token 非必填").option("-m, --setModel","设置模型,默认 deepseek-chat\t").option("-s, --skip","跳过git add,直接commit").action(exec),program.command("pull").description("拉取项目 等同于 git pull origin [branch]").option("-b, --branch","分支名,默认当前分支").action(exec),program.command("status").description("查看git状态,等同于 git status").action(exec),program.on("command:*",(function(e){const o=program.commands.map((e=>e.name()));console.log(colors.red("未知的命令:"+e[0])),o.length>0&&console.log(colors.red("可用命令:"+o.join(",")))})),program.parse(process.argv),program.args&&program.args.length<1&&program.outputHelp()}async function prepare(){checkPkgVersion(),checkRoot(),checkUserHome(),checkEnv(),await checkGlobalUpdate()}async function getLog(e){return new Promise((o=>{let r={};try{r=JSON.parse(fs.readFileSync(e,"utf-8"))}catch(o){log.warn(`${e}文件读取出错,将自动创建。错误原因:${o?.message}`)}o("object"==typeof r&&!Array.isArray(r)&&null!=r&&Object.keys(r).length>0&&r.lastVersion?r:{})}))}async function checkGlobalUpdate(){const e=pkg.version,o=pkg.name;pathExists(process.env.CLI_HOME_PATH)||fs.mkdirSync(process.env.CLI_HOME_PATH,{recursive:!0});const r=path.resolve(process.env.CLI_HOME_PATH,"check-update.json");pathExists(r)||fs.writeFileSync(r,JSON.stringify({}));const t=await getLog(r),n=(new Date).getDate();if(t[n]&&t.lastVersion===e)return;const{getNpmSemverVersion:s}=require("./get-npm-info"),c=await s(e,o);t.lastVersion=c||e,t[n]=n,fs.writeFileSync(r,JSON.stringify({day:n,lastVersion:t.lastVersion})),c&&semver.gt(c,e)&&log.warn(colors.yellow(`请手动更新 ${o},当前版本:${e},最新版本:${c}\n 更新命令: npm install -g ${o}`))}function checkEnv(){const e=require("dotenv"),o=path.resolve(userHome,".env");pathExists(o)&&e.config({path:o}),createDefaultConfig()}function createDefaultConfig(){const e={home:userHome};process.env.CLI_HOME?e.cliHome=path.join(userHome,process.env.CLI_HOME):e.cliHome=path.join(userHome,constant.DEFAULT_CLI_HOME),process.env.CLI_HOME_PATH=e.cliHome}function checkUserHome(){if(!userHome||!pathExists(userHome))throw new Error(colors.red("当前登录用户主目录不存在!"))}function checkRoot(){require("root-check")()}function checkPkgVersion(){log.info("cli",pkg.version)}process.on("unhandledRejection",((e,o)=>{throw e})),process.on("uncaughtException",(e=>{process.exit(1)}));