ecto 4.3.0 → 4.4.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.
Files changed (4) hide show
  1. package/README.md +34 -0
  2. package/dist/ecto.d.ts +22 -15
  3. package/dist/ecto.js +126 -104
  4. package/package.json +12 -11
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 {
@@ -14,7 +15,7 @@ declare class BaseEngine {
14
15
  opts?: any;
15
16
  engine: any;
16
17
  rootTemplatePath?: string;
17
- private __extensions;
18
+ private _extensions;
18
19
  getExtensions(): string[];
19
20
  setExtensions(extensions: string[]): void;
20
21
  deleteExtension(name: string): void;
@@ -104,19 +105,25 @@ type EctoOptions = {
104
105
  * @default false
105
106
  */
106
107
  cacheSync?: boolean | CacheableMemory;
107
- };
108
- declare class Ecto {
109
- private readonly __mapping;
110
- private readonly __engines;
111
- private __cache;
112
- private __cacheSync;
113
- private __defaultEngine;
114
- private readonly __ejs;
115
- private readonly __markdown;
116
- private readonly __pug;
117
- private readonly __nunjucks;
118
- private readonly __handlebars;
119
- private readonly __liquid;
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 {
116
+ private readonly _mapping;
117
+ private readonly _engines;
118
+ private _cache;
119
+ private _cacheSync;
120
+ private _defaultEngine;
121
+ private readonly _ejs;
122
+ private readonly _markdown;
123
+ private readonly _pug;
124
+ private readonly _nunjucks;
125
+ private readonly _handlebars;
126
+ private readonly _liquid;
120
127
  /**
121
128
  * Ecto constructor
122
129
  * @param {EctoOptions} [options] - The options for the ecto engine
@@ -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
 
@@ -58,23 +59,23 @@ var BaseEngine = class {
58
59
  opts = void 0;
59
60
  engine;
60
61
  rootTemplatePath = void 0;
61
- __extensions = new Array();
62
+ _extensions = new Array();
62
63
  getExtensions() {
63
- return this.__extensions;
64
+ return this._extensions;
64
65
  }
65
66
  setExtensions(extensions) {
66
- this.__extensions = new Array();
67
+ this._extensions = new Array();
67
68
  for (const extension of extensions) {
68
69
  const newExtension = extension.trim().toLowerCase();
69
- if (!this.__extensions.includes(newExtension)) {
70
- this.__extensions.push(newExtension);
70
+ if (!this._extensions.includes(newExtension)) {
71
+ this._extensions.push(newExtension);
71
72
  }
72
73
  }
73
74
  }
74
75
  deleteExtension(name) {
75
- for (const [index, extension] of this.__extensions.entries()) {
76
+ for (const [index, extension] of this._extensions.entries()) {
76
77
  if (name.toLowerCase().trim() === extension) {
77
- this.__extensions.splice(index, 1);
78
+ this._extensions.splice(index, 1);
78
79
  }
79
80
  }
80
81
  }
@@ -289,50 +290,54 @@ var Liquid = class extends BaseEngine {
289
290
  };
290
291
 
291
292
  // src/ecto.ts
292
- var Ecto = class {
293
- __mapping = new EngineMap();
294
- __engines = new Array();
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 {
301
+ _mapping = new EngineMap();
302
+ _engines = new Array();
295
303
  // Cacheable instance for caching rendered templates
296
- __cache;
297
- __cacheSync;
298
- __defaultEngine = "ejs";
304
+ _cache;
305
+ _cacheSync;
306
+ _defaultEngine = "ejs";
299
307
  // Engines
300
- __ejs;
301
- __markdown;
302
- __pug;
303
- __nunjucks;
304
- __handlebars;
305
- __liquid;
308
+ _ejs;
309
+ _markdown;
310
+ _pug;
311
+ _nunjucks;
312
+ _handlebars;
313
+ _liquid;
306
314
  /**
307
315
  * Ecto constructor
308
316
  * @param {EctoOptions} [options] - The options for the ecto engine
309
317
  */
310
318
  // eslint-disable-next-line complexity
311
319
  constructor(options) {
312
- this.__ejs = new EJS(options?.engineOptions?.ejs);
313
- this.__markdown = new Markdown(options?.engineOptions?.markdown);
314
- this.__pug = new Pug(options?.engineOptions?.pug);
315
- this.__nunjucks = new Nunjucks(options?.engineOptions?.nunjucks);
316
- this.__handlebars = new Handlebars(options?.engineOptions?.handlebars);
317
- this.__liquid = new Liquid(options?.engineOptions?.liquid);
318
- this.__engines.push(this.__ejs, this.__markdown, this.__pug, this.__nunjucks, this.__handlebars, this.__liquid);
320
+ super();
321
+ this._ejs = new EJS(options?.engineOptions?.ejs);
322
+ this._markdown = new Markdown(options?.engineOptions?.markdown);
323
+ this._pug = new Pug(options?.engineOptions?.pug);
324
+ this._nunjucks = new Nunjucks(options?.engineOptions?.nunjucks);
325
+ this._handlebars = new Handlebars(options?.engineOptions?.handlebars);
326
+ this._liquid = new Liquid(options?.engineOptions?.liquid);
327
+ this._engines.push(this._ejs, this._markdown, this._pug, this._nunjucks, this._handlebars, this._liquid);
319
328
  this.registerEngineMappings();
320
- if (options?.cache !== void 0) {
321
- if (typeof options.cache === "boolean") {
322
- this.__cache = new Cacheable();
323
- } else {
324
- this.__cache = options.cache;
325
- }
329
+ if (options?.cache === true) {
330
+ this._cache = new Cacheable();
331
+ } else if (options?.cache instanceof Cacheable) {
332
+ this._cache = options.cache;
326
333
  }
327
- if (options?.cacheSync !== void 0) {
328
- if (typeof options.cacheSync === "boolean") {
329
- this.__cacheSync = new CacheableMemory();
330
- } else {
331
- this.__cacheSync = options.cacheSync;
332
- }
334
+ if (options?.cacheSync === true) {
335
+ this._cacheSync = new CacheableMemory();
336
+ } else if (options?.cacheSync instanceof CacheableMemory) {
337
+ this._cacheSync = options.cacheSync;
333
338
  }
334
339
  if (options !== void 0 && this.isValidEngine(options.defaultEngine)) {
335
- this.__defaultEngine = options.defaultEngine;
340
+ this._defaultEngine = options.defaultEngine;
336
341
  }
337
342
  }
338
343
  /**
@@ -340,7 +345,7 @@ var Ecto = class {
340
345
  * @returns {string} - the engine name such as 'ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid'
341
346
  */
342
347
  get defaultEngine() {
343
- return this.__defaultEngine;
348
+ return this._defaultEngine;
344
349
  }
345
350
  /**
346
351
  * Set the default engine
@@ -349,7 +354,9 @@ var Ecto = class {
349
354
  set defaultEngine(value) {
350
355
  value = value.toLowerCase().trim();
351
356
  if (this.isValidEngine(value)) {
352
- this.__defaultEngine = value;
357
+ this._defaultEngine = value;
358
+ } else {
359
+ this.emit("warn" /* warn */, `Invalid engine name: ${value}. Defaulting to ${this._defaultEngine}.`);
353
360
  }
354
361
  }
355
362
  /**
@@ -357,77 +364,77 @@ var Ecto = class {
357
364
  * @returns {Cacheable | undefined} - The cacheable instance or undefined if caching is disabled
358
365
  */
359
366
  get cache() {
360
- return this.__cache;
367
+ return this._cache;
361
368
  }
362
369
  /**
363
370
  * Set the cacheable instance
364
371
  * @param {Cacheable | undefined} value - The cacheable instance to set. If set to undefined, caching will be disabled.
365
372
  */
366
373
  set cache(value) {
367
- this.__cache = value;
374
+ this._cache = value;
368
375
  }
369
376
  /**
370
377
  * Get the cacheable memory instance
371
378
  * @returns {CacheableMemory | undefined} - The cacheable memory instance or undefined if caching is disabled
372
379
  */
373
380
  get cacheSync() {
374
- return this.__cacheSync;
381
+ return this._cacheSync;
375
382
  }
376
383
  /**
377
384
  * Set the cacheable memory instance
378
385
  * @param {CacheableMemory | undefined} value - The cacheable memory instance to set. If set to undefined, caching will be disabled.
379
386
  */
380
387
  set cacheSync(value) {
381
- this.__cacheSync = value;
388
+ this._cacheSync = value;
382
389
  }
383
390
  /**
384
391
  * Get the Engine Mappings. This is used to map file extensions to engines
385
392
  * @returns {EngineMap}
386
393
  */
387
394
  get mappings() {
388
- return this.__mapping;
395
+ return this._mapping;
389
396
  }
390
397
  /**
391
398
  * Get the EJS Engine
392
399
  * @returns {EJS}
393
400
  */
394
401
  get ejs() {
395
- return this.__ejs;
402
+ return this._ejs;
396
403
  }
397
404
  /**
398
405
  * Get the Markdown Engine
399
406
  * @returns {Markdown}
400
407
  */
401
408
  get markdown() {
402
- return this.__markdown;
409
+ return this._markdown;
403
410
  }
404
411
  /**
405
412
  * Get the Pug Engine
406
413
  * @returns {Pug}
407
414
  */
408
415
  get pug() {
409
- return this.__pug;
416
+ return this._pug;
410
417
  }
411
418
  /**
412
419
  * Get the Nunjucks Engine
413
420
  * @returns {Nunjucks}
414
421
  */
415
422
  get nunjucks() {
416
- return this.__nunjucks;
423
+ return this._nunjucks;
417
424
  }
418
425
  /**
419
426
  * Get the Handlebars Engine
420
427
  * @returns {Handlebars}
421
428
  */
422
429
  get handlebars() {
423
- return this.__handlebars;
430
+ return this._handlebars;
424
431
  }
425
432
  /**
426
433
  * Get the Liquid Engine
427
434
  * @returns {Liquid}
428
435
  */
429
436
  get liquid() {
430
- return this.__liquid;
437
+ return this._liquid;
431
438
  }
432
439
  /**
433
440
  * Async render the source with the data
@@ -440,27 +447,34 @@ var Ecto = class {
440
447
  */
441
448
  // eslint-disable-next-line max-params
442
449
  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;
450
+ try {
451
+ const cacheKey = `${engineName ?? this._defaultEngine}-${source}-${JSON.stringify(data)}`;
452
+ if (this._cache) {
453
+ const cachedResult = await this._cache.get(cacheKey);
454
+ if (cachedResult) {
455
+ this.emit("cacheHit" /* cacheHit */, `Cache hit for key: ${cacheKey}`);
456
+ await this.writeFile(filePathOutput, cachedResult);
457
+ return cachedResult;
458
+ }
459
+ this.emit("cacheMiss" /* cacheMiss */, `Cache miss for key: ${cacheKey}`);
449
460
  }
461
+ let result = "";
462
+ let renderEngineName = this._defaultEngine;
463
+ if (this.isValidEngine(engineName) && engineName !== void 0) {
464
+ renderEngineName = engineName;
465
+ }
466
+ const renderEngine = this.getRenderEngine(renderEngineName);
467
+ renderEngine.rootTemplatePath = rootTemplatePath;
468
+ result = await renderEngine.render(source, data);
469
+ if (this._cache) {
470
+ await this._cache.set(cacheKey, result);
471
+ }
472
+ await this.writeFile(filePathOutput, result);
473
+ return result;
474
+ } catch (error) {
475
+ this.emit("error" /* error */, error);
476
+ return "";
450
477
  }
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
478
  }
465
479
  /**
466
480
  * Synchronously render the source with the data
@@ -473,27 +487,34 @@ var Ecto = class {
473
487
  */
474
488
  // eslint-disable-next-line max-params
475
489
  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;
490
+ try {
491
+ const cacheKey = `${engineName ?? this._defaultEngine}-${source}-${JSON.stringify(data)}`;
492
+ if (this._cacheSync) {
493
+ const cachedResult = this._cacheSync.get(cacheKey);
494
+ if (cachedResult) {
495
+ this.emit("cacheHit" /* cacheHit */, `Cache hit for key: ${cacheKey}`);
496
+ this.writeFileSync(filePathOutput, cachedResult);
497
+ return cachedResult;
498
+ }
499
+ this.emit("cacheMiss" /* cacheMiss */, `Cache miss for key: ${cacheKey}`);
482
500
  }
501
+ let result = "";
502
+ let renderEngineName = this._defaultEngine;
503
+ if (this.isValidEngine(engineName) && engineName !== void 0) {
504
+ renderEngineName = engineName;
505
+ }
506
+ const renderEngine = this.getRenderEngine(renderEngineName);
507
+ renderEngine.rootTemplatePath = rootTemplatePath;
508
+ result = renderEngine.renderSync(source, data);
509
+ if (this._cacheSync) {
510
+ this._cacheSync.set(cacheKey, result);
511
+ }
512
+ this.writeFileSync(filePathOutput, result);
513
+ return result;
514
+ } catch (error) {
515
+ this.emit("error" /* error */, error);
516
+ return "";
483
517
  }
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
518
  }
498
519
  /**
499
520
  * Render from a file path
@@ -561,10 +582,10 @@ var Ecto = class {
561
582
  * @returns {string} - will return the engine name such as 'ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid'
562
583
  */
563
584
  getEngineByFilePath(filePath) {
564
- let result = this.__defaultEngine;
585
+ let result = this._defaultEngine;
565
586
  if (filePath !== void 0) {
566
587
  const extension = filePath.includes(".") ? filePath.slice(filePath.lastIndexOf(".") + 1) : "";
567
- const engExtension = this.__mapping.getName(extension);
588
+ const engExtension = this._mapping.getName(extension);
568
589
  if (engExtension !== void 0) {
569
590
  result = engExtension;
570
591
  }
@@ -612,7 +633,7 @@ var Ecto = class {
612
633
  */
613
634
  isValidEngine(engineName) {
614
635
  let result = false;
615
- if (engineName !== void 0 && this.__mapping.get(engineName) !== void 0) {
636
+ if (engineName !== void 0 && this._mapping.get(engineName) !== void 0) {
616
637
  result = true;
617
638
  }
618
639
  return result;
@@ -622,9 +643,9 @@ var Ecto = class {
622
643
  * @returns {void}
623
644
  */
624
645
  registerEngineMappings() {
625
- for (const eng of this.__engines) {
646
+ for (const eng of this._engines) {
626
647
  for (const name of eng.names) {
627
- this.__mapping.set(name, eng.getExtensions());
648
+ this._mapping.set(name, eng.getExtensions());
628
649
  }
629
650
  }
630
651
  }
@@ -634,35 +655,35 @@ var Ecto = class {
634
655
  * @returns {EngineInterface}
635
656
  */
636
657
  getRenderEngine(engineName) {
637
- let result = this.__ejs;
658
+ let result = this._ejs;
638
659
  const cleanEngineName = engineName.trim().toLowerCase();
639
660
  switch (cleanEngineName) {
640
661
  case "markdown": {
641
- result = this.__markdown;
662
+ result = this._markdown;
642
663
  break;
643
664
  }
644
665
  case "pug": {
645
- result = this.__pug;
666
+ result = this._pug;
646
667
  break;
647
668
  }
648
669
  case "nunjucks": {
649
- result = this.__nunjucks;
670
+ result = this._nunjucks;
650
671
  break;
651
672
  }
652
673
  case "mustache": {
653
- result = this.__handlebars;
674
+ result = this._handlebars;
654
675
  break;
655
676
  }
656
677
  case "handlebars": {
657
- result = this.__handlebars;
678
+ result = this._handlebars;
658
679
  break;
659
680
  }
660
681
  case "liquid": {
661
- result = this.__liquid;
682
+ result = this._liquid;
662
683
  break;
663
684
  }
664
685
  default: {
665
- result = this.__ejs;
686
+ result = this._ejs;
666
687
  break;
667
688
  }
668
689
  }
@@ -723,5 +744,6 @@ var Ecto = class {
723
744
  }
724
745
  };
725
746
  export {
726
- Ecto
747
+ Ecto,
748
+ EctoEvents
727
749
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ecto",
3
- "version": "4.3.0",
3
+ "version": "4.4.1",
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.4",
51
51
  "ejs": "^3.1.10",
52
+ "hookified": "^1.11.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.6"
57
58
  },
58
59
  "devDependencies": {
59
60
  "@types/ejs": "^3.1.5",
60
- "@types/node": "^24.0.1",
61
+ "@types/node": "^24.3.0",
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.20.0",
66
67
  "rimraf": "^6.0.1",
67
68
  "tsup": "^8.5.0",
68
- "typescript": "^5.8.3",
69
- "vitest": "^3.2.3",
70
- "webpack": "^5.99.9",
71
- "xo": "^1.1.0"
69
+ "typescript": "^5.9.2",
70
+ "vitest": "^3.2.4",
71
+ "webpack": "^5.101.3",
72
+ "xo": "^1.2.0"
72
73
  },
73
74
  "files": [
74
75
  "dist",