UNPKG

convex

Version:

Client for the Convex Cloud

215 lines (214 loc) 7.79 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var localDeployment_exports = {}; __export(localDeployment_exports, { handleLocalDeployment: () => handleLocalDeployment }); module.exports = __toCommonJS(localDeployment_exports); var import_context = require("../../../bundler/context.js"); var import_detect_port = __toESM(require("detect-port"), 1); var import_bigBrain = require("./bigBrain.js"); var import_filePaths = require("./filePaths.js"); var import_run = require("./run.js"); var import_upgrade = require("./upgrade.js"); var import_prompts = require("../utils/prompts.js"); async function handleLocalDeployment(ctx, options) { if (await isOffline()) { return handleOffline(ctx, options); } const existingDeploymentForProject = await getExistingDeployment(ctx, { projectSlug: options.projectSlug, teamSlug: options.teamSlug }); if (existingDeploymentForProject !== null) { (0, import_context.logVerbose)( ctx, `Found existing deployment for project ${options.projectSlug}` ); await (0, import_run.ensureBackendStopped)(ctx, { ports: { cloud: existingDeploymentForProject.config.ports.cloud }, maxTimeSecs: 5, deploymentName: existingDeploymentForProject.deploymentName, allowOtherDeployments: true }); } const { binaryPath, version } = await (0, import_run.ensureBackendBinaryDownloaded)( ctx, options.backendVersion === void 0 ? { kind: "latest" } : { kind: "version", version: options.backendVersion } ); const ports = await choosePorts(ctx, options.ports); const { deploymentName, adminKey } = await (0, import_bigBrain.bigBrainStart)(ctx, { port: ports.cloud, projectSlug: options.projectSlug, teamSlug: options.teamSlug, instanceName: existingDeploymentForProject?.deploymentName ?? null }); const onActivity = async (isOffline2, _wasOffline) => { await (0, import_run.ensureBackendRunning)(ctx, { cloudPort: ports.cloud, deploymentName, maxTimeSecs: 5 }); if (isOffline2) { return; } await (0, import_bigBrain.bigBrainRecordActivity)(ctx, { instanceName: deploymentName }); }; const { cleanupHandle } = await (0, import_upgrade.handlePotentialUpgrade)(ctx, { deploymentName, oldVersion: existingDeploymentForProject?.config.backendVersion ?? null, newBinaryPath: binaryPath, newVersion: version, ports, adminKey, forceUpgrade: options.forceUpgrade }); const cleanupFunc = ctx.removeCleanup(cleanupHandle); ctx.registerCleanup(async () => { if (cleanupFunc !== null) { await cleanupFunc(); } await (0, import_bigBrain.bigBrainPause)(ctx, { projectSlug: options.projectSlug, teamSlug: options.teamSlug }); }); return { adminKey, deploymentName, deploymentUrl: (0, import_run.localDeploymentUrl)(ports.cloud), onActivity }; } async function handleOffline(ctx, options) { const { deploymentName, config } = await chooseFromExistingLocalDeployments(ctx); const { binaryPath } = await (0, import_run.ensureBackendBinaryDownloaded)(ctx, { kind: "version", version: config.backendVersion }); const ports = await choosePorts(ctx, options.ports); (0, import_filePaths.saveDeploymentConfig)(ctx, deploymentName, config); await (0, import_run.runLocalBackend)(ctx, { binaryPath, ports, deploymentName }); return { adminKey: config.adminKey, deploymentName, deploymentUrl: (0, import_run.localDeploymentUrl)(ports.cloud), onActivity: async (isOffline2, wasOffline) => { await (0, import_run.ensureBackendRunning)(ctx, { cloudPort: ports.cloud, deploymentName, maxTimeSecs: 5 }); if (isOffline2) { return; } if (wasOffline) { await (0, import_bigBrain.bigBrainStart)(ctx, { port: ports.cloud, projectSlug: options.projectSlug, teamSlug: options.teamSlug, instanceName: deploymentName }); } await (0, import_bigBrain.bigBrainRecordActivity)(ctx, { instanceName: deploymentName }); } }; } async function getExistingDeployment(ctx, options) { const { projectSlug, teamSlug } = options; const prefix = `local-${teamSlug.replace(/-/g, "_")}-${projectSlug.replace(/-/g, "_")}`; const localDeployments = await getLocalDeployments(ctx); const existingDeploymentForProject = localDeployments.find( (d) => d.deploymentName.startsWith(prefix) ); if (existingDeploymentForProject === void 0) { return null; } return { deploymentName: existingDeploymentForProject.deploymentName, config: existingDeploymentForProject.config }; } async function getLocalDeployments(ctx) { const dir = (0, import_filePaths.rootDeploymentStateDir)(); if (!ctx.fs.exists(dir)) { return []; } const deploymentNames = ctx.fs.listDir(dir).map((d) => d.name); return deploymentNames.flatMap((deploymentName) => { const config = (0, import_filePaths.loadDeploymentConfig)(ctx, deploymentName); if (config !== null) { return [{ deploymentName, config }]; } return []; }); } async function chooseFromExistingLocalDeployments(ctx) { const localDeployments = await getLocalDeployments(ctx); return (0, import_prompts.promptSearch)(ctx, { message: "Choose from an existing local deployment?", choices: localDeployments.map((d) => ({ name: d.deploymentName, value: d })) }); } async function choosePorts(ctx, requestedPorts) { if (requestedPorts !== void 0) { const availableCloudPort2 = await (0, import_detect_port.default)(requestedPorts.cloud); const availableSitePort2 = await (0, import_detect_port.default)(requestedPorts.site); if (availableCloudPort2 !== requestedPorts.cloud || availableSitePort2 !== requestedPorts.site) { return ctx.crash({ exitCode: 1, errorType: "fatal", printedMessage: "Requested ports are not available" }); } return { cloud: availableCloudPort2, site: availableSitePort2 }; } const availableCloudPort = await (0, import_detect_port.default)(3210); const availableSitePort = await (0, import_detect_port.default)(availableCloudPort + 1); return { cloud: availableCloudPort, site: availableSitePort }; } async function isOffline() { return false; } //# sourceMappingURL=localDeployment.js.map