Skip to content

Commit

Permalink
Implement review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
khamilowicz committed Aug 26, 2024
1 parent 94f7c96 commit 5a70a4d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 31 deletions.
44 changes: 27 additions & 17 deletions packages/eas-cli/src/commands/env/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export default class EnvironmentVariableInit extends EasCommand {
...EASNonInteractiveFlag,
};

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

async runAsync(): Promise<void> {
const {
flags: { 'non-interactive': nonInteractive },
Expand All @@ -29,12 +33,16 @@ export default class EnvironmentVariableInit extends EasCommand {
throw new Error("Non-interactive mode is not supported for 'eas env:init'");
}

await this.ensureDirenvInstalledAsync();
await this.setupEnvrcFileAsync();
const { projectDir } = await this.getContextAsync(EnvironmentVariableInit, {
nonInteractive,
});

await this.ensureDirenvInstalledAsync(projectDir);
await this.setupEnvrcFileAsync(projectDir);
await this.addDirenvHookToShellConfigAsync();
await this.addToGitIgnoreAsync();
await this.addToGitIgnoreAsync(projectDir);
Log.log('Running direnv allow...');
await spawnAsync('direnv', ['allow']);
await spawnAsync('direnv', ['allow'], { cwd: projectDir, stdio: 'inherit' });
}

private async addDirenvHookToShellConfigAsync(): Promise<void> {
Expand Down Expand Up @@ -104,9 +112,10 @@ export default class EnvironmentVariableInit extends EasCommand {
}
}

private async addToGitIgnoreAsync(): Promise<void> {
if (await pathExists('.gitignore')) {
const gitignoreContent = await readFile('.gitignore', 'utf8');
private async addToGitIgnoreAsync(cwd: string): Promise<void> {
const gitIgnorePath = path.resolve(cwd, '.gitignore');
if (await pathExists(gitIgnorePath)) {
const gitignoreContent = await readFile(gitIgnorePath, 'utf8');

const filesToIgnore = ['.envrc', '.env.eas.local', '.env.eas.local.original'];
const linesToAdd = filesToIgnore.filter(file => !gitignoreContent.includes(file));
Expand All @@ -116,7 +125,7 @@ export default class EnvironmentVariableInit extends EasCommand {
message: `Do you want to add ${linesToAdd.join(',')} to .gitignore?`,
});
if (confirm) {
await appendFile('.gitignore', linesToAdd.join('\n') + '\n', 'utf8');
await appendFile(gitIgnorePath, linesToAdd.join('\n') + '\n', 'utf8');
Log.log(`${linesToAdd.join(',')} added to .gitignore`);
} else {
Log.log('Skipping adding .envrc and .env.local to .gitignore');
Expand All @@ -127,10 +136,11 @@ export default class EnvironmentVariableInit extends EasCommand {
}
}

private async setupEnvrcFileAsync(): Promise<void> {
if (await pathExists('.envrc')) {
private async setupEnvrcFileAsync(cwd: string): Promise<void> {
const envrcPath = path.resolve(cwd, '.envrc');
if (await pathExists(envrcPath)) {
Log.log('.envrc file already exists');
const envrcContent = await readFile('.envrc', 'utf8');
const envrcContent = await readFile(envrcPath, 'utf8');
if (envrcContent.includes(ENVRC_TEMPLATE)) {
Log.log('.envrc file is already set up');
return;
Expand All @@ -141,19 +151,19 @@ export default class EnvironmentVariableInit extends EasCommand {
});
if (confirm) {
Log.log('Modifying existing .envrc file...');
await appendFile('.envrc', ENVRC_TEMPLATE, 'utf8');
await appendFile(envrcPath, ENVRC_TEMPLATE, 'utf8');
Log.log('.envrc file modified');
} else {
Log.log('Skipping modifying .envrc file');
}
} else {
Log.log('Creating .envrc file...');
await writeFile('.envrc', ENVRC_TEMPLATE, 'utf8');
await writeFile(envrcPath, ENVRC_TEMPLATE, 'utf8');
Log.log('.envrc file created');
}
}

private async ensureDirenvInstalledAsync(): Promise<void> {
private async ensureDirenvInstalledAsync(cwd: string): Promise<void> {
Log.log('Checking direnv installation...');
try {
await spawnAsync('direnv', ['--version']);
Expand All @@ -164,7 +174,7 @@ export default class EnvironmentVariableInit extends EasCommand {
message: 'Do you want EAS CLI to install direnv for you?',
});
if (install) {
await this.installDirenvAsync();
await this.installDirenvAsync(cwd);
Log.log('direnv installed');
} else {
Log.error("You'll need to install direnv manually");
Expand All @@ -173,7 +183,7 @@ export default class EnvironmentVariableInit extends EasCommand {
}
}

private async installDirenvAsync(): Promise<void> {
private async installDirenvAsync(cwd: string): Promise<void> {
const platform = os.platform();

let installCommand;
Expand Down Expand Up @@ -212,7 +222,7 @@ export default class EnvironmentVariableInit extends EasCommand {

try {
Log.log(`Running: ${installCommand}`);
await spawnAsync(installCommand, installArgs, { stdio: 'inherit' });
await spawnAsync(installCommand, installArgs, { stdio: 'inherit', cwd });
} catch (error: any) {
Log.error(`Failed to install direnv: ${error.message}`);
throw error;
Expand Down
38 changes: 27 additions & 11 deletions packages/eas-cli/src/commands/env/load.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import assert from 'assert';
import * as fs from 'fs-extra';
import { exists } from 'fs-extra';
import path from 'path';

import { withSudoModeAsync } from '../../authUtils';
import EasCommand from '../../commandUtils/EasCommand';
import { EASEnvironmentFlag, EASNonInteractiveFlag } from '../../commandUtils/flags';
import { EnvironmentVariableFragment } from '../../graphql/generated';
Expand All @@ -24,7 +24,7 @@ export default class EnvironmentVariableLoad extends EasCommand {
static override contextDefinition = {
...this.ContextOptions.ProjectConfig,
...this.ContextOptions.LoggedIn,
...this.ContextOptions.SessionManagment,
...this.ContextOptions.ProjectDir,
};

async runAsync(): Promise<void> {
Expand All @@ -34,34 +34,50 @@ export default class EnvironmentVariableLoad extends EasCommand {

const {
privateProjectConfig: { projectId },
projectDir,
loggedIn: { graphqlClient },
sessionManager,
} = await this.getContextAsync(EnvironmentVariableLoad, {
nonInteractive,
});

assert(environment, 'Environment is required');

if ((await exists(EnvLocalFile)) && !(await exists(EnvOriginalLocalFile))) {
await fs.rename(EnvLocalFile, EnvOriginalLocalFile);
const envLocalFile = path.resolve(projectDir, EnvLocalFile);
const envOriginalLocalFile = path.resolve(projectDir, EnvOriginalLocalFile);

if ((await exists(envLocalFile)) && !(await exists(envOriginalLocalFile))) {
await fs.rename(envLocalFile, envOriginalLocalFile);
}
Log.log('Pulling environment variables...');

const environmentVariables = await withSudoModeAsync(sessionManager, async () => {
assert(environment);
return await EnvironmentVariablesQuery.byAppIdWithSensitiveAsync(graphqlClient, {
const environmentVariables = await EnvironmentVariablesQuery.byAppIdWithSensitiveAsync(
graphqlClient,
{
appId: projectId,
environment,
});
});
}
);

const secretVariables = environmentVariables
.filter(({ value }) => value === null)
.map(({ name }) => name);

const envFileContent = environmentVariables
.filter((variable: EnvironmentVariableFragment) => variable.value !== null)
.map((variable: EnvironmentVariableFragment) => {
return `${variable.name}=${variable.value}`;
})
.join('\n');
await fs.writeFile(EnvLocalFile, envFileContent);
await fs.writeFile(envLocalFile, envFileContent);
await fs.appendFile(envLocalFile, `\nEAS_CURRENT_ENVIRONMENT=${environment}\n`);
Log.log(`Environment variables for ${environment} have been loaded.`);
if (secretVariables.length > 0) {
Log.addNewLineIfNone();
Log.warn(
`Some variables are not available for reading. You can edit them in ${envLocalFile} manually.`
);
Log.warn(`Variables that are not available for reading: ${secretVariables.join(', ')}.`);
Log.addNewLineIfNone();
}
}
}
18 changes: 15 additions & 3 deletions packages/eas-cli/src/commands/env/unload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from 'fs-extra';
import { exists } from 'fs-extra';
import path from 'path';

import EasCommand from '../../commandUtils/EasCommand';
import Log from '../../log';
Expand All @@ -12,11 +13,22 @@ export default class EnvironmentVariableUnload extends EasCommand {

static override hidden = true;

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

async runAsync(): Promise<void> {
if (await exists(EnvOriginalLocalFile)) {
await fs.rename(EnvOriginalLocalFile, EnvLocalFile);
const { projectDir } = await this.getContextAsync(EnvironmentVariableUnload, {
nonInteractive: true,
});

const envLocalFile = path.resolve(projectDir, EnvLocalFile);
const envOriginalLocalFile = path.resolve(projectDir, EnvOriginalLocalFile);

if (await exists(envOriginalLocalFile)) {
await fs.rename(envOriginalLocalFile, envLocalFile);
} else {
await fs.remove(EnvLocalFile);
await fs.remove(envLocalFile);
}
Log.log(`Unloaded environment variables.`);
}
Expand Down

0 comments on commit 5a70a4d

Please sign in to comment.