diff options
| author | Kevin J Hoerr <kjhoerr@protonmail.com> | 2019-12-12 14:30:58 -0500 |
|---|---|---|
| committer | Kevin J Hoerr <kjhoerr@protonmail.com> | 2019-12-12 14:30:58 -0500 |
| commit | 91efa5ab51904667eaf3353008753ea99bf5b17d (patch) | |
| tree | 91e6b26f5e7b27c6a2bb22c1c5388afc8edec5d7 /src/templates.ts | |
| parent | 0d810aa92e9b54493a2e075c3bb497b3857a2119 (diff) | |
| download | ao-coverage-91efa5ab51904667eaf3353008753ea99bf5b17d.tar.gz ao-coverage-91efa5ab51904667eaf3353008753ea99bf5b17d.tar.bz2 ao-coverage-91efa5ab51904667eaf3353008753ea99bf5b17d.zip | |
Simplify and generalize template processing
"For the unit tests. For the unit tests!"
Diffstat (limited to 'src/templates.ts')
| -rw-r--r-- | src/templates.ts | 46 |
1 files changed, 21 insertions, 25 deletions
diff --git a/src/templates.ts b/src/templates.ts index 2ffd0dd..5458a36 100644 --- a/src/templates.ts +++ b/src/templates.ts @@ -1,30 +1,26 @@ -import winston from "winston"; import handlebars from "handlebars"; -import path from "path"; import fs from "fs"; -import loggerConfig from "./util/logger"; -import { configOrError } from "./util/config"; +export interface Template { + inputFile: string; + outputFile: string; + context: object; + data: string | undefined; +} -const logger = winston.createLogger(loggerConfig("TEMPLATE")); +export default (_template: Template): Promise<Template> => + fs.promises + .readFile(_template.inputFile, "utf-8") + .then(buffer => { + const translate = handlebars.compile(buffer); -const HOST_DIR = configOrError("HOST_DIR"); -const TARGET_URL = process.env.TARGET_URL || "http://localhost:3000"; - -fs.promises - .readFile(path.join(__dirname, "..", "public", "bash.template"), "utf-8") - .then(buffer => { - const translate = handlebars.compile(buffer); - - return { - name: "bash", - data: translate({ TARGET_URL }) - }; - }) - .then(file => - fs.promises - .writeFile(path.join(HOST_DIR, file.name), file.data) - .then(() => file) - ) - .then(file => logger.debug("Generated '%s' from template file", file.name)) - .catch(err => logger.error("Error while generating template file: %s", err)); + return { + ..._template, + data: translate(_template.context) + }; + }) + .then(template => + fs.promises + .writeFile(template.outputFile, template.data) + .then(() => template) + ); |
