feat: fix default templates fetch and allow local override
This commit is contained in:
parent
d33dd4539a
commit
64fe0ab3d7
|
@ -1,7 +1,7 @@
|
||||||
import { fromFileUrl } from '@std/path'
|
|
||||||
import type { JSX } from 'preact'
|
import type { JSX } from 'preact'
|
||||||
import { EnumType } from '@cliffy/command'
|
import { EnumType } from '@cliffy/command'
|
||||||
import type { Template } from '../types.ts'
|
import type { Template } from '../types.ts'
|
||||||
|
import * as defaultTemplates from '../templates/mod.ts'
|
||||||
|
|
||||||
export const templates: Map<
|
export const templates: Map<
|
||||||
string,
|
string,
|
||||||
|
@ -11,25 +11,36 @@ export const templates: Map<
|
||||||
>
|
>
|
||||||
> = new Map()
|
> = new Map()
|
||||||
|
|
||||||
const templatesDirUrl = import.meta.resolve('../templates')
|
//Load default templates
|
||||||
const templatesDir = fromFileUrl(templatesDirUrl)
|
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 (
|
for await (
|
||||||
const template of Deno.readDir(templatesDir)
|
const template of Deno.readDir(basePath)
|
||||||
) {
|
) {
|
||||||
if (
|
if (!template.isFile) continue
|
||||||
template.isFile &&
|
if (!template.name.endsWith('.tsx')) continue
|
||||||
template.name.endsWith('.tsx') &&
|
if (template.name.startsWith('_')) continue
|
||||||
!template.name.startsWith('_')
|
|
||||||
) {
|
const mod = await import(`${basePath}/${template.name}`).catch(checkFsErrors)
|
||||||
const modPath = new URL(
|
templates.set(mod.default.name, mod.default)
|
||||||
template.name,
|
}
|
||||||
`${import.meta.resolve('../templates')}/`,
|
|
||||||
|
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()])
|
export const templateType = new EnumType([...templates.keys()])
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
"imports": {
|
"imports": {
|
||||||
"@cliffy/command": "jsr:@cliffy/command@^1.0.0-rc.5",
|
"@cliffy/command": "jsr:@cliffy/command@^1.0.0-rc.5",
|
||||||
"@cliffy/prompt": "jsr:@cliffy/prompt@^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",
|
"@types/nodemailer": "npm:@types/nodemailer@^6.4.15",
|
||||||
"jsx-email": "npm:jsx-email@^1.10.12",
|
"jsx-email": "npm:jsx-email@^1.10.12",
|
||||||
"nodemailer": "npm:nodemailer@^6.9.13",
|
"nodemailer": "npm:nodemailer@^6.9.13",
|
||||||
|
|
Loading…
Reference in a new issue