diff options
author | 2022-10-11 20:00:04 -0700 | |
---|---|---|
committer | 2022-10-11 20:00:04 -0700 | |
commit | 9fe1ad93cb8a8c0d398061a3c4548e0d8ef99f70 (patch) | |
tree | 4236620f550a67cc4e3f5938199320f9619c975d /examples/spawn.ts | |
parent | 8311761c82b29eae93b381cd66efd4eb8c68c065 (diff) | |
download | bun-9fe1ad93cb8a8c0d398061a3c4548e0d8ef99f70.tar.gz bun-9fe1ad93cb8a8c0d398061a3c4548e0d8ef99f70.tar.zst bun-9fe1ad93cb8a8c0d398061a3c4548e0d8ef99f70.zip |
Fixup example
Diffstat (limited to 'examples/spawn.ts')
-rw-r--r-- | examples/spawn.ts | 60 |
1 files changed, 45 insertions, 15 deletions
diff --git a/examples/spawn.ts b/examples/spawn.ts index ff53d84ea..a590a94f3 100644 --- a/examples/spawn.ts +++ b/examples/spawn.ts @@ -1,23 +1,53 @@ -import { readableStreamToText } from "bun"; -import { spawn } from "bun"; +import { spawn, which } from "bun"; +import { rmSync } from "fs"; +import { basename } from "path"; -const proc = spawn({ - cmd: ["ls", "-l"], +const repo = process.argv.at(3) || "TheoBr/vercel-vite-demo"; - // Both of these forms work: +const target = basename(repo) + "-main"; +console.log("Downloading", repo, "to", "/tmp/" + target); - // as an array: - stdio: ["ignore", "pipe", "ignore"], +const archive = await fetch( + `https://github.com/${repo}/archive/refs/heads/main.tar.gz` +); - // You can also use "inherit" to inherit the parent's stdio. - // stdin: "inherit", +// remove the directory if it already exists locally +rmSync("/tmp/" + target, { recursive: true, force: true }); - // You can pass a Bun.file to save it to a file: - // stdout: Bun.file("/tmp/stdout.txt"), -}); +const tar = spawn({ + cmd: ["tar", "-xzf", "-"], + stdin: archive.body, -const result = await readableStreamToText(proc.stdout); + stderr: "inherit", + stdout: "inherit", + cwd: "/tmp", +}); -await proc.exited(); +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", +}); -console.log(result); +await deployed; |