Skip to content

Commit

Permalink
Make eas env:pull pull file env vars (#2636)
Browse files Browse the repository at this point in the history
# Why

File variables were not handled by the `env:pull` command. They should be downloaded just like the other values.

# How

* `env:pull` saves file type variables to `.eas/.env` directory and sets its value in `.env.local` file to its file path
* `env:push` skips file type variables

# Test Plan

Tested manually
  • Loading branch information
khamilowicz authored Oct 22, 2024
1 parent 3a7dfb0 commit 8c4be1c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
40 changes: 33 additions & 7 deletions packages/eas-cli/src/commands/env/pull.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { Flags } from '@oclif/core';
import * as fs from 'fs-extra';
import path from 'path';

import EasCommand from '../../commandUtils/EasCommand';
import { EASEnvironmentFlag, EASNonInteractiveFlag } from '../../commandUtils/flags';
import { EnvironmentVariableFragment } from '../../graphql/generated';
import { EnvironmentVariablesQuery } from '../../graphql/queries/EnvironmentVariablesQuery';
import {
EnvironmentSecretType,
EnvironmentVariableFragment,
EnvironmentVariableVisibility,
} from '../../graphql/generated';
import {
EnvironmentVariableWithFileContent,
EnvironmentVariablesQuery,
} from '../../graphql/queries/EnvironmentVariablesQuery';
import Log from '../../log';
import { confirmAsync } from '../../prompts';
import { promptVariableEnvironmentAsync } from '../../utils/prompts';
Expand All @@ -16,6 +24,7 @@ export default class EnvironmentVariablePull extends EasCommand {

static override contextDefinition = {
...this.ContextOptions.ProjectConfig,
...this.ContextOptions.ProjectDir,
...this.ContextOptions.LoggedIn,
};

Expand All @@ -39,6 +48,7 @@ export default class EnvironmentVariablePull extends EasCommand {
const {
privateProjectConfig: { projectId },
loggedIn: { graphqlClient },
projectDir,
} = await this.getContextAsync(EnvironmentVariablePull, {
nonInteractive,
});
Expand All @@ -50,6 +60,7 @@ export default class EnvironmentVariablePull extends EasCommand {
{
appId: projectId,
environment,
includeFileContent: true,
}
);

Expand All @@ -65,15 +76,30 @@ export default class EnvironmentVariablePull extends EasCommand {

const filePrefix = `# Environment: ${environment.toLocaleLowerCase()}\n\n`;

const envFileContent = environmentVariables
.map((variable: EnvironmentVariableFragment) => {
if (variable.value === null) {
const isFileVariablePresent = environmentVariables.some(v => {
return v.type === EnvironmentSecretType.FileBase64 && v.valueWithFileContent;
});

const envDir = path.join(projectDir, '.eas', '.env');
if (isFileVariablePresent) {
await fs.mkdir(envDir, { recursive: true });
}

const envFileContentLines = await Promise.all(
environmentVariables.map(async (variable: EnvironmentVariableWithFileContent) => {
if (variable.visibility === EnvironmentVariableVisibility.Secret) {
return `# ${variable.name}=***** (secret variables are not available for reading)`;
}
if (variable.type === EnvironmentSecretType.FileBase64 && variable.valueWithFileContent) {
const filePath = path.join(envDir, variable.name);
await fs.writeFile(filePath, variable.valueWithFileContent, 'base64');
return `${variable.name}=${filePath}`;
}
return `${variable.name}=${variable.value}`;
})
.join('\n');
await fs.writeFile(targetPath, filePrefix + envFileContent);
);

await fs.writeFile(targetPath, filePrefix + envFileContentLines.join('\n'));

const secretEnvVariables = environmentVariables.filter(
(variable: EnvironmentVariableFragment) => variable.value === null
Expand Down
15 changes: 15 additions & 0 deletions packages/eas-cli/src/commands/env/push.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Flags } from '@oclif/core';
import dotenv from 'dotenv';
import fs from 'fs-extra';
import path from 'path';

import EasCommand from '../../commandUtils/EasCommand';
import { EASMultiEnvironmentFlag } from '../../commandUtils/flags';
Expand Down Expand Up @@ -199,7 +200,21 @@ export default class EnvironmentVariablePush extends EasCommand {

const variables: Record<string, string> = dotenv.parse(await fs.readFile(envPath, 'utf8'));

const hasFileVariables = Object.values(variables).some(value =>
value.includes(path.join('.eas', '.env'))
);

if (hasFileVariables) {
Log.warn('File variables are not supported in this command.');
}

for (const [name, value] of Object.entries(variables)) {
// Skip file variables
const fileVariablePath = path.join('.eas', '.env', name);
if (value.endsWith(fileVariablePath)) {
Log.warn(`Skipping file variable ${name}`);
continue;
}
pushInput[name] = {
name,
value,
Expand Down

0 comments on commit 8c4be1c

Please sign in to comment.