napcat-plugin-debug-cli 1.2.3 → 1.2.5

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