aboutsummaryrefslogtreecommitdiff
path: root/examples/spawn.ts
blob: a590a94f335fc43f6e6b3771ab7ab3ab55f367b8 (plain) (blame)
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
53
import { spawn, which } from "bun";
import { rmSync } from "fs";
import { basename } from "path";

const repo = process.argv.at(3) || "TheoBr/vercel-vite-demo";

const target = basename(repo) + "-main";
console.log("Downloading", repo, "to", "/tmp/" + target);

const archive = await fetch(
  `https://github.com/${repo}/archive/refs/heads/main.tar.gz`
);

// remove the directory if it already exists locally
rmSync("/tmp/" + target, { recursive: true, force: true });

const tar = spawn({
  cmd: ["tar", "-xzf", "-"],
  stdin: archive.body,

  stderr: "inherit",
  stdout: "inherit",
  cwd: "/tmp",
});

await tar.exited;

// if vercel isn't installed, install it
if (!which("vercel")) {
  console.log("Installing vercel...");

  const installer = spawn(
    { cmd: ["bun", "install", "-g", "vercel"] },
    {
      stderr: "inherit",
      stdout: "inherit",
      stdin: "inherit",
    }
  );
  await installer.exited;

  if (!which("vercel")) {
    throw new Error("Failed to install Vercel CLI");
  }
}

const { exited: deployed } = spawn({
  cmd: ["vercel", "deploy", "--yes", "--public", target],
  stdio: ["inherit", "inherit", "inherit"],
  cwd: "/tmp",
});

await deployed;