From 58d624ff54eb3773ae53dfbb4e1de28299c44024 Mon Sep 17 00:00:00 2001 From: Seth Trowbridge Date: Sun, 16 Jul 2023 21:37:39 -0400 Subject: [PATCH] start deploy script --- run-deploy.tsx | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 run-deploy.tsx diff --git a/run-deploy.tsx b/run-deploy.tsx new file mode 100644 index 0000000..59542cd --- /dev/null +++ b/run-deploy.tsx @@ -0,0 +1,98 @@ +import { load } from "https://deno.land/std/dotenv/mod.ts"; +import { parse } from "https://deno.land/std@0.194.0/flags/mod.ts"; + + +const collect =async(inKey:string, inArg:Record, inEnv:Record):Promise=> +{ + const scanArg = inArg[inKey]; + const scanEnvFile = inEnv[inKey]; + const scanEnvDeno = Deno.env.get(inKey); + + if(scanArg) + { + console.log(`Using "${inKey}" from passed argument.`); + return scanArg; + } + if(scanEnvFile) + { + console.log(`Using "${inKey}" from .env file.`); + return scanEnvFile; + } + if(scanEnvDeno) + { + console.log(`Using "${inKey}" from environment variable.`); + return scanEnvDeno; + } + + const scanUser = await prompt(`No "${inKey}" found. Enter one here:`); + if(!scanUser || scanUser?.length < 3) + { + console.log("Exiting..."); + Deno.exit(); + } +}; + +const prompt =async(question: string):Promise=> +{ + const buf = new Uint8Array(1024); + await Deno.stdout.write(new TextEncoder().encode(question)); + const bytes = await Deno.stdin.read(buf); + if (bytes) { + return new TextDecoder().decode(buf.subarray(0, bytes)).trim(); + } + throw new Error("Unexpected end of input"); +}; + +try +{ + console.log("Runing deploy!"); + const serveScript = import.meta.resolve("./run-serve.tsx"); + console.log("Serve script:", serveScript); + + let arg = parse(Deno.args); + let env = await load(); + + let useToken = await collect("DENO_DEPLOY_TOKEN", arg, env); + let useProject = await collect("DENO_DEPLOY_PROJECT", arg, env); + let useEntry = await collect("DENO_DEPLOY_ENTRY", arg, env); + + Deno.env.set("DENO_DEPLOY_TOKEN", useToken || ""); + Deno.env.set("DENO_DEPLOY_ENTRY", useEntry || ""); + + + const command = new Deno.Command( + `deno`, + { + args:[ + "run", + "-A", + "--no-lock", + "https://deno.land/x/deploy/deployctl.ts", + "deploy", + `--project=${useProject}`, + serveScript + ], + stdin: "piped", + stdout: "piped" + } + ); + + const child = command.spawn(); + + // open a file and pipe the subprocess output to it. + const writableStream = new WritableStream({ + write(chunk: Uint8Array): Promise { + Deno.stdout.write(chunk); + return Promise.resolve(); + }, + }); + child.stdout.pipeTo(writableStream); + + // manually close stdin + child.stdin.close(); + const status = await child.status; +} +catch(e) +{ + console.error(e); +} \ No newline at end of file