import { Command } from "commander"; import chalk from "chalk"; import { provisionHost, readInstanceConfig, configName, envFilename, } from "./config.js"; import { version } from "../index.js"; import { BETA_KEY_DEPRECATION_MESSAGE, BETA_KEY_OPTION, createClient, deprecationCheck, flushAndExit, getAuthHeader, printServerErr, prompt, } from "./utils.js"; import fs from "fs"; export const deactivate = new Command("deactivate") .description( "Deactivate an existing deployment associated with the Convex app in current directory, " + "returning your beta-key redemption. The data still lives on Convex's servers. If you " + "really want all data purged, please contact support@convex.dev" ) .addOption(BETA_KEY_OPTION) .option( "--deployment-type ", "Type of deployment to deactivate (`convex..json`). Defaults to `convex.json`" ) .option("--force", "Deactivate without prompt to confirm deactivation") .action(async options => { if (options.betaKey) { console.error(chalk.yellow(BETA_KEY_DEPRECATION_MESSAGE)); } console.log( chalk.red( "Data on this deployment will no longer be accessible after deactivation." ) ); if (!options.force) { await prompt( "Are you sure you want to deactivate this deployment?\nPress enter to continue or ctrl-C to abort.\n" ); } const { instanceConfig } = await readInstanceConfig(options.deploymentType); interface DeactivateArgs { betaKey?: string; instanceName: string; } const authHeader = await getAuthHeader(); const headers: Record = authHeader ? { Authorization: authHeader } : {}; const deactivateArgs: DeactivateArgs = { betaKey: options.betaKey, instanceName: instanceConfig.instanceName, }; const deactivateUrl = `${provisionHost}/api/${version}/deactivate`; const client = createClient(); try { const res = await client.post(deactivateUrl, deactivateArgs, { headers }); deprecationCheck(res); // Remove the convex.json and .env.local for the now-inactive deployment fs.unlinkSync(configName(options.deploymentType)); const envLocal = envFilename(options.deploymentType); if (fs.existsSync(envLocal)) { fs.unlinkSync(envFilename(options.deploymentType)); } console.log( chalk.green( `Done! Your account now has ${res.data.redemptionsRemaining} deployments remaining.` ) ); } catch (err) { console.error(chalk.red("Error: Unable to deactivate deployment")); printServerErr(err); return await flushAndExit(1, err); } });