zumito-framework 1.23.3 → 1.24.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.
@@ -0,0 +1,32 @@
1
+ import { ZumitoFramework } from '../../ZumitoFramework.js';
2
+ import type { ModuleEntry } from '../settings/FrameworkSettings.js';
3
+ /**
4
+ * Creates a type-safe factory function that returns a `ModuleEntry`.
5
+ * Reads the module name from the nearest `package.json` by walking up
6
+ * from the caller's location.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * // admin/index.ts
11
+ * export const adminModule = createModuleEntry<AdminConfig>(import.meta.url);
12
+ *
13
+ * // Consumer's zumito.config.ts
14
+ * adminModule({ colors: { primary: '#ff0000' } })
15
+ * ```
16
+ */
17
+ export declare function createModuleEntry<T = Record<string, any>>(metaUrl: string): (config?: T) => ModuleEntry;
18
+ /**
19
+ * Retrieves the module-specific configuration from the framework settings
20
+ * by matching the module's package name against the `modules` array.
21
+ *
22
+ * Modules that are registered as plain strings in the `modules` array
23
+ * (without config) will return an empty object.
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * import { getModuleConfig } from 'zumito-framework';
28
+ *
29
+ * const config = getModuleConfig<MyConfig>(framework, import.meta.url);
30
+ * ```
31
+ */
32
+ export declare function getModuleConfig<T = Record<string, any>>(framework: ZumitoFramework | undefined, metaUrl: string): T;
@@ -0,0 +1,71 @@
1
+ import { createRequire } from 'module';
2
+ import { fileURLToPath } from 'url';
3
+ import { dirname, resolve } from 'path';
4
+ import { existsSync } from 'fs';
5
+ function findPackageJson(startPath) {
6
+ let dir = startPath;
7
+ for (let i = 0; i < 10; i++) {
8
+ const candidate = resolve(dir, 'package.json');
9
+ if (existsSync(candidate))
10
+ return candidate;
11
+ const parent = resolve(dir, '..');
12
+ if (parent === dir)
13
+ break;
14
+ dir = parent;
15
+ }
16
+ return null;
17
+ }
18
+ function readModuleName(metaUrl) {
19
+ const filePath = fileURLToPath(metaUrl);
20
+ const callerDir = dirname(filePath);
21
+ const pkgPath = findPackageJson(callerDir);
22
+ if (!pkgPath)
23
+ return null;
24
+ const require = createRequire(pkgPath);
25
+ return require(pkgPath)?.name ?? null;
26
+ }
27
+ /**
28
+ * Creates a type-safe factory function that returns a `ModuleEntry`.
29
+ * Reads the module name from the nearest `package.json` by walking up
30
+ * from the caller's location.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * // admin/index.ts
35
+ * export const adminModule = createModuleEntry<AdminConfig>(import.meta.url);
36
+ *
37
+ * // Consumer's zumito.config.ts
38
+ * adminModule({ colors: { primary: '#ff0000' } })
39
+ * ```
40
+ */
41
+ export function createModuleEntry(metaUrl) {
42
+ const name = readModuleName(metaUrl);
43
+ if (!name) {
44
+ throw new Error(`createModuleEntry: could not read 'name' from package.json near ${metaUrl}`);
45
+ }
46
+ return (config) => ({ name, config });
47
+ }
48
+ /**
49
+ * Retrieves the module-specific configuration from the framework settings
50
+ * by matching the module's package name against the `modules` array.
51
+ *
52
+ * Modules that are registered as plain strings in the `modules` array
53
+ * (without config) will return an empty object.
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * import { getModuleConfig } from 'zumito-framework';
58
+ *
59
+ * const config = getModuleConfig<MyConfig>(framework, import.meta.url);
60
+ * ```
61
+ */
62
+ export function getModuleConfig(framework, metaUrl) {
63
+ if (!framework)
64
+ return {};
65
+ const name = readModuleName(metaUrl);
66
+ if (!name)
67
+ return {};
68
+ const modules = framework.settings?.modules ?? [];
69
+ const entry = modules.find((m) => typeof m !== 'string' && m.name === name);
70
+ return (entry?.config ?? {});
71
+ }
package/dist/index.d.ts CHANGED
@@ -42,6 +42,7 @@ export { CommandBinds } from './definitions/commands/CommandBinds.js';
42
42
  export { Injectable } from './definitions/decorators/Injectable.decorator.js';
43
43
  export { LauncherConfig } from './definitions/config/LauncherConfig.js';
44
44
  export { defineConfig } from './definitions/config/defineConfig.js';
45
+ export { createModuleEntry, getModuleConfig } from './definitions/config/moduleHelpers.js';
45
46
  export type { ModuleEntry } from './definitions/settings/FrameworkSettings.js';
46
47
  export { ModuleParameters } from './definitions/parameters/ModuleParameters.js';
47
48
  export { ZumitoFramework, FrameworkSettings, Command, Module, ModuleDeclaration, ModuleRequeriments, CommandParameters, CommandArguments, FrameworkEvent, Translation, TranslationManager, ApiResponse, SelectMenuParameters, CommandType, CommandArgDefinition, CommandChoiceDefinition, ButtonPressed, ButtonPressedParams, TextFormatter, EmojiFallback, DatabaseConfigLoader, PresenceDataRule, RuledPresenceData, StatusManagerOptions, discord, EventParameters, ServiceContainer, GuildDataGetter, SlashCommandRefresher, CommandParser, ErrorHandler, ErrorType, Route, RouteMethod, InteractionHandler, CommandManager, InviteUrlGenerator, PrefixResolver, CommandExecutionChecker, DatabaseManager, };
package/dist/index.js CHANGED
@@ -29,6 +29,7 @@ import { DatabaseManager } from 'zumito-db';
29
29
  export { Collection, Field, HasMany, BelongsTo, HasOne, Migration, Repository, QueryBuilder } from 'zumito-db';
30
30
  export { Injectable } from './definitions/decorators/Injectable.decorator.js';
31
31
  export { defineConfig } from './definitions/config/defineConfig.js';
32
+ export { createModuleEntry, getModuleConfig } from './definitions/config/moduleHelpers.js';
32
33
  ServiceContainer.addService(TextFormatter, []);
33
34
  ServiceContainer.addService(EmojiFallback, [discord.Client.name, TranslationManager.name]);
34
35
  ServiceContainer.addService(GuildDataGetter, [ZumitoFramework.name]);
@@ -42,6 +42,8 @@ export class CommandManager {
42
42
  if (!filePath.endsWith('.js') && !filePath.endsWith('.ts')) {
43
43
  return;
44
44
  }
45
+ if (filePath.endsWith('.d.ts'))
46
+ return;
45
47
  const counter = (this.importCounters.get(filePath) || 0) + 1;
46
48
  this.importCounters.set(filePath, counter);
47
49
  const baseName = path.basename(filePath).split('.').slice(0, -1).join('.');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zumito-framework",
3
- "version": "1.23.3",
3
+ "version": "1.24.0",
4
4
  "description": "Discord.js bot framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",