@spinajs/templates 2.0.480 → 2.0.482
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/lib/cjs/CompiledTemplateRenderer.d.ts +41 -0
- package/lib/cjs/CompiledTemplateRenderer.d.ts.map +1 -0
- package/lib/cjs/CompiledTemplateRenderer.js +83 -0
- package/lib/cjs/CompiledTemplateRenderer.js.map +1 -0
- package/lib/cjs/cli/renderHelpers.d.ts +26 -0
- package/lib/cjs/cli/renderHelpers.d.ts.map +1 -0
- package/lib/cjs/cli/renderHelpers.js +101 -0
- package/lib/cjs/cli/renderHelpers.js.map +1 -0
- package/lib/cjs/config/templates.d.ts +3 -1
- package/lib/cjs/config/templates.d.ts.map +1 -1
- package/lib/cjs/config/templates.js +15 -3
- package/lib/cjs/config/templates.js.map +1 -1
- package/lib/cjs/index.d.ts +21 -4
- package/lib/cjs/index.d.ts.map +1 -1
- package/lib/cjs/index.js +35 -20
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/interfaces.d.ts +52 -9
- package/lib/cjs/interfaces.d.ts.map +1 -1
- package/lib/cjs/interfaces.js +101 -5
- package/lib/cjs/interfaces.js.map +1 -1
- package/lib/cjs/io.d.ts +3 -0
- package/lib/cjs/io.d.ts.map +1 -0
- package/lib/cjs/io.js +46 -0
- package/lib/cjs/io.js.map +1 -0
- package/lib/cjs/progress.d.ts +57 -0
- package/lib/cjs/progress.d.ts.map +1 -0
- package/lib/cjs/progress.js +23 -0
- package/lib/cjs/progress.js.map +1 -0
- package/lib/mjs/CompiledTemplateRenderer.d.ts +41 -0
- package/lib/mjs/CompiledTemplateRenderer.d.ts.map +1 -0
- package/lib/mjs/CompiledTemplateRenderer.js +46 -0
- package/lib/mjs/CompiledTemplateRenderer.js.map +1 -0
- package/lib/mjs/cli/renderHelpers.d.ts +26 -0
- package/lib/mjs/cli/renderHelpers.d.ts.map +1 -0
- package/lib/mjs/cli/renderHelpers.js +62 -0
- package/lib/mjs/cli/renderHelpers.js.map +1 -0
- package/lib/mjs/config/templates.d.ts +3 -1
- package/lib/mjs/config/templates.d.ts.map +1 -1
- package/lib/mjs/config/templates.js +15 -3
- package/lib/mjs/config/templates.js.map +1 -1
- package/lib/mjs/index.d.ts +21 -4
- package/lib/mjs/index.d.ts.map +1 -1
- package/lib/mjs/index.js +36 -21
- package/lib/mjs/index.js.map +1 -1
- package/lib/mjs/interfaces.d.ts +52 -9
- package/lib/mjs/interfaces.d.ts.map +1 -1
- package/lib/mjs/interfaces.js +101 -5
- package/lib/mjs/interfaces.js.map +1 -1
- package/lib/mjs/io.d.ts +3 -0
- package/lib/mjs/io.d.ts.map +1 -0
- package/lib/mjs/io.js +10 -0
- package/lib/mjs/io.js.map +1 -0
- package/lib/mjs/progress.d.ts +57 -0
- package/lib/mjs/progress.d.ts.map +1 -0
- package/lib/mjs/progress.js +20 -0
- package/lib/mjs/progress.js.map +1 -0
- package/lib/tsconfig.cjs.tsbuildinfo +1 -1
- package/lib/tsconfig.mjs.tsbuildinfo +1 -1
- package/package.json +16 -16
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { TemplateRenderer } from './interfaces.js';
|
|
2
|
+
/**
|
|
3
|
+
* A compiled template delegate: takes a locals/context object and returns the
|
|
4
|
+
* rendered string. Both pug's `compileTemplate` and handlebars' template
|
|
5
|
+
* delegate satisfy this shape.
|
|
6
|
+
*/
|
|
7
|
+
export type CompiledTemplate = (locals: any) => string;
|
|
8
|
+
/**
|
|
9
|
+
* Base class for template engines that compile a template file into a reusable
|
|
10
|
+
* delegate (pug, handlebars). It captures the render skeleton - validation,
|
|
11
|
+
* lazy compile + caching, language resolution, timing/logging and writing to
|
|
12
|
+
* file - leaving engines to supply only what actually differs:
|
|
13
|
+
*
|
|
14
|
+
* - {@link buildContext} - the locals object passed to the compiled delegate
|
|
15
|
+
* (model merged with engine-specific helpers).
|
|
16
|
+
* - {@link compile} - how a template source is compiled into a delegate.
|
|
17
|
+
*
|
|
18
|
+
* Caching (including dev-mode recompilation) is owned by the base
|
|
19
|
+
* {@link TemplateRenderer.withCache} machinery, not this class.
|
|
20
|
+
*/
|
|
21
|
+
export declare abstract class CompiledTemplateRenderer<TDelegate extends CompiledTemplate = CompiledTemplate> extends TemplateRenderer {
|
|
22
|
+
renderToFile(template: string, model: unknown, filePath: string, language?: string): Promise<void>;
|
|
23
|
+
render(templateName: string, model: unknown, language?: string): Promise<string>;
|
|
24
|
+
/**
|
|
25
|
+
* Effective language for a render: explicit argument, else the ambient
|
|
26
|
+
* (async-context) language, else the framework default.
|
|
27
|
+
*/
|
|
28
|
+
protected resolveLanguage(language?: string): string;
|
|
29
|
+
/**
|
|
30
|
+
* Build the locals object passed to the compiled template - the model merged
|
|
31
|
+
* with any engine-specific helpers (translation functions, `lang`, ...).
|
|
32
|
+
*/
|
|
33
|
+
protected abstract buildContext(model: unknown, language: string): Record<string, unknown>;
|
|
34
|
+
/**
|
|
35
|
+
* Compile the template identified by `template` (a bare path or `fs://` URI)
|
|
36
|
+
* into a reusable delegate. Called only on a cache miss; the returned delegate
|
|
37
|
+
* is cached by {@link TemplateRenderer.withCache}.
|
|
38
|
+
*/
|
|
39
|
+
protected abstract compile(template: string): Promise<TDelegate>;
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=CompiledTemplateRenderer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CompiledTemplateRenderer.d.ts","sourceRoot":"","sources":["../../src/CompiledTemplateRenderer.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAGnD;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,GAAG,CAAC,MAAM,EAAE,GAAG,KAAK,MAAM,CAAC;AAEvD;;;;;;;;;;;;GAYG;AACH,8BAAsB,wBAAwB,CAAC,SAAS,SAAS,gBAAgB,GAAG,gBAAgB,CAAE,SAAQ,gBAAgB;IAC/G,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAMlG,MAAM,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAkB7F;;;OAGG;IACH,SAAS,CAAC,eAAe,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM;IAIpD;;;OAGG;IACH,SAAS,CAAC,QAAQ,CAAC,YAAY,CAAC,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAE1F;;;;OAIG;IACH,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC;CACjE"}
|
|
@@ -0,0 +1,83 @@
|
|
|
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
|
+
exports.CompiledTemplateRenderer = void 0;
|
|
37
|
+
const exceptions_1 = require("@spinajs/exceptions");
|
|
38
|
+
const intl_1 = require("@spinajs/intl");
|
|
39
|
+
const fs = __importStar(require("fs"));
|
|
40
|
+
const interfaces_js_1 = require("./interfaces.js");
|
|
41
|
+
const io_js_1 = require("./io.js");
|
|
42
|
+
/**
|
|
43
|
+
* Base class for template engines that compile a template file into a reusable
|
|
44
|
+
* delegate (pug, handlebars). It captures the render skeleton - validation,
|
|
45
|
+
* lazy compile + caching, language resolution, timing/logging and writing to
|
|
46
|
+
* file - leaving engines to supply only what actually differs:
|
|
47
|
+
*
|
|
48
|
+
* - {@link buildContext} - the locals object passed to the compiled delegate
|
|
49
|
+
* (model merged with engine-specific helpers).
|
|
50
|
+
* - {@link compile} - how a template source is compiled into a delegate.
|
|
51
|
+
*
|
|
52
|
+
* Caching (including dev-mode recompilation) is owned by the base
|
|
53
|
+
* {@link TemplateRenderer.withCache} machinery, not this class.
|
|
54
|
+
*/
|
|
55
|
+
class CompiledTemplateRenderer extends interfaces_js_1.TemplateRenderer {
|
|
56
|
+
async renderToFile(template, model, filePath, language) {
|
|
57
|
+
const content = await this.render(template, model, language);
|
|
58
|
+
(0, io_js_1.ensureParentDir)(filePath);
|
|
59
|
+
fs.writeFileSync(filePath, content);
|
|
60
|
+
}
|
|
61
|
+
async render(templateName, model, language) {
|
|
62
|
+
if (!templateName) {
|
|
63
|
+
throw new exceptions_1.InvalidArgument('template parameter cannot be null or empty');
|
|
64
|
+
}
|
|
65
|
+
const label = `${this.constructor.name}.render.${templateName}`;
|
|
66
|
+
this.Log.trace(`Rendering template ${templateName}`);
|
|
67
|
+
this.Log.timeStart(label);
|
|
68
|
+
const delegate = await this.withCache(templateName, () => this.compile(templateName));
|
|
69
|
+
const content = delegate(this.buildContext(model, this.resolveLanguage(language)));
|
|
70
|
+
const time = this.Log.timeEnd(label);
|
|
71
|
+
this.Log.trace(`Rendering template ${templateName} ended, (${time} ms)`);
|
|
72
|
+
return content;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Effective language for a render: explicit argument, else the ambient
|
|
76
|
+
* (async-context) language, else the framework default.
|
|
77
|
+
*/
|
|
78
|
+
resolveLanguage(language) {
|
|
79
|
+
return (language ? language : (0, intl_1.guessLanguage)()) ?? (0, intl_1.defaultLanguage)();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
exports.CompiledTemplateRenderer = CompiledTemplateRenderer;
|
|
83
|
+
//# sourceMappingURL=CompiledTemplateRenderer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CompiledTemplateRenderer.js","sourceRoot":"","sources":["../../src/CompiledTemplateRenderer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oDAAsD;AACtD,wCAA+D;AAC/D,uCAAyB;AACzB,mDAAmD;AACnD,mCAA0C;AAS1C;;;;;;;;;;;;GAYG;AACH,MAAsB,wBAAgF,SAAQ,gCAAgB;IACrH,KAAK,CAAC,YAAY,CAAC,QAAgB,EAAE,KAAc,EAAE,QAAgB,EAAE,QAAiB;Q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tE,CAAC;CAcF;AA7CD,4DA6CC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { IRenderProgress, RenderProgressCallback } from '../progress.js';
|
|
2
|
+
/** Load an optional JSON model file, returning an empty object when no readable file is given. */
|
|
3
|
+
export declare function loadJsonModel(modelPath?: string): Record<string, unknown>;
|
|
4
|
+
export interface IResolveHtmlOptions {
|
|
5
|
+
template?: boolean;
|
|
6
|
+
model?: string;
|
|
7
|
+
lang?: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Resolve the HTML for a CLI render. In template mode the input is rendered
|
|
11
|
+
* through the text engine selected by its file extension (pug/handlebars/...);
|
|
12
|
+
* in raw mode the input file is read as-is.
|
|
13
|
+
*/
|
|
14
|
+
export declare function resolveInputHtml(input: string, options: IResolveHtmlOptions): Promise<string>;
|
|
15
|
+
/** Format one progress snapshot as a compact single line. */
|
|
16
|
+
export declare function formatProgressLine(p: IRenderProgress): string;
|
|
17
|
+
/**
|
|
18
|
+
* A progress callback for one-shot CLI commands. On a TTY it rewrites a single
|
|
19
|
+
* live line; otherwise (piped / CI) it prints one line per phase change. The
|
|
20
|
+
* live line is terminated with a newline on Done/Failed.
|
|
21
|
+
*
|
|
22
|
+
* @param write - sink for output (defaults to stdout); injectable for testing.
|
|
23
|
+
* @param isTty - whether to use the live single-line mode (defaults to stdout's TTY state).
|
|
24
|
+
*/
|
|
25
|
+
export declare function cliProgressReporter(write?: (s: string) => void, isTty?: boolean): RenderProgressCallback;
|
|
26
|
+
//# sourceMappingURL=renderHelpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"renderHelpers.d.ts","sourceRoot":"","sources":["../../../src/cli/renderHelpers.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,eAAe,EAAe,sBAAsB,EAAE,MAAM,gBAAgB,CAAC;AAEtF,kGAAkG;AAClG,wBAAgB,aAAa,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAMzE;AAED,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED;;;;GAIG;AACH,wBAAsB,gBAAgB,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAAC,MAAM,CAAC,CAWnG;AASD,6DAA6D;AAC7D,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,eAAe,GAAG,MAAM,CAG7D;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,GAAE,CAAC,CAAC,EAAE,MAAM,KAAK,IAA0C,EAChE,KAAK,GAAE,OAAgC,GACtC,sBAAsB,CAiBxB"}
|
|
@@ -0,0 +1,101 @@
|
|
|
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
|
+
exports.loadJsonModel = loadJsonModel;
|
|
37
|
+
exports.resolveInputHtml = resolveInputHtml;
|
|
38
|
+
exports.formatProgressLine = formatProgressLine;
|
|
39
|
+
exports.cliProgressReporter = cliProgressReporter;
|
|
40
|
+
const di_1 = require("@spinajs/di");
|
|
41
|
+
const fs = __importStar(require("fs"));
|
|
42
|
+
const index_js_1 = require("../index.js");
|
|
43
|
+
const progress_js_1 = require("../progress.js");
|
|
44
|
+
/** Load an optional JSON model file, returning an empty object when no readable file is given. */
|
|
45
|
+
function loadJsonModel(modelPath) {
|
|
46
|
+
if (modelPath && fs.existsSync(modelPath)) {
|
|
47
|
+
return JSON.parse(fs.readFileSync(modelPath, { encoding: 'utf-8' }));
|
|
48
|
+
}
|
|
49
|
+
return {};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Resolve the HTML for a CLI render. In template mode the input is rendered
|
|
53
|
+
* through the text engine selected by its file extension (pug/handlebars/...);
|
|
54
|
+
* in raw mode the input file is read as-is.
|
|
55
|
+
*/
|
|
56
|
+
async function resolveInputHtml(input, options) {
|
|
57
|
+
if (options.template) {
|
|
58
|
+
const templates = await di_1.DI.resolve(index_js_1.Templates);
|
|
59
|
+
return templates.render(input, loadJsonModel(options.model), options.lang);
|
|
60
|
+
}
|
|
61
|
+
if (!fs.existsSync(input)) {
|
|
62
|
+
throw new Error(`Input HTML file ${input} does not exist`);
|
|
63
|
+
}
|
|
64
|
+
return fs.readFileSync(input, { encoding: 'utf-8' });
|
|
65
|
+
}
|
|
66
|
+
const PROGRESS_BAR_WIDTH = 20;
|
|
67
|
+
function progressBar(percent) {
|
|
68
|
+
const filled = Math.max(0, Math.min(PROGRESS_BAR_WIDTH, Math.round((percent / 100) * PROGRESS_BAR_WIDTH)));
|
|
69
|
+
return '█'.repeat(filled) + '·'.repeat(PROGRESS_BAR_WIDTH - filled);
|
|
70
|
+
}
|
|
71
|
+
/** Format one progress snapshot as a compact single line. */
|
|
72
|
+
function formatProgressLine(p) {
|
|
73
|
+
const secs = (p.elapsedMs / 1000).toFixed(1);
|
|
74
|
+
return `[${progressBar(p.percent)}] ${String(p.percent).padStart(3)}% ${p.phase.padEnd(9)} ${p.message ?? ''} ${secs}s`;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* A progress callback for one-shot CLI commands. On a TTY it rewrites a single
|
|
78
|
+
* live line; otherwise (piped / CI) it prints one line per phase change. The
|
|
79
|
+
* live line is terminated with a newline on Done/Failed.
|
|
80
|
+
*
|
|
81
|
+
* @param write - sink for output (defaults to stdout); injectable for testing.
|
|
82
|
+
* @param isTty - whether to use the live single-line mode (defaults to stdout's TTY state).
|
|
83
|
+
*/
|
|
84
|
+
function cliProgressReporter(write = (s) => void process.stdout.write(s), isTty = !!process.stdout.isTTY) {
|
|
85
|
+
let lastPhase;
|
|
86
|
+
return (p) => {
|
|
87
|
+
const line = formatProgressLine(p);
|
|
88
|
+
const finished = p.phase === progress_js_1.RenderPhase.Done || p.phase === progress_js_1.RenderPhase.Failed;
|
|
89
|
+
if (isTty) {
|
|
90
|
+
write(`\r${line.padEnd(80)}`);
|
|
91
|
+
if (finished) {
|
|
92
|
+
write('\n');
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
else if (p.phase !== lastPhase) {
|
|
96
|
+
lastPhase = p.phase;
|
|
97
|
+
write(`${line}\n`);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
//# sourceMappingURL=renderHelpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"renderHelpers.js","sourceRoot":"","sources":["../../../src/cli/renderHelpers.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA,sCAMC;AAaD,4CAWC;AAUD,gDAGC;AAUD,kDAoBC;AA/ED,oCAAiC;AACjC,uCAAyB;AACzB,0CAAwC;AACxC,gDAAsF;AAEtF,kGAAkG;AAClG,SAAgB,aAAa,CAAC,SAAkB;IAC9C,IAAI,SAAS,IAAI,EAAE,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC;QAC1C,OAAO,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,YAAY,CAAC,SAAS,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC;IACvE,CAAC;IAED,OAAO,EAAE,CAAC;AACZ,CAAC;AAQD;;;;GAIG;AACI,KAAK,UAAU,gBAAgB,CAAC,KAAa,EAAE,OAA4B;IAChF,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC;QACrB,MAAM,SAAS,GAAG,MAAM,OAAE,CAAC,OAAO,CAAC,oBAAS,CAAC,CAAC;QAC9C,OAAO,SAAS,CAAC,MAAM,CAAC,KAAK,EAAE,aAAa,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7E,CAAC;IAED,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;QAC1B,MAAM,IAAI,KAAK,CAAC,mBAAmB,KAAK,iBAAiB,CAAC,CAAC;IAC7D,CAAC;IAED,OAAO,EAAE,CAAC,YAAY,CAAC,KAAK,EAAE,EAAE,QAAQ,EAAE,OAA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AAK,yBAAW,CAAC,MAAM,CAAC;QAEhF,IAAI,KAAK,EAAE,CAAC;YACV,KAAK,CAAC,KAAK,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;YAC9B,IAAI,QAAQ,EAAE,CAAC;gBACb,KAAK,CAAC,IAAI,CAAC,CAAC;YACd,CAAC;QACH,CAAC;aAAM,IAAI,CAAC,CAAC,KAAK,KAAK,SAAS,EAAE,CAAC;YACjC,SAAS,GAAG,CAAC,CAAC,KAAK,CAAC;YACpB,KAAK,CAAC,GAAG,IAAI,IAAI,CAAC,CAAC;QACrB,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"templates.d.ts","sourceRoot":"","sources":["../../../src/config/templates.ts"],"names":[],"mappings":"AAYA,QAAA,MAAM,SAAS
|
|
1
|
+
{"version":3,"file":"templates.d.ts","sourceRoot":"","sources":["../../../src/config/templates.ts"],"names":[],"mappings":"AAYA,QAAA,MAAM,SAAS;;;;;;;;;CAmBd,CAAC;AAEF,eAAe,SAAS,CAAC"}
|
|
@@ -4,18 +4,30 @@ const path_1 = require("path");
|
|
|
4
4
|
function dir(path) {
|
|
5
5
|
const inCommonJs = typeof module !== 'undefined';
|
|
6
6
|
return [
|
|
7
|
-
(0, path_1.resolve)((0, path_1.normalize)((0, path_1.join)(process.env.WORKSPACE_ROOT_PATH ?? process.cwd(), 'node_modules', '@spinajs', '
|
|
7
|
+
(0, path_1.resolve)((0, path_1.normalize)((0, path_1.join)(process.env.WORKSPACE_ROOT_PATH ?? process.cwd(), 'node_modules', '@spinajs', 'templates', 'lib', inCommonJs ? 'cjs' : 'mjs', path))),
|
|
8
8
|
// one up if we run from app or build folder
|
|
9
|
-
(0, path_1.resolve)((0, path_1.normalize)((0, path_1.join)(process.env.WORKSPACE_ROOT_PATH ?? process.cwd(), '../', 'node_modules', '@spinajs', '
|
|
9
|
+
(0, path_1.resolve)((0, path_1.normalize)((0, path_1.join)(process.env.WORKSPACE_ROOT_PATH ?? process.cwd(), '../', 'node_modules', '@spinajs', 'templates', 'lib', inCommonJs ? 'cjs' : 'mjs', path))),
|
|
10
10
|
];
|
|
11
11
|
}
|
|
12
12
|
const templates = {
|
|
13
13
|
system: {
|
|
14
14
|
dirs: {
|
|
15
|
-
templates: [...dir('templates')],
|
|
16
15
|
cli: [...dir('cli')],
|
|
17
16
|
},
|
|
18
17
|
},
|
|
18
|
+
templates: {
|
|
19
|
+
cache: {
|
|
20
|
+
// How aggressively compiled templates are reused. Accepts:
|
|
21
|
+
// 'cache' - compile once, reuse forever.
|
|
22
|
+
// 'revalidate' - stat() the source and recompile only when it changes.
|
|
23
|
+
// 'always' - recompile on every render.
|
|
24
|
+
// Intentionally left unset: when no mode is configured the renderer falls
|
|
25
|
+
// back to 'always' in development and 'cache' in production (see
|
|
26
|
+
// TemplateRenderer.CacheMode). Shipping a concrete value here would defeat
|
|
27
|
+
// that dev/prod fallback.
|
|
28
|
+
// mode: 'cache',
|
|
29
|
+
},
|
|
30
|
+
},
|
|
19
31
|
};
|
|
20
32
|
exports.default = templates;
|
|
21
33
|
//# sourceMappingURL=templates.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"templates.js","sourceRoot":"","sources":["../../../src/config/templates.ts"],"names":[],"mappings":";;AAAA,+BAAgD;AAEhD,SAAS,GAAG,CAAC,IAAY;IACvB,MAAM,UAAU,GAAG,OAAO,MAAM,KAAK,WAAW,CAAC;IACjD,OAAO;QACL,IAAA,cAAO,EAAC,IAAA,gBAAS,EAAC,IAAA,WAAI,EAAC,OAAO,CAAC,GAAG,CAAC,mBAAmB,IAAI,OAAO,CAAC,GAAG,EAAE,EAAE,cAAc,EAAE,UAAU,EAAE,
|
|
1
|
+
{"version":3,"file":"templates.js","sourceRoot":"","sources":["../../../src/config/templates.ts"],"names":[],"mappings":";;AAAA,+BAAgD;AAEhD,SAAS,GAAG,CAAC,IAAY;IACvB,MAAM,UAAU,GAAG,OAAO,MAAM,KAAK,WAAW,CAAC;IACjD,OAAO;QACL,IAAA,cAAO,EAAC,IAAA,gBAAS,EAAC,IAAA,WAAI,EAAC,OAAO,CAAC,GAAG,CAAC,mBAAmB,IAAI,OAAO,CAAC,GAAG,EAAE,EAAE,cAAc,EAAE,UAAU,EAAE,WAAW,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC;QAE5J,4CAA4C;QAC5C,IAAA,cAAO,EAAC,IAAA,gBAAS,EAAC,IAAA,WAAI,EAAC,OAAO,CAAC,GAAG,CAAC,mBAAmB,IAAI,OAAO,CAAC,GAAG,EAAE,EAAE,KAAK,EAAE,cAAc,EAAE,UAAU,EAAE,WAAW,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC;KACpK,CAAC;AACJ,CAAC;AAED,MAAM,SAAS,GAAG;IAChB,MAAM,EAAE;QACN,IAAI,EAAE;YACJ,GAAG,EAAE,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC;SACrB;KACF;IACD,SAAS,EAAE;QACT,KAAK,EAAE;QACL,2DAA2D;QAC3D,gDAAgD;QAChD,yEAAyE;QACzE,8CAA8C;QAC9C,0EAA0E;QAC1E,iEAAiE;QACjE,2EAA2E;QAC3E,0BAA0B;QAC1B,iBAAiB;SAClB;KACF;CACF,CAAC;AAEF,kBAAe,SAAS,CAAC"}
|
package/lib/cjs/index.d.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import { AsyncService } from '@spinajs/di';
|
|
2
2
|
import { Log } from '@spinajs/log';
|
|
3
3
|
import { TemplateRenderer } from './interfaces.js';
|
|
4
|
+
import { IRenderOptions } from './progress.js';
|
|
4
5
|
export * from './interfaces.js';
|
|
6
|
+
export * from './progress.js';
|
|
7
|
+
export * from './io.js';
|
|
8
|
+
export * from './CompiledTemplateRenderer.js';
|
|
9
|
+
export * from './cli/renderHelpers.js';
|
|
5
10
|
/**
|
|
6
11
|
* Inject INTL module for language support. We does nothing but to initialize module for use in templates.
|
|
7
12
|
*/
|
|
@@ -9,7 +14,7 @@ export declare class Templates extends AsyncService {
|
|
|
9
14
|
protected Log: Log;
|
|
10
15
|
protected Renderers: TemplateRenderer[];
|
|
11
16
|
getRendererFor(extname: string): TemplateRenderer;
|
|
12
|
-
compileToFile(template: string, renderer: string, model: unknown, filePath: string, language?: string): Promise<void>;
|
|
17
|
+
compileToFile(template: string, renderer: string, model: unknown, filePath: string, language?: string, options?: IRenderOptions): Promise<void>;
|
|
13
18
|
/**
|
|
14
19
|
*
|
|
15
20
|
* Renders template with given model and language. Returns compiled content
|
|
@@ -18,9 +23,10 @@ export declare class Templates extends AsyncService {
|
|
|
18
23
|
* @param renderer
|
|
19
24
|
* @param model
|
|
20
25
|
* @param language
|
|
26
|
+
* @param options - optional render options (e.g. progress reporting)
|
|
21
27
|
* @returns
|
|
22
28
|
*/
|
|
23
|
-
compile(template: string, renderer: string, model: unknown, language?: string): Promise<string>;
|
|
29
|
+
compile(template: string, renderer: string, model: unknown, language?: string, options?: IRenderOptions): Promise<string>;
|
|
24
30
|
/**
|
|
25
31
|
*
|
|
26
32
|
* Renders template with given model and language. Returns compiled content
|
|
@@ -29,9 +35,20 @@ export declare class Templates extends AsyncService {
|
|
|
29
35
|
* @param template
|
|
30
36
|
* @param model
|
|
31
37
|
* @param language
|
|
38
|
+
* @param options - optional render options (e.g. progress reporting)
|
|
32
39
|
* @returns
|
|
33
40
|
*/
|
|
34
|
-
render(template: string, model: unknown, language?: string): Promise<string>;
|
|
35
|
-
renderToFile(template: string, model: unknown, filePath: string, language?: string): Promise<void>;
|
|
41
|
+
render(template: string, model: unknown, language?: string, options?: IRenderOptions): Promise<string>;
|
|
42
|
+
renderToFile(template: string, model: unknown, filePath: string, language?: string, options?: IRenderOptions): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Finds the renderer registered under the given renderer type (e.g. `handlebars`,
|
|
45
|
+
* `pdf`), throwing if none is registered.
|
|
46
|
+
*/
|
|
47
|
+
private engineByType;
|
|
48
|
+
/**
|
|
49
|
+
* Finds the renderer matching the template file's extension, throwing if none
|
|
50
|
+
* is registered for that extension.
|
|
51
|
+
*/
|
|
52
|
+
private engineByExtension;
|
|
36
53
|
}
|
|
37
54
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/cjs/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAsB,MAAM,aAAa,CAAC;AAC/D,OAAO,EAAE,GAAG,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAsB,MAAM,aAAa,CAAC;AAC/D,OAAO,EAAE,GAAG,EAAgB,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAG/C,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,SAAS,CAAC;AACxB,cAAc,+BAA+B,CAAC;AAC9C,cAAc,wBAAwB,CAAC;AAEvC;;GAEG;AACH,qBACa,SAAU,SAAQ,YAAY;IAEzC,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC;IAGnB,SAAS,CAAC,SAAS,EAAE,gBAAgB,EAAE,CAAC;IAEjC,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,gBAAgB;IAI3C,aAAa,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;IAK5J;;;;;;;;;;OAUG;IACU,OAAO,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC;IAKtI;;;;;;;;;;OAUG;IACU,MAAM,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC;IAKtG,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;IAKzI;;;OAGG;IACH,OAAO,CAAC,YAAY;IASpB;;;OAGG;IACH,OAAO,CAAC,iBAAiB;CAS1B"}
|
package/lib/cjs/index.js
CHANGED
|
@@ -31,6 +31,10 @@ const interfaces_js_1 = require("./interfaces.js");
|
|
|
31
31
|
const path_1 = require("path");
|
|
32
32
|
const intl_1 = require("@spinajs/intl");
|
|
33
33
|
__exportStar(require("./interfaces.js"), exports);
|
|
34
|
+
__exportStar(require("./progress.js"), exports);
|
|
35
|
+
__exportStar(require("./io.js"), exports);
|
|
36
|
+
__exportStar(require("./CompiledTemplateRenderer.js"), exports);
|
|
37
|
+
__exportStar(require("./cli/renderHelpers.js"), exports);
|
|
34
38
|
/**
|
|
35
39
|
* Inject INTL module for language support. We does nothing but to initialize module for use in templates.
|
|
36
40
|
*/
|
|
@@ -38,12 +42,9 @@ let Templates = class Templates extends di_1.AsyncService {
|
|
|
38
42
|
getRendererFor(extname) {
|
|
39
43
|
return this.Renderers.find((x) => x.Extension === extname);
|
|
40
44
|
}
|
|
41
|
-
async compileToFile(template, renderer, model, filePath, language) {
|
|
42
|
-
const engine = this.
|
|
43
|
-
|
|
44
|
-
throw new exceptions_1.InvalidOperation(`No renderer for ${renderer}`);
|
|
45
|
-
}
|
|
46
|
-
return await engine.renderToFile(template, model, filePath, language);
|
|
45
|
+
async compileToFile(template, renderer, model, filePath, language, options) {
|
|
46
|
+
const engine = this.engineByType(renderer);
|
|
47
|
+
return await log_1.Perf.measure('template.compile', () => engine.renderToFile(template, model, filePath, language, options), { labels: { engine: engine.Type }, fields: { template } });
|
|
47
48
|
}
|
|
48
49
|
/**
|
|
49
50
|
*
|
|
@@ -53,14 +54,12 @@ let Templates = class Templates extends di_1.AsyncService {
|
|
|
53
54
|
* @param renderer
|
|
54
55
|
* @param model
|
|
55
56
|
* @param language
|
|
57
|
+
* @param options - optional render options (e.g. progress reporting)
|
|
56
58
|
* @returns
|
|
57
59
|
*/
|
|
58
|
-
async compile(template, renderer, model, language) {
|
|
59
|
-
const engine = this.
|
|
60
|
-
|
|
61
|
-
throw new exceptions_1.InvalidOperation(`No renderer for ${renderer}`);
|
|
62
|
-
}
|
|
63
|
-
return await engine.render(template, model, language);
|
|
60
|
+
async compile(template, renderer, model, language, options) {
|
|
61
|
+
const engine = this.engineByType(renderer);
|
|
62
|
+
return await log_1.Perf.measure('template.compile', () => engine.render(template, model, language, options), { labels: { engine: engine.Type }, fields: { template } });
|
|
64
63
|
}
|
|
65
64
|
/**
|
|
66
65
|
*
|
|
@@ -70,23 +69,39 @@ let Templates = class Templates extends di_1.AsyncService {
|
|
|
70
69
|
* @param template
|
|
71
70
|
* @param model
|
|
72
71
|
* @param language
|
|
72
|
+
* @param options - optional render options (e.g. progress reporting)
|
|
73
73
|
* @returns
|
|
74
74
|
*/
|
|
75
|
-
async render(template, model, language) {
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
75
|
+
async render(template, model, language, options) {
|
|
76
|
+
const renderer = this.engineByExtension(template);
|
|
77
|
+
return await log_1.Perf.measure('template.render', () => renderer.render(template, model, language, options), { labels: { engine: renderer.Type }, fields: { template } });
|
|
78
|
+
}
|
|
79
|
+
async renderToFile(template, model, filePath, language, options) {
|
|
80
|
+
const renderer = this.engineByExtension(template);
|
|
81
|
+
return await log_1.Perf.measure('template.render', () => renderer.renderToFile(template, model, filePath, language, options), { labels: { engine: renderer.Type }, fields: { template } });
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Finds the renderer registered under the given renderer type (e.g. `handlebars`,
|
|
85
|
+
* `pdf`), throwing if none is registered.
|
|
86
|
+
*/
|
|
87
|
+
engineByType(renderer) {
|
|
88
|
+
const engine = this.Renderers.find((x) => x.Type === renderer);
|
|
89
|
+
if (!engine) {
|
|
90
|
+
throw new exceptions_1.InvalidOperation(`No renderer for ${renderer}`);
|
|
80
91
|
}
|
|
81
|
-
return
|
|
92
|
+
return engine;
|
|
82
93
|
}
|
|
83
|
-
|
|
94
|
+
/**
|
|
95
|
+
* Finds the renderer matching the template file's extension, throwing if none
|
|
96
|
+
* is registered for that extension.
|
|
97
|
+
*/
|
|
98
|
+
engineByExtension(template) {
|
|
84
99
|
const extension = (0, path_1.extname)(template);
|
|
85
100
|
const renderer = this.Renderers.find((x) => x.Extension === extension);
|
|
86
101
|
if (!renderer) {
|
|
87
102
|
throw new exceptions_1.InvalidOperation(`No renderer for file ${template} with extension ${extension}`);
|
|
88
103
|
}
|
|
89
|
-
return
|
|
104
|
+
return renderer;
|
|
90
105
|
}
|
|
91
106
|
};
|
|
92
107
|
exports.Templates = Templates;
|
package/lib/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oDAAuD;AACvD,oCAA+D;AAC/D,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRo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;QAC/D,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,MAAM,IAAI,6BAAgB,CAAC,mBAAmB,QAAQ,EAAE,CAAC,CAAC;QAC5D,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;IAED;;;OAGG;IACK,iBAAiB,CAAC,QAAgB;QACxC,MAAM,SAAS,GAAG,IAAA,cAAO,EAAC,QAAQ,CAAC,CAAC;QACpC,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,SAAS,KAAK,SAAS,CAAC,CAAC;QACvE,IAAI,CAAC,QAAQ,EAAE,CAAC;YACd,MAAM,IAAI,6BAAgB,CAAC,wBAAwB,QAAQ,mBAAmB,SAAS,EAAE,CAAC,CAAC;QAC7F,CAAC;QAED,OAAO,QAAQ,CAAC;IAClB,CAAC;CACF,CAAA;AA/EY,8BAAS;AAEV;IADT,IAAA,YAAM,EAAC,WAAW,CAAC;8BACL,SAAG;sCAAC;AAGT;IADT,IAAA,eAAU,EAAC,gCAAgB,CAAC;;4CACW;oBAL7B,SAAS;IADrB,IAAA,WAAM,EAAC,WAAI,CAAC;GACA,SAAS,CA+ErB"}
|
package/lib/cjs/interfaces.d.ts
CHANGED
|
@@ -1,21 +1,64 @@
|
|
|
1
1
|
import { IMappableService } from '@spinajs/di';
|
|
2
2
|
import { AsyncService } from '@spinajs/di';
|
|
3
3
|
import { Log } from '@spinajs/log';
|
|
4
|
+
import { IStat } from '@spinajs/fs';
|
|
5
|
+
import { IRenderOptions } from './progress.js';
|
|
6
|
+
/**
|
|
7
|
+
* How aggressively compiled templates are reused.
|
|
8
|
+
*
|
|
9
|
+
* cache - compile once, reuse forever. Default.
|
|
10
|
+
* revalidate - stat() the source and recompile only when its change token differs.
|
|
11
|
+
* always - recompile on every render.
|
|
12
|
+
*/
|
|
13
|
+
export type TemplateCacheMode = 'cache' | 'revalidate' | 'always';
|
|
14
|
+
export interface ITemplateCacheEntry {
|
|
15
|
+
compiled: unknown;
|
|
16
|
+
/**
|
|
17
|
+
* Change token captured when this entry was compiled. Null when the mode does
|
|
18
|
+
* not track one, or when the source could not be stat-ed.
|
|
19
|
+
*/
|
|
20
|
+
token: string | null;
|
|
21
|
+
}
|
|
4
22
|
export declare abstract class TemplateRenderer extends AsyncService implements IMappableService {
|
|
5
23
|
protected Log: Log;
|
|
6
|
-
protected
|
|
7
|
-
protected
|
|
24
|
+
protected ConfiguredCacheMode: TemplateCacheMode;
|
|
25
|
+
protected DevMode: boolean;
|
|
26
|
+
protected Cache: Map<string, ITemplateCacheEntry>;
|
|
8
27
|
abstract get Type(): string;
|
|
9
28
|
abstract get Extension(): string;
|
|
10
29
|
get ServiceName(): string;
|
|
11
|
-
abstract render(templatePath: string, model: unknown, language?: string): Promise<string>;
|
|
12
|
-
abstract renderToFile(templatePath: string, model: unknown, filePath: string, language?: string): Promise<void>;
|
|
30
|
+
abstract render(templatePath: string, model: unknown, language?: string, options?: IRenderOptions): Promise<string>;
|
|
31
|
+
abstract renderToFile(templatePath: string, model: unknown, filePath: string, language?: string, options?: IRenderOptions): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* Explicit config wins; otherwise dev mode implies always-recompile.
|
|
34
|
+
*/
|
|
35
|
+
protected get CacheMode(): TemplateCacheMode;
|
|
36
|
+
protected isUri(template: string): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Bare paths are normalized as before. URIs must be left alone - path.normalize
|
|
39
|
+
* would turn fs://name/x into fs:\name\x on windows and break URI parsing.
|
|
40
|
+
*/
|
|
41
|
+
protected normalizeTemplate(template: string): string;
|
|
42
|
+
/**
|
|
43
|
+
* Template source as text. Bare paths read from local disk exactly as before -
|
|
44
|
+
* routing them through the default provider would re-resolve relative paths
|
|
45
|
+
* against its basePath and break existing callers.
|
|
46
|
+
*/
|
|
47
|
+
protected resolveContent(template: string): Promise<string>;
|
|
48
|
+
/**
|
|
49
|
+
* A real local path. For renderers that cannot compile from a string (pug),
|
|
50
|
+
* remote sources are materialised to temp storage.
|
|
51
|
+
*/
|
|
52
|
+
protected resolveLocalPath(template: string): Promise<string>;
|
|
53
|
+
/**
|
|
54
|
+
* Opaque token that changes when the source changes. Null means "cannot tell" -
|
|
55
|
+
* callers must then trust whatever they already have cached.
|
|
56
|
+
*/
|
|
57
|
+
protected changeToken(template: string): Promise<string | null>;
|
|
58
|
+
protected tokenFromStat(s: IStat): string;
|
|
13
59
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* @param templateName - template name
|
|
17
|
-
* @param path - template full path
|
|
60
|
+
* Compiled-template cache honouring CacheMode. `compile` runs only on a miss.
|
|
18
61
|
*/
|
|
19
|
-
protected
|
|
62
|
+
protected withCache<T>(template: string, compile: () => Promise<T>): Promise<T>;
|
|
20
63
|
}
|
|
21
64
|
//# sourceMappingURL=interfaces.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interfaces.d.ts","sourceRoot":"","sources":["../../src/interfaces.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"interfaces.d.ts","sourceRoot":"","sources":["../../src/interfaces.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,OAAO,EAAU,GAAG,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAM,KAAK,EAAO,MAAM,aAAa,CAAC;AAG7C,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE/C;;;;;;GAMG;AACH,MAAM,MAAM,iBAAiB,GAAG,OAAO,GAAG,YAAY,GAAG,QAAQ,CAAC;AAElE,MAAM,WAAW,mBAAmB;IAClC,QAAQ,EAAE,OAAO,CAAC;IAElB;;;OAGG;IACH,KAAK,EAAE,MAAM,GAAG,IA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