From 96d84a8a857602382145c1ef498780a8158cee9b Mon Sep 17 00:00:00 2001 From: Drew Short Date: Fri, 10 Jan 2020 14:34:27 -0600 Subject: [PATCH] Converting the base message and module to TS --- scripts/run_development_local.sh | 2 + src/config.ts | 2 +- src/message.ts | 4 +- src/module/{abstract.js => abstract.ts} | 70 ++++++++++++++----------- src/module/{index.js => index.ts} | 4 +- 5 files changed, 47 insertions(+), 35 deletions(-) rename src/module/{abstract.js => abstract.ts} (79%) rename src/module/{index.js => index.ts} (84%) diff --git a/scripts/run_development_local.sh b/scripts/run_development_local.sh index b2db88f..6cf8ded 100755 --- a/scripts/run_development_local.sh +++ b/scripts/run_development_local.sh @@ -10,7 +10,9 @@ echo "LOCAL_DEVELOPMENT_"$(cat build.info) > build.info export NODE_ENV=development export LOG_LEVEL=debug +set -e npm run build +set +e ./entrypoint.sh run diff --git a/src/config.ts b/src/config.ts index eb717a4..bdd29b7 100644 --- a/src/config.ts +++ b/src/config.ts @@ -11,7 +11,7 @@ function sanitizeConfig(config: any, fields = []) : any { return clonedConfig; } -function getConfig(configFile: String, sanitizedFields: Array = [], reload: Boolean = false) : any { +function getConfig(configFile: string, sanitizedFields: Array = [], reload: Boolean = false) : any { if (loadedConfigs.has(configFile) && !reload) { return loadedConfigs.get(configFile); } else { diff --git a/src/message.ts b/src/message.ts index 88952fd..621fe3c 100644 --- a/src/message.ts +++ b/src/message.ts @@ -7,14 +7,14 @@ enum MessageTypes { NOTICE = 'm.notice' } -function createBasicMessage(body: String, msgtype: MessageTypes = MessageTypes.TEXT) { +function createBasicMessage(body: string, msgtype: MessageTypes = MessageTypes.TEXT) { return { "body": body, "msgtype": msgtype }; } -function createMarkdownMessage(body: String, markdown: String, msgtype: MessageTypes = MessageTypes.TEXT) { +function createMarkdownMessage(body: string, markdown: string, msgtype: MessageTypes = MessageTypes.TEXT) { return { "body": body, "msgtype": msgtype, diff --git a/src/module/abstract.js b/src/module/abstract.ts similarity index 79% rename from src/module/abstract.js rename to src/module/abstract.ts index 35fc526..f104b5f 100644 --- a/src/module/abstract.js +++ b/src/module/abstract.ts @@ -11,74 +11,82 @@ class AbstractModule { /* Name of the module used in help documentation and logging. */ - name = "AbstractModule"; + name: string = "AbstractModule"; /* Short description of the module functionality. */ - description = "Base Module That All Other Modules Extend"; + description: string = "Base Module That All Other Modules Extend"; /* A helpful multiline string that defines the module usage */ - helpAndUsage = `Example: !abstract_module + helpAndUsage: string = `Example: !abstract_module !abstract_module : scares people` /* The exported command used to invoke the module directly. */ - command = "abstract_module"; + command: string = "abstract_module"; /* The default method to call when a command word is not recognized. */ - defaultCommand = null; + defaultCommand?: string = null; /* The module should be hidden from help and command dialogs. */ - hidden = false; + hidden: boolean = false; /* This module should receive all messages, regardless of whether the module was directly referenced with a command. */ - canHandleIndirectMessages = false; + canHandleIndirectMessages: boolean = false; /* Indicates if the modules needs access to the global config */ - needGlobalConfig = false; + needGlobalConfig: boolean = false; /* Indicates if the module requires a readable config file. */ - needConfig = false; + needConfig: boolean = false; /* internal */ /* The global config passed in */ - _global_config = null; + _global_config: any = null; /* The loaded config file, if it exists. */ - _config = null; + _config: any = null; - constructor(name, description, command) { + /** + * The recognized commands for the module + */ + _recognizedCommands: Array = []; + + /** + * The map of commands to functions + */ + _recognizedCommandMap: Map = new Map(); + + constructor(name: string, description: string, command: string) { this.name = name; this.description = description; this.command = command; - this._recognizedCommands = []; - this._recognizedCommandMap = new Map(); } /** * Called after the module is initialized. */ - postInit() { + postInit() : any { return this; } @@ -88,7 +96,7 @@ class AbstractModule { * @param {*} command * @param {*} methodName */ - addRecognizedCommand(command, methodName) { + addRecognizedCommand(command: string, methodName: string) { this._recognizedCommands.push(command); this._recognizedCommandMap.set(command, methodName); } @@ -96,21 +104,21 @@ class AbstractModule { /** * Returns the list of recognized commands. */ - getRecognizedCommands() { + getRecognizedCommands() : Iterable { return this._recognizedCommandMap.keys(); } /** * The file path that the module configuration file is expected at. */ - getConfigFilePath() { + getConfigFilePath() : string { return process.env.NODE_PATH + '/data/' + this.name.toLowerCase().replace(' ', '_') + '-config.json'; } /** * Fields that should be sanitized before printing the config information for the user. */ - getConfigSensitiveFields() { + getConfigSensitiveFields() : Array { return []; } @@ -120,7 +128,7 @@ class AbstractModule { * @param {*} key * @param {*} defaultValue */ - getGlobal(key, defaultValue = null) { + getGlobal(key: string, defaultValue: string = null) { if (this._global_config !== null && typeof this._global_config[key] !== 'undefined') { return this._global_config[key]; } @@ -133,7 +141,7 @@ class AbstractModule { * @param {*} key * @param {*} defaultValue */ - get(key, defaultValue = null) { + get(key: string, defaultValue: string = null) { if (this._config !== null && typeof this._config[key] !== 'undefined') { return this._config[key]; } @@ -145,7 +153,7 @@ class AbstractModule { * * Override this if the module needs to do more complicated message processing. */ - handleMessage(event, room, callback) { + handleMessage(event: any, room: any, callback: CallableFunction) { logger.debug("[%s] [%s] [%s]", this.name, room.name, event.event.content.body); let messageBody = event.event.content.body; @@ -169,24 +177,24 @@ class AbstractModule { /* Call the command method with the args */ - processMessage(event, command, ...args) { + processMessage(event: any, command: string, ...args: Array) { if (this._recognizedCommands.includes(command)) { logger.debug("Calling %s with %s", this._recognizedCommandMap.get(command), args); return this[this._recognizedCommandMap.get(command)](event, ...args); } else { if (this.defaultCommand != null) { logger.debug("Attempting to use default command %s", this.defaultCommand); + let newArgs = [command].concat(...args); try { - let newArgs = [command].concat(...args); logger.debug("Calling %s with %s", this._recognizedCommandMap.get(this.defaultCommand), newArgs); return this[this._recognizedCommandMap.get(this.defaultCommand)](event, ...newArgs); } catch (e) { logger.error("Error while calling default command %s %s", this.defaultCommand, e); - return this.cmd_help(); + return this.cmd_help(event, ...newArgs); } } else { logger.debug("Unrecognized command %s", command); - return this.cmd_help(); + return this.cmd_help(event, ...[command].concat(...args)); } } } @@ -196,7 +204,7 @@ class AbstractModule { /* return basic help information,. */ - cmd_help(event, ...args) { + cmd_help(event: any, ...args: Array) { return message.createBasic(this.helpAndUsage); } @@ -205,7 +213,7 @@ class AbstractModule { * * @param {...any} args */ - cmd_config(event, ...args) { + cmd_config(event: any, ...args: Array) { if (this._config != null) { let configBody = JSON.stringify(sanitizeConfig(this._config, this.getConfigSensitiveFields()), null, 2) return message.createMarkdown(configBody, "```" + configBody + "```"); @@ -219,7 +227,7 @@ let abstractModulePrototype = Object.getPrototypeOf(new AbstractModule('', '', ' /* Initialization of a module. */ -function init(mod, globalConfig) { +function init(mod: AbstractModule, globalConfig: any) { logger.debug("Initializing module %s", mod.name); if (mod.needConfig) { logger.debug("Loading config file %s", mod.getConfigFilePath()); @@ -248,5 +256,5 @@ function init(mod, globalConfig) { mod.postInit(); } -exports.AbstractModule = AbstractModule -exports.initModule = init; \ No newline at end of file +export { AbstractModule }; +export { init as initModule }; \ No newline at end of file diff --git a/src/module/index.js b/src/module/index.ts similarity index 84% rename from src/module/index.js rename to src/module/index.ts index 3dd5517..6993300 100644 --- a/src/module/index.js +++ b/src/module/index.ts @@ -14,4 +14,6 @@ function getModules() { ]; } -exports.modules = getModules(); \ No newline at end of file +let modules = getModules(); + +export { modules }; \ No newline at end of file