@breadstone-infrastructure/utilities 0.0.144 → 0.0.145
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/IO/Template.d.ts +23 -4
- package/IO/Template.d.ts.map +1 -1
- package/IO/Template.js +71 -8
- package/IO/Template.js.map +1 -1
- package/package.json +1 -1
package/IO/Template.d.ts
CHANGED
|
@@ -21,21 +21,40 @@ export declare class Template<TContext = TemplateContext> {
|
|
|
21
21
|
/**
|
|
22
22
|
* Constructs a new instance of the `Template` class.
|
|
23
23
|
*
|
|
24
|
-
* @
|
|
24
|
+
* @param context - The context data to apply to the template.
|
|
25
|
+
* @param options - Optional settings for the template rendering.
|
|
25
26
|
*/
|
|
26
27
|
constructor(context: TContext, options?: TemplateOptions);
|
|
27
28
|
/**
|
|
28
|
-
*
|
|
29
|
-
*
|
|
29
|
+
* Creates a new Template instance.
|
|
30
|
+
*
|
|
31
|
+
* @param context - The base context for rendering.
|
|
32
|
+
* @param options - Template rendering options.
|
|
33
|
+
* @returns A new Template instance.
|
|
30
34
|
*/
|
|
31
35
|
static create<TContext = TemplateContext>(context: TContext, options?: TemplateOptions): Template<TContext>;
|
|
32
36
|
/**
|
|
33
|
-
*
|
|
37
|
+
* Applies the given template content with (optional) additional context.
|
|
38
|
+
*
|
|
39
|
+
* @param content - The raw template string.
|
|
40
|
+
* @param context - Additional context that may override or extend the base context.
|
|
41
|
+
* @returns The same instance for chaining.
|
|
42
|
+
* @throws Error with enhanced message if compilation or rendering fails, showing file, line, column.
|
|
34
43
|
*/
|
|
35
44
|
apply(content: string, context?: TContext): this;
|
|
36
45
|
/**
|
|
37
46
|
* @public
|
|
38
47
|
*/
|
|
39
48
|
toString(): string;
|
|
49
|
+
/**
|
|
50
|
+
* Enhances the given error with file, line, column context.
|
|
51
|
+
*
|
|
52
|
+
* @param err - original error object
|
|
53
|
+
* @param src - the template source content
|
|
54
|
+
* @param file - the name/hint of the template file
|
|
55
|
+
* @param compiledSource - optional source code of compiled template
|
|
56
|
+
* @returns A new Error that wraps message + position + snippet.
|
|
57
|
+
*/
|
|
58
|
+
private annotateTemplateError;
|
|
40
59
|
}
|
|
41
60
|
//# sourceMappingURL=Template.d.ts.map
|
package/IO/Template.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Template.d.ts","sourceRoot":"","sources":["../../src/IO/Template.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Template.d.ts","sourceRoot":"","sources":["../../src/IO/Template.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAM7C;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAEtD;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,UAAU,CAAC,OAAO,QAAQ,CAAC,CAAC,GAAG,CAAC,GAAG;IAAE,eAAe,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;AAE/F;;;;GAIG;AACH,qBAAa,QAAQ,CAAC,QAAQ,GAAG,eAAe;IAI5C,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAW;IACpC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAkB;IAC5C,OAAO,CAAC,OAAO,CAAS;IAMxB;;;;;OAKG;gBACgB,OAAO,EAAE,QAAQ,EAAE,OAAO,CAAC,EAAE,eAAe;IAsB/D;;;;;;OAMG;WACW,MAAM,CAAC,QAAQ,GAAG,eAAe,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,CAAC,EAAE,eAAe,GAAG,QAAQ,CAAC,QAAQ,CAAC;IAIlH;;;;;;;OAOG;IACI,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,QAAQ,GAAG,IAAI;IAkCvD;;OAEG;IACI,QAAQ,IAAI,MAAM;IAIzB;;;;;;;;OAQG;IACH,OAAO,CAAC,qBAAqB;CA6BhC"}
|
package/IO/Template.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
/* eslint-disable max-params */
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.Template = void 0;
|
|
5
|
+
// #region Imports
|
|
5
6
|
const compat_1 = require("es-toolkit/compat");
|
|
6
7
|
const String_1 = require("../System/String");
|
|
7
8
|
const es_toolkit_1 = require("es-toolkit");
|
|
@@ -20,7 +21,8 @@ class Template {
|
|
|
20
21
|
/**
|
|
21
22
|
* Constructs a new instance of the `Template` class.
|
|
22
23
|
*
|
|
23
|
-
* @
|
|
24
|
+
* @param context - The context data to apply to the template.
|
|
25
|
+
* @param options - Optional settings for the template rendering.
|
|
24
26
|
*/
|
|
25
27
|
constructor(context, options) {
|
|
26
28
|
this._context = context;
|
|
@@ -28,9 +30,11 @@ class Template {
|
|
|
28
30
|
imports: {
|
|
29
31
|
camelCase: es_toolkit_1.camelCase,
|
|
30
32
|
lowerFirst: es_toolkit_1.lowerFirst,
|
|
33
|
+
escape: es_toolkit_1.escape,
|
|
31
34
|
_: {
|
|
32
35
|
camelCase: es_toolkit_1.camelCase,
|
|
33
|
-
lowerFirst: es_toolkit_1.lowerFirst
|
|
36
|
+
lowerFirst: es_toolkit_1.lowerFirst,
|
|
37
|
+
escape: es_toolkit_1.escape
|
|
34
38
|
}
|
|
35
39
|
},
|
|
36
40
|
...options
|
|
@@ -40,14 +44,22 @@ class Template {
|
|
|
40
44
|
// #endregion
|
|
41
45
|
// #region Methods
|
|
42
46
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
47
|
+
* Creates a new Template instance.
|
|
48
|
+
*
|
|
49
|
+
* @param context - The base context for rendering.
|
|
50
|
+
* @param options - Template rendering options.
|
|
51
|
+
* @returns A new Template instance.
|
|
45
52
|
*/
|
|
46
53
|
static create(context, options) {
|
|
47
54
|
return new Template(context, options);
|
|
48
55
|
}
|
|
49
56
|
/**
|
|
50
|
-
*
|
|
57
|
+
* Applies the given template content with (optional) additional context.
|
|
58
|
+
*
|
|
59
|
+
* @param content - The raw template string.
|
|
60
|
+
* @param context - Additional context that may override or extend the base context.
|
|
61
|
+
* @returns The same instance for chaining.
|
|
62
|
+
* @throws Error with enhanced message if compilation or rendering fails, showing file, line, column.
|
|
51
63
|
*/
|
|
52
64
|
apply(content, context) {
|
|
53
65
|
const ctx = {
|
|
@@ -57,8 +69,27 @@ class Template {
|
|
|
57
69
|
if (!this._options?.unescapeContent) {
|
|
58
70
|
content = String_1.StringExtensions.unescape(content);
|
|
59
71
|
}
|
|
60
|
-
|
|
61
|
-
|
|
72
|
+
// Determine a filename hint for debugging, could come from options or context
|
|
73
|
+
const fileHint = this._options?.sourceURL ?? ctx?.__templateName ?? 'inline-template.ejs';
|
|
74
|
+
let compiled = null;
|
|
75
|
+
try {
|
|
76
|
+
compiled = (0, compat_1.template)(content, {
|
|
77
|
+
...this._options,
|
|
78
|
+
sourceURL: fileHint
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
catch (compileError) {
|
|
82
|
+
// Error during compiling the template (syntax etc.)
|
|
83
|
+
throw this.annotateTemplateError(compileError, content, fileHint);
|
|
84
|
+
}
|
|
85
|
+
try {
|
|
86
|
+
this._result = compiled(ctx);
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
catch (renderError) {
|
|
90
|
+
// Error during rendering (runtime)
|
|
91
|
+
throw this.annotateTemplateError(renderError, content, fileHint, compiled.source);
|
|
92
|
+
}
|
|
62
93
|
}
|
|
63
94
|
/**
|
|
64
95
|
* @public
|
|
@@ -66,6 +97,38 @@ class Template {
|
|
|
66
97
|
toString() {
|
|
67
98
|
return this._result;
|
|
68
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* Enhances the given error with file, line, column context.
|
|
102
|
+
*
|
|
103
|
+
* @param err - original error object
|
|
104
|
+
* @param src - the template source content
|
|
105
|
+
* @param file - the name/hint of the template file
|
|
106
|
+
* @param compiledSource - optional source code of compiled template
|
|
107
|
+
* @returns A new Error that wraps message + position + snippet.
|
|
108
|
+
*/
|
|
109
|
+
annotateTemplateError(err, src, file, compiledSource) {
|
|
110
|
+
const stack = String(err?.stack ?? '');
|
|
111
|
+
const escapedFile = file.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
112
|
+
const regex = new RegExp(`${escapedFile}:(\\d+):(\\d+)`);
|
|
113
|
+
const match = stack.match(regex);
|
|
114
|
+
let col = 0;
|
|
115
|
+
let line = 0;
|
|
116
|
+
if (match) {
|
|
117
|
+
line = Number(match[1]);
|
|
118
|
+
col = Number(match[2]);
|
|
119
|
+
}
|
|
120
|
+
const srcLines = src.split(/\r?\n/);
|
|
121
|
+
const lineNum = line && line <= srcLines.length ? line : 1;
|
|
122
|
+
const snippet = srcLines[lineNum - 1] || '';
|
|
123
|
+
const caret = `${' '.repeat(Math.max((col || 1) - 1, 0))}^`;
|
|
124
|
+
const message = `Template error in ${file}:${lineNum}:${col || '?'}\n` +
|
|
125
|
+
`${err?.message ?? err}\n\n` +
|
|
126
|
+
`${snippet}\n${caret}${compiledSource ? `\n\n[compiled source preview]\n${compiledSource.slice(0, 400)}…` : ''}`;
|
|
127
|
+
const newErr = new Error(message);
|
|
128
|
+
newErr.original = err;
|
|
129
|
+
newErr.stack = `${message}\n${stack}`;
|
|
130
|
+
return newErr;
|
|
131
|
+
}
|
|
69
132
|
}
|
|
70
133
|
exports.Template = Template;
|
|
71
134
|
//# sourceMappingURL=Template.js.map
|
package/IO/Template.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Template.js","sourceRoot":"","sources":["../../src/IO/Template.ts"],"names":[],"mappings":";AAAA,kBAAkB
|
|
1
|
+
{"version":3,"file":"Template.js","sourceRoot":"","sources":["../../src/IO/Template.ts"],"names":[],"mappings":";AAAA,+BAA+B;;;AAE/B,kBAAkB;AAElB,8CAA6C;AAC7C,6CAAoD;AACpD,2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AAG,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QACpC,MAAM,OAAO,GAAG,IAAI,IAAI,IAAI,IAAI,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;QAC3D,MAAM,OAAO,GAAG,QAAQ,CAAC,OAAO,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;QAC5C,MAAM,KAAK,GAAG,GAAG,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC;QAE5D,MAAM,OAAO,GACT,qBAAqB,IAAI,IAAI,OAAO,IAAI,GAAG,IAAI,GAAG,IAAI;YACtD,GAAG,GAAG,EAAE,OAAO,IAAI,GAAG,MAAM;YAC5B,GAAG,OAAO,KAAK,KAAK,GAAG,cAAc,CAAC,CAAC,CAAC,kCAAkC,cAAc,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC;QAErH,MAAM,MAAM,GAAG,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;QACjC,MAAc,CAAC,QAAQ,GAAG,GAAG,CAAC;QAC/B,MAAM,CAAC,KAAK,GAAG,GAAG,OAAO,KAAK,KAAK,EAAE,CAAC;QACtC,OAAO,MAAM,CAAC;IAClB,CAAC;CAIJ;AA1ID,4BA0IC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@breadstone-infrastructure/utilities",
|
|
3
3
|
"description": "Common utility classes and functions",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.145",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "andre.wehlert <awehlert@breadstone.de> (https://www.breadstone.de)",
|
|
7
7
|
"repository": {
|