Skip to content
TypeScript
981 B
Raw Blame
/**
 * 브라우저 열기 (PRD v2 §W2, --view open). 플랫폼별 opener, fail-soft.
 */
import { spawn } from "node:child_process";
import { existsSync } from "node:fs";

function opener(): string {
  return process.platform === "darwin"
    ? "open"
    : process.platform === "win32"
      ? "start"
      : "xdg-open";
}

export function openInBrowser(file: string): boolean {
  if (!existsSync(file)) return false;
  return launch(file);
}

/** Open a URL (http://…) — no filesystem check. */
export function openUrl(url: string): boolean {
  return launch(url);
}

function launch(target: string): boolean {
  try {
    // win32 의 start 는 cmd 내장 명령 — 직접 spawn 하면 ENOENT. cmd /c 로 감싼다.
    const [bin, args] =
      process.platform === "win32" ? ["cmd", ["/c", "start", "", target]] : [opener(), [target]];
    spawn(bin, args as string[], { stdio: "ignore", detached: true }).unref();
    return true;
  } catch {
    return false;
  }
}