Files
kit.program/kit/src/kitcommand.js
T
2025-03-13 16:02:37 +08:00

134 lines
4.7 KiB
JavaScript

const shell = require("./shell");
const os = require("os");
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 = "kit"
}
const stat = fs.existsSync(`./${file}.js`)
if (!stat) {
if (!cli.getParamValue("file")) {
fs.writeFileSync("./kit.js", `
async function main(){
console.log("hello kit!")
}
`);
} else {
$logger.info(`文件 ${file} 不存在`);
return
}
}
const projectScript = fs.readFileSync("./kit.js", "utf-8");
const vm = require('vm');
const script = new vm.Script(projectScript + ";" + step + "()");
const context = {
animal: 'cat', count: 2, $logger, shell
};
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 () {
let remote = await $context.get("remote")
if (!(await remote.check())) {
$logger.error("远程仓库不可用")
return
} else {
$logger.info("远程仓库可用")
}
});
parser.addCmdLine("publish [localFile] [remotePath]", "发布到远程仓库;", 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 ( ! remotePath ) {
$logger.info("使用本地配置路径")
if ( ! fs.existsSync("meta.json") ) {
$logger.error("没有找到本地配置")
return
}
} else {
$logger.info("发布到远程路径" + remotePath)
}
let remote = await $context.get("remote")
if (!(await remote.check())) {
$logger.error("远程仓库不可用")
return
} else {
$logger.info("远程仓库可用")
}
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("远程仓库可用")
}
remote.install(cli.getParamValue("module"))
});
}
}