From 2a38c887167014c8f134df6bed544d3b589f15b6 Mon Sep 17 00:00:00 2001 From: Julien Oculi Date: Wed, 3 Apr 2024 17:56:38 +0200 Subject: [PATCH] feat(cli): add mail `preview` command --- cli.ts | 2 ++ cli/preview.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++++ deno.json | 1 + deno.lock | 14 ++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 cli/preview.ts diff --git a/cli.ts b/cli.ts index c406c4a..3901998 100644 --- a/cli.ts +++ b/cli.ts @@ -1,11 +1,13 @@ import config from './deno.json' with { type: 'json' } import { Command } from '@cliffy/command/command.ts' import { cmd as send } from './cli/send.ts' +import { cmd as preview } from './cli/preview.ts' const cli = new Command() .name('cohamail') .description('Mail cli for coh@bit.') .version(config.version) + .command('preview', preview) .command('send', send) if (import.meta.main) { diff --git a/cli/preview.ts b/cli/preview.ts new file mode 100644 index 0000000..f08b2be --- /dev/null +++ b/cli/preview.ts @@ -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 ', + 'Port to serve preview, default random free port.', + ) + .option('-t, --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, + }) + }) + }, + ) diff --git a/deno.json b/deno.json index e61baad..0a9319a 100644 --- a/deno.json +++ b/deno.json @@ -18,6 +18,7 @@ }, "imports": { "@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", "nodemailer": "npm:nodemailer@^6.9.13", "nodemailer-smime": "npm:nodemailer-smime@^1.1.0", diff --git a/deno.lock b/deno.lock index 4901d62..9b669cc 100644 --- a/deno.lock +++ b/deno.lock @@ -2,6 +2,8 @@ "version": "3", "packages": { "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: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", @@ -14,6 +16,17 @@ "npm:turndown": "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": { "@alloc/quick-lru@5.2.0": { "integrity": "sha512-UrcABB+4bUrFABwbluTIBErXwvbsU/V7TZWfmbgJfbkwiBuziS9gxdODUyuiecfdGQ85jglMW6juS3+z5TsKLw==", @@ -3966,6 +3979,7 @@ }, "workspace": { "dependencies": [ + "jsr:@std/path@^0.221.0", "npm:jsx-email@^1.10.12", "npm:nodemailer-smime@^1.1.0", "npm:nodemailer@^6.9.13",