From 580d36edd6a32a17081c6167de5548b4c1a32d07 Mon Sep 17 00:00:00 2001 From: Piotr Szeremeta Date: Tue, 20 Aug 2024 13:17:58 +0200 Subject: [PATCH] Implement review suggestions --- packages/eas-cli/src/commands/env/init.ts | 44 +++++++++++++-------- packages/eas-cli/src/commands/env/load.ts | 38 ++++++++++++------ packages/eas-cli/src/commands/env/unload.ts | 18 +++++++-- 3 files changed, 69 insertions(+), 31 deletions(-) diff --git a/packages/eas-cli/src/commands/env/init.ts b/packages/eas-cli/src/commands/env/init.ts index 0541113e98..76c3044fd4 100644 --- a/packages/eas-cli/src/commands/env/init.ts +++ b/packages/eas-cli/src/commands/env/init.ts @@ -20,6 +20,10 @@ export default class EnvironmentVariableInit extends EasCommand { ...EASNonInteractiveFlag, }; + static override contextDefinition = { + ...this.ContextOptions.ProjectDir, + }; + async runAsync(): Promise { const { flags: { 'non-interactive': nonInteractive }, @@ -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 { @@ -104,9 +112,10 @@ export default class EnvironmentVariableInit extends EasCommand { } } - private async addToGitIgnoreAsync(): Promise { - if (await pathExists('.gitignore')) { - const gitignoreContent = await readFile('.gitignore', 'utf8'); + private async addToGitIgnoreAsync(cwd: string): Promise { + 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)); @@ -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'); @@ -127,10 +136,11 @@ export default class EnvironmentVariableInit extends EasCommand { } } - private async setupEnvrcFileAsync(): Promise { - if (await pathExists('.envrc')) { + private async setupEnvrcFileAsync(cwd: string): Promise { + 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; @@ -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 { + private async ensureDirenvInstalledAsync(cwd: string): Promise { Log.log('Checking direnv installation...'); try { await spawnAsync('direnv', ['--version']); @@ -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"); @@ -173,7 +183,7 @@ export default class EnvironmentVariableInit extends EasCommand { } } - private async installDirenvAsync(): Promise { + private async installDirenvAsync(cwd: string): Promise { const platform = os.platform(); let installCommand; @@ -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; diff --git a/packages/eas-cli/src/commands/env/load.ts b/packages/eas-cli/src/commands/env/load.ts index 79a0005c4c..a650d8aa40 100644 --- a/packages/eas-cli/src/commands/env/load.ts +++ b/packages/eas-cli/src/commands/env/load.ts @@ -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'; @@ -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 { @@ -34,26 +34,33 @@ 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) @@ -61,7 +68,16 @@ export default class EnvironmentVariableLoad extends EasCommand { 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(); + } } } diff --git a/packages/eas-cli/src/commands/env/unload.ts b/packages/eas-cli/src/commands/env/unload.ts index c99eab23b8..e06ff382c5 100644 --- a/packages/eas-cli/src/commands/env/unload.ts +++ b/packages/eas-cli/src/commands/env/unload.ts @@ -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'; @@ -12,11 +13,22 @@ export default class EnvironmentVariableUnload extends EasCommand { static override hidden = true; + static override contextDefinition = { + ...this.ContextOptions.ProjectDir, + }; + async runAsync(): Promise { - 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.`); }