mailer/cli/preview.ts

47 lines
1.2 KiB
TypeScript
Raw Permalink Normal View History

2024-07-15 12:10:57 +02:00
import { Command } from '@cliffy/command'
2024-04-03 17:56:38 +02:00
import { renderTemplate } from '../src/template.tsx'
import { templates, templateType } from './_templates_loader.ts'
import { promptProps } from './_prompt_template.ts'
export const cmd = new Command()
.name('preview')
.description('Preview a mail.')
.type('template', templateType)
.option(
'-p, --port <number:number>',
'Port to serve preview, default random free port.',
)
.option('-t, --template <name:template>', 'HTML template from config', {
default: 'message',
})
.action(
async ({ template, port }) => {
const selectedTemplate = templates.get(template)!
const props = await promptProps(selectedTemplate)
const body = selectedTemplate.builder(props)!
const { html, text } = await renderTemplate(body)
Deno.serve({ port }, (req) => {
const url = new URL(req.url)
if (url.pathname === '/') {
return new Response(html, {
headers: {
'Content-Type': 'text/html; charset=utf-8',
},
})
}
if (url.pathname === '/text') {
return new Response(text, {
headers: {
'Content-Type': 'text/plain; charset=utf-8',
},
})
}
return new Response('Not found', {
status: 404,
})
})
},
)