-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement eas env:load and env:unload
- Loading branch information
1 parent
3b71496
commit d6950fa
Showing
3 changed files
with
108 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import assert from 'assert'; | ||
import * as fs from 'fs-extra'; | ||
import { exists } from 'fs-extra'; | ||
|
||
import { withSudoModeAsync } from '../../authUtils'; | ||
import EasCommand from '../../commandUtils/EasCommand'; | ||
import { EASEnvironmentFlag, EASNonInteractiveFlag } from '../../commandUtils/flags'; | ||
import { EnvironmentVariableFragment } from '../../graphql/generated'; | ||
import { EnvironmentVariablesQuery } from '../../graphql/queries/EnvironmentVariablesQuery'; | ||
import Log from '../../log'; | ||
|
||
const EnvLocalFile = '.env.local'; | ||
const EnvOriginalLocalFile = `${EnvLocalFile}.original`; | ||
|
||
export default class EnvironmentVariableLoad extends EasCommand { | ||
static override description = 'change environment variables'; | ||
|
||
static override hidden = true; | ||
|
||
static override flags = { | ||
...EASEnvironmentFlag, | ||
...EASNonInteractiveFlag, | ||
}; | ||
static override contextDefinition = { | ||
...this.ContextOptions.ProjectConfig, | ||
...this.ContextOptions.LoggedIn, | ||
...this.ContextOptions.SessionManagment, | ||
}; | ||
|
||
async runAsync(): Promise<void> { | ||
const { | ||
flags: { environment, 'non-interactive': nonInteractive }, | ||
} = await this.parse(EnvironmentVariableLoad); | ||
|
||
const { | ||
privateProjectConfig: { projectId }, | ||
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); | ||
} | ||
Log.log('Pulling environment variables...'); | ||
|
||
const environmentVariables = await withSudoModeAsync(sessionManager, async () => { | ||
assert(environment); | ||
return await EnvironmentVariablesQuery.byAppIdWithSensitiveAsync(graphqlClient, { | ||
appId: projectId, | ||
environment, | ||
}); | ||
}); | ||
|
||
const envFileContent = environmentVariables | ||
.filter((variable: EnvironmentVariableFragment) => variable.value !== null) | ||
.map((variable: EnvironmentVariableFragment) => { | ||
return `${variable.name}=${variable.value}`; | ||
}) | ||
.join('\n'); | ||
await fs.writeFile(EnvLocalFile, envFileContent); | ||
Log.log(`Environment variables for ${environment} have been loaded.`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import * as fs from 'fs-extra'; | ||
import { exists } from 'fs-extra'; | ||
|
||
import EasCommand from '../../commandUtils/EasCommand'; | ||
import Log from '../../log'; | ||
|
||
const EnvLocalFile = '.env.local'; | ||
const EnvOriginalLocalFile = `${EnvLocalFile}.original`; | ||
|
||
export default class EnvironmentVariableUnload extends EasCommand { | ||
static override description = 'unload environment variables'; | ||
|
||
static override hidden = true; | ||
|
||
async runAsync(): Promise<void> { | ||
if (await exists(EnvOriginalLocalFile)) { | ||
await fs.rename(EnvOriginalLocalFile, EnvLocalFile); | ||
} else { | ||
await fs.remove(EnvLocalFile); | ||
} | ||
Log.log(`Unloaded environment variables.`); | ||
} | ||
} |