Moved the NodeJS version of the application to the app/ directory.
This commit is contained in:
41
app/backend/src/template-service.ts
Normal file
41
app/backend/src/template-service.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { existsSync, readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import nunjucks from "nunjucks";
|
||||
import type { ImageSelection, UsageTemplate } from "./types";
|
||||
|
||||
const findAppNewRoot = (): string => {
|
||||
const candidates = [join(__dirname, "../../.."), join(__dirname, "../..")];
|
||||
|
||||
for (const candidate of candidates) {
|
||||
if (existsSync(join(candidate, "templates.json"))) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error("Unable to resolve app-new template root");
|
||||
};
|
||||
|
||||
export class TemplateService {
|
||||
private readonly appNewRoot = findAppNewRoot();
|
||||
|
||||
private readonly env = nunjucks.configure(join(this.appNewRoot, "templates"), {
|
||||
autoescape: false,
|
||||
noCache: true
|
||||
});
|
||||
|
||||
private readonly templates: UsageTemplate[] = JSON.parse(
|
||||
readFileSync(join(this.appNewRoot, "templates.json"), "utf8")
|
||||
) as UsageTemplate[];
|
||||
|
||||
public getTemplates(): UsageTemplate[] {
|
||||
return this.templates;
|
||||
}
|
||||
|
||||
public render(templateFile: string, selection: ImageSelection): string {
|
||||
return this.env.render(templateFile, selection);
|
||||
}
|
||||
|
||||
public buildSkuExport(skus: string[]): string {
|
||||
return `[\n${skus.map((sku) => `\t\"${sku}\"`).join(",\n")}\n]`;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user