更改handlebars helper加载逻辑

main
落雨楓 3 years ago
parent 23f122ed1c
commit 1c496b41fd

@ -9,6 +9,7 @@ import { ProviderManager } from './ProviderManager';
import { RestfulApiManager } from './RestfulApiManager'; import { RestfulApiManager } from './RestfulApiManager';
import { RobotManager } from './RobotManager'; import { RobotManager } from './RobotManager';
import { Service, ServiceManager } from './ServiceManager'; import { Service, ServiceManager } from './ServiceManager';
import { Setup } from './Setup';
import { SubscribeManager, Target } from './SubscribeManager'; import { SubscribeManager, Target } from './SubscribeManager';
export default class App { export default class App {
@ -28,6 +29,7 @@ export default class App {
} }
async initialize() { async initialize() {
await this.initModules();
await this.initRobot(); await this.initRobot();
await this.initProviderManager(); await this.initProviderManager();
await this.initServiceManager(); await this.initServiceManager();
@ -36,6 +38,10 @@ export default class App {
console.log('初始化完成,正在接收消息'); console.log('初始化完成,正在接收消息');
} }
async initModules() {
await Setup.initHandlebars();
}
async initRobot() { async initRobot() {
this.robot = new RobotManager(this, this.config.robot); this.robot = new RobotManager(this, this.config.robot);
await this.robot.initialize(); await this.robot.initialize();

@ -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);
}
}

@ -3,7 +3,6 @@ import Handlebars from "handlebars";
import App from "../App"; import App from "../App";
import { MultipleMessage } from "../base/provider/BaseProvider"; import { MultipleMessage } from "../base/provider/BaseProvider";
import { ConfigCheckError } from "../error/ConfigCheckError"; import { ConfigCheckError } from "../error/ConfigCheckError";
import { Utils } from "../Utils";
export type TemplateFilterConfig = { [key: string]: string }; export type TemplateFilterConfig = { [key: string]: string };
@ -21,7 +20,6 @@ export class TemplateFilter {
} }
async initialize() { async initialize() {
this.initHandlebars();
for (let key in this.config) { for (let key in this.config) {
let template = this.config[key]; let template = this.config[key];
if (key === "default") { 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() { checkConfig() {
if (!('base' in this.config) && !('default' in this.config)) { if (!('base' in this.config) && !('default' in this.config)) {
throw new ConfigCheckError('Unset template.base or template.default'); throw new ConfigCheckError('Unset template.base or template.default');

Loading…
Cancel
Save