kit.program/kit/src/kitcommand.js
cheney d93316be15
Some checks failed
TDevOPsCICD / build-image (push) Has been cancelled
publish
2026-06-17 11:13:02 +08:00

187 lines
6.7 KiB
JavaScript

const shell = require("./shell");
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;
},
regTo: function (parser) {
parser.addCmdLine("help", "打开在线帮助;", async function () {
const uConfig = await $context.get("uConfig")
if (os.type() === 'Windows_NT') {
shell.execSync("start " + uConfig.get("man"))
} else {
console.log("请用浏览器访问 " + uConfig.get("man"))
}
});
parser.addCmdLine("manu", "人工维护配置;", function () {
if (os.type() === 'Windows_NT') {
shell.execSync("start " + $sConfig.getUserConfigPath())
} else {
console.log("cd " + $sConfig.getUserConfigPath())
}
});
parser.addCmdLine("run [--file] [func]", "运行项目脚本;", function (cli) {
$logger.info("kit 当前执行目录 " + process.cwd());
const fs = require("fs");
let step = cli.getParamValue("func");
if (!step) {
step = "main"
}
let file = cli.getParamValue("file");
if (!file) {
file = "kitx"
}
const scriptFile = `./${file}.js`;
const stat = fs.existsSync(scriptFile)
if (!stat) {
if (!cli.getParamValue("file")) {
fs.writeFileSync("./kitx.js", `
async function main(){
console.log("hello kit!");
}
`);
} else {
$logger.info(`文件 ${file} 不存在`);
return
}
}
const projectScript = fs.readFileSync(scriptFile, "utf-8");
const vm = require('vm');
const script = new vm.Script(projectScript + ";" + step + "()");
const context = kitfunction;
script.runInNewContext(context, {
timeout: 10 * 60 * 1000, breakOnSigint: true,
});
$logger.info("执行结束");
});
parser.addCmdLine("config <key> [value]", "查询/设置配置;", async function (cli) {
let key = cli.getParamValue("key");
let value = cli.getParamValue("value");
const uConfig = await $context.get("uConfig")
if (null != key && null == value) {
let v = uConfig.get(key);
$logger.info("{} : {}", key, v);
} else if (null != key && null != value) {
uConfig.set(key, value);
$logger.info("set {} : {}", key, value);
} else {
$logger.error("参数异常");
}
});
parser.addCmdLine("remote", "验证远程仓库可达;", async function () {
const remote = await getAvailableRemote();
if (remote) {
$logger.info("远程仓库可用");
}
});
parser.addCmdLine("check [module]", "验证远程仓库可达;指定 module 时查看远程 meta 信息;", async function (cli) {
const remote = await getAvailableRemote();
if (!remote) {
return;
}
const module = cli.getParamValue("module");
if (!module) {
$logger.info("远程仓库可用");
return;
}
try {
const parsed = remote.parseProjectVersion(module);
const meta = await remote.getProjectMetadata(parsed.projectId, parsed.group);
$logger.info(JSON.stringify(meta, null, 4));
} catch (e) {
$logger.info(`远程 module 不存在: ${module}`);
}
});
parser.addCmdLine("publish [localFile] [remotePath]", "发布到远程仓库; localFile 为空时使用当前目录 meta.json", async function (cli) {
const fs = require("fs");
let localFile = cli.getParamValue("localFile");
let remotePath = cli.getParamValue("remotePath");
if (localFile && !fs.existsSync(localFile)) {
$logger.error("本地文件不存在");
return;
}
if (!localFile && !fs.existsSync("meta.json")) {
$logger.error("没有找到本地配置 meta.json");
return;
}
const remote = await getAvailableRemote();
if (!remote) {
return;
}
if (remotePath) {
$logger.info("发布到远程路径 " + remotePath);
} else {
$logger.info("使用 meta.json 中的 group/id/version 规则发布");
}
await remote.publish(localFile, remotePath);
$logger.info("发布成功");
process.exit(0);
});
parser.addCmdLine("install [module]", "安装/更新模块; 支持 myapp、myapp@1.0、/docker/net/myapp", async function (cli) {
const remote = await getAvailableRemote();
if (!remote) {
return;
}
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 <module-version>", "切换到已安装版本,例如 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 <module>", "回滚到上一个已安装版本;", async function (cli) {
const remote = await $context.get("remote");
const version = await remote.rollback(cli.getParamValue("module"));
$logger.info("当前版本 " + version);
});
parser.addCmdLine("remove <module-version>", "删除本地已安装版本,例如 myapp@1.0;", async function (cli) {
const remote = await $context.get("remote");
await remote.remove(cli.getParamValue("module-version"));
$logger.info("删除成功");
});
}
}