napcat-plugin-debug-cli 1.2.3 → 1.2.4

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/vite.mjs +109 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "napcat-plugin-debug-cli",
3
- "version": "1.2.3",
3
+ "version": "1.2.4",
4
4
  "type": "module",
5
5
  "description": "NapCat 插件调试 CLI — 连接调试服务实现热重载",
6
6
  "author": "NapNeko",
package/vite.mjs CHANGED
@@ -96,6 +96,8 @@ function napcatHmrPlugin(options = {}) {
96
96
  let connecting = false;
97
97
  let config;
98
98
  let isFirstBuild = true;
99
+ const webuiWatchers = [];
100
+ let webuiDeployDebounceTimer = null;
99
101
  async function connect() {
100
102
  if (rpc?.connected) return true;
101
103
  if (connecting) return false;
@@ -237,6 +239,98 @@ function napcatHmrPlugin(options = {}) {
237
239
  }
238
240
  }
239
241
  }
242
+ async function deployWebuiOnly() {
243
+ if (!rpc?.connected || !remotePluginPath) {
244
+ logErr("未连接到调试服务,跳过 WebUI 部署");
245
+ return;
246
+ }
247
+ const distDir = path.resolve(config.build.outDir);
248
+ const pkgPath = path.join(distDir, "package.json");
249
+ if (!fs.existsSync(pkgPath)) {
250
+ logErr("dist/package.json 不存在,跳过 WebUI 部署");
251
+ return;
252
+ }
253
+ let pluginName;
254
+ try {
255
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
256
+ pluginName = pkg.name;
257
+ if (!pluginName) return;
258
+ } catch {
259
+ return;
260
+ }
261
+ const destDir = path.join(remotePluginPath, pluginName);
262
+ const projectRoot = config.root || process.cwd();
263
+ let hasChanges = false;
264
+ for (const wc of webuiConfigs) {
265
+ const webuiTargetDir = wc.targetDir || "webui";
266
+ const webuiDistDir = path.resolve(projectRoot, wc.distDir);
267
+ const webuiRoot = wc.root ? path.resolve(projectRoot, wc.root) : path.dirname(webuiDistDir);
268
+ if (wc.buildCommand) {
269
+ try {
270
+ log(`构建 WebUI (${co(webuiTargetDir, C.cyan)})...`);
271
+ execSync(wc.buildCommand, {
272
+ cwd: webuiRoot,
273
+ stdio: "pipe",
274
+ env: { ...process.env, NODE_ENV: "production" }
275
+ });
276
+ logOk(`WebUI (${webuiTargetDir}) 构建完成`);
277
+ } catch (e) {
278
+ logErr(`WebUI (${webuiTargetDir}) 构建失败: ${e.stderr?.toString() || e.message}`);
279
+ continue;
280
+ }
281
+ }
282
+ if (!fs.existsSync(webuiDistDir)) {
283
+ logErr(`WebUI 产物目录不存在: ${webuiDistDir}`);
284
+ continue;
285
+ }
286
+ try {
287
+ const webuiDestDir = path.join(destDir, webuiTargetDir);
288
+ if (fs.existsSync(webuiDestDir)) {
289
+ fs.rmSync(webuiDestDir, { recursive: true, force: true });
290
+ }
291
+ copyDirRecursive(webuiDistDir, webuiDestDir);
292
+ logOk(`WebUI (${webuiTargetDir}) 已部署 (${countFiles(webuiDistDir)} 个文件)`);
293
+ hasChanges = true;
294
+ } catch (e) {
295
+ logErr(`WebUI (${webuiTargetDir}) 部署失败: ${e.message}`);
296
+ }
297
+ }
298
+ if (hasChanges) {
299
+ try {
300
+ await rpc.call("reloadPlugin", pluginName);
301
+ logHmr(`${co(pluginName, C.green, C.bold)} 已重载 (WebUI 更新)`);
302
+ } catch (e) {
303
+ logErr(`重载失败: ${e.message}`);
304
+ }
305
+ }
306
+ }
307
+ function startWebuiWatchers(projectRoot) {
308
+ for (const wc of webuiConfigs) {
309
+ if (!wc.watchDir) continue;
310
+ const watchPath = path.resolve(projectRoot, wc.watchDir);
311
+ const webuiTargetDir = wc.targetDir || "webui";
312
+ if (!fs.existsSync(watchPath)) {
313
+ logErr(`WebUI watchDir 不存在: ${watchPath}`);
314
+ continue;
315
+ }
316
+ try {
317
+ const watcher = fs.watch(watchPath, { recursive: true }, (_event, filename) => {
318
+ if (!filename) return;
319
+ const normalized = filename.replace(/\\/g, "/");
320
+ if (normalized.includes("node_modules") || normalized.includes("/dist/") || normalized.startsWith("dist/") || normalized.startsWith(".")) return;
321
+ if (webuiDeployDebounceTimer) clearTimeout(webuiDeployDebounceTimer);
322
+ webuiDeployDebounceTimer = setTimeout(() => {
323
+ log(`WebUI 文件变化: ${co(normalized, C.dim)}`);
324
+ deployWebuiOnly().catch((e) => logErr(`WebUI 部署出错: ${e.message}`));
325
+ }, 300);
326
+ });
327
+ webuiWatchers.push(watcher);
328
+ logOk(`监听 WebUI (${webuiTargetDir}): ${co(watchPath, C.dim)}`);
329
+ } catch (e) {
330
+ logErr(`无法监听 WebUI 目录: ${e.message}`);
331
+ }
332
+ }
333
+ }
240
334
  return {
241
335
  name: "napcat-hmr",
242
336
  apply: "build",
@@ -264,9 +358,24 @@ function napcatHmrPlugin(options = {}) {
264
358
  if (!ok) return;
265
359
  }
266
360
  await deployAndReload(distDir);
361
+ if (isFirstBuild && config.build.watch && webuiConfigs.some((wc) => wc.watchDir)) {
362
+ const projectRoot = config.root || process.cwd();
363
+ startWebuiWatchers(projectRoot);
364
+ }
267
365
  isFirstBuild = false;
268
366
  },
269
367
  closeBundle() {
368
+ for (const w of webuiWatchers) {
369
+ try {
370
+ w.close();
371
+ } catch {
372
+ }
373
+ }
374
+ webuiWatchers.length = 0;
375
+ if (webuiDeployDebounceTimer) {
376
+ clearTimeout(webuiDeployDebounceTimer);
377
+ webuiDeployDebounceTimer = null;
378
+ }
270
379
  if (config.build.watch) return;
271
380
  rpc?.close();
272
381
  rpc = null;