diff --git a/src/App.ts b/src/App.ts index b2bdab2..12f6729 100644 --- a/src/App.ts +++ b/src/App.ts @@ -9,6 +9,7 @@ import { ProviderManager } from './ProviderManager'; import { RestfulApiManager } from './RestfulApiManager'; import { RobotManager } from './RobotManager'; import { Service, ServiceManager } from './ServiceManager'; +import { Setup } from './Setup'; import { SubscribeManager, Target } from './SubscribeManager'; export default class App { @@ -28,6 +29,7 @@ export default class App { } async initialize() { + await this.initModules(); await this.initRobot(); await this.initProviderManager(); await this.initServiceManager(); @@ -36,6 +38,10 @@ export default class App { console.log('初始化完成,正在接收消息'); } + async initModules() { + await Setup.initHandlebars(); + } + async initRobot() { this.robot = new RobotManager(this, this.config.robot); await this.robot.initialize(); diff --git a/src/Setup.ts b/src/Setup.ts new file mode 100644 index 0000000..cc66384 --- /dev/null +++ b/src/Setup.ts @@ -0,0 +1,21 @@ +import Handlebars from "handlebars"; +import { Utils } from "./Utils"; + +export class Setup { + public static initHandlebars() { + + Handlebars.registerHelper('excerpt', (...args) => { + if (args.length > 2) { + let text: any = args[0]; + let maxLength: any = parseInt(args[1]); + let ellipsis: any = undefined; + if (args.length > 3) { + return Utils.excerpt(text, parseInt(maxLength), ellipsis); + } + } + return args[0]; + }); + + Handlebars.registerHelper('currentDate', Utils.getCurrentDate); + } +} \ No newline at end of file diff --git a/src/generator/TemplateFilter.ts b/src/generator/TemplateFilter.ts index 7a7324c..cdb5d55 100644 --- a/src/generator/TemplateFilter.ts +++ b/src/generator/TemplateFilter.ts @@ -3,7 +3,6 @@ import Handlebars from "handlebars"; import App from "../App"; import { MultipleMessage } from "../base/provider/BaseProvider"; import { ConfigCheckError } from "../error/ConfigCheckError"; -import { Utils } from "../Utils"; export type TemplateFilterConfig = { [key: string]: string }; @@ -21,7 +20,6 @@ export class TemplateFilter { } async initialize() { - this.initHandlebars(); for (let key in this.config) { let template = this.config[key]; if (key === "default") { @@ -39,22 +37,6 @@ export class TemplateFilter { } } - initHandlebars() { - Handlebars.registerHelper('excerpt', (...args) => { - if (args.length > 2) { - let text: any = args[0]; - let maxLength: any = parseInt(args[1]); - let ellipsis: any = undefined; - if (args.length > 3) { - return Utils.excerpt(text, parseInt(maxLength), ellipsis); - } - } - return args[0]; - }); - - Handlebars.registerHelper('currentDate', Utils.getCurrentDate); - } - checkConfig() { if (!('base' in this.config) && !('default' in this.config)) { throw new ConfigCheckError('Unset template.base or template.default');