Browse Source

Converting the base message and module to TS

develop
Drew Short 4 years ago
parent
commit
96d84a8a85
  1. 2
      scripts/run_development_local.sh
  2. 2
      src/config.ts
  3. 4
      src/message.ts
  4. 70
      src/module/abstract.ts
  5. 4
      src/module/index.ts

2
scripts/run_development_local.sh

@ -10,7 +10,9 @@ echo "LOCAL_DEVELOPMENT_"$(cat build.info) > build.info
export NODE_ENV=development export NODE_ENV=development
export LOG_LEVEL=debug export LOG_LEVEL=debug
set -e
npm run build npm run build
set +e
./entrypoint.sh run ./entrypoint.sh run

2
src/config.ts

@ -11,7 +11,7 @@ function sanitizeConfig(config: any, fields = []) : any {
return clonedConfig; return clonedConfig;
} }
function getConfig(configFile: String, sanitizedFields: Array<String> = [], reload: Boolean = false) : any {
function getConfig(configFile: string, sanitizedFields: Array<string> = [], reload: Boolean = false) : any {
if (loadedConfigs.has(configFile) && !reload) { if (loadedConfigs.has(configFile) && !reload) {
return loadedConfigs.get(configFile); return loadedConfigs.get(configFile);
} else { } else {

4
src/message.ts

@ -7,14 +7,14 @@ enum MessageTypes {
NOTICE = 'm.notice' NOTICE = 'm.notice'
} }
function createBasicMessage(body: String, msgtype: MessageTypes = MessageTypes.TEXT) {
function createBasicMessage(body: string, msgtype: MessageTypes = MessageTypes.TEXT) {
return { return {
"body": body, "body": body,
"msgtype": msgtype "msgtype": msgtype
}; };
} }
function createMarkdownMessage(body: String, markdown: String, msgtype: MessageTypes = MessageTypes.TEXT) {
function createMarkdownMessage(body: string, markdown: string, msgtype: MessageTypes = MessageTypes.TEXT) {
return { return {
"body": body, "body": body,
"msgtype": msgtype, "msgtype": msgtype,

70
src/module/abstract.js → src/module/abstract.ts

@ -11,74 +11,82 @@ class AbstractModule {
/* /*
Name of the module used in help documentation and logging. Name of the module used in help documentation and logging.
*/ */
name = "AbstractModule";
name: string = "AbstractModule";
/* /*
Short description of the module functionality. 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 A helpful multiline string that defines the module usage
*/ */
helpAndUsage = `Example: !abstract_module <command>
helpAndUsage: string = `Example: !abstract_module <command>
!abstract_module <boo> : scares people` !abstract_module <boo> : scares people`
/* /*
The exported command used to invoke the module directly. 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. 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. The module should be hidden from help and command dialogs.
*/ */
hidden = false;
hidden: boolean = false;
/* /*
This module should receive all messages, regardless of whether This module should receive all messages, regardless of whether
the module was directly referenced with a command. the module was directly referenced with a command.
*/ */
canHandleIndirectMessages = false;
canHandleIndirectMessages: boolean = false;
/* /*
Indicates if the modules needs access to the global config Indicates if the modules needs access to the global config
*/ */
needGlobalConfig = false;
needGlobalConfig: boolean = false;
/* /*
Indicates if the module requires a readable config file. Indicates if the module requires a readable config file.
*/ */
needConfig = false;
needConfig: boolean = false;
/* internal */ /* internal */
/* /*
The global config passed in The global config passed in
*/ */
_global_config = null;
_global_config: any = null;
/* /*
The loaded config file, if it exists. The loaded config file, if it exists.
*/ */
_config = null;
_config: any = null;
constructor(name, description, command) {
/**
* The recognized commands for the module
*/
_recognizedCommands: Array<string> = [];
/**
* The map of commands to functions
*/
_recognizedCommandMap: Map<string, string> = new Map();
constructor(name: string, description: string, command: string) {
this.name = name; this.name = name;
this.description = description; this.description = description;
this.command = command; this.command = command;
this._recognizedCommands = [];
this._recognizedCommandMap = new Map();
} }
/** /**
* Called after the module is initialized. * Called after the module is initialized.
*/ */
postInit() {
postInit() : any {
return this; return this;
} }
@ -88,7 +96,7 @@ class AbstractModule {
* @param {*} command * @param {*} command
* @param {*} methodName * @param {*} methodName
*/ */
addRecognizedCommand(command, methodName) {
addRecognizedCommand(command: string, methodName: string) {
this._recognizedCommands.push(command); this._recognizedCommands.push(command);
this._recognizedCommandMap.set(command, methodName); this._recognizedCommandMap.set(command, methodName);
} }
@ -96,21 +104,21 @@ class AbstractModule {
/** /**
* Returns the list of recognized commands. * Returns the list of recognized commands.
*/ */
getRecognizedCommands() {
getRecognizedCommands() : Iterable<string> {
return this._recognizedCommandMap.keys(); return this._recognizedCommandMap.keys();
} }
/** /**
* The file path that the module configuration file is expected at. * 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'; 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. * Fields that should be sanitized before printing the config information for the user.
*/ */
getConfigSensitiveFields() {
getConfigSensitiveFields() : Array<string> {
return []; return [];
} }
@ -120,7 +128,7 @@ class AbstractModule {
* @param {*} key * @param {*} key
* @param {*} defaultValue * @param {*} defaultValue
*/ */
getGlobal(key, defaultValue = null) {
getGlobal(key: string, defaultValue: string = null) {
if (this._global_config !== null && typeof this._global_config[key] !== 'undefined') { if (this._global_config !== null && typeof this._global_config[key] !== 'undefined') {
return this._global_config[key]; return this._global_config[key];
} }
@ -133,7 +141,7 @@ class AbstractModule {
* @param {*} key * @param {*} key
* @param {*} defaultValue * @param {*} defaultValue
*/ */
get(key, defaultValue = null) {
get(key: string, defaultValue: string = null) {
if (this._config !== null && typeof this._config[key] !== 'undefined') { if (this._config !== null && typeof this._config[key] !== 'undefined') {
return this._config[key]; return this._config[key];
} }
@ -145,7 +153,7 @@ class AbstractModule {
* *
* Override this if the module needs to do more complicated message processing. * 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); logger.debug("[%s] [%s] [%s]", this.name, room.name, event.event.content.body);
let messageBody = event.event.content.body; let messageBody = event.event.content.body;
@ -169,24 +177,24 @@ class AbstractModule {
/* /*
Call the command method with the args Call the command method with the args
*/ */
processMessage(event, command, ...args) {
processMessage(event: any, command: string, ...args: Array<string>) {
if (this._recognizedCommands.includes(command)) { if (this._recognizedCommands.includes(command)) {
logger.debug("Calling %s with %s", this._recognizedCommandMap.get(command), args); logger.debug("Calling %s with %s", this._recognizedCommandMap.get(command), args);
return this[this._recognizedCommandMap.get(command)](event, ...args); return this[this._recognizedCommandMap.get(command)](event, ...args);
} else { } else {
if (this.defaultCommand != null) { if (this.defaultCommand != null) {
logger.debug("Attempting to use default command %s", this.defaultCommand); logger.debug("Attempting to use default command %s", this.defaultCommand);
let newArgs = [command].concat(...args);
try { try {
let newArgs = [command].concat(...args);
logger.debug("Calling %s with %s", this._recognizedCommandMap.get(this.defaultCommand), newArgs); logger.debug("Calling %s with %s", this._recognizedCommandMap.get(this.defaultCommand), newArgs);
return this[this._recognizedCommandMap.get(this.defaultCommand)](event, ...newArgs); return this[this._recognizedCommandMap.get(this.defaultCommand)](event, ...newArgs);
} catch (e) { } catch (e) {
logger.error("Error while calling default command %s %s", this.defaultCommand, e); logger.error("Error while calling default command %s %s", this.defaultCommand, e);
return this.cmd_help();
return this.cmd_help(event, ...newArgs);
} }
} else { } else {
logger.debug("Unrecognized command %s", command); 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,. return basic help information,.
*/ */
cmd_help(event, ...args) {
cmd_help(event: any, ...args: Array<string>) {
return message.createBasic(this.helpAndUsage); return message.createBasic(this.helpAndUsage);
} }
@ -205,7 +213,7 @@ class AbstractModule {
* *
* @param {...any} args * @param {...any} args
*/ */
cmd_config(event, ...args) {
cmd_config(event: any, ...args: Array<string>) {
if (this._config != null) { if (this._config != null) {
let configBody = JSON.stringify(sanitizeConfig(this._config, this.getConfigSensitiveFields()), null, 2) let configBody = JSON.stringify(sanitizeConfig(this._config, this.getConfigSensitiveFields()), null, 2)
return message.createMarkdown(configBody, "```" + configBody + "```"); return message.createMarkdown(configBody, "```" + configBody + "```");
@ -219,7 +227,7 @@ let abstractModulePrototype = Object.getPrototypeOf(new AbstractModule('', '', '
/* /*
Initialization of a module. Initialization of a module.
*/ */
function init(mod, globalConfig) {
function init(mod: AbstractModule, globalConfig: any) {
logger.debug("Initializing module %s", mod.name); logger.debug("Initializing module %s", mod.name);
if (mod.needConfig) { if (mod.needConfig) {
logger.debug("Loading config file %s", mod.getConfigFilePath()); logger.debug("Loading config file %s", mod.getConfigFilePath());
@ -248,5 +256,5 @@ function init(mod, globalConfig) {
mod.postInit(); mod.postInit();
} }
exports.AbstractModule = AbstractModule
exports.initModule = init;
export { AbstractModule };
export { init as initModule };

4
src/module/index.js → src/module/index.ts

@ -14,4 +14,6 @@ function getModules() {
]; ];
} }
exports.modules = getModules();
let modules = getModules();
export { modules };
Loading…
Cancel
Save