From 36eb0f35e8552810720bfaa9a81a3c6a5e9dddfb Mon Sep 17 00:00:00 2001 From: Julien Oculi Date: Wed, 3 Apr 2024 17:19:36 +0200 Subject: [PATCH] refactor(cli): extract template loading to own module --- cli/_templates_loader.ts | 35 +++++++++++++++++++++++++++++++++++ cli/send.ts | 38 +++----------------------------------- 2 files changed, 38 insertions(+), 35 deletions(-) create mode 100644 cli/_templates_loader.ts diff --git a/cli/_templates_loader.ts b/cli/_templates_loader.ts new file mode 100644 index 0000000..231a5b3 --- /dev/null +++ b/cli/_templates_loader.ts @@ -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) => JSX.Element, + Record + > +> = 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()]) diff --git a/cli/send.ts b/cli/send.ts index 7c69354..5f9af98 100644 --- a/cli/send.ts +++ b/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) => JSX.Element, - Record - > -> = 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.')