openapi-typescript-codegen/src/utils/readHandlebarsTemplate.ts
2019-11-21 11:06:23 +01:00

32 lines
968 B
TypeScript

import * as fs from 'fs';
import * as Handlebars from 'handlebars';
/**
* Read and compile the Handlebars template.
* @param filePath
*/
export function readHandlebarsTemplate(filePath: string): Handlebars.TemplateDelegate {
if (fs.existsSync(filePath)) {
try {
const template = fs
.readFileSync(filePath, 'utf8')
.toString()
.trim();
return Handlebars.compile(template, {
strict: true,
noEscape: true,
preventIndent: true,
ignoreStandalone: true,
knownHelpersOnly: true,
knownHelpers: {
indent: true,
eq: true,
},
});
} catch (e) {
throw new Error(`Could not compile Handlebar template: "${filePath}"`);
}
}
throw new Error(`Could not find Handlebar template: "${filePath}"`);
}