47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
import { Command } from '@cliffy/command/mod.ts'
|
|
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,
|
|
})
|
|
})
|
|
},
|
|
)
|