refactor(cli): extract template loading to own module

This commit is contained in:
Julien Oculi 2024-04-03 17:19:36 +02:00
parent 4ee9759ee0
commit 36eb0f35e8
2 changed files with 38 additions and 35 deletions

35
cli/_templates_loader.ts Normal file
View file

@ -0,0 +1,35 @@
import { fromFileUrl } from '@std/path'
import type { JSX } from 'preact'
import { EnumType } from '@cliffy/command/mod.ts'
import type { Template } from '../types.ts'
export const templates: Map<
string,
Template<
(props: Record<string, unknown>) => JSX.Element,
Record<string, unknown>
>
> = new Map()
const templatesDirUrl = import.meta.resolve('../templates')
const templatesDir = fromFileUrl(templatesDirUrl)
//Load templates dynamicaly
for await (
const template of Deno.readDir(templatesDir)
) {
if (
template.isFile &&
template.name.endsWith('.tsx') &&
!template.name.startsWith('_')
) {
const modPath = new URL(
template.name,
`${import.meta.resolve('../templates')}/`,
)
const mod = await import(modPath.href)
templates.set(mod.default.name, mod.default)
}
}
export const templateType = new EnumType([...templates.keys()])

View file

@ -1,45 +1,13 @@
import { Command, EnumType } from '@cliffy/command/mod.ts'
import { Command } from '@cliffy/command/mod.ts'
import { Input } from '@cliffy/prompt/mod.ts'
import { Contact } from '../src/contact.ts'
import { send } from '../src/send.ts'
import type { Mail, Template } from '../types.ts'
import type { JSX } from 'preact'
import { fromFileUrl } from '@std/path'
const templates: Map<
string,
Template<
(props: Record<string, unknown>) => JSX.Element,
Record<string, unknown>
>
> = new Map()
const templatesDirUrl = import.meta.resolve('../templates')
const templatesDir = fromFileUrl(templatesDirUrl)
//Load templates dynamicaly
for await (
const template of Deno.readDir(templatesDir)
) {
if (
template.isFile &&
template.name.endsWith('.tsx') &&
!template.name.startsWith('_')
) {
const modPath = new URL(
template.name,
`${import.meta.resolve('../templates')}/`,
)
const mod = await import(modPath.href)
templates.set(mod.default.name, mod.default)
}
}
import type { Mail } from '../types.ts'
import { templates, templateType } from './_templates_loader.ts'
//TODO completions for "--from"
//TODO require sudo for "--from !== !me"
const templateType = new EnumType([...templates.keys()])
export const cmd = new Command()
.name('send')
.description('Send a mail.')