eas-cli
Version:
EAS command line tool
129 lines (128 loc) • 6.82 kB
JavaScript
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 EnvLink extends EasCommand_1.default {
static description = 'link an account-wide environment variable to 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',
}),
'variable-environment': core_1.Flags.enum({
...flags_1.EasEnvironmentFlagParameters,
description: 'Current environment of the variable to link',
}),
...flags_1.EASMultiEnvironmentFlag,
...flags_1.EASNonInteractiveFlag,
};
static args = [
{
name: 'environment',
description: "Environment to pull variables from. One of 'production', 'preview', or 'development'.",
required: false,
},
];
static contextDefinition = {
...this.ContextOptions.ProjectId,
...this.ContextOptions.LoggedIn,
};
async runAsync() {
const { args, flags } = await this.parse(EnvLink);
let { 'variable-name': name, 'variable-environment': currentEnvironment, 'non-interactive': nonInteractive, environment: environments, } = this.validateInputs(flags, args);
const { projectId, loggedIn: { graphqlClient }, } = await this.getContextAsync(EnvLink, {
nonInteractive,
});
const projectDisplayName = await (0, projectUtils_1.getDisplayNameForProjectIdAsync)(graphqlClient, projectId);
const variables = await EnvironmentVariablesQuery_1.EnvironmentVariablesQuery.sharedAsync(graphqlClient, {
appId: projectId,
environment: currentEnvironment,
filterNames: name ? [name] : undefined,
});
let selectedVariable = variables[0];
if (variables.length > 1) {
if (nonInteractive) {
throw new Error('Multiple variables found, run command with --variable-name and --variable-environment arguments.');
}
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 && !environments) {
const selectedEnvironments = (selectedVariable.linkedEnvironments ?? []).length > 0
? selectedVariable.linkedEnvironments
: selectedVariable.environments;
environments = await (0, prompts_2.promptVariableEnvironmentAsync)({
nonInteractive,
multiple: true,
selectedEnvironments: selectedEnvironments ?? [],
});
explicitSelect = true;
}
if (!environments) {
await EnvironmentVariableMutation_1.EnvironmentVariableMutation.linkSharedEnvironmentVariableAsync(graphqlClient, selectedVariable.id, projectId);
log_1.default.withTick(`Linked variable ${chalk_1.default.bold(selectedVariable.name)} to project ${chalk_1.default.bold(projectDisplayName)} in ${selectedVariable.environments?.join(', ').toLocaleLowerCase()}.`);
return;
}
for (const environment of Object.values(generated_1.EnvironmentVariableEnvironment)) {
try {
if (selectedVariable.linkedEnvironments?.includes(environment) ===
environments.includes(environment)) {
if (!explicitSelect && environments.includes(environment)) {
log_1.default.withTick(`Variable ${chalk_1.default.bold(selectedVariable.name)} is already linked to ${environment.toLocaleLowerCase()}.`);
}
continue;
}
if (environments.includes(environment)) {
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()}.`);
}
else if (explicitSelect) {
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()}.`);
}
}
catch (err) {
log_1.default.warn(err.message);
}
}
}
validateInputs(flags, { environment }) {
environment = environment?.toUpperCase();
if (environment && !(0, variableUtils_1.isEnvironment)(environment)) {
throw new Error("Invalid environment. Use one of 'production', 'preview', or 'development'.");
}
const environments = flags.environment
? flags.environment
: environment
? [environment]
: undefined;
if (flags['non-interactive']) {
if (!flags['variable-name']) {
throw new Error(`Environment variable needs 'name' to be specified when running in non-interactive mode. Run the command with ${chalk_1.default.bold('--variable-name VARIABLE_NAME')} flag to fix the issue`);
}
}
return {
...flags,
environment: environments,
};
}
}
exports.default = EnvLink;
;