UNPKG

eas-cli

Version:
118 lines (117 loc) 6.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const core_1 = require("@oclif/core"); const chalk_1 = tslib_1.__importDefault(require("chalk")); const EasCommand_1 = tslib_1.__importDefault(require("../../commandUtils/EasCommand")); const flags_1 = require("../../commandUtils/flags"); const generated_1 = require("../../graphql/generated"); const EnvironmentVariableMutation_1 = require("../../graphql/mutations/EnvironmentVariableMutation"); const EnvironmentVariablesQuery_1 = require("../../graphql/queries/EnvironmentVariablesQuery"); const log_1 = tslib_1.__importDefault(require("../../log")); const projectUtils_1 = require("../../project/projectUtils"); const prompts_1 = require("../../prompts"); const prompts_2 = require("../../utils/prompts"); const variableUtils_1 = require("../../utils/variableUtils"); class EnvUnlink extends EasCommand_1.default { static description = 'unlink an account-wide environment variable from the current project'; // for now we only roll out global account-wide env variables so this should stay hidden static hidden = true; static flags = { 'variable-name': core_1.Flags.string({ description: 'Name of the variable', }), ...flags_1.EASMultiEnvironmentFlag, ...flags_1.EASNonInteractiveFlag, }; static contextDefinition = { ...this.ContextOptions.ProjectId, ...this.ContextOptions.LoggedIn, }; static args = [ { name: 'environment', description: "Environment to unlink the variable from. One of 'production', 'preview', or 'development'.", required: false, }, ]; async runAsync() { const { args, flags } = await this.parse(EnvUnlink); let { 'variable-name': name, 'non-interactive': nonInteractive, environment: unlinkEnvironments, } = this.validateInputs(flags, args); const { projectId, loggedIn: { graphqlClient }, } = await this.getContextAsync(EnvUnlink, { nonInteractive, }); const projectDisplayName = await (0, projectUtils_1.getDisplayNameForProjectIdAsync)(graphqlClient, projectId); const variables = await EnvironmentVariablesQuery_1.EnvironmentVariablesQuery.sharedAsync(graphqlClient, { appId: projectId, filterNames: name ? [name] : undefined, }); let selectedVariable = variables[0]; if (variables.length > 1) { if (nonInteractive) { throw new Error("Multiple variables found, please select one using '--variable-name'"); } selectedVariable = await (0, prompts_1.selectAsync)('Select account-wide variable', variables.map(variable => ({ title: (0, variableUtils_1.formatVariableName)(variable), value: variable, }))); } if (!selectedVariable) { throw new Error(`Account-wide variable ${name} doesn't exist`); } let explicitSelect = false; if (!nonInteractive && !unlinkEnvironments) { const selectedEnvironments = (selectedVariable.linkedEnvironments ?? []).length > 0 ? selectedVariable.linkedEnvironments : selectedVariable.environments; const environments = await (0, prompts_2.promptVariableEnvironmentAsync)({ nonInteractive, multiple: true, selectedEnvironments: selectedEnvironments ?? [], }); explicitSelect = true; unlinkEnvironments = Object.values(generated_1.EnvironmentVariableEnvironment).filter(env => !environments.includes(env)); } if (!unlinkEnvironments) { await EnvironmentVariableMutation_1.EnvironmentVariableMutation.unlinkSharedEnvironmentVariableAsync(graphqlClient, selectedVariable.id, projectId); log_1.default.withTick(`Unlinked variable ${chalk_1.default.bold(selectedVariable.name)} from project ${chalk_1.default.bold(projectDisplayName)} in ${selectedVariable.environments?.join(', ').toLocaleLowerCase()}.`); return; } for (const environment of Object.values(generated_1.EnvironmentVariableEnvironment)) { if (selectedVariable.linkedEnvironments?.includes(environment) !== unlinkEnvironments.includes(environment)) { if (!explicitSelect && unlinkEnvironments.includes(environment)) { log_1.default.withTick(`Variable ${chalk_1.default.bold(selectedVariable.name)} is already unlinked from ${environment.toLocaleLowerCase()}.`); } continue; } if (unlinkEnvironments.includes(environment)) { await EnvironmentVariableMutation_1.EnvironmentVariableMutation.unlinkSharedEnvironmentVariableAsync(graphqlClient, selectedVariable.id, projectId, environment); log_1.default.withTick(`Unlinked variable ${chalk_1.default.bold(selectedVariable.name)} from project ${chalk_1.default.bold(projectDisplayName)} in ${environment.toLocaleLowerCase()}.`); } else if (explicitSelect) { await EnvironmentVariableMutation_1.EnvironmentVariableMutation.linkSharedEnvironmentVariableAsync(graphqlClient, selectedVariable.id, projectId, environment); log_1.default.withTick(`Linked variable ${chalk_1.default.bold(selectedVariable.name)} to project ${chalk_1.default.bold(projectDisplayName)} in ${environment.toLocaleLowerCase()}.`); } } } validateInputs(flags, { environment }) { if (flags['non-interactive']) { if (!flags['variable-name']) { throw new Error('Current name is required in non-interactive mode. Run the command with --variable-name flag.'); } } if (environment) { environment = environment.toUpperCase(); if (!(0, variableUtils_1.isEnvironment)(environment)) { throw new Error("Invalid environment. Use one of 'production', 'preview', or 'development'."); } return { environment: [environment], ...flags, }; } return flags; } } exports.default = EnvUnlink;