106 lines
2.9 KiB
TypeScript
106 lines
2.9 KiB
TypeScript
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);
|
|
}
|