193 lines · 7.4 KB
import { execFileSync } from "node:child_process";
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { createInterface } from "node:readline/promises";
import { stdin, stdout } from "node:process";

import { printBanner } from "../src/banner.ts";

const rl = createInterface({ input: stdin, output: stdout });

async function ask(question: string, fallback = ""): Promise<string> {
  const suffix = fallback ? ` [${fallback}]` : "";
  const answer = (await rl.question(`${question}${suffix}: `)).trim();
  return answer || fallback;
}

async function confirm(question: string): Promise<boolean> {
  const answer = (await rl.question(`${question} [y/N]: `)).trim().toLowerCase();
  return answer === "y" || answer === "yes";
}

async function writeGuarded(path: string, content: string): Promise<boolean> {
  if (existsSync(path) && !(await confirm(`${path} already exists. Overwrite it?`))) {
    console.log(`Left ${path} alone. Nothing written.`);
    return false;
  }
  writeFileSync(path, content);
  console.log(`Wrote ${path}`);
  return true;
}

// Secrets go through `wrangler secret put`, which prompts for the value
// itself, so nothing sensitive is read here or written to any file.
async function putDiscordSecrets(): Promise<void> {
  console.log("\nDiscord credentials, from discord.com/developers/applications.");
  console.log("wrangler will prompt for each value. They're stored encrypted by");
  console.log("Cloudflare, never written to a file in this repo.\n");

  for (const name of ["DISCORD_TOKEN", "DISCORD_PUBLIC_KEY", "DISCORD_APPLICATION_ID"]) {
    try {
      execFileSync("npx", ["wrangler", "secret", "put", name], { stdio: "inherit" });