aboutsummaryrefslogtreecommitdiff
path: root/src/templates.ts
blob: 5458a36c23c91a586861409920b3a875edd7112f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import handlebars from "handlebars";
import fs from "fs";

export interface Template {
  inputFile: string;
  outputFile: string;
  context: object;
  data: string | undefined;
}

export default (_template: Template): Promise<Template> =>
  fs.promises
    .readFile(_template.inputFile, "utf-8")
    .then(buffer => {
      const translate = handlebars.compile(buffer);

      return {
        ..._template,
        data: translate(_template.context)
      };
    })
    .then(template =>
      fs.promises
        .writeFile(template.outputFile, template.data)
        .then(() => template)
    );