276 lines · 11.2 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;
}

// Writes to a live Worker, so it confirms first and names the target. Two
// reasons that matters: `wrangler secret put` silently CREATES a Worker if
// the name in wrangler.jsonc doesn't exist yet, and if it does exist this
// overwrites whatever config it's currently running on.
async function putSecrets(secrets: Record<string, string>): Promise<void> {
  const names = Object.keys(secrets);
  if (names.length === 0) {
    return;
  }