napcat-plugin-debug-cli 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli.mjs +78 -0
- package/package.json +1 -1
package/cli.mjs
CHANGED
|
@@ -5053,6 +5053,8 @@ function parseArgs() {
|
|
|
5053
5053
|
opts.watchAll = true;
|
|
5054
5054
|
} else if (arg === "--verbose" || arg === "-v") {
|
|
5055
5055
|
opts.verbose = true;
|
|
5056
|
+
} else if (arg === "--deploy" || arg === "-d") {
|
|
5057
|
+
opts.deploy = args[++i] || ".";
|
|
5056
5058
|
} else if (arg.startsWith("ws://") || arg.startsWith("wss://")) {
|
|
5057
5059
|
opts.wsUrl = arg;
|
|
5058
5060
|
}
|
|
@@ -5070,6 +5072,7 @@ napcat-plugin-debug CLI — NapCat 插件调试 & 热重载
|
|
|
5070
5072
|
-t, --token <token> 认证 token
|
|
5071
5073
|
-w, --watch <dir> 监听目录自动热重载
|
|
5072
5074
|
-W, --watch-all 监听远程插件目录所有插件
|
|
5075
|
+
-d, --deploy [dir] 部署插件 dist/ 到远程插件目录并重载 (默认: .)
|
|
5073
5076
|
-v, --verbose 详细输出
|
|
5074
5077
|
-h, --help 帮助
|
|
5075
5078
|
|
|
@@ -5079,6 +5082,7 @@ napcat-plugin-debug CLI — NapCat 插件调试 & 热重载
|
|
|
5079
5082
|
load <id> 加载插件
|
|
5080
5083
|
unload <id> 卸载插件
|
|
5081
5084
|
info <id> 插件详情
|
|
5085
|
+
deploy [dir] 部署插件到远程并重载
|
|
5082
5086
|
watch <dir> 开始监听
|
|
5083
5087
|
unwatch 停止监听
|
|
5084
5088
|
status 服务状态
|
|
@@ -5202,6 +5206,66 @@ function createWatcher(watchPath, onPluginChange) {
|
|
|
5202
5206
|
}
|
|
5203
5207
|
};
|
|
5204
5208
|
}
|
|
5209
|
+
function copyDirRecursive(src, dest) {
|
|
5210
|
+
if (!fs.existsSync(dest)) fs.mkdirSync(dest, { recursive: true });
|
|
5211
|
+
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
|
|
5212
|
+
const srcPath = path.join(src, entry.name);
|
|
5213
|
+
const destPath = path.join(dest, entry.name);
|
|
5214
|
+
if (entry.isDirectory()) copyDirRecursive(srcPath, destPath);
|
|
5215
|
+
else fs.copyFileSync(srcPath, destPath);
|
|
5216
|
+
}
|
|
5217
|
+
}
|
|
5218
|
+
async function deployPlugin(projectDir, remotePluginPath, rpc) {
|
|
5219
|
+
const distDir = path.resolve(projectDir, "dist");
|
|
5220
|
+
if (!fs.existsSync(distDir)) {
|
|
5221
|
+
logErr(`dist/ 目录不存在: ${distDir}`);
|
|
5222
|
+
logInfo("请先运行 pnpm run build 构建插件");
|
|
5223
|
+
return false;
|
|
5224
|
+
}
|
|
5225
|
+
const distPkgPath = path.join(distDir, "package.json");
|
|
5226
|
+
if (!fs.existsSync(distPkgPath)) {
|
|
5227
|
+
logErr("dist/package.json 不存在,无法确定插件名称");
|
|
5228
|
+
return false;
|
|
5229
|
+
}
|
|
5230
|
+
let pluginName;
|
|
5231
|
+
try {
|
|
5232
|
+
const pkg = JSON.parse(fs.readFileSync(distPkgPath, "utf-8"));
|
|
5233
|
+
pluginName = pkg.name;
|
|
5234
|
+
if (!pluginName) {
|
|
5235
|
+
logErr("dist/package.json 中缺少 name 字段");
|
|
5236
|
+
return false;
|
|
5237
|
+
}
|
|
5238
|
+
} catch (e) {
|
|
5239
|
+
logErr(`解析 dist/package.json 失败: ${e.message}`);
|
|
5240
|
+
return false;
|
|
5241
|
+
}
|
|
5242
|
+
const destDir = path.join(remotePluginPath, pluginName);
|
|
5243
|
+
logInfo(`部署 ${co(pluginName, C.bold, C.cyan)} → ${co(destDir, C.dim)}`);
|
|
5244
|
+
try {
|
|
5245
|
+
if (fs.existsSync(destDir)) {
|
|
5246
|
+
fs.rmSync(destDir, { recursive: true, force: true });
|
|
5247
|
+
}
|
|
5248
|
+
copyDirRecursive(distDir, destDir);
|
|
5249
|
+
logOk(`文件复制完成 (${fs.readdirSync(distDir, { recursive: true }).length} 个文件)`);
|
|
5250
|
+
} catch (e) {
|
|
5251
|
+
logErr(`复制文件失败: ${e.message}`);
|
|
5252
|
+
return false;
|
|
5253
|
+
}
|
|
5254
|
+
try {
|
|
5255
|
+
await rpc.call("reloadPlugin", pluginName);
|
|
5256
|
+
logOk(`${co(pluginName, C.green, C.bold)} 重载成功`);
|
|
5257
|
+
} catch {
|
|
5258
|
+
try {
|
|
5259
|
+
logInfo("插件未注册,尝试扫描加载...");
|
|
5260
|
+
await rpc.call("scanPlugins");
|
|
5261
|
+
await rpc.call("loadPluginById", pluginName);
|
|
5262
|
+
logOk(`${co(pluginName, C.green, C.bold)} 首次加载成功`);
|
|
5263
|
+
} catch (e2) {
|
|
5264
|
+
logWarn(`自动加载失败: ${e2.message},请手动 load ${pluginName}`);
|
|
5265
|
+
}
|
|
5266
|
+
}
|
|
5267
|
+
return true;
|
|
5268
|
+
}
|
|
5205
5269
|
async function main() {
|
|
5206
5270
|
const opts = parseArgs();
|
|
5207
5271
|
console.log(co("\n napcat-plugin-debug CLI", C.bold, C.cyan));
|
|
@@ -5254,6 +5318,11 @@ async function main() {
|
|
|
5254
5318
|
} catch (e) {
|
|
5255
5319
|
logWarn(`获取信息失败: ${e.message}`);
|
|
5256
5320
|
}
|
|
5321
|
+
if (opts.deploy && remotePluginPath && rpc) {
|
|
5322
|
+
const ok = await deployPlugin(path.resolve(opts.deploy), remotePluginPath, rpc);
|
|
5323
|
+
ws.close(1e3);
|
|
5324
|
+
process.exit(ok ? 0 : 1);
|
|
5325
|
+
}
|
|
5257
5326
|
if (opts.watch) {
|
|
5258
5327
|
watcher = createWatcher(path.resolve(opts.watch), onFileChange);
|
|
5259
5328
|
watcher.start();
|
|
@@ -5361,6 +5430,15 @@ function startRepl(rpc, watcher, remotePath, onFileChange) {
|
|
|
5361
5430
|
`);
|
|
5362
5431
|
break;
|
|
5363
5432
|
}
|
|
5433
|
+
case "deploy": {
|
|
5434
|
+
if (!remotePath) {
|
|
5435
|
+
logErr("远程插件目录未知,无法部署");
|
|
5436
|
+
break;
|
|
5437
|
+
}
|
|
5438
|
+
const dir = args[0] || ".";
|
|
5439
|
+
await deployPlugin(path.resolve(dir), remotePath, rpc);
|
|
5440
|
+
break;
|
|
5441
|
+
}
|
|
5364
5442
|
case "watch": {
|
|
5365
5443
|
if (!args[0]) {
|
|
5366
5444
|
logErr("用法: watch <dir>");
|