ecto 4.3.0 → 4.4.0

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/README.md CHANGED
@@ -40,6 +40,7 @@ Ecto is a modern template consolidation engine that enables the best template en
40
40
  * [Liquid](#liquid)
41
41
  * [FrontMatter Helper Functions](#frontmatter-helper-functions)
42
42
  * [Caching on Rendering](#caching-on-rendering)
43
+ * [Emitting Events](#emitting-events)
43
44
  * [How to Contribute](#how-to-contribute)
44
45
  * [License](#license)
45
46
 
@@ -608,6 +609,39 @@ const result = await ecto.render(source, data);
608
609
  console.log(result); // <h1>Hello John Doe!</h1>
609
610
  ```
610
611
 
612
+ # Emitting Events
613
+
614
+ Ecto has a built-in event emitter that allows you to listen for events that occur during the rendering process. This is useful for debugging and monitoring the rendering process. The following events are emitted:
615
+
616
+ - `cacheHit` - Emitted when a cached result is found and returned.
617
+ - `cacheMiss` - Emitted when a cached result is not found and the template needs to be rendered.
618
+ - `info` - Emitted for informational messages.
619
+ - `warn` - Emitted for warning messages.
620
+ - `error` - Emitted for error messages.
621
+
622
+ You can listen for these events by using the `on` method of the `Ecto` instance:
623
+
624
+ ```javascript
625
+ import { Ecto } from 'ecto';
626
+ const ecto = new Ecto();
627
+ ecto.on('cacheHit', (data) => {
628
+ console.log('Cache hit:', data);
629
+ });
630
+ ecto.on('cacheMiss', (data) => {
631
+ console.log('Cache miss:', data);
632
+ });
633
+ ecto.on('warn', (data) => {
634
+ console.warn('Warning:', data);
635
+ });
636
+ ecto.on('error', (data) => {
637
+ console.error('Error:', data);
638
+ });
639
+
640
+ const source = "<h1>Hello <%= firstName%> <%= lastName %>!</h1>";
641
+ const data = { firstName: "John", lastName: "Doe" };
642
+ await ecto.render(source, data); // <h1>Hello John Doe!</h1>
643
+ ```
644
+
611
645
  # How to Contribute
612
646
 
613
647
  To contribute please ready our [CONTRIBUTING.md](CONTRIBUTING.md) guide. Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
package/dist/ecto.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { HookifiedOptions, Hookified } from 'hookified';
1
2
  import { Cacheable, CacheableMemory } from 'cacheable';
2
3
 
3
4
  declare class EngineMap {
@@ -104,8 +105,14 @@ type EctoOptions = {
104
105
  * @default false
105
106
  */
106
107
  cacheSync?: boolean | CacheableMemory;
107
- };
108
- declare class Ecto {
108
+ } & HookifiedOptions;
109
+ declare enum EctoEvents {
110
+ cacheHit = "cacheHit",
111
+ cacheMiss = "cacheMiss",
112
+ warn = "warn",
113
+ error = "error"
114
+ }
115
+ declare class Ecto extends Hookified {
109
116
  private readonly __mapping;
110
117
  private readonly __engines;
111
118
  private __cache;
@@ -305,4 +312,4 @@ declare class Ecto {
305
312
  private writeFileSync;
306
313
  }
307
314
 
308
- export { Ecto, type EctoOptions };
315
+ export { Ecto, EctoEvents, type EctoOptions };
package/dist/ecto.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // src/ecto.ts
2
2
  import fs2 from "fs";
3
+ import { Hookified } from "hookified";
3
4
  import { Writr as Writr2 } from "writr";
4
5
  import { Cacheable, CacheableMemory } from "cacheable";
5
6
 
@@ -289,7 +290,14 @@ var Liquid = class extends BaseEngine {
289
290
  };
290
291
 
291
292
  // src/ecto.ts
292
- var Ecto = class {
293
+ var EctoEvents = /* @__PURE__ */ ((EctoEvents2) => {
294
+ EctoEvents2["cacheHit"] = "cacheHit";
295
+ EctoEvents2["cacheMiss"] = "cacheMiss";
296
+ EctoEvents2["warn"] = "warn";
297
+ EctoEvents2["error"] = "error";
298
+ return EctoEvents2;
299
+ })(EctoEvents || {});
300
+ var Ecto = class extends Hookified {
293
301
  __mapping = new EngineMap();
294
302
  __engines = new Array();
295
303
  // Cacheable instance for caching rendered templates
@@ -309,6 +317,7 @@ var Ecto = class {
309
317
  */
310
318
  // eslint-disable-next-line complexity
311
319
  constructor(options) {
320
+ super();
312
321
  this.__ejs = new EJS(options?.engineOptions?.ejs);
313
322
  this.__markdown = new Markdown(options?.engineOptions?.markdown);
314
323
  this.__pug = new Pug(options?.engineOptions?.pug);
@@ -350,6 +359,8 @@ var Ecto = class {
350
359
  value = value.toLowerCase().trim();
351
360
  if (this.isValidEngine(value)) {
352
361
  this.__defaultEngine = value;
362
+ } else {
363
+ this.emit("warn" /* warn */, `Invalid engine name: ${value}. Defaulting to ${this.__defaultEngine}.`);
353
364
  }
354
365
  }
355
366
  /**
@@ -440,27 +451,34 @@ var Ecto = class {
440
451
  */
441
452
  // eslint-disable-next-line max-params
442
453
  async render(source, data, engineName, rootTemplatePath, filePathOutput) {
443
- const cacheKey = `${engineName ?? this.__defaultEngine}-${source}-${JSON.stringify(data)}`;
444
- if (this.__cache) {
445
- const cachedResult = await this.__cache.get(cacheKey);
446
- if (cachedResult) {
447
- await this.writeFile(filePathOutput, cachedResult);
448
- return cachedResult;
454
+ try {
455
+ const cacheKey = `${engineName ?? this.__defaultEngine}-${source}-${JSON.stringify(data)}`;
456
+ if (this.__cache) {
457
+ const cachedResult = await this.__cache.get(cacheKey);
458
+ if (cachedResult) {
459
+ this.emit("cacheHit" /* cacheHit */, `Cache hit for key: ${cacheKey}`);
460
+ await this.writeFile(filePathOutput, cachedResult);
461
+ return cachedResult;
462
+ }
463
+ this.emit("cacheMiss" /* cacheMiss */, `Cache miss for key: ${cacheKey}`);
449
464
  }
465
+ let result = "";
466
+ let renderEngineName = this.__defaultEngine;
467
+ if (this.isValidEngine(engineName) && engineName !== void 0) {
468
+ renderEngineName = engineName;
469
+ }
470
+ const renderEngine = this.getRenderEngine(renderEngineName);
471
+ renderEngine.rootTemplatePath = rootTemplatePath;
472
+ result = await renderEngine.render(source, data);
473
+ if (this.__cache) {
474
+ await this.__cache.set(cacheKey, result);
475
+ }
476
+ await this.writeFile(filePathOutput, result);
477
+ return result;
478
+ } catch (error) {
479
+ this.emit("error" /* error */, error);
480
+ return "";
450
481
  }
451
- let result = "";
452
- let renderEngineName = this.__defaultEngine;
453
- if (this.isValidEngine(engineName) && engineName !== void 0) {
454
- renderEngineName = engineName;
455
- }
456
- const renderEngine = this.getRenderEngine(renderEngineName);
457
- renderEngine.rootTemplatePath = rootTemplatePath;
458
- result = await renderEngine.render(source, data);
459
- if (this.__cache) {
460
- await this.__cache.set(cacheKey, result);
461
- }
462
- await this.writeFile(filePathOutput, result);
463
- return result;
464
482
  }
465
483
  /**
466
484
  * Synchronously render the source with the data
@@ -473,27 +491,34 @@ var Ecto = class {
473
491
  */
474
492
  // eslint-disable-next-line max-params
475
493
  renderSync(source, data, engineName, rootTemplatePath, filePathOutput) {
476
- const cacheKey = `${engineName ?? this.__defaultEngine}-${source}-${JSON.stringify(data)}`;
477
- if (this.__cacheSync) {
478
- const cachedResult = this.__cacheSync.get(cacheKey);
479
- if (cachedResult) {
480
- this.writeFileSync(filePathOutput, cachedResult);
481
- return cachedResult;
494
+ try {
495
+ const cacheKey = `${engineName ?? this.__defaultEngine}-${source}-${JSON.stringify(data)}`;
496
+ if (this.__cacheSync) {
497
+ const cachedResult = this.__cacheSync.get(cacheKey);
498
+ if (cachedResult) {
499
+ this.emit("cacheHit" /* cacheHit */, `Cache hit for key: ${cacheKey}`);
500
+ this.writeFileSync(filePathOutput, cachedResult);
501
+ return cachedResult;
502
+ }
503
+ this.emit("cacheMiss" /* cacheMiss */, `Cache miss for key: ${cacheKey}`);
482
504
  }
505
+ let result = "";
506
+ let renderEngineName = this.__defaultEngine;
507
+ if (this.isValidEngine(engineName) && engineName !== void 0) {
508
+ renderEngineName = engineName;
509
+ }
510
+ const renderEngine = this.getRenderEngine(renderEngineName);
511
+ renderEngine.rootTemplatePath = rootTemplatePath;
512
+ result = renderEngine.renderSync(source, data);
513
+ if (this.__cacheSync) {
514
+ this.__cacheSync.set(cacheKey, result);
515
+ }
516
+ this.writeFileSync(filePathOutput, result);
517
+ return result;
518
+ } catch (error) {
519
+ this.emit("error" /* error */, error);
520
+ return "";
483
521
  }
484
- let result = "";
485
- let renderEngineName = this.__defaultEngine;
486
- if (this.isValidEngine(engineName) && engineName !== void 0) {
487
- renderEngineName = engineName;
488
- }
489
- const renderEngine = this.getRenderEngine(renderEngineName);
490
- renderEngine.rootTemplatePath = rootTemplatePath;
491
- result = renderEngine.renderSync(source, data);
492
- if (this.__cacheSync) {
493
- this.__cacheSync.set(cacheKey, result);
494
- }
495
- this.writeFileSync(filePathOutput, result);
496
- return result;
497
522
  }
498
523
  /**
499
524
  * Render from a file path
@@ -723,5 +748,6 @@ var Ecto = class {
723
748
  }
724
749
  };
725
750
  export {
726
- Ecto
751
+ Ecto,
752
+ EctoEvents
727
753
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ecto",
3
- "version": "4.3.0",
3
+ "version": "4.4.0",
4
4
  "description": "Modern Template Consolidation Engine for EJS, Markdown, Pug, Nunjucks, Liquid, and Handlebars",
5
5
  "type": "module",
6
6
  "main": "./dist/ecto.js",
@@ -46,29 +46,30 @@
46
46
  "liquidjs"
47
47
  ],
48
48
  "dependencies": {
49
- "@jaredwray/fumanchu": "^3.1.1",
50
- "cacheable": "^1.10.0",
49
+ "@jaredwray/fumanchu": "^3.1.2",
50
+ "cacheable": "^1.10.1",
51
51
  "ejs": "^3.1.10",
52
+ "hookified": "^1.10.0",
52
53
  "liquidjs": "^10.21.1",
53
54
  "nunjucks": "^3.2.4",
54
55
  "pug": "^3.0.3",
55
56
  "underscore": "^1.13.7",
56
- "writr": "^4.4.4"
57
+ "writr": "^4.4.5"
57
58
  },
58
59
  "devDependencies": {
59
60
  "@types/ejs": "^3.1.5",
60
- "@types/node": "^24.0.1",
61
+ "@types/node": "^24.0.14",
61
62
  "@types/nunjucks": "^3.2.6",
62
63
  "@types/pug": "^2.0.10",
63
64
  "@types/underscore": "^1.13.0",
64
- "@vitest/coverage-v8": "^3.2.3",
65
- "docula": "^0.12.2",
65
+ "@vitest/coverage-v8": "^3.2.4",
66
+ "docula": "^0.13.0",
66
67
  "rimraf": "^6.0.1",
67
68
  "tsup": "^8.5.0",
68
69
  "typescript": "^5.8.3",
69
- "vitest": "^3.2.3",
70
- "webpack": "^5.99.9",
71
- "xo": "^1.1.0"
70
+ "vitest": "^3.2.4",
71
+ "webpack": "^5.100.2",
72
+ "xo": "^1.2.0"
72
73
  },
73
74
  "files": [
74
75
  "dist",