nuxt-discord 0.0.6 → 0.0.8

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/dist/module.d.mts CHANGED
@@ -60,6 +60,7 @@ interface DiscordRuntimeConfig {
60
60
  sync: NuxtDiscordOptions['watch']['sync'];
61
61
  dir: string;
62
62
  buildDir: string;
63
+ rootDir: string;
63
64
  }
64
65
  declare const slashCommandOptionTypeIdentifiers: readonly ["number", "integer", "string", "boolean"];
65
66
  declare const typeIdentifierToEnum: {
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxt-discord",
3
3
  "configKey": "discord",
4
- "version": "0.0.6",
4
+ "version": "0.0.8",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.1",
7
7
  "unbuild": "3.5.0"
package/dist/module.mjs CHANGED
@@ -1,9 +1,11 @@
1
1
  import { useLogger, createResolver, installModule, extendPages, updateTemplates, addVitePlugin, isIgnored, addServerTemplate, addTemplate, addTypeTemplate, defineNuxtModule, addServerScanDir, addImports } from '@nuxt/kit';
2
2
  import { ApplicationCommandOptionType, GatewayIntentBits } from 'discord.js';
3
- import { globSync, readFileSync, existsSync, writeFileSync } from 'node:fs';
3
+ import { globSync, readFileSync, existsSync, mkdirSync, writeFileSync } from 'node:fs';
4
4
  import path from 'node:path';
5
5
  import ts from 'typescript';
6
+ import chokidar from 'chokidar';
6
7
  import { listen } from 'listhen';
8
+ import { rollup } from 'rollup';
7
9
  import { WebSocketServer } from 'ws';
8
10
  import defu from 'defu';
9
11
  import { genSafeVariableName, genImport, genArrayFromRaw, genString } from 'knitwork';
@@ -251,6 +253,7 @@ function prepareHMR(ctx) {
251
253
  }
252
254
  return isIgnored(file);
253
255
  };
256
+ let rollupConfig;
254
257
  nuxt.hook("nitro:config", (nitroConfig) => {
255
258
  nitroConfig.watchOptions ??= {};
256
259
  const existingNitroIgnored = nitroConfig.watchOptions.ignored;
@@ -261,6 +264,12 @@ function prepareHMR(ctx) {
261
264
  else
262
265
  nitroIgnored.push(existingNitroIgnored);
263
266
  }
267
+ nitroConfig.watchOptions.ignored = nitroIgnored;
268
+ nitroConfig.hooks ??= {
269
+ "rollup:before": (_, config) => {
270
+ rollupConfig = config;
271
+ }
272
+ };
264
273
  });
265
274
  nuxt.options.vite.server ??= {};
266
275
  nuxt.options.vite.server.watch ??= {};
@@ -298,12 +307,15 @@ function prepareHMR(ctx) {
298
307
  });
299
308
  });
300
309
  }
301
- nuxt.hook("builder:watch", async (event, path) => {
302
- const fullPath = ctx.resolve.root(path);
310
+ chokidar.watch(ctx.resolve.root(ctx.options.dir, "commands"), { ignoreInitial: true }).on("all", async (event, path2) => {
311
+ const fullPath = ctx.resolve.root(path2);
303
312
  if (fullPath.startsWith(commandsDir)) {
304
- path = ctx.resolve.root(path);
305
- const command = processCommandFile(ctx, path) ?? null;
306
- websocket?.broadcast({ event, path, command });
313
+ path2 = ctx.resolve.root(path2);
314
+ const command = processCommandFile(ctx, path2) ?? null;
315
+ if (event === "add" || event === "change") {
316
+ await generateDynamicCommandBuild(path2, rollupConfig, ctx);
317
+ }
318
+ websocket?.broadcast({ event, path: path2, command });
307
319
  await updateTemplates({
308
320
  filter: (template) => template.filename === "discord/slashCommands"
309
321
  });
@@ -320,12 +332,12 @@ function prepareHMR(ctx) {
320
332
  const config = (await import('../dist/runtime/client/uno.config.js')).default;
321
333
  const generator = await createGenerator(config);
322
334
  const clientDir = ctx.resolve.module("runtime/client");
323
- const generateCSS = async (path) => {
324
- if (path.startsWith(clientDir) && path.endsWith(".vue")) {
325
- const content = readFileSync(path, "utf-8");
326
- const tokens = await generator.applyExtractors(content, path);
335
+ const generateCSS = async (path2) => {
336
+ if (path2.startsWith(clientDir) && path2.endsWith(".vue")) {
337
+ const content = readFileSync(path2, "utf-8");
338
+ const tokens = await generator.applyExtractors(content, path2);
327
339
  const { css } = await generator.generate(tokens);
328
- writeFileSync(path.replace(".vue", ".css"), css, "utf-8");
340
+ writeFileSync(path2.replace(".vue", ".css"), css, "utf-8");
329
341
  }
330
342
  };
331
343
  const globPattern = `${clientDir}/**/*.vue`;
@@ -388,6 +400,19 @@ function createWebSocket() {
388
400
  }
389
401
  };
390
402
  }
403
+ async function generateDynamicCommandBuild(file, config, ctx) {
404
+ let bundle;
405
+ try {
406
+ bundle = await rollup({ ...config, input: file });
407
+ const { output } = await bundle.generate({ ...config.output, sourcemap: false });
408
+ mkdirSync(path.join(ctx.nuxt.options.buildDir, "discord", "commands"), { recursive: true });
409
+ writeFileSync(file.replace(ctx.resolve.root(ctx.nuxt.options.rootDir), ctx.nuxt.options.buildDir).replace(".ts", ".mjs"), output[0].code, { encoding: "utf-8", flush: true });
410
+ } catch (error) {
411
+ ctx.logger.error(`Error processing command file ${file}:`, error);
412
+ return;
413
+ }
414
+ await bundle.close();
415
+ }
391
416
 
392
417
  function prepareRuntimeConfig(ctx) {
393
418
  ctx.nuxt.hook("nitro:config", (config) => {
@@ -398,6 +423,7 @@ function prepareRuntimeConfig(ctx) {
398
423
  config.runtimeConfig.discord.sync = ctx.options.watch.sync ?? false;
399
424
  config.runtimeConfig.discord.dir = ctx.resolve.root(ctx.options.dir);
400
425
  config.runtimeConfig.discord.buildDir = ctx.nuxt.options.buildDir;
426
+ config.runtimeConfig.discord.rootDir = ctx.nuxt.options.rootDir;
401
427
  });
402
428
  }
403
429
 
@@ -156,9 +156,9 @@ if (import.meta.dev && runtimeConfig.public.discord?.wsUrl) {
156
156
  autoReconnect: true,
157
157
  onConnected: () => {
158
158
  console.log("WebSocket connection established for slash command hot reload");
159
+ ws?.send(JSON.stringify({ event: "full-update" }));
159
160
  }
160
161
  });
161
- ws?.send(JSON.stringify({ event: "full-update" }));
162
162
  }
163
163
  }
164
164
  watchEffect(() => {
@@ -0,0 +1,5 @@
1
+ export declare const logger: {
2
+ log: (...args: unknown[]) => void;
3
+ warn: (...args: unknown[]) => void;
4
+ error: (...args: unknown[]) => void;
5
+ };
@@ -0,0 +1,7 @@
1
+ import ansis from "ansis";
2
+ export const logger = {
3
+ // eslint-disable-next-line no-console
4
+ log: (...args) => console.log(ansis.hex("#5865F2").bold("[Discord]"), ...args),
5
+ warn: (...args) => console.warn(ansis.yellow.bold("[Discord]"), ...args),
6
+ error: (...args) => console.error(ansis.red.bold("[Discord]"), ...args)
7
+ };
@@ -1,5 +1,7 @@
1
+ import { existsSync } from "node:fs";
1
2
  import slashCommands from "discord/slashCommands";
2
3
  import { defineNitroPlugin, useRuntimeConfig } from "nitropack/runtime";
4
+ import { logger } from "../internal/logger.js";
3
5
  import { DiscordClient } from "../utils/client.js";
4
6
  export default defineNitroPlugin(async (nitro) => {
5
7
  const runtimeConfig = useRuntimeConfig();
@@ -16,7 +18,17 @@ export default defineNitroPlugin(async (nitro) => {
16
18
  const commands = [];
17
19
  for (const cmd of data.commands) {
18
20
  const runtimeCommand = cmd;
19
- runtimeCommand.load = () => import(cmd.path).then((m) => m.default);
21
+ const buildPath = cmd.path.replace(runtimeConfig.discord.rootDir, runtimeConfig.discord.buildDir).replace(/\.ts$/, ".mjs");
22
+ const existingCommand = slashCommands.find((c) => c.path === cmd.path);
23
+ if (existsSync(buildPath)) {
24
+ logger.log(`Dynamically loading slash command at ${buildPath}`);
25
+ runtimeCommand.load = () => import(`${buildPath}?${Date.now()}`).then((m) => m.default);
26
+ } else if (existingCommand) {
27
+ runtimeCommand.execute = existingCommand.execute;
28
+ } else {
29
+ logger.error(`Unable to dynamically load slash command at ${cmd.path}`);
30
+ continue;
31
+ }
20
32
  commands.push(runtimeCommand);
21
33
  }
22
34
  client.clearSlashCommands();
@@ -25,9 +37,6 @@ export default defineNitroPlugin(async (nitro) => {
25
37
  socket.send(JSON.stringify({ event: "full-update" }));
26
38
  }
27
39
  });
28
- socket.addEventListener("open", () => {
29
- socket.send(JSON.stringify({ event: "full-update" }));
30
- });
31
40
  nitro.hooks.hook("close", () => socket.close());
32
41
  }
33
42
  client.addSlashCommands(slashCommands);
@@ -1,17 +1,11 @@
1
1
  import process from "node:process";
2
- import ansis from "ansis";
3
2
  import { ApplicationCommandOptionType, Events, Client as InternalClient, REST, Routes, SlashCommandBuilder } from "discord.js";
4
3
  import { useNitroApp } from "nitropack/runtime";
4
+ import { logger } from "../internal/logger.js";
5
5
  let currentInteraction = null;
6
6
  export function useInteraction() {
7
7
  return currentInteraction;
8
8
  }
9
- const logger = {
10
- // eslint-disable-next-line no-console
11
- log: (...args) => console.log(ansis.hex("#5865F2").bold("[Discord]"), ...args),
12
- warn: (...args) => console.warn(ansis.yellow.bold("[Discord]"), ...args),
13
- error: (...args) => console.error(ansis.red.bold("[Discord]"), ...args)
14
- };
15
9
  export class DiscordClient {
16
10
  #client = null;
17
11
  get internalClient() {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxt-discord",
3
3
  "type": "module",
4
- "version": "0.0.6",
4
+ "version": "0.0.8",
5
5
  "description": "A Nuxt module for building Discord bots",
6
6
  "author": "Jize Zou <jeffzou2001@gmail.com> (https://github.com/zojize)",
7
7
  "license": "MIT",
@@ -37,9 +37,11 @@
37
37
  "@nuxt/kit": "^3.17.5",
38
38
  "@nuxt/ui": "^3.2.0",
39
39
  "ansis": "^4.1.0",
40
+ "chokidar": "^4.0.3",
40
41
  "defu": "^6.1.4",
41
42
  "knitwork": "^1.2.0",
42
43
  "listhen": "^1.9.0",
44
+ "rollup": "^4.44.1",
43
45
  "simple-icons": "^15.3.0",
44
46
  "unocss": "^66.3.2",
45
47
  "ws": "^8.18.2"