feat(cli): add mail preview command

This commit is contained in:
Julien Oculi 2024-04-03 17:56:38 +02:00
parent 55a1ca7e9d
commit 2a38c88716
4 changed files with 63 additions and 0 deletions

2
cli.ts
View file

@ -1,11 +1,13 @@
import config from './deno.json' with { type: 'json' } import config from './deno.json' with { type: 'json' }
import { Command } from '@cliffy/command/command.ts' import { Command } from '@cliffy/command/command.ts'
import { cmd as send } from './cli/send.ts' import { cmd as send } from './cli/send.ts'
import { cmd as preview } from './cli/preview.ts'
const cli = new Command() const cli = new Command()
.name('cohamail') .name('cohamail')
.description('Mail cli for coh@bit.') .description('Mail cli for coh@bit.')
.version(config.version) .version(config.version)
.command('preview', preview)
.command('send', send) .command('send', send)
if (import.meta.main) { if (import.meta.main) {

46
cli/preview.ts Normal file
View file

@ -0,0 +1,46 @@
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,
})
})
},
)

View file

@ -18,6 +18,7 @@
}, },
"imports": { "imports": {
"@cliffy/": "https://deno.land/x/cliffy@v1.0.0-rc.3/", "@cliffy/": "https://deno.land/x/cliffy@v1.0.0-rc.3/",
"@std/path": "jsr:@std/path@^0.221.0",
"jsx-email": "npm:jsx-email@^1.10.12", "jsx-email": "npm:jsx-email@^1.10.12",
"nodemailer": "npm:nodemailer@^6.9.13", "nodemailer": "npm:nodemailer@^6.9.13",
"nodemailer-smime": "npm:nodemailer-smime@^1.1.0", "nodemailer-smime": "npm:nodemailer-smime@^1.1.0",

View file

@ -2,6 +2,8 @@
"version": "3", "version": "3",
"packages": { "packages": {
"specifiers": { "specifiers": {
"jsr:@std/assert@^0.221.0": "jsr:@std/assert@0.221.0",
"jsr:@std/path@^0.221.0": "jsr:@std/path@0.221.0",
"npm:@types/nodemailer": "npm:@types/nodemailer@6.4.14", "npm:@types/nodemailer": "npm:@types/nodemailer@6.4.14",
"npm:html-to-md": "npm:html-to-md@0.8.5", "npm:html-to-md": "npm:html-to-md@0.8.5",
"npm:jsx-email@^1.10.12": "npm:jsx-email@1.10.12_@jsx-email+app-preview@1.2.5__react@18.2.0__react-dom@18.2.0___react@18.2.0__vite@4.5.3_react@18.2.0_vite@4.5.3_postcss@8.4.38", "npm:jsx-email@^1.10.12": "npm:jsx-email@1.10.12_@jsx-email+app-preview@1.2.5__react@18.2.0__react-dom@18.2.0___react@18.2.0__vite@4.5.3_react@18.2.0_vite@4.5.3_postcss@8.4.38",
@ -14,6 +16,17 @@
"npm:turndown": "npm:turndown@7.1.3", "npm:turndown": "npm:turndown@7.1.3",
"npm:turndown@^7.1.3": "npm:turndown@7.1.3" "npm:turndown@^7.1.3": "npm:turndown@7.1.3"
}, },
"jsr": {
"@std/assert@0.221.0": {
"integrity": "a5f1aa6e7909dbea271754fd4ab3f4e687aeff4873b4cef9a320af813adb489a"
},
"@std/path@0.221.0": {
"integrity": "0a36f6b17314ef653a3a1649740cc8db51b25a133ecfe838f20b79a56ebe0095",
"dependencies": [
"jsr:@std/assert@^0.221.0"
]
}
},
"npm": { "npm": {
"@alloc/quick-lru@5.2.0": { "@alloc/quick-lru@5.2.0": {
"integrity": "sha512-UrcABB+4bUrFABwbluTIBErXwvbsU/V7TZWfmbgJfbkwiBuziS9gxdODUyuiecfdGQ85jglMW6juS3+z5TsKLw==", "integrity": "sha512-UrcABB+4bUrFABwbluTIBErXwvbsU/V7TZWfmbgJfbkwiBuziS9gxdODUyuiecfdGQ85jglMW6juS3+z5TsKLw==",
@ -3966,6 +3979,7 @@
}, },
"workspace": { "workspace": {
"dependencies": [ "dependencies": [
"jsr:@std/path@^0.221.0",
"npm:jsx-email@^1.10.12", "npm:jsx-email@^1.10.12",
"npm:nodemailer-smime@^1.1.0", "npm:nodemailer-smime@^1.1.0",
"npm:nodemailer@^6.9.13", "npm:nodemailer@^6.9.13",