UNPKG

gchcg-cli

Version:
1 lines 4.13 kB
"use strict";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("发布项目,会自动commit、push、使用--prod会发布到正式环境").option("-r, --refreshToken","强制更新远程仓库token").option("-p, --prod","是否为正式环境").option("-d, --delete","publish --prod 时,是否为删除本地dev分支和远程dev分支").action(exec),program.command("push").description("推送项目,publish的简版,会自动commit、push,支持-t、-m参数").option("-s, --skip","跳过git add,直接commit").action(exec),program.command("commit").description("ai 生成 commit 信息").option("--baseUrl","设置 api base url,默认 https://openrouter.ai/api/v1,本地如 http://localhost:3000/v1").option("-t, --setToken","设置token,本地 localhost 地址 token 非必填").option("-m, --setModel","设置模型,默认 google/gemini-2.0-flash-thinking-exp:free").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 n=await getLog(r),t=(new Date).getDate();if(n[t]&&n.lastVersion===e)return;const{getNpmSemverVersion:s}=require("./get-npm-info"),i=await s(e,o);n.lastVersion=i||e,n[t]=t,fs.writeFileSync(r,JSON.stringify({day:t,lastVersion:n.lastVersion})),i&&semver.gt(i,e)&&log.warn(colors.yellow(`请手动更新 ${o},当前版本:${e},最新版本:${i}\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)}));