|
1 | 1 | #!/usr/bin/env node |
2 | 2 | import * as clack from "@clack/prompts"; |
3 | | -import { openBrowser } from "./browser"; |
4 | | -import { ensureConfig } from "./config"; |
5 | | -import { yellow, colorize, colors } from "./colors"; |
6 | | -import { generateHandlerFiles } from "./files"; |
7 | | -import { envVarForPlatform } from "./templates"; |
8 | | -import { startSession } from "./tunnel"; |
9 | | -import { runWizard } from "./wizard"; |
10 | | - |
11 | | -function printLogo(): void { |
12 | | - const pink = colors.pink; |
13 | | - const gray = colors.gray; |
14 | | - const reset = colors.reset; |
15 | | - process.stdout.write(`${pink} ████████╗███████╗██████╗ ███╗ ██╗\n`); |
16 | | - process.stdout.write(` ██║ ██╔════╝██╔══██╗████╗ ██║\n`); |
17 | | - process.stdout.write(` ██║ █████╗ ██████╔╝██╔██╗██║\n`); |
18 | | - process.stdout.write(` ██║ ██╔══╝ ██╔══██╗██║╚████║\n`); |
19 | | - process.stdout.write(` ██║ ███████╗██║ ██║██║ ╚███║\n`); |
20 | | - process.stdout.write(` ╚═╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚══╝${reset}\n`); |
21 | | - process.stdout.write(`${gray} v0.1.0 · webhook toolkit${reset}\n\n`); |
22 | | -} |
23 | | - |
24 | | -/** Entrypoint for the tern interactive setup CLI. */ |
| 3 | +import { GRAY, RESET } from "./colors"; |
| 4 | +import { createConfig } from "./config"; |
| 5 | +import { createHandlerFile, getFilePath, getWebhookPath } from "./files"; |
| 6 | +import { installTern } from "./install"; |
| 7 | +import { printEnvBox, printLogo } from "./print"; |
| 8 | +import { getTemplate } from "./templates"; |
| 9 | +import { startTunnel } from "./tunnel"; |
| 10 | +import { askQuestions, ENV_VARS, getPlatformLabel } from "./wizard"; |
| 11 | + |
| 12 | +/** CLI entrypoint for @hookflo/tern-cli. */ |
25 | 13 | export async function main(): Promise<void> { |
26 | | - process.on("SIGINT", () => { |
27 | | - clack.outro("session ended · all event data cleared"); |
28 | | - process.exit(0); |
29 | | - }); |
30 | | - |
31 | 14 | printLogo(); |
32 | | - clack.intro(" tern · webhook toolkit "); |
33 | 15 |
|
34 | | - const answers = await runWizard(); |
35 | | - |
36 | | - if (answers.action !== "tunnel") { |
37 | | - await generateHandlerFiles(answers.framework, answers.platform); |
38 | | - const envVar = envVarForPlatform(answers.platform); |
39 | | - if (envVar) { |
40 | | - clack.note(`${yellow(envVar)}=`, "add to .env.local"); |
41 | | - } |
| 16 | + const { platform, framework, action, port } = await askQuestions(); |
| 17 | + |
| 18 | + if (action === "handler") { |
| 19 | + await installTern(); |
| 20 | + const filePath = getFilePath(framework, platform); |
| 21 | + const envVar = ENV_VARS[platform]; |
| 22 | + const content = getTemplate( |
| 23 | + framework, |
| 24 | + platform, |
| 25 | + envVar, |
| 26 | + getPlatformLabel(platform), |
| 27 | + ); |
| 28 | + await createHandlerFile(filePath, content); |
| 29 | + if (envVar) printEnvBox(envVar); |
| 30 | + clack.outro("handler ready · add the env variable above to get started"); |
| 31 | + return; |
42 | 32 | } |
43 | 33 |
|
44 | | - if (answers.action === "handler") { |
45 | | - clack.outro("ready"); |
| 34 | + if (action === "tunnel") { |
| 35 | + const webhookPath = getWebhookPath(platform); |
| 36 | + createConfig(port, webhookPath, platform, framework); |
| 37 | + clack.log.step("connecting..."); |
| 38 | + startTunnel(port, webhookPath, getPlatformLabel(platform)); |
46 | 39 | return; |
47 | 40 | } |
48 | 41 |
|
49 | | - const port = answers.port ?? "3000"; |
50 | | - const { webhookPath } = ensureConfig({ |
51 | | - framework: answers.framework, |
52 | | - platform: answers.platform, |
53 | | - port: Number(port), |
54 | | - }); |
55 | | - |
56 | | - await startSession({ |
57 | | - port, |
58 | | - webhookPath, |
59 | | - platform: answers.platform, |
60 | | - }); |
61 | | - |
62 | | - clack.log.info(`opening dashboard · ${colorize("localhost:2019", colors.cyan)}`); |
63 | | - openBrowser("http://localhost:2019"); |
| 42 | + await installTern(); |
| 43 | + const filePath = getFilePath(framework, platform); |
| 44 | + const webhookPath = getWebhookPath(platform); |
| 45 | + const envVar = ENV_VARS[platform]; |
| 46 | + const content = getTemplate( |
| 47 | + framework, |
| 48 | + platform, |
| 49 | + envVar ?? "", |
| 50 | + getPlatformLabel(platform), |
| 51 | + ); |
| 52 | + await createHandlerFile(filePath, content); |
| 53 | + createConfig(port, webhookPath, platform, framework); |
| 54 | + if (envVar) printEnvBox(envVar); |
| 55 | + clack.log.step("connecting..."); |
| 56 | + startTunnel(port, webhookPath, getPlatformLabel(platform)); |
64 | 57 | } |
65 | 58 |
|
66 | | -main().catch((error: unknown) => { |
67 | | - const message = error instanceof Error ? error.message : String(error); |
68 | | - clack.log.error(message); |
| 59 | +main().catch((err: unknown) => { |
| 60 | + const message = err instanceof Error ? err.message : String(err); |
| 61 | + console.error(`\n ${GRAY}error: ${message}${RESET}\n`); |
69 | 62 | process.exit(1); |
70 | 63 | }); |
0 commit comments