Skip to content

Commit

Permalink
feat: enable support for default extensions (#266)
Browse files Browse the repository at this point in the history
* Fixes for #265 - Enable support for default extensions.

Added unit test coverage to the two new possibilities and it works for existing .eta formats as well.

* Removed .bak file generated from a diff tool
  • Loading branch information
gettoarun authored Nov 30, 2023
1 parent a160d23 commit 8054ffc
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ export interface EtaConfig {

/** Directory that contains templates */
views?: string;

/** Control template file extension defaults. Default `.eta` */
defaultExtension?: string;
}

/* END TYPES */
Expand All @@ -95,6 +98,7 @@ const defaultConfig: EtaConfig = {
tags: ["<%", "%>"],
useWith: false,
varName: "it",
defaultExtension: ".eta",
};

export { defaultConfig };
3 changes: 2 additions & 1 deletion src/file-handling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export function resolvePath(
}

const baseFilePath = options && options.filepath;
const defaultExtension = this.config.defaultExtension === undefined ? ".eta" : this.config.defaultExtension;

// how we index cached template paths
const cacheIndex = JSON.stringify({
Expand All @@ -48,7 +49,7 @@ export function resolvePath(
views: this.config.views,
});

templatePath += path.extname(templatePath) ? "" : ".eta";
templatePath += path.extname(templatePath) ? "" : defaultExtension;

// if the file was included from another template
if (baseFilePath) {
Expand Down
18 changes: 18 additions & 0 deletions test/file-handling.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,21 @@ describe("file handling errors", () => {
}).toThrow();
});
});

describe("filepath default extension tests", () => {
console.log("Templates are in ", viewsDir)

it("works with defaultExtension", async () => {
const eta = new Eta({ views: viewsDir, cache: true, defaultExtension: ".tmpl" });
const templateResult = await eta.render("template-extn", { name: "TMPL Run" });

expect(templateResult).toEqual(`Hi TMPL Run`);
});

it("works with no extension", async () => {
const eta = new Eta({ views: viewsDir, cache: true, defaultExtension: "" });
const templateResult = await eta.render("template-without-extn", { name: "TMPL Run" });

expect(templateResult).toEqual(`Hi TMPL Run`);
});
})
1 change: 1 addition & 0 deletions test/templates/template-extn.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hi <%= it.name %>
1 change: 1 addition & 0 deletions test/templates/template-without-extn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hi <%= it.name %>

0 comments on commit 8054ffc

Please sign in to comment.