zumito-framework 1.1.79 → 1.1.80

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.
@@ -64,7 +64,7 @@ export declare class ZumitoFramework {
64
64
  * @see {@link TranslationManager}
65
65
  */
66
66
  translations: TranslationManager;
67
- routes: Map<string, (req: Request, res: Response) => void>;
67
+ routes: any;
68
68
  /**
69
69
  * The database models loaded in the framework.
70
70
  * @type {Array<DatabaseModel>}
@@ -131,7 +131,6 @@ export class ZumitoFramework {
131
131
  this.events = new Map();
132
132
  this.translations = new TranslationManager();
133
133
  this.models = [];
134
- this.routes = new Map();
135
134
  this.eventManager = new EventManager();
136
135
  if (settings.logLevel) {
137
136
  console.logLevel = settings.logLevel;
@@ -210,9 +209,6 @@ export class ZumitoFramework {
210
209
  //Route Prefixes
211
210
  //this.app.use("/", indexRouter);
212
211
  //this.app.use("/api/", apiRouter);
213
- this.routes.forEach((router, path) => {
214
- this.app.use(path, router);
215
- });
216
212
  // throw 404 if URL not found
217
213
  this.app.all('*', function (req, res) {
218
214
  return ApiResponse.notFoundResponse(res, 'Page not found');
@@ -304,8 +300,12 @@ export class ZumitoFramework {
304
300
  moduleInstance.getModels().forEach((model) => {
305
301
  this.models.push(model);
306
302
  });
303
+ /*
304
+
307
305
  // Register module routes
308
306
  this.routes = new Map([...this.routes, ...moduleInstance.getRoutes()]);
307
+
308
+ */
309
309
  }
310
310
  /**
311
311
  * Initializes the Discord client using the Discord.js library.
package/dist/index.d.ts CHANGED
@@ -21,5 +21,4 @@ import { TranslationManager } from './TranslationManager.js';
21
21
  import { ZumitoFramework } from './ZumitoFramework.js';
22
22
  import * as discord from 'discord.js';
23
23
  import { EventParameters } from './types/EventParameters.js';
24
- import { FrameworkRouter } from './types/FrameworkRouter.js';
25
- export { ZumitoFramework, FrameworkSettings, Command, Module, CommandParameters, CommandArguments, FrameworkEvent, Translation, TranslationManager, ApiResponse, SelectMenuParameters, CommandType, CommandArgDefinition, CommandChoiceDefinition, ButtonPressed, ButtonPressedParams, TextFormatter, EmojiFallback, DatabaseConfigLoader, DatabaseModel, PresenceDataRule, RuledPresenceData, StatusManagerOptions, discord, EventParameters, FrameworkRouter, };
24
+ export { ZumitoFramework, FrameworkSettings, Command, Module, CommandParameters, CommandArguments, FrameworkEvent, Translation, TranslationManager, ApiResponse, SelectMenuParameters, CommandType, CommandArgDefinition, CommandChoiceDefinition, ButtonPressed, ButtonPressedParams, TextFormatter, EmojiFallback, DatabaseConfigLoader, DatabaseModel, PresenceDataRule, RuledPresenceData, StatusManagerOptions, discord, EventParameters, };
package/dist/index.js CHANGED
@@ -13,5 +13,4 @@ import { Translation } from './types/Translation.js';
13
13
  import { TranslationManager } from './TranslationManager.js';
14
14
  import { ZumitoFramework } from './ZumitoFramework.js';
15
15
  import * as discord from 'discord.js';
16
- import { FrameworkRouter } from './types/FrameworkRouter.js';
17
- export { ZumitoFramework, Command, Module, CommandArguments, FrameworkEvent, Translation, TranslationManager, ApiResponse, CommandType, ButtonPressed, TextFormatter, EmojiFallback, DatabaseConfigLoader, DatabaseModel, discord, FrameworkRouter, };
16
+ export { ZumitoFramework, Command, Module, CommandArguments, FrameworkEvent, Translation, TranslationManager, ApiResponse, CommandType, ButtonPressed, TextFormatter, EmojiFallback, DatabaseConfigLoader, DatabaseModel, discord, };
@@ -2,14 +2,12 @@ import { ZumitoFramework } from '../ZumitoFramework.js';
2
2
  import { Command } from './Command.js';
3
3
  import { FrameworkEvent } from './FrameworkEvent.js';
4
4
  import { DatabaseModel } from './DatabaseModel.js';
5
- import { Request, Response } from 'express';
6
5
  export declare abstract class Module {
7
6
  protected path: string;
8
7
  protected framework: ZumitoFramework;
9
8
  protected commands: Map<string, Command>;
10
9
  protected events: Map<string, FrameworkEvent>;
11
10
  protected models: Array<DatabaseModel>;
12
- protected routes: Map<string, (req: Request, res: Response) => void>;
13
11
  constructor(path: any, framework: any);
14
12
  initialize(): Promise<void>;
15
13
  registerCommands(): Promise<void>;
@@ -23,10 +21,11 @@ export declare abstract class Module {
23
21
  parseEventArgs(args: any[]): any;
24
22
  getEvents(): Map<string, FrameworkEvent>;
25
23
  registerTranslations(subpath?: string): Promise<void>;
24
+ onTranslationCreated(filePath: string): Promise<void>;
25
+ onTranslationChanged(filePath: string): Promise<void>;
26
+ onErrorLoadingTranslation(error: Error): void;
26
27
  loadTranslationFile(subpath: string, file: string): Promise<any>;
27
28
  parseTranslation(path: string, lang: string, json: any): any;
28
29
  registerModels(): Promise<void>;
29
30
  getModels(): Array<DatabaseModel>;
30
- registerRoutes(subpath?: string): Promise<void>;
31
- getRoutes(): Map<string, (req: Request, res: Response) => void>;
32
31
  }
@@ -4,14 +4,12 @@ import boxen from 'boxen';
4
4
  import * as fs from 'fs';
5
5
  import path from 'path';
6
6
  import { ButtonInteraction, CommandInteraction, ModalSubmitInteraction, StringSelectMenuInteraction, } from 'discord.js';
7
- import { FrameworkRouter } from "./FrameworkRouter.js";
8
7
  export class Module {
9
8
  path;
10
9
  framework;
11
10
  commands = new Map();
12
11
  events = new Map();
13
12
  models = [];
14
- routes = new Map();
15
13
  constructor(path, framework) {
16
14
  this.path = path;
17
15
  this.framework = framework;
@@ -21,7 +19,6 @@ export class Module {
21
19
  await this.registerEvents();
22
20
  await this.registerTranslations();
23
21
  await this.registerModels();
24
- await this.registerRoutes();
25
22
  }
26
23
  async registerCommands() {
27
24
  if (fs.existsSync(path.join(this.path, 'commands'))) {
@@ -40,7 +37,7 @@ export class Module {
40
37
  // register watcher
41
38
  if (process.env.DEBUG) {
42
39
  /*
43
- Debug only cause in prod environment commands should't be changed.
40
+ Debug only cause in prod environment commands should't be changed in runtime.
44
41
  Appart from that, esm module cache invalidation is not working properly
45
42
  and can cause memory leaks and crashes.
46
43
  */
@@ -167,11 +164,59 @@ export class Module {
167
164
  await this.registerTranslations(path.join(subpath, file));
168
165
  }
169
166
  }
167
+ // register watcher
168
+ if (process.env.DEBUG) {
169
+ /*
170
+ Debug only cause in prod environment translations should't be changed on runtime.
171
+ Appart from that, esm module cache invalidation is not working properly
172
+ and can cause memory leaks and crashes.
173
+ */
174
+ chokidar
175
+ .watch(path.resolve(path.join(this.path, 'translations')), {
176
+ ignored: /^\./,
177
+ persistent: true,
178
+ ignoreInitial: true,
179
+ })
180
+ .on('add', this.onTranslationCreated.bind(this))
181
+ .on('change', this.onTranslationChanged.bind(this))
182
+ //.on('unlink', function(path) {console.log('File', path, 'has been removed');})
183
+ .on('error', this.onErrorLoadingTranslation.bind(this));
184
+ }
185
+ }
186
+ async onTranslationCreated(filePath) {
187
+ if (filePath.endsWith('.json')) {
188
+ const subpath = path.dirname(filePath).replace(this.path + '/translations', '');
189
+ const fileName = path.basename(filePath);
190
+ const json = await this.loadTranslationFile(subpath, fileName);
191
+ const lang = fileName.slice(0, -5);
192
+ const baseKey = subpath
193
+ ? subpath.substring(1).replaceAll('/', '.').replaceAll('\\', '.') + '.'
194
+ : '';
195
+ this.parseTranslation(baseKey, lang, json);
196
+ console.debug('[🆕🟢 ] translations file ' + chalk.blue(filePath.replace(/^.*[\\\/]/, '').split('.').slice(0, -1).join('.')) + ' loaded');
197
+ }
198
+ }
199
+ async onTranslationChanged(filePath) {
200
+ if (filePath.endsWith('.json')) {
201
+ const subpath = path.dirname(filePath).replace(this.path + '/translations', '');
202
+ const fileName = path.basename(filePath);
203
+ const json = await this.loadTranslationFile(subpath, fileName);
204
+ const lang = fileName.slice(0, -5);
205
+ const baseKey = subpath
206
+ ? subpath.substring(1).replaceAll('/', '.').replaceAll('\\', '.') + '.'
207
+ : '';
208
+ this.parseTranslation(baseKey, lang, json);
209
+ console.debug('[🆕🟢 ] translations file ' + chalk.blue(filePath.replace(/^.*[\\\/]/, '').split('.').slice(0, -1).join('.')) + ' reloaded');
210
+ }
211
+ }
212
+ onErrorLoadingTranslation(error) {
213
+ console.error('[🔄🔴 ] Error reloading translation file');
214
+ console.log(boxen(error + '\n' + error.stack, { padding: 1 }));
170
215
  }
171
216
  async loadTranslationFile(subpath, file) {
172
217
  if (subpath)
173
218
  subpath = subpath + '/';
174
- const json = await import('file://' + `${this.path}/translations/${subpath}${file}`, {
219
+ const json = await import('file://' + `${this.path}/translations/${subpath}${file}?update=${Date.now().toString()}`, {
175
220
  assert: {
176
221
  type: 'json',
177
222
  },
@@ -210,28 +255,4 @@ export class Module {
210
255
  getModels() {
211
256
  return this.models;
212
257
  }
213
- async registerRoutes(subpath = '') {
214
- if (!fs.existsSync(path.join(this.path, 'routes', subpath)))
215
- return;
216
- const files = fs.readdirSync(path.join(this.path, 'routes', subpath));
217
- for (const file of files) {
218
- if (file.endsWith('.js') || file.endsWith('.ts')) {
219
- const Router = await import('file://' + `${this.path}/routes/${subpath}/${file}`).then(r => Object.values(r)[0]);
220
- if (Router.prototype instanceof FrameworkRouter) {
221
- const router = new Router(subpath ? '/' + subpath : '');
222
- this.routes = new Map([...this.routes, ...router.getRoutes()]);
223
- }
224
- else {
225
- console.warn(`[🔄🟡 ] ${subpath}/${file} is not a valid router on module ${this.constructor.name} \n It must extend the FrameworkRouter class instead of ${Router.prototype}`);
226
- continue;
227
- }
228
- }
229
- else if (fs.lstatSync(path.join(this.path, 'routes', subpath, file)).isDirectory()) {
230
- await this.registerRoutes(path.join(subpath, file));
231
- }
232
- }
233
- }
234
- getRoutes() {
235
- return this.routes;
236
- }
237
258
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zumito-framework",
3
- "version": "1.1.79",
3
+ "version": "1.1.80",
4
4
  "description": "Discord.js bot framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,6 +0,0 @@
1
- import { Request, Response } from 'express';
2
- export declare abstract class FrameworkRouter {
3
- basePath: string;
4
- constructor(basePath: string);
5
- abstract getRoutes(): Map<string, (req: Request, res: Response) => void>;
6
- }
@@ -1,6 +0,0 @@
1
- export class FrameworkRouter {
2
- basePath = '';
3
- constructor(basePath) {
4
- this.basePath = basePath;
5
- }
6
- }