Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Allow default template in template engines.
- Loading branch information
1 parent
e43b579
commit 2882431
Showing
15 changed files
with
231 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import type { TemplateDelegate } from 'handlebars'; | ||
import { compile } from 'handlebars'; | ||
import type { TemplateEngine, TemplateOptions } from './TemplateEngine'; | ||
import { readTemplate } from './TemplateEngine'; | ||
|
||
/** | ||
* Fills in Handlebars templates. | ||
*/ | ||
export class HandlebarsTemplateEngine implements TemplateEngine { | ||
private readonly applyTemplate: Promise<TemplateDelegate>; | ||
|
||
public constructor(defaultTemplate?: TemplateOptions) { | ||
this.applyTemplate = readTemplate(defaultTemplate) | ||
.then((template: string): TemplateDelegate => compile(template)); | ||
} | ||
|
||
public async render(contents: NodeJS.Dict<string>, template?: TemplateOptions): Promise<string> { | ||
const applyTemplate = template ? compile(await readTemplate(template)) : await this.applyTemplate; | ||
return applyTemplate(contents); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { promises as fsPromises } from 'fs'; | ||
import { joinFilePath, resolveAssetPath } from '../../util/PathUtil'; | ||
import Dict = NodeJS.Dict; | ||
|
||
export interface TemplateOptions { | ||
// String contents of the template | ||
template?: string; | ||
// Name of the template file | ||
templateFile?: string; | ||
// Path of the template file | ||
templatePath?: string; | ||
} | ||
|
||
/** | ||
* A template engine renders content into a template. | ||
*/ | ||
export interface TemplateEngine { | ||
/** | ||
* Renders the given contents into the template. | ||
* | ||
* @param contents - The contents to render. | ||
* @param template - The template string to use for rendering; | ||
* if omitted, a default template is used. | ||
* @returns The rendered contents. | ||
*/ | ||
render: (contents: Dict<string>, template?: TemplateOptions) => Promise<string>; | ||
} | ||
|
||
/** | ||
* Reads the template and returns it as a string. | ||
*/ | ||
export async function readTemplate({ template, templateFile, templatePath }: TemplateOptions = {}): Promise<string> { | ||
// The template has already been given as a string | ||
if (typeof template === 'string') { | ||
return template; | ||
} | ||
// The template needs to be read from disk | ||
if (typeof templateFile === 'string') { | ||
const fullTemplatePath = templatePath ? joinFilePath(templatePath, templateFile) : templateFile; | ||
return fsPromises.readFile(resolveAssetPath(fullTemplatePath), 'utf8'); | ||
} | ||
// No template specified | ||
return ''; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.