Skip to content

Commit

Permalink
feat(engines): add eta engine
Browse files Browse the repository at this point in the history
  • Loading branch information
beatyt authored and Romakita committed Feb 22, 2024
1 parent 5306644 commit b7ada12
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/docs/templating.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ If `dustjs-helpers` is installed, `dustjs-linkedin` will not be used by consolid
| [dust](https://github.com/linkedin/dustjs) | [`npm install dustjs-helpers`](https://www.npmjs.com/package/dustjs-helpers) (2) or<br>[`npm install dustjs-linkedin`](https://www.npmjs.com/package/dustjs-linkedin) (3) | [(website)](http://linkedin.github.io/dustjs/) |
| [ect](https://github.com/baryshev/ect) | [`npm install ect`](https://www.npmjs.com/package/ect) | [(website)](http://ectjs.com/) |
| [ejs](https://github.com/mde/ejs) | [`npm install ejs`](https://www.npmjs.com/package/ejs) | [(website)](http://ejs.co/) |
| [eta](https://eta.js.org/) | [`npm install eta`](https://www.npmjs.com/package/eta) | [(website)](http://ejs.co/) |
| [hamlet](https://github.com/gregwebs/hamlet.js) | [`npm install hamlet`](https://www.npmjs.com/package/hamlet) | - |
| [hamljs](https://github.com/visionmedia/haml.js) | [`npm install hamljs`](https://www.npmjs.com/package/hamljs) | - |
| [haml-coffee](https://github.com/netzpirat/haml-coffee) | [`npm install haml-coffee`](https://www.npmjs.com/package/haml-coffee) | - |
Expand Down
1 change: 1 addition & 0 deletions packages/engines/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"ect": "^0.5.9",
"ejs": "^3.1.6",
"eslint": "^8.12.0",
"eta": "3.2.0",
"haml": "^0.4.3",
"haml-coffee": "^1.14.1",
"hamlet": "^0.3.3",
Expand Down
1 change: 1 addition & 0 deletions packages/engines/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ If `dustjs-helpers` is installed, `dustjs-linkedin` will not be used by consolid
| [dust](https://github.com/linkedin/dustjs) | [`npm install dustjs-helpers`](https://www.npmjs.com/package/dustjs-helpers) (2) or<br>[`npm install dustjs-linkedin`](https://www.npmjs.com/package/dustjs-linkedin) (3) | [(website)](http://linkedin.github.io/dustjs/) |
| [ect](https://github.com/baryshev/ect) | [`npm install ect`](https://www.npmjs.com/package/ect) | [(website)](http://ectjs.com/) |
| [ejs](https://github.com/mde/ejs) | [`npm install ejs`](https://www.npmjs.com/package/ejs) | [(website)](http://ejs.co/) |
| [eta](https://eta.js.org/) | [`npm install eta`](https://www.npmjs.com/package/eta) | [(website)](http://ejs.co/) |
| [hamlet](https://github.com/gregwebs/hamlet.js) | [`npm install hamlet`](https://www.npmjs.com/package/hamlet) | - |
| [hamljs](https://github.com/visionmedia/haml.js) | [`npm install hamljs`](https://www.npmjs.com/package/hamljs) | - |
| [haml-coffee](https://github.com/netzpirat/haml-coffee) | [`npm install haml-coffee`](https://www.npmjs.com/package/haml-coffee) | - |
Expand Down
1 change: 1 addition & 0 deletions packages/engines/src/components/Engine.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {Type} from "@tsed/core";
import {cache, getCachedEngine, importEngine, read, readPartials} from "../utils/cache";

export interface ViewEngineOptions {
Expand Down
79 changes: 79 additions & 0 deletions packages/engines/src/components/EtaEngine.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {expect} from "chai";
import {renderFile} from "ejs";
import {dirname} from "node:path";
import {getEngineFixture} from "../../test/getEngineFixture";
import {EtaEngine} from "./EtaEngine";

describe("EtaEngine", () => {
it("should render the given content", async () => {
const {render, locals, $compile, template} = await getEngineFixture({
token: EtaEngine
});

await render();

expect(await render()).to.eq("<p>Tobi</p>\n");
expect($compile()).to.have.been.callCount(2);
expect($compile()).to.have.been.calledWithExactly(template, {...locals, cache: false});
});

it("should render the given content (by string - no cache)", async () => {
const {render, locals, $compile, template} = await getEngineFixture({token: EtaEngine});
await render();

expect(await render()).to.eq("<p>Tobi</p>\n");
expect($compile()).to.have.been.callCount(2);
expect($compile()).to.have.been.calledWithExactly(template, {...locals, cache: false});
});
it("should render the given content (by string - with cache)", async () => {
const {render, locals, $compile, template} = await getEngineFixture({token: EtaEngine, cache: true});
await render();

expect(await render()).to.eq("<p>Tobi</p>\n");
expect($compile()).to.have.been.callCount(2);
expect($compile()).to.have.been.calledWithExactly(template, {...locals, cache: true});
});
it("should render the given content (by file - no cache)", async () => {
const {renderFile, locals, $compileFile, path, template} = await getEngineFixture({token: EtaEngine, useTemplateName: true});

await renderFile({
views: dirname(path)
});

const content = await renderFile({
views: dirname(path)
});

expect(content).to.eq("<p>Tobi</p>\n");
expect($compileFile()).to.have.been.callCount(2);
expect($compileFile()).to.have.been.calledWithExactly("user", {
...locals,
partials: undefined,
filename: "user",
cache: false,
views: dirname(path)
});
});
it("should render the given content (by file - with cache)", async () => {
const {renderFile, locals, $compileFile, path, template} = await getEngineFixture({
token: EtaEngine,
useTemplateName: true,
cache: true
});

await renderFile({
views: dirname(path)
});
const content = await renderFile();

expect(content).to.eq("<p>Tobi</p>\n");
expect($compileFile()).to.have.been.callCount(1);
expect($compileFile()).to.have.been.calledWithExactly("user", {
...locals,
partials: undefined,
filename: "user",
cache: true,
views: dirname(path)
});
});
});
40 changes: 40 additions & 0 deletions packages/engines/src/components/EtaEngine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {ViewEngine} from "../decorators/viewEngine";
import {Engine, EngineOptions} from "./Engine";

@ViewEngine("eta", {
requires: "eta"
})
export class EtaEngine extends Engine {
protected cache = new Map();

protected getEngine(options: EngineOptions) {
const root = options.root || options.views;
const key = String(root || "default");

if (!this.cache.get(key)) {
const {Eta} = this.engine;

this.cache.set(
key,
new Eta({
...options,
views: root
})
);
}

return this.cache.get(key);
}

protected $compile(template: string, options: any) {
const eta = this.getEngine(options);

return () => eta.renderStringAsync(template, options);
}

protected $compileFile(file: string, options: EngineOptions): Promise<(options: EngineOptions) => Promise<string>> {
const eta = this.getEngine(options);

return Promise.resolve(() => eta.renderAsync(file, options));
}
}
1 change: 1 addition & 0 deletions packages/engines/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from "./components/DotEngine";
export * from "./components/DustEngine";
export * from "./components/EctEngine";
export * from "./components/EjsEngine";
export * from "./components/EtaEngine";
export * from "./components/Engine";
export * from "./components/HamlCoffeeEngine";
export * from "./components/HamlEngine";
Expand Down
1 change: 1 addition & 0 deletions packages/engines/test/fixtures/eta/user.eta
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p><%= it.user.name %></p>
5 changes: 3 additions & 2 deletions packages/engines/test/getEngineFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ interface EngineFixtureOptions {
token: string | typeof Engine;
cache?: boolean;
templateName?: string;
useTemplateName?: boolean;
}

export async function getEngineFixture({token, cache = false, templateName = "user"}: EngineFixtureOptions) {
export async function getEngineFixture({token, cache = false, useTemplateName = false, templateName = "user"}: EngineFixtureOptions) {
const engine = engines.get(token)!;

await engine.$onInit();
Expand Down Expand Up @@ -54,7 +55,7 @@ export async function getEngineFixture({token, cache = false, templateName = "us
});
},
renderFile(options: any = {}) {
return engine.renderFile(path, {
return engine.renderFile(useTemplateName ? templateName : path, {
cache,
...locals,
...options
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6532,6 +6532,7 @@ __metadata:
ect: "npm:^0.5.9"
ejs: "npm:^3.1.6"
eslint: "npm:^8.12.0"
eta: "npm:3.2.0"
filedirname: "npm:^2.7.0"
fs-extra: "npm:11.1.1"
haml: "npm:^0.4.3"
Expand Down Expand Up @@ -14965,6 +14966,13 @@ __metadata:
languageName: node
linkType: hard

"eta@npm:3.2.0":
version: 3.2.0
resolution: "eta@npm:3.2.0"
checksum: 10/f4aa8ff6b9a4adf3d5db83255793be0136bdbcf2341154ad64c019bacc04d69e10218e54764af572eb8ba016996b8cbac9dc7a6fc2e9eb5005a0323bab883692
languageName: node
linkType: hard

"eta@npm:^2.0.1":
version: 2.0.1
resolution: "eta@npm:2.0.1"
Expand Down

0 comments on commit b7ada12

Please sign in to comment.