|
| 1 | +import { findProject, resolveProjectId, API, updateProject, loadKeyStore, saveKeyStore } from "./config.mjs"; |
| 2 | + |
| 3 | +const HELP = `run402 email — Send template-based emails from your project |
| 4 | +
|
| 5 | +Usage: |
| 6 | + run402 email <subcommand> [args...] |
| 7 | +
|
| 8 | +Subcommands: |
| 9 | + create <slug> [--project <id>] Create a mailbox (<slug>@mail.run402.com) |
| 10 | + send --template <name> --to <email> [--var key=value ...] [--project <id>] |
| 11 | + Send a template email |
| 12 | + list [--project <id>] List sent emails |
| 13 | + get <message_id> [--project <id>] Get a message with replies |
| 14 | +
|
| 15 | +Templates: |
| 16 | + project_invite — requires --var project_name=... --var invite_url=... |
| 17 | + magic_link — requires --var project_name=... --var link_url=... --var expires_in=... |
| 18 | + notification — requires --var project_name=... --var message=... (max 500 chars) |
| 19 | +
|
| 20 | +Examples: |
| 21 | + run402 email create my-app |
| 22 | + run402 email send --template project_invite --to user@example.com \\ |
| 23 | + --var project_name="My App" --var invite_url="https://example.com/invite/abc" |
| 24 | + run402 email send --template notification --to admin@example.com \\ |
| 25 | + --var project_name="My App" --var message="Deploy complete" |
| 26 | + run402 email list |
| 27 | + run402 email get msg_abc123 |
| 28 | +
|
| 29 | +Notes: |
| 30 | + - One mailbox per project |
| 31 | + - Single recipient per send (no CC/BCC) |
| 32 | + - Slug: 3-63 chars, lowercase alphanumeric + hyphens, no consecutive hyphens |
| 33 | + - Rate limits vary by tier (prototype: 10/day, hobby: 50/day, team: 200/day) |
| 34 | + - --project defaults to the active project |
| 35 | +`; |
| 36 | + |
| 37 | +const SLUG_RE = /^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/; |
| 38 | + |
| 39 | +function parseFlag(args, flag) { |
| 40 | + for (let i = 0; i < args.length; i++) { |
| 41 | + if (args[i] === flag && args[i + 1]) return args[i + 1]; |
| 42 | + } |
| 43 | + return null; |
| 44 | +} |
| 45 | + |
| 46 | +function parseVars(args) { |
| 47 | + const vars = {}; |
| 48 | + for (let i = 0; i < args.length; i++) { |
| 49 | + if (args[i] === "--var" && args[i + 1]) { |
| 50 | + const raw = args[++i]; |
| 51 | + const eq = raw.indexOf("="); |
| 52 | + if (eq > 0) { |
| 53 | + vars[raw.slice(0, eq)] = raw.slice(eq + 1); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + return vars; |
| 58 | +} |
| 59 | + |
| 60 | +async function resolveMailboxId(projectId, serviceKey) { |
| 61 | + const store = loadKeyStore(); |
| 62 | + const proj = store.projects[projectId]; |
| 63 | + if (proj && proj.mailbox_id) return proj.mailbox_id; |
| 64 | + |
| 65 | + // Fallback: discover via API |
| 66 | + const res = await fetch(`${API}/mailboxes/v1`, { |
| 67 | + headers: { "Authorization": `Bearer ${serviceKey}` }, |
| 68 | + }); |
| 69 | + if (!res.ok) { |
| 70 | + const data = await res.json().catch(() => ({})); |
| 71 | + console.error(JSON.stringify({ status: "error", http: res.status, ...data })); |
| 72 | + process.exit(1); |
| 73 | + } |
| 74 | + const mailboxes = await res.json(); |
| 75 | + if (!Array.isArray(mailboxes) || mailboxes.length === 0) { |
| 76 | + console.error(JSON.stringify({ status: "error", message: "No mailbox found. Run: run402 email create <slug>" })); |
| 77 | + process.exit(1); |
| 78 | + } |
| 79 | + const mb = mailboxes[0]; |
| 80 | + updateProject(projectId, { mailbox_id: mb.id, mailbox_address: mb.address }); |
| 81 | + return mb.id; |
| 82 | +} |
| 83 | + |
| 84 | +async function create(args) { |
| 85 | + const slug = args.find(a => !a.startsWith("--")); |
| 86 | + const projectId = resolveProjectId(parseFlag(args, "--project")); |
| 87 | + const p = findProject(projectId); |
| 88 | + |
| 89 | + if (!slug) { |
| 90 | + console.error(JSON.stringify({ status: "error", message: "Missing slug. Usage: run402 email create <slug>" })); |
| 91 | + process.exit(1); |
| 92 | + } |
| 93 | + if (slug.length < 3 || slug.length > 63) { |
| 94 | + console.error(JSON.stringify({ status: "error", message: "Slug must be 3-63 characters." })); |
| 95 | + process.exit(1); |
| 96 | + } |
| 97 | + if (!SLUG_RE.test(slug)) { |
| 98 | + console.error(JSON.stringify({ status: "error", message: "Slug must be lowercase alphanumeric + hyphens, start/end with alphanumeric." })); |
| 99 | + process.exit(1); |
| 100 | + } |
| 101 | + if (slug.includes("--")) { |
| 102 | + console.error(JSON.stringify({ status: "error", message: "Slug must not contain consecutive hyphens." })); |
| 103 | + process.exit(1); |
| 104 | + } |
| 105 | + |
| 106 | + const res = await fetch(`${API}/mailboxes/v1`, { |
| 107 | + method: "POST", |
| 108 | + headers: { "Authorization": `Bearer ${p.service_key}`, "Content-Type": "application/json" }, |
| 109 | + body: JSON.stringify({ slug, project_id: projectId }), |
| 110 | + }); |
| 111 | + const data = await res.json(); |
| 112 | + if (!res.ok) { |
| 113 | + console.error(JSON.stringify({ status: "error", http: res.status, ...data })); |
| 114 | + process.exit(1); |
| 115 | + } |
| 116 | + |
| 117 | + updateProject(projectId, { mailbox_id: data.id, mailbox_address: data.address }); |
| 118 | + console.log(JSON.stringify({ status: "ok", mailbox_id: data.id, address: data.address, slug: data.slug })); |
| 119 | +} |
| 120 | + |
| 121 | +async function send(args) { |
| 122 | + const template = parseFlag(args, "--template"); |
| 123 | + const to = parseFlag(args, "--to"); |
| 124 | + const projectId = resolveProjectId(parseFlag(args, "--project")); |
| 125 | + const p = findProject(projectId); |
| 126 | + const variables = parseVars(args); |
| 127 | + |
| 128 | + if (!template) { |
| 129 | + console.error(JSON.stringify({ status: "error", message: "Missing --template. Options: project_invite, magic_link, notification" })); |
| 130 | + process.exit(1); |
| 131 | + } |
| 132 | + if (!to) { |
| 133 | + console.error(JSON.stringify({ status: "error", message: "Missing --to <email>" })); |
| 134 | + process.exit(1); |
| 135 | + } |
| 136 | + |
| 137 | + const mailboxId = await resolveMailboxId(projectId, p.service_key); |
| 138 | + |
| 139 | + const res = await fetch(`${API}/mailboxes/v1/${mailboxId}/messages`, { |
| 140 | + method: "POST", |
| 141 | + headers: { "Authorization": `Bearer ${p.service_key}`, "Content-Type": "application/json" }, |
| 142 | + body: JSON.stringify({ template, to, variables }), |
| 143 | + }); |
| 144 | + const data = await res.json(); |
| 145 | + if (!res.ok) { |
| 146 | + console.error(JSON.stringify({ status: "error", http: res.status, ...data })); |
| 147 | + process.exit(1); |
| 148 | + } |
| 149 | + |
| 150 | + console.log(JSON.stringify({ status: "ok", message_id: data.id, to: data.to, template: data.template })); |
| 151 | +} |
| 152 | + |
| 153 | +async function list(args) { |
| 154 | + const projectId = resolveProjectId(parseFlag(args, "--project")); |
| 155 | + const p = findProject(projectId); |
| 156 | + const mailboxId = await resolveMailboxId(projectId, p.service_key); |
| 157 | + |
| 158 | + const res = await fetch(`${API}/mailboxes/v1/${mailboxId}/messages`, { |
| 159 | + headers: { "Authorization": `Bearer ${p.service_key}` }, |
| 160 | + }); |
| 161 | + const data = await res.json(); |
| 162 | + if (!res.ok) { |
| 163 | + console.error(JSON.stringify({ status: "error", http: res.status, ...data })); |
| 164 | + process.exit(1); |
| 165 | + } |
| 166 | + |
| 167 | + console.log(JSON.stringify(data, null, 2)); |
| 168 | +} |
| 169 | + |
| 170 | +async function get(args) { |
| 171 | + const messageId = args.find(a => !a.startsWith("--")); |
| 172 | + const projectId = resolveProjectId(parseFlag(args, "--project")); |
| 173 | + const p = findProject(projectId); |
| 174 | + |
| 175 | + if (!messageId) { |
| 176 | + console.error(JSON.stringify({ status: "error", message: "Missing message_id. Usage: run402 email get <message_id>" })); |
| 177 | + process.exit(1); |
| 178 | + } |
| 179 | + |
| 180 | + const mailboxId = await resolveMailboxId(projectId, p.service_key); |
| 181 | + |
| 182 | + const res = await fetch(`${API}/mailboxes/v1/${mailboxId}/messages/${messageId}`, { |
| 183 | + headers: { "Authorization": `Bearer ${p.service_key}` }, |
| 184 | + }); |
| 185 | + const data = await res.json(); |
| 186 | + if (!res.ok) { |
| 187 | + console.error(JSON.stringify({ status: "error", http: res.status, ...data })); |
| 188 | + process.exit(1); |
| 189 | + } |
| 190 | + |
| 191 | + console.log(JSON.stringify(data, null, 2)); |
| 192 | +} |
| 193 | + |
| 194 | +export async function run(sub, args) { |
| 195 | + if (!sub || sub === '--help' || sub === '-h') { console.log(HELP); process.exit(0); } |
| 196 | + switch (sub) { |
| 197 | + case "create": await create(args); break; |
| 198 | + case "send": await send(args); break; |
| 199 | + case "list": await list(args); break; |
| 200 | + case "get": await get(args); break; |
| 201 | + default: |
| 202 | + console.error(`Unknown subcommand: ${sub}\n`); |
| 203 | + console.log(HELP); |
| 204 | + process.exit(1); |
| 205 | + } |
| 206 | +} |
0 commit comments