run 命令试用

This commit is contained in:
Cheney 2024-09-03 15:40:58 +08:00
parent 4275120cba
commit 4b6508a606

108
kit/src/kitcommand.js Normal file
View File

@ -0,0 +1,108 @@
const shell = require("./shell");
const fs = require("fs");
const vm = require("node:vm");
module.exports = {
init : function () {
return this;
},
regTo: function (parser) {
parser.addCmdLine("help", "打开在线帮助;", async function () {
const uConfig = await $context.get("uConfig")
shell.execSync("start " + uConfig.get("man"))
});
parser.addCmdLine("manu", "人工维护配置;", function () {
shell.execSync("start " + $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('node:vm');
const script = new vm.Script(projectScript + ";" + step + "()");
const context = {
animal: 'cat', count: 2, $logger
};
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-check", "验证远程仓库;", async function () {
let remote = await $context.get("remote")
if (!(await remote.check())) {
$logger.error("远程仓库不可用")
return
} else {
$logger.info("远程仓库可用")
}
});
parser.addCmdLine("publish", "发布到远程仓库;", async function () {
let remote = await $context.get("remote")
if (!(await remote.check())) {
$logger.error("远程仓库不可用")
return
} else {
$logger.info("远程仓库可用")
}
remote.publish()
});
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"))
});
}
}