| 1 | import Handlebars from "handlebars"; |
| 2 | import { v4 as uuidv4 } from "uuid"; |
| 3 | |
| 4 | function convertToLetter(num: number): string { |
| 5 | let result = ""; |
| 6 | while (num > 0) { |
| 7 | const remainder = (num - 1) % 26; |
| 8 | result = String.fromCharCode(97 + remainder) + result; |
| 9 | num = Math.floor((num - 1) / 26); |
| 10 | } |
| 11 | return result; |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * We replace filepaths with alphabetic characters to handle |
| 16 | * escaping issues. |
| 17 | */ |
| 18 | const replaceFilepaths = ( |
| 19 | value: string, |
| 20 | ctxProviderNames: string[], |
| 21 | ): [string, { [key: string]: string }] => { |
| 22 | const ast = Handlebars.parse(value); |
| 23 | |
| 24 | const keysToFilepath: { [key: string]: string } = {}; |
| 25 | |
| 26 | let keyIndex = 1; |
| 27 | |
| 28 | for (const i in ast.body) { |
| 29 | const node = ast.body[i] as any; |
| 30 | |
| 31 | if (node.type === "MustacheStatement") { |
| 32 | const originalNodeVal = node.path.original; |
| 33 | const isFilepath = !ctxProviderNames.includes(originalNodeVal); |
| 34 | |
| 35 | if (isFilepath) { |
| 36 | const letter = convertToLetter(keyIndex); |
| 37 | |
| 38 | keysToFilepath[letter] = originalNodeVal; |
| 39 | |
| 40 | value = value.replace( |
| 41 | new RegExp(`{{\\s*${originalNodeVal}\\s*}}`), |
| 42 | `{{${letter}}}`, |
| 43 | ); |
| 44 | |
| 45 | keyIndex++; |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | return [value, keysToFilepath]; |
| 51 | }; |
| 52 | |
| 53 | export function registerHelpers( |
| 54 | helpers: Array<[string, Handlebars.HelperDelegate]>, |
| 55 | ): { |
| 56 | [key: string]: Promise<string>; |
| 57 | } { |
| 58 | const promises: { [key: string]: Promise<string> } = {}; |
| 59 | |
| 60 | for (const [name, helper] of helpers) { |
| 61 | Handlebars.registerHelper(name, (...args) => { |
| 62 | const id = uuidv4(); |
| 63 | promises[id] = helper(...args); |
| 64 | return `__${id}__`; |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | return promises; |
| 69 | } |
| 70 | |
| 71 | export async function prepareTemplateAndData( |
| 72 | template: string, |
| 73 | readFile: (filepath: string) => Promise<string>, |
| 74 | inputData: Record<string, string>, |
| 75 | ctxProviderNames: string[], |
| 76 | ): Promise<[string, any]> { |
| 77 | const [newTemplate, vars] = replaceFilepaths(template, ctxProviderNames); |
| 78 | |
| 79 | const data: any = { ...inputData }; |
| 80 | |
| 81 | for (const [replacedName, originalCtxProviderName] of Object.entries(vars)) { |
| 82 | data[replacedName] = |
| 83 | inputData[originalCtxProviderName] ?? originalCtxProviderName; |
| 84 | |
| 85 | // If it's not a context provider, assume it's a filepath. |
| 86 | if (!ctxProviderNames.includes(originalCtxProviderName)) { |
| 87 | const fileContents = await readFile(vars[replacedName]); |
| 88 | |
| 89 | if (fileContents) { |
| 90 | data[replacedName] = fileContents; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return [newTemplate, data]; |
| 96 | } |
| 97 | |
| 98 | export function compileAndRenderTemplate( |
| 99 | template: string, |
| 100 | data: Record<string, string>, |
| 101 | ): string { |
| 102 | const templateFn = Handlebars.compile(template); |
| 103 | return templateFn(data); |
| 104 | } |
| 105 | |
| 106 | export async function resolveHelperPromises( |
| 107 | renderedString: string, |
| 108 | promises: { [key: string]: Promise<string> }, |
| 109 | ): Promise<string> { |
| 110 | await Promise.all(Object.values(promises)); |
| 111 | |
| 112 | for (const id in promises) { |
| 113 | renderedString = renderedString.replace(`__${id}__`, await promises[id]); |
| 114 | } |
| 115 | |
| 116 | return renderedString; |
| 117 | } |