All files / src/commands up.ts

0% Statements 0/16
0% Branches 0/8
0% Functions 0/1
0% Lines 0/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52                                                                                                       
import type { CliDeps, RuntimeEnv } from "../index.js";
import { waitForever as defaultWaitForever } from "../index.js";
 
export async function upCommand(
	opts: { port: string; path: string; verbose?: boolean; yes?: boolean },
	deps: CliDeps,
	runtime: RuntimeEnv,
	waiter: typeof defaultWaitForever = defaultWaitForever,
) {
	const port = Number.parseInt(opts.port, 10);
	if (Number.isNaN(port) || port <= 0 || port >= 65536) {
		throw new Error("Port must be between 1 and 65535");
	}
 
	await deps.ensurePortAvailable(port);
	const env = deps.readEnv(runtime);
	await deps.ensureBinary("tailscale", undefined, runtime);
	await deps.ensureFunnel(port, undefined, runtime);
	const host = await deps.getTailnetHostname();
	const publicUrl = `https://${host}${opts.path}`;
	runtime.log(`🌐 Public webhook URL (via Funnel): ${publicUrl}`);
 
	const server = await deps.startWebhook(
		port,
		opts.path,
		undefined,
		Boolean(opts.verbose),
		runtime,
	);
 
	const twilioClient = deps.createClient ? deps.createClient(env) : undefined;
	const senderSid = await deps.findWhatsappSenderSid(
		twilioClient as any,
		env.whatsappFrom,
		env.whatsappSenderSid,
		runtime,
	);
	await deps.updateWebhook(
		twilioClient as any,
		senderSid,
		publicUrl,
		"POST",
		runtime,
	);
 
	runtime.log(
		"\nSetup complete. Leave this process running to keep the webhook online. Ctrl+C to stop.",
	);
 
	return { server, publicUrl, senderSid, waiter };
}