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 { Input } from '@cliffy/prompt/mod.ts'
import { Contact } from '../src/contact.ts' import { Contact } from '../src/contact.ts'
import { send } from '../src/send.ts' import { send } from '../src/send.ts'
import type { Mail, Template } from '../types.ts' import type { Mail } from '../types.ts'
import type { JSX } from 'preact' import { templates, templateType } from './_templates_loader.ts'
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)
}
}
//TODO completions for "--from" //TODO completions for "--from"
//TODO require sudo for "--from !== !me" //TODO require sudo for "--from !== !me"
const templateType = new EnumType([...templates.keys()])
export const cmd = new Command() export const cmd = new Command()
.name('send') .name('send')
.description('Send a mail.') .description('Send a mail.')