refactor(cli): extract template loading to own module
This commit is contained in:
parent
4ee9759ee0
commit
36eb0f35e8
35
cli/_templates_loader.ts
Normal file
35
cli/_templates_loader.ts
Normal 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()])
|
38
cli/send.ts
38
cli/send.ts
|
@ -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.')
|
||||
|
|
Loading…
Reference in a new issue