@appweaver/handlebars 0.0.0-stage → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +37 -2
- package/handlebars-helpers.d.ts +9 -0
- package/handlebars-helpers.js +108 -0
- package/handlebars-template-engine.d.ts +10 -0
- package/handlebars-template-engine.js +270 -0
- package/index.d.ts +2 -0
- package/index.js +18 -0
- package/module.d.ts +3 -0
- package/module.js +47 -0
- package/package.json +49 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Luka Matosevic
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,38 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://appweaver.co" target="_blank"><img src="https://raw.githubusercontent.com/lmatosevic/appweaver/refs/heads/main/resources/appweaver-logo.svg" width="460" alt="Appweaver Logo" /></a>
|
|
3
|
+
</p>
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
<p align="center">
|
|
6
|
+
The Handlebars driver of Appweaver, the AI-first <a href="https://nodejs.org" target="_blank">Node.js</a> framework for backends built with agents.
|
|
7
|
+
</p>
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
<a href="https://www.npmjs.com/package/@appweaver/handlebars" target="_blank"><img src="https://img.shields.io/npm/v/@appweaver/handlebars.svg" alt="NPM Version" /></a>
|
|
11
|
+
<a href="https://github.com/lmatosevic/appweaver/blob/main/LICENSE" target="_blank"><img src="https://img.shields.io/npm/l/@appweaver/handlebars.svg" alt="Package License" /></a>
|
|
12
|
+
<a href="https://www.npmjs.com/package/@appweaver/handlebars" target="_blank"><img src="https://img.shields.io/npm/dw/@appweaver/handlebars.svg" alt="NPM Downloads" /></a>
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
## Description
|
|
16
|
+
|
|
17
|
+
`@appweaver/handlebars` is the Handlebars template engine of [Appweaver](https://github.com/lmatosevic/appweaver), a module enabled by
|
|
18
|
+
installing it, which provides the `handlebars` `TemplateEngine` provider. With `auto`, the default of the `template.provider` config key,
|
|
19
|
+
it is selected whenever the module is installed.
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
npm install @appweaver/handlebars
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
or `weaver module add templates` in a project created with `@appweaver/create`.
|
|
28
|
+
|
|
29
|
+
It renders the templates of `template.path`, with the translations of the locale service.
|
|
30
|
+
|
|
31
|
+
## Documentation
|
|
32
|
+
|
|
33
|
+
- [Appweaver README](https://github.com/lmatosevic/appweaver#readme) – getting started, configuration, and the overview of every package
|
|
34
|
+
- [Agent skill](https://github.com/lmatosevic/appweaver/blob/main/skill/SKILL.md) – the complete framework reference shipped to every scaffolded project
|
|
35
|
+
|
|
36
|
+
## License
|
|
37
|
+
|
|
38
|
+
Appweaver is [MIT licensed](LICENSE).
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { HelperDelegate } from 'handlebars';
|
|
2
|
+
import { LocaleService } from '@appweaver/core';
|
|
3
|
+
/**
|
|
4
|
+
* Creates the built-in helpers of the Handlebars template engine.
|
|
5
|
+
*
|
|
6
|
+
* @param {LocaleService} localeService - The locale service translating the `t` helper keys.
|
|
7
|
+
* @return {Record<string, HelperDelegate>} The helpers keyed by name.
|
|
8
|
+
*/
|
|
9
|
+
export declare function createHandlebarsHelpers(localeService: LocaleService): Record<string, HelperDelegate>;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createHandlebarsHelpers = createHandlebarsHelpers;
|
|
7
|
+
const handlebars_1 = __importDefault(require("handlebars"));
|
|
8
|
+
const common_1 = require("@appweaver/common");
|
|
9
|
+
const DATE_STYLES = new Set(['full', 'long', 'medium', 'short']);
|
|
10
|
+
/**
|
|
11
|
+
* Creates the built-in helpers of the Handlebars template engine.
|
|
12
|
+
*
|
|
13
|
+
* @param {LocaleService} localeService - The locale service translating the `t` helper keys.
|
|
14
|
+
* @return {Record<string, HelperDelegate>} The helpers keyed by name.
|
|
15
|
+
*/
|
|
16
|
+
function createHandlebarsHelpers(localeService) {
|
|
17
|
+
return {
|
|
18
|
+
t(key, ...args) {
|
|
19
|
+
const [options, [locale]] = splitArgs(args);
|
|
20
|
+
return localeService.translate(String(key), (0, common_1.isString)(locale) ? locale : options.data?.locale, options.hash);
|
|
21
|
+
},
|
|
22
|
+
eq: (a, b) => a === b,
|
|
23
|
+
ne: (a, b) => a !== b,
|
|
24
|
+
lt: (a, b) => a < b,
|
|
25
|
+
lte: (a, b) => a <= b,
|
|
26
|
+
gt: (a, b) => a > b,
|
|
27
|
+
gte: (a, b) => a >= b,
|
|
28
|
+
and: (...args) => splitArgs(args)[1].every(Boolean),
|
|
29
|
+
or: (...args) => splitArgs(args)[1].some(Boolean),
|
|
30
|
+
not: (value) => !value,
|
|
31
|
+
default: (value, fallback) => value === undefined || value === null || value === '' ? fallback : value,
|
|
32
|
+
includes: (value, item) => (0, common_1.isArray)(value) || (0, common_1.isString)(value) ? value.includes(item) : false,
|
|
33
|
+
length: (value) => (0, common_1.isArray)(value) || (0, common_1.isString)(value) ? value.length : 0,
|
|
34
|
+
add: (a, b) => Number(a) + Number(b),
|
|
35
|
+
subtract: (a, b) => Number(a) - Number(b),
|
|
36
|
+
multiply: (a, b) => Number(a) * Number(b),
|
|
37
|
+
divide: (a, b) => Number(a) / Number(b),
|
|
38
|
+
uppercase: (value) => (0, common_1.toText)(value).toUpperCase(),
|
|
39
|
+
lowercase: (value) => (0, common_1.toText)(value).toLowerCase(),
|
|
40
|
+
trim: (value) => (0, common_1.toText)(value).trim(),
|
|
41
|
+
truncate(value, length, ...args) {
|
|
42
|
+
const [, [suffix]] = splitArgs(args);
|
|
43
|
+
const text = (0, common_1.toText)(value);
|
|
44
|
+
const end = (0, common_1.isString)(suffix) ? suffix : '…';
|
|
45
|
+
return text.length > length ? text.slice(0, length) + end : text;
|
|
46
|
+
},
|
|
47
|
+
join(value, ...args) {
|
|
48
|
+
const [, [separator]] = splitArgs(args);
|
|
49
|
+
return (0, common_1.isArray)(value)
|
|
50
|
+
? value.join((0, common_1.isString)(separator) ? separator : ', ')
|
|
51
|
+
: (0, common_1.toText)(value);
|
|
52
|
+
},
|
|
53
|
+
concat: (...args) => splitArgs(args)[1].map(common_1.toText).join(''),
|
|
54
|
+
json(value, ...args) {
|
|
55
|
+
const [, [indent]] = splitArgs(args);
|
|
56
|
+
return JSON.stringify(value, null, Number(indent) || undefined) ?? '';
|
|
57
|
+
},
|
|
58
|
+
cdata: (value) => new handlebars_1.default.SafeString(`<![CDATA[${(0, common_1.toText)(value).replaceAll(']]>', ']]]]><![CDATA[>')}]]>`),
|
|
59
|
+
formatDate(value, ...args) {
|
|
60
|
+
const [options, [style]] = splitArgs(args);
|
|
61
|
+
const date = toDate(value);
|
|
62
|
+
if (!date) {
|
|
63
|
+
return '';
|
|
64
|
+
}
|
|
65
|
+
if (style === 'iso') {
|
|
66
|
+
return date.toISOString();
|
|
67
|
+
}
|
|
68
|
+
const formatOptions = { ...options.hash };
|
|
69
|
+
if ((0, common_1.isString)(style) && DATE_STYLES.has(style)) {
|
|
70
|
+
formatOptions.dateStyle =
|
|
71
|
+
style;
|
|
72
|
+
}
|
|
73
|
+
return new Intl.DateTimeFormat(options.data?.locale, formatOptions).format(date);
|
|
74
|
+
},
|
|
75
|
+
formatNumber(value, ...args) {
|
|
76
|
+
const [options] = splitArgs(args);
|
|
77
|
+
return formatNumber(value, options.data?.locale, options.hash);
|
|
78
|
+
},
|
|
79
|
+
formatCurrency(value, currency, ...args) {
|
|
80
|
+
const [options] = splitArgs(args);
|
|
81
|
+
return formatNumber(value, options.data?.locale, {
|
|
82
|
+
...options.hash,
|
|
83
|
+
style: 'currency',
|
|
84
|
+
currency
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/** Splits the helper arguments into the Handlebars options, always passed last, and the positional arguments. */
|
|
90
|
+
function splitArgs(args) {
|
|
91
|
+
return [args[args.length - 1], args.slice(0, -1)];
|
|
92
|
+
}
|
|
93
|
+
function toDate(value) {
|
|
94
|
+
if (value === undefined || value === null || value === '') {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
const date = value instanceof Date ? value : new Date(value);
|
|
98
|
+
return Number.isNaN(date.getTime()) ? null : date;
|
|
99
|
+
}
|
|
100
|
+
function formatNumber(value, locale, options) {
|
|
101
|
+
if (value === undefined || value === null || value === '') {
|
|
102
|
+
return '';
|
|
103
|
+
}
|
|
104
|
+
const number = Number(value);
|
|
105
|
+
return Number.isNaN(number)
|
|
106
|
+
? (0, common_1.toText)(value)
|
|
107
|
+
: new Intl.NumberFormat(locale, options).format(number);
|
|
108
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { TemplateData, TemplateEngine, TemplateHelper, TemplateRenderOptions, TemplateStringRenderOptions } from '@appweaver/common';
|
|
2
|
+
export declare class HandlebarsTemplateEngine extends TemplateEngine {
|
|
3
|
+
constructor();
|
|
4
|
+
onInit(): Promise<void>;
|
|
5
|
+
render(name: string, data?: TemplateData, options?: TemplateRenderOptions): Promise<string>;
|
|
6
|
+
renderString(source: string, data?: TemplateData, options?: TemplateStringRenderOptions): Promise<string>;
|
|
7
|
+
hasTemplate(name: string, locale?: string): Promise<boolean>;
|
|
8
|
+
registerTemplate(name: string, source: string, locale?: string): void;
|
|
9
|
+
registerHelper(name: string, helper: TemplateHelper): void;
|
|
10
|
+
}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.HandlebarsTemplateEngine = void 0;
|
|
7
|
+
const promises_1 = __importDefault(require("node:fs/promises"));
|
|
8
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
9
|
+
const handlebars_1 = __importDefault(require("handlebars"));
|
|
10
|
+
const common_1 = require("@appweaver/common");
|
|
11
|
+
const core_1 = require("@appweaver/core");
|
|
12
|
+
const handlebars_helpers_1 = require("./handlebars-helpers");
|
|
13
|
+
const TEMPLATE_EXTENSIONS = ['.hbs', '.handlebars'];
|
|
14
|
+
class HandlebarsTemplateEngine extends common_1.TemplateEngine {
|
|
15
|
+
/** @internal */
|
|
16
|
+
_localeService = (0, core_1.inject)(core_1.LocaleService);
|
|
17
|
+
/** @internal */
|
|
18
|
+
_handlebars = {
|
|
19
|
+
escaped: handlebars_1.default.create(),
|
|
20
|
+
raw: handlebars_1.default.create()
|
|
21
|
+
};
|
|
22
|
+
/** @internal */
|
|
23
|
+
_registered = new Map();
|
|
24
|
+
/** @internal */
|
|
25
|
+
_files = new Map();
|
|
26
|
+
/** @internal */
|
|
27
|
+
_loading;
|
|
28
|
+
/** @internal */
|
|
29
|
+
_partials = new Map();
|
|
30
|
+
/** @internal */
|
|
31
|
+
_variantLocales;
|
|
32
|
+
constructor() {
|
|
33
|
+
super();
|
|
34
|
+
const helpers = (0, handlebars_helpers_1.createHandlebarsHelpers)(this._localeService);
|
|
35
|
+
for (const [name, helper] of Object.entries(helpers)) {
|
|
36
|
+
this.registerHelper(name, helper);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async onInit() {
|
|
40
|
+
await this.load();
|
|
41
|
+
}
|
|
42
|
+
async render(name, data = {}, options = {}) {
|
|
43
|
+
await this.load();
|
|
44
|
+
const locales = this._localeService.resolveLocales(options.locale);
|
|
45
|
+
const entry = this.findTemplate(name, locales);
|
|
46
|
+
if (!entry) {
|
|
47
|
+
throw new common_1.TemplateError(common_1.ErrorCode.TemplateNotFound, `Template '${name}' not found`, { template: name, locale: locales[0] });
|
|
48
|
+
}
|
|
49
|
+
return this.execute(name, entry, data, locales);
|
|
50
|
+
}
|
|
51
|
+
async renderString(source, data = {}, options = {}) {
|
|
52
|
+
await this.load();
|
|
53
|
+
const locales = this._localeService.resolveLocales(options.locale);
|
|
54
|
+
const entry = {
|
|
55
|
+
source,
|
|
56
|
+
format: (options.format ?? 'html').toLowerCase(),
|
|
57
|
+
compiled: {}
|
|
58
|
+
};
|
|
59
|
+
return this.execute('<string>', entry, data, locales);
|
|
60
|
+
}
|
|
61
|
+
async hasTemplate(name, locale) {
|
|
62
|
+
await this.load();
|
|
63
|
+
const locales = this._localeService.resolveLocales(locale);
|
|
64
|
+
return this.findTemplate(name, locales) !== undefined;
|
|
65
|
+
}
|
|
66
|
+
registerTemplate(name, source, locale) {
|
|
67
|
+
if (locale !== undefined && !(0, common_1.isLocaleTag)(locale.replace(/_/g, '-'))) {
|
|
68
|
+
throw new common_1.TemplateError(common_1.ErrorCode.TemplateLoadFailed, `Template '${name}' has an invalid locale '${locale}'`, { path: name });
|
|
69
|
+
}
|
|
70
|
+
parseTemplate(name, source);
|
|
71
|
+
addTemplate(this._registered, name, source, locale);
|
|
72
|
+
this.clearPartials();
|
|
73
|
+
}
|
|
74
|
+
registerHelper(name, helper) {
|
|
75
|
+
this._handlebars.escaped.registerHelper(name, helper);
|
|
76
|
+
this._handlebars.raw.registerHelper(name, helper);
|
|
77
|
+
}
|
|
78
|
+
/** @internal */
|
|
79
|
+
load() {
|
|
80
|
+
if (!this._loading || !core_1.templateConfig.CACHE) {
|
|
81
|
+
// Without the cache, reload the translations too
|
|
82
|
+
if (this._loading) {
|
|
83
|
+
this._localeService.reload();
|
|
84
|
+
}
|
|
85
|
+
this._loading = this.loadFiles().catch((e) => {
|
|
86
|
+
this._loading = undefined;
|
|
87
|
+
throw e;
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
return this._loading;
|
|
91
|
+
}
|
|
92
|
+
/** @internal */
|
|
93
|
+
async loadFiles() {
|
|
94
|
+
const files = new Map();
|
|
95
|
+
// The templates of the application override the ones of the modules
|
|
96
|
+
for (const dir of [
|
|
97
|
+
...(0, core_1.moduleDirectories)('templates'),
|
|
98
|
+
node_path_1.default.resolve(process.cwd(), core_1.templateConfig.PATH)
|
|
99
|
+
]) {
|
|
100
|
+
await readTemplateFiles(dir, files);
|
|
101
|
+
}
|
|
102
|
+
this._files = files;
|
|
103
|
+
this.clearPartials();
|
|
104
|
+
}
|
|
105
|
+
/** @internal */
|
|
106
|
+
findTemplate(name, locales) {
|
|
107
|
+
const layers = [this._registered, this._files];
|
|
108
|
+
for (const locale of [...locales, '']) {
|
|
109
|
+
for (const layer of layers) {
|
|
110
|
+
const entry = layer.get(name)?.get(locale);
|
|
111
|
+
if (entry) {
|
|
112
|
+
return entry;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
/** @internal */
|
|
119
|
+
execute(name, entry, data, locales) {
|
|
120
|
+
const mode = (0, common_1.isEscapedTemplateFormat)(entry.format)
|
|
121
|
+
? 'escaped'
|
|
122
|
+
: 'raw';
|
|
123
|
+
try {
|
|
124
|
+
const template = this.compile(entry, mode);
|
|
125
|
+
return template(data, {
|
|
126
|
+
data: {
|
|
127
|
+
locale: locales[0],
|
|
128
|
+
app: {
|
|
129
|
+
name: common_1.appConfig.NAME,
|
|
130
|
+
description: common_1.appConfig.DESCRIPTION,
|
|
131
|
+
hostname: common_1.appConfig.HOSTNAME,
|
|
132
|
+
version: common_1.appConfig.VERSION
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
partials: this.partials(mode, entry.format, locales)
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
catch (e) {
|
|
139
|
+
throw new common_1.TemplateError(common_1.ErrorCode.TemplateRenderFailed, `Template '${name}' rendering failed`, { template: name }, { cause: e });
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/** @internal */
|
|
143
|
+
compile(entry, mode) {
|
|
144
|
+
entry.compiled[mode] ??= this._handlebars[mode].compile(entry.source, {
|
|
145
|
+
noEscape: mode === 'raw'
|
|
146
|
+
});
|
|
147
|
+
return entry.compiled[mode];
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Every template is a partial included by its name, i.e. `{{> partials/button.html}}`, or by its name without the
|
|
151
|
+
* format, which prefers the format of the including template, i.e. `{{> partials/button}}`. The partials resolve
|
|
152
|
+
* to the variants of the rendered locale.
|
|
153
|
+
*
|
|
154
|
+
* @internal
|
|
155
|
+
*/
|
|
156
|
+
partials(mode, format, locales) {
|
|
157
|
+
// Keyed by the locales having template variants only, so a requested
|
|
158
|
+
// locale without any keeps the cache bounded
|
|
159
|
+
const variantLocales = this.variantLocales();
|
|
160
|
+
const key = [
|
|
161
|
+
mode,
|
|
162
|
+
format,
|
|
163
|
+
...locales.filter((locale) => variantLocales.has(locale))
|
|
164
|
+
].join('|');
|
|
165
|
+
const cached = this._partials.get(key);
|
|
166
|
+
if (cached) {
|
|
167
|
+
return cached;
|
|
168
|
+
}
|
|
169
|
+
const names = new Set([...this._files.keys(), ...this._registered.keys()]);
|
|
170
|
+
const partials = {};
|
|
171
|
+
const aliases = new Map();
|
|
172
|
+
for (const name of names) {
|
|
173
|
+
const entry = this.findTemplate(name, locales);
|
|
174
|
+
if (!entry) {
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
const partial = this.compile(entry, mode);
|
|
178
|
+
partials[name] = partial;
|
|
179
|
+
const nameFormat = (0, common_1.templateFormat)(name);
|
|
180
|
+
if (nameFormat) {
|
|
181
|
+
const alias = name.slice(0, -(nameFormat.length + 1));
|
|
182
|
+
// The format of the including template wins over any other format
|
|
183
|
+
if (!aliases.has(alias) || nameFormat === format) {
|
|
184
|
+
aliases.set(alias, name);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
for (const [alias, name] of aliases) {
|
|
189
|
+
partials[alias] ??= partials[name];
|
|
190
|
+
}
|
|
191
|
+
this._partials.set(key, partials);
|
|
192
|
+
return partials;
|
|
193
|
+
}
|
|
194
|
+
/** @internal */
|
|
195
|
+
variantLocales() {
|
|
196
|
+
if (!this._variantLocales) {
|
|
197
|
+
this._variantLocales = new Set();
|
|
198
|
+
for (const layer of [this._registered, this._files]) {
|
|
199
|
+
for (const variants of layer.values()) {
|
|
200
|
+
for (const locale of variants.keys()) {
|
|
201
|
+
this._variantLocales.add(locale);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return this._variantLocales;
|
|
207
|
+
}
|
|
208
|
+
/** @internal */
|
|
209
|
+
clearPartials() {
|
|
210
|
+
this._partials.clear();
|
|
211
|
+
this._variantLocales = undefined;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
exports.HandlebarsTemplateEngine = HandlebarsTemplateEngine;
|
|
215
|
+
/** Checks the syntax of a template, so an invalid one fails when it is loaded instead of when it is rendered. */
|
|
216
|
+
function parseTemplate(name, source) {
|
|
217
|
+
try {
|
|
218
|
+
handlebars_1.default.parse(source);
|
|
219
|
+
}
|
|
220
|
+
catch (e) {
|
|
221
|
+
throw new common_1.TemplateError(common_1.ErrorCode.TemplateLoadFailed, `Template '${name}' is invalid`, { path: name }, { cause: e });
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
/** Reads the template files of a directory into a layer, replacing the templates of the same name and locale. */
|
|
225
|
+
async function readTemplateFiles(dir, files) {
|
|
226
|
+
let fileNames;
|
|
227
|
+
try {
|
|
228
|
+
fileNames = await promises_1.default.readdir(dir, { recursive: true });
|
|
229
|
+
}
|
|
230
|
+
catch {
|
|
231
|
+
common_1.logger.debug({ dir }, 'Template directory not found');
|
|
232
|
+
fileNames = [];
|
|
233
|
+
}
|
|
234
|
+
for (const fileName of fileNames) {
|
|
235
|
+
const extension = TEMPLATE_EXTENSIONS.find((ext) => fileName.endsWith(ext));
|
|
236
|
+
if (!extension) {
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
const filePath = node_path_1.default.join(dir, fileName);
|
|
240
|
+
let source;
|
|
241
|
+
try {
|
|
242
|
+
// An editor ends a file with a line break, which would otherwise be
|
|
243
|
+
// written after every included partial
|
|
244
|
+
source = (await promises_1.default.readFile(filePath, 'utf8')).replace(/\r?\n$/, '');
|
|
245
|
+
}
|
|
246
|
+
catch (e) {
|
|
247
|
+
throw new common_1.TemplateError(common_1.ErrorCode.TemplateLoadFailed, `Template file '${filePath}' loading failed`, { path: filePath }, { cause: e });
|
|
248
|
+
}
|
|
249
|
+
parseTemplate(filePath, source);
|
|
250
|
+
const parsed = (0, common_1.parseTemplatePath)(fileName.slice(0, -extension.length));
|
|
251
|
+
addTemplate(files, parsed.name, source, parsed.locale);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
/** Adds a template of a name and locale to a layer, replacing the existing one. */
|
|
255
|
+
function addTemplate(layer, name, source, locale) {
|
|
256
|
+
const templateName = name.replace(/\\/g, '/').replace(/^\.?\//, '');
|
|
257
|
+
const tag = locale
|
|
258
|
+
? Intl.getCanonicalLocales(locale.replace(/_/g, '-'))[0]
|
|
259
|
+
: '';
|
|
260
|
+
let variants = layer.get(templateName);
|
|
261
|
+
if (!variants) {
|
|
262
|
+
variants = new Map();
|
|
263
|
+
layer.set(templateName, variants);
|
|
264
|
+
}
|
|
265
|
+
variants.set(tag, {
|
|
266
|
+
source,
|
|
267
|
+
format: (0, common_1.templateFormat)(templateName),
|
|
268
|
+
compiled: {}
|
|
269
|
+
});
|
|
270
|
+
}
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./handlebars-helpers"), exports);
|
|
18
|
+
__exportStar(require("./handlebars-template-engine"), exports);
|
package/module.d.ts
ADDED
package/module.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
const common_1 = require("@appweaver/common");
|
|
37
|
+
/** The Handlebars template engine. */
|
|
38
|
+
exports.default = (0, common_1.defineModule)({
|
|
39
|
+
name: '@appweaver/handlebars',
|
|
40
|
+
providers: [
|
|
41
|
+
{
|
|
42
|
+
contract: common_1.TemplateEngine,
|
|
43
|
+
name: 'handlebars',
|
|
44
|
+
load: () => Promise.resolve().then(() => __importStar(require('./handlebars-template-engine')))
|
|
45
|
+
}
|
|
46
|
+
]
|
|
47
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@appweaver/handlebars",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "2.0.1",
|
|
4
|
+
"description": "Appweaver - the backend framework for AI-first development (@handlebars)",
|
|
5
|
+
"author": "Luka Matosevic",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "./index.js",
|
|
8
|
+
"types": "./index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./index.d.ts",
|
|
12
|
+
"default": "./index.js"
|
|
13
|
+
},
|
|
14
|
+
"./package.json": "./package.json"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"appweaver",
|
|
18
|
+
"appweaver-module"
|
|
19
|
+
],
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/lmatosevic/appweaver.git",
|
|
23
|
+
"directory": "packages/handlebars"
|
|
24
|
+
},
|
|
25
|
+
"maintainers": [
|
|
26
|
+
{
|
|
27
|
+
"name": "Luka Matosevic",
|
|
28
|
+
"email": "lukamatosevic5@gmail.com",
|
|
29
|
+
"web": "https://lukamatosevic.com"
|
|
30
|
+
}
|
|
31
|
+
],
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">= 24.7"
|
|
37
|
+
},
|
|
38
|
+
"appweaver": {
|
|
39
|
+
"module": "./module.js"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"handlebars": "4.7.10"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"@appweaver/common": "^2.0.1",
|
|
46
|
+
"@appweaver/core": "^2.0.1"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@appweaver/common": "^1.12.2",
|
|
50
|
+
"@appweaver/core": "^1.12.2"
|
|
51
|
+
}
|
|
6
52
|
}
|