Browse Source

Simplify and generalize template processing

"For the unit tests. For the unit tests!"
trunk
Kevin Hoerr 1 year ago
parent
commit
91efa5ab51
Signed by: kjhoerr GPG Key ID: 78E4BD33ACC22C86
5 changed files with 91 additions and 26 deletions
  1. +0
    -0
      example_reports/blank.template
  2. +1
    -0
      example_reports/ex.template
  3. +22
    -1
      src/index.ts
  4. +47
    -0
      src/templates.test.ts
  5. +21
    -25
      src/templates.ts

+ 0
- 0
example_reports/blank.template View File


+ 1
- 0
example_reports/ex.template View File

@ -0,0 +1 @@
But what is {{that}} other than {{potential}}?

+ 22
- 1
src/index.ts View File

@ -9,6 +9,7 @@ import expressWinston from "express-winston";
dotenv.config();
import processTemplate, { Template } from "./templates";
import routes from "./routes";
import Metadata from "./metadata";
import loggerConfig from "./util/logger";
@ -17,6 +18,7 @@ import { configOrError, handleShutdown } from "./util/config";
// Start-up configuration
const BIND_ADDRESS = process.env.BIND_ADDRESS || "localhost";
const PORT = Number(process.env.PORT || 3000);
const TARGET_URL = process.env.TARGET_URL || "http://localhost:3000";
const logger = winston.createLogger(loggerConfig("ROOT"));
@ -31,7 +33,26 @@ if (!path.isAbsolute(HOST_DIR)) {
}
// prepare template files
require("./templates");
const bashTemplate = {
inputFile: path.join(__dirname, "..", "public", "bash.template"),
outputFile: path.join(HOST_DIR, "bash"),
context: { TARGET_URL }
} as Template;
processTemplate(bashTemplate)
.then(template => {
logger.debug("Generated '%s' from template file", template.outputFile);
})
.catch(err => {
logger.error("Unable to process template file: %s", err);
// if the output file exists, then we are fine with continuing without
return fs.promises.access(bashTemplate.outputFile, fs.constants.R_OK);
})
.catch(err => {
logger.error("Cannot proceed: %s", err);
process.exit(1);
});
new MongoClient(MONGO_URI, { useUnifiedTopology: true }).connect(
(err, mongo) => {


+ 47
- 0
src/templates.test.ts View File

@ -0,0 +1,47 @@
import processTemplate, { Template } from "./templates";
import path from "path";
import fs from "fs";
const genTemplate = (filename: string): Template =>
({
inputFile: path.join(__dirname, "..", "example_reports", filename),
outputFile: path.join(
__dirname,
"..",
"build",
filename.replace(/template/, "txt")
),
context: { that: "this", potential: "resolved" }
} as Template);
describe("processTemplate", () => {
beforeAll(() =>
fs.promises.mkdir(path.join(__dirname, "..", "build")).catch(() => null)
);
it("should process the template file with the given context", async () => {
// Arrange
const template = genTemplate("ex.template");
// Act
const result = await processTemplate(template);
// Assert
expect(result.data).not.toBeNull();
expect(result.data).toEqual("But what is this other than resolved?");
expect(fs.existsSync(result.outputFile)).toEqual(true);
});
it("should process the blank file", async () => {
// Arrange
const template = genTemplate("blank.template");
// Act
const result = await processTemplate(template);
// Assert
expect(result.data).not.toBeNull();
expect(result.data).toEqual("");
expect(fs.existsSync(result.outputFile)).toEqual(true);
});
});

+ 21
- 25
src/templates.ts View File

@ -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)
);

Loading…
Cancel
Save