This commit is contained in:
1 parent
5977b1b272
commit
09af3bd8c0
18 files changed
+57
-30
No files matched your search
@@ -0,0 +1,105 @@
|
||||
import { parse as parseToml } from "@std/toml";
|
||||
import nunjucks from "nunjucks";
|
||||
|
||||
export type TemplateVariables = Record<string, unknown>;
|
||||
|
||||
const engine = new nunjucks.Environment(undefined, {
|
||||
autoescape: false,
|
||||
throwOnUndefined: false,
|
||||
});
|
||||
|
||||
function valueFromCli(value: unknown): unknown {
|
||||
if (typeof value !== "string") return value;
|
||||
if (value === "true") return true;
|
||||
if (value === "false") return false;
|
||||
if (value === "null") return null;
|
||||
if (value.trim() !== "" && Number.isFinite(Number(value))) {
|
||||
return Number(value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function mergeObjects(...values: Array<unknown>): TemplateVariables {
|
||||
const result: TemplateVariables = {};
|
||||
for (const value of values) {
|
||||
if (value && typeof value === "object" && !Array.isArray(value)) {
|
||||
Object.assign(result, value);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function cliVariables(args: Record<string, unknown>): TemplateVariables {
|
||||
const reserved = new Set([
|
||||
"_",
|
||||
"config",
|
||||
"name",
|
||||
"vars-file",
|
||||
"vars_file",
|
||||
"v",
|
||||
"from",
|
||||
"local-cache",
|
||||
"just-local-cache",
|
||||
]);
|
||||
const variables: TemplateVariables = {};
|
||||
for (const [key, value] of Object.entries(args)) {
|
||||
if (!reserved.has(key) && !key.startsWith("_")) {
|
||||
variables[key] = valueFromCli(value);
|
||||
}
|
||||
}
|
||||
return variables;
|
||||
}
|
||||
|
||||
async function readVariablesFile(path: string): Promise<TemplateVariables> {
|
||||
const text = await Deno.readTextFile(path);
|
||||
const parsed = parseToml(text);
|
||||
return mergeObjects(parsed);
|
||||
}
|
||||
|
||||
function builtinVariables(config: Record<string, unknown>): TemplateVariables {
|
||||
const now = new Date();
|
||||
const iso = now.toISOString();
|
||||
return {
|
||||
now: iso,
|
||||
currentTime: iso,
|
||||
currentDate: iso.slice(0, 10),
|
||||
year: now.getFullYear(),
|
||||
month: now.getMonth() + 1,
|
||||
day: now.getDate(),
|
||||
weekday: now.toLocaleDateString("en-US", { weekday: "long" }),
|
||||
timestamp: now.getTime(),
|
||||
unixTimestamp: Math.floor(now.getTime() / 1000),
|
||||
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
||||
locale: Intl.DateTimeFormat().resolvedOptions().locale,
|
||||
platform: Deno.build.os,
|
||||
architecture: Deno.build.arch,
|
||||
runtime: `Deno ${Deno.version.deno}`,
|
||||
config,
|
||||
};
|
||||
}
|
||||
|
||||
export async function createTemplateVariables(
|
||||
args: Record<string, unknown> = {},
|
||||
config: Record<string, unknown> = {},
|
||||
configVariables: TemplateVariables = {},
|
||||
): Promise<TemplateVariables> {
|
||||
const journal = (config.journal ?? {}) as Record<string, unknown>;
|
||||
const varsFile = args["vars-file"] ?? args.vars_file ?? args.v;
|
||||
const fromFile = typeof varsFile === "string"
|
||||
? await readVariablesFile(varsFile)
|
||||
: {};
|
||||
return mergeObjects(
|
||||
builtinVariables(config),
|
||||
configVariables,
|
||||
journal.variables,
|
||||
fromFile,
|
||||
cliVariables(args),
|
||||
);
|
||||
}
|
||||
|
||||
export function renderTemplate(
|
||||
source: string,
|
||||
variables: TemplateVariables,
|
||||
): string {
|
||||
return engine.renderString(source, variables);
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
import { assertEquals } from "@std/assert";
|
||||
import { parse as parseToml } from "@std/toml";
|
||||
import { createTemplateVariables, renderTemplate } from "./engine.ts";
|
||||
|
||||
Deno.test("renders Nunjucks variables, conditionals, loops, and constants", () => {
|
||||
const result = renderTemplate(
|
||||
`{% set color = "red" %}{% if enabled %}{% for item in items %}{{ item }}:{{ color }};{% endfor %}{% endif %}`,
|
||||
{ enabled: true, items: ["a", "b"] },
|
||||
);
|
||||
assertEquals(result, "a:red;b:red;");
|
||||
});
|
||||
|
||||
Deno.test("parses TOML variables and applies CLI overrides", async () => {
|
||||
const path = await Deno.makeTempFile({ suffix: ".toml" });
|
||||
try {
|
||||
await Deno.writeTextFile(path, `title = "From TOML"\ncount = 2\n`);
|
||||
const variables = await createTemplateVariables({
|
||||
"vars-file": path,
|
||||
title: "From CLI",
|
||||
enabled: "false",
|
||||
}, { journal: { variables: { fallback: true } } });
|
||||
assertEquals(variables.title, "From CLI");
|
||||
assertEquals(variables.count, 2);
|
||||
assertEquals(variables.enabled, false);
|
||||
assertEquals(variables.fallback, true);
|
||||
assertEquals(typeof variables.currentDate, "string");
|
||||
} finally {
|
||||
await Deno.remove(path);
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("config TOML variables are included below per-setting and CLI values", async () => {
|
||||
const path = await Deno.makeTempFile({ suffix: ".toml" });
|
||||
try {
|
||||
await Deno.writeTextFile(path, `title = "From file"\n`);
|
||||
const variables = await createTemplateVariables(
|
||||
{
|
||||
"vars-file": path,
|
||||
title: "From CLI",
|
||||
},
|
||||
{ journal: { variables: { title: "From setting", setting: true } } },
|
||||
{
|
||||
title: "From config",
|
||||
global: true,
|
||||
},
|
||||
);
|
||||
assertEquals(variables.title, "From CLI");
|
||||
assertEquals(variables.global, true);
|
||||
assertEquals(variables.setting, true);
|
||||
} finally {
|
||||
await Deno.remove(path);
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("default TOML config contains build settings and vars tables", async () => {
|
||||
const config = parseToml(await Deno.readTextFile("./config.toml")) as {
|
||||
asset_base: string;
|
||||
settings: Array<{ name: string }>;
|
||||
vars: Record<string, unknown>;
|
||||
};
|
||||
assertEquals(config.asset_base, "../../assets");
|
||||
assertEquals(config.settings.map((setting) => setting.name), [
|
||||
"mobile",
|
||||
"computer",
|
||||
"printable",
|
||||
]);
|
||||
assertEquals(typeof config.vars, "object");
|
||||
});
|
||||
Reference in new issue
Block a user