From 4207d6e46779bc006c0b0ba40d0052f678df211a Mon Sep 17 00:00:00 2001 From: cheney Date: Wed, 17 Jun 2026 10:31:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20cli=20=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kit/src/kitcommand.js | 109 ++++++++++++++++++++++++++++-------------- 1 file changed, 72 insertions(+), 37 deletions(-) diff --git a/kit/src/kitcommand.js b/kit/src/kitcommand.js index f39ca3c..7fac4f2 100644 --- a/kit/src/kitcommand.js +++ b/kit/src/kitcommand.js @@ -2,6 +2,15 @@ const os = require("os"); const kitfunction = require("./kitfunction"); +async function getAvailableRemote() { + const remote = await $context.get("remote"); + if (!(await remote.check())) { + $logger.error("远程仓库不可用"); + return null; + } + return remote; +} + module.exports = { init : function () { return this; @@ -57,8 +66,6 @@ async function main(){ const vm = require('vm'); const script = new vm.Script(projectScript + ";" + step + "()"); - // 注入全局变量 - const kitfunction = require("./kitfunction") const context = kitfunction; script.runInNewContext(context, { @@ -83,53 +90,81 @@ async function main(){ }); parser.addCmdLine("remote", "验证远程仓库可达;", async function () { - let remote = await $context.get("remote") - if (!(await remote.check())) { - $logger.error("远程仓库不可用") - return - } else { - $logger.info("远程仓库可用") + const remote = await getAvailableRemote(); + if (remote) { + $logger.info("远程仓库可用"); } }); - parser.addCmdLine("publish [localFile] [remotePath]", "发布到远程仓库", async function (cli) { + parser.addCmdLine("check", "验证远程仓库可达;", async function () { + const remote = await getAvailableRemote(); + if (remote) { + $logger.info("远程仓库可用"); + } + }); + + parser.addCmdLine("publish [localFile] [remotePath]", "发布到远程仓库; localFile 为空时使用当前目录 meta.json", async function (cli) { const fs = require("fs"); let localFile = cli.getParamValue("localFile"); - if (!fs.existsSync(localFile)) { - $logger.error("本地文件不存在") - return + let remotePath = cli.getParamValue("remotePath"); + + if (localFile && !fs.existsSync(localFile)) { + $logger.error("本地文件不存在"); + return; } - let remotePath = cli.getParamValue("remotePath"); - if (!remotePath) { - $logger.info("使用本地配置路径") - if (!fs.existsSync("meta.json")) { - $logger.error("没有找到本地配置") - return - } - } else { - $logger.info("发布到远程路径 " + remotePath) + if (!localFile && !fs.existsSync("meta.json")) { + $logger.error("没有找到本地配置 meta.json"); + return; } - let remote = await $context.get("remote") - if (!(await remote.check())) { - $logger.error("远程仓库不可用") - return - } else { - $logger.info("远程仓库可用") + + const remote = await getAvailableRemote(); + if (!remote) { + return; } - await remote.publish(localFile, remotePath) - $logger.info("发布成功") + + if (remotePath) { + $logger.info("发布到远程路径 " + remotePath); + } else { + $logger.info("使用 meta.json 中的 group/id/version 规则发布"); + } + await remote.publish(localFile, remotePath); + $logger.info("发布成功"); }); - parser.addCmdLine("install [module]", "安装/更新模块;", async function (cli) { - let remote = await $context.get("remote") - if (!(await remote.check())) { - $logger.error("远程仓库不可用") - return - } else { - $logger.info("远程仓库可用") + parser.addCmdLine("install [module]", "安装/更新模块; 支持 myapp、myapp@1.0、/docker/net/myapp", async function (cli) { + const remote = await getAvailableRemote(); + if (!remote) { + return; } - remote.install(cli.getParamValue("module")) + await remote.install(cli.getParamValue("module")); + }); + + parser.addCmdLine("list [module]", "列出本地已安装版本;", async function (cli) { + let module = cli.getParamValue("module"); + if (!module) { + module = "kit"; + } + const remote = await $context.get("remote"); + await remote.list(module); + }); + + parser.addCmdLine("use ", "切换到已安装版本,例如 myapp@1.0;", async function (cli) { + const remote = await $context.get("remote"); + const version = await remote.use(cli.getParamValue("module-version")); + $logger.info("当前版本 " + version); + }); + + parser.addCmdLine("rollback ", "回滚到上一个已安装版本;", async function (cli) { + const remote = await $context.get("remote"); + const version = await remote.rollback(cli.getParamValue("module")); + $logger.info("当前版本 " + version); + }); + + parser.addCmdLine("remove ", "删除本地已安装版本,例如 myapp@1.0;", async function (cli) { + const remote = await $context.get("remote"); + await remote.remove(cli.getParamValue("module-version")); + $logger.info("删除成功"); }); } }