feat: fix default templates fetch and allow local override

This commit is contained in:
Julien Oculi 2025-02-20 16:06:23 +01:00
parent d33dd4539a
commit 64fe0ab3d7
3 changed files with 3835 additions and 4074 deletions

View file

@ -1,7 +1,7 @@
import { fromFileUrl } from '@std/path'
import type { JSX } from 'preact'
import { EnumType } from '@cliffy/command'
import type { Template } from '../types.ts'
import * as defaultTemplates from '../templates/mod.ts'
export const templates: Map<
string,
@ -11,25 +11,36 @@ export const templates: Map<
>
> = new Map()
const templatesDirUrl = import.meta.resolve('../templates')
const templatesDir = fromFileUrl(templatesDirUrl)
//Load default templates
for (const template of Object.values(defaultTemplates)) {
//@ts-expect-error types are checked at runtime later
templates.set(template.name, template)
}
//Load local templates
const basePath = '/etc/cohabit/mailer'
//Load templates dynamicaly
for await (
const template of Deno.readDir(templatesDir)
const template of Deno.readDir(basePath)
) {
if (
template.isFile &&
template.name.endsWith('.tsx') &&
!template.name.startsWith('_')
) {
const modPath = new URL(
template.name,
`${import.meta.resolve('../templates')}/`,
if (!template.isFile) continue
if (!template.name.endsWith('.tsx')) continue
if (template.name.startsWith('_')) continue
const mod = await import(`${basePath}/${template.name}`).catch(checkFsErrors)
templates.set(mod.default.name, mod.default)
}
function checkFsErrors(error: Error) {
if (error instanceof Deno.errors.PermissionDenied) {
throw new Error(
'unable to load config file due to read access permissions issues',
{
cause: error,
},
)
const mod = await import(modPath.href)
templates.set(mod.default.name, mod.default)
}
throw error
}
export const templateType = new EnumType([...templates.keys()])

View file

@ -19,7 +19,7 @@
"imports": {
"@cliffy/command": "jsr:@cliffy/command@^1.0.0-rc.5",
"@cliffy/prompt": "jsr:@cliffy/prompt@^1.0.0-rc.5",
"@std/path": "jsr:@std/path@^0.221.0",
"@std/json": "jsr:@std/json@^1.0.1",
"@types/nodemailer": "npm:@types/nodemailer@^6.4.15",
"jsx-email": "npm:jsx-email@^1.10.12",
"nodemailer": "npm:nodemailer@^6.9.13",

7866
deno.lock

File diff suppressed because it is too large Load diff