UNPKG

1.97 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const Path = require("path");
4const Handlebars = require("handlebars");
5const stack_1 = require("./stack");
6class Helper extends stack_1.Resource {
7 getHelpers() {
8 if (!this.helpers) {
9 const file = require(this.fileName);
10 if (typeof file === 'object') {
11 this.helpers = file;
12 }
13 else if (typeof file === 'function') {
14 this.helpers = file();
15 }
16 else {
17 throw new Error('Invalid helper.');
18 }
19 }
20 return this.helpers;
21 }
22}
23exports.Helper = Helper;
24class HelperStack extends stack_1.ResourceStack {
25 constructor() {
26 super(Helper, /((?!\.d).{2}|^.{0,1})\.ts$|\.js$/);
27 this.registeredNames = [];
28 this.addCoreHelpers();
29 }
30 activate() {
31 if (!super.activate()) {
32 return false;
33 }
34 const resources = this.getAllResources();
35 for (let resourceName in resources) {
36 const helpers = resources[resourceName].getHelpers();
37 for (let name in helpers) {
38 if (this.registeredNames.includes(name)) {
39 continue;
40 }
41 this.registeredNames.push(name);
42 Handlebars.registerHelper(name, helpers[name]);
43 }
44 }
45 return true;
46 }
47 deactivate() {
48 if (!super.deactivate()) {
49 return false;
50 }
51 for (let name of this.registeredNames) {
52 Handlebars.unregisterHelper(name);
53 }
54 this.registeredNames = [];
55 return true;
56 }
57 addCoreHelpers() {
58 this.addOrigin('core', Path.join(__dirname, '..', '..', 'helpers'));
59 }
60 removeAllOrigins() {
61 super.removeAllOrigins();
62 this.addCoreHelpers();
63 }
64}
65exports.HelperStack = HelperStack;
66//# sourceMappingURL=helpers.js.map
\No newline at end of file