Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { createDeployment, createGlue, getGlueByName, type Runner, streamChangesTillDeploymentReady, updateGlue } from "../backend.ts";
import { render } from "ink";
import { type Instance, render } from "ink";
import { DeployUI, type DeployUIProps } from "../ui/deploy.tsx";
import React from "react";
import { checkForAuthCredsOtherwiseExit } from "../auth.ts";
import { getCreateDeploymentParams } from "../lib/getCreateDeploymentParams.ts";
import { getGlueName } from "../lib/glueNaming.ts";
import { addTags, normalizeTags } from "../lib/tagUtils.ts";
import { kv } from "../db.ts";
import { GLUE_API_SERVER } from "../common.ts";
import { Confirm } from "@cliffy/prompt/confirm";
import { resolve } from "@std/path/resolve";

interface DeployOptions {
name?: string;
Expand All @@ -28,9 +32,21 @@ export async function deploy(options: DeployOptions, file: string) {
glueName,
};

let instance: Instance | undefined;
const unmountUI = () => {
if (instance) {
instance.unmount();
instance = undefined;
}
};
const updateUI = (patch: Partial<DeployUIProps>) => {
deploymentProgressProps = { ...deploymentProgressProps, ...patch };
render(React.createElement(DeployUI, deploymentProgressProps));
const element = React.createElement(DeployUI, deploymentProgressProps);
if (instance) {
instance.rerender(element);
} else {
instance = render(element);
}
};

updateUI({ codeAnalysisState: "in_progress", codeAnalysisDuration: 0 });
Expand All @@ -42,6 +58,7 @@ export async function deploy(options: DeployOptions, file: string) {

duration = performance.now();
updateUI({ uploadingCodeState: "in_progress", uploadingCodeDuration: 0 });
const absPath = resolve(file);
let existingGlue = await getGlueByName(glueName, "deploy");
let newDeploymentId: string;
if (!existingGlue) {
Expand All @@ -50,7 +67,38 @@ export async function deploy(options: DeployOptions, file: string) {
throw new Error("Failed to create glue");
}
newDeploymentId = newGlue.pendingDeployment.id;
await kv.set(["glue-last-deployed-path", GLUE_API_SERVER, newGlue.id], absPath);
} else {
// If there's an existing glue, we want to warn the user that it will be
// overwritten *unless* they have already deployed this file (identified by
// absolute path) on their current machine to this glue before.
const lookupResult = await kv.get<string>(["glue-last-deployed-path", GLUE_API_SERVER, existingGlue.id]);
if (!lookupResult.value || absPath.localeCompare(lookupResult.value, undefined, { sensitivity: "base" }) !== 0) {
unmountUI();
console.warn(
`Warning: You are deploying to an existing glue named %c${
JSON.stringify(glueName)
}%c which was created previously.\nThis deployment will overwrite the existing glue.`,
"color: orange",
"",
);
if (lookupResult.value != null) {
console.warn(
`\nThe previous deployment to this glue from this machine was made from the following path:\n - %c${lookupResult.value}\n`,
"color: blue",
);
}
const confirm = await Confirm.prompt({
message: `Do you want to continue and overwrite this glue?`,
default: false,
});
if (!confirm) {
return;
}
updateUI({}); // Rerender the UI after unmounting
await kv.set(["glue-last-deployed-path", GLUE_API_SERVER, existingGlue.id], absPath);
}

const newDeployment = await createDeployment(existingGlue.id, deploymentParams);
if (tags.length) {
const desiredTags = addTags(existingGlue.tags, tags);
Expand Down
Loading