Skip to content

Commit

Permalink
Allow env vars to be loaded from .env and EAS env simultaneously
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten committed Dec 20, 2024
1 parent 2a96d04 commit 8b9ccad
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
11 changes: 9 additions & 2 deletions packages/eas-cli/src/commands/worker/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,21 +253,28 @@ export default class WorkerDeploy extends EasCommand {
let progress = ora('Preparing project').start();

try {
const manifest = await WorkerAssets.createManifestAsync(
const manifestResult = await WorkerAssets.createManifestAsync(
{
environment: flags.environment,
projectDir,
projectId,
},
graphqlClient
);
if (manifestResult.conflictingVariableNames?.length) {
Log.warn(
'> The following environment variables were loaded both from local .env files as well as EAS environment variables, '
+ ' and will be set to the EAS environment variable values instead: '
+ manifestResult.conflictingVariableNames.join(' '),
);
}
assetMap = await WorkerAssets.createAssetMapAsync(
projectDist.type === 'server' ? projectDist.clientPath : projectDist.path
);
tarPath = await WorkerAssets.packFilesIterableAsync(
emitWorkerTarballAsync({
assetMap,
manifest,
manifest: manifestResult.manifest,
})
);

Expand Down
43 changes: 28 additions & 15 deletions packages/eas-cli/src/worker/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ export interface Manifest {
env: Record<string, string | undefined>;
}

export interface CreateManifestResult {
conflictingVariableNames: string[] | undefined;
manifest: Manifest;
}

interface CreateManifestParams {
projectId: string;
projectDir: string;
Expand All @@ -108,23 +113,31 @@ interface CreateManifestParams {
export async function createManifestAsync(
params: CreateManifestParams,
graphqlClient: ExpoGraphqlClient
): Promise<Manifest> {
let env: Record<string, string | undefined>;
): Promise<CreateManifestResult> {
// NOTE: This is required for the .env resolution
process.env.NODE_ENV = 'production';
// Resolve .env file variables
const env: Record<string, string | undefined> = getEnv(params.projectDir).env;
// Maybe load EAS Environment Variables (based on `--environment` arg)
let conflictingVariableNames: string[] | undefined;
if (params.environment) {
env = Object.fromEntries(
(
await EnvironmentVariablesQuery.byAppIdWithSensitiveAsync(graphqlClient, {
appId: params.projectId,
environment: params.environment,
})
).map(variable => [variable.name, variable.value ?? undefined])
);
} else {
// NOTE: This is required for the .env resolution
process.env.NODE_ENV = 'production';
env = getEnv(params.projectDir).env;
const loadedVariables = await EnvironmentVariablesQuery.byAppIdWithSensitiveAsync(graphqlClient, {
appId: params.projectId,
environment: params.environment,
});
// Load EAS Env vars into `env` object, keeping track of conflicts
conflictingVariableNames = [];
for (const variable of loadedVariables) {
if (variable.value != null) {
if (env[variable.name] != null) {
conflictingVariableNames.push(variable.name);
}
env[variable.name] = variable.value;
}
}
}
return { env };
const manifest: Manifest = { env };
return { conflictingVariableNames, manifest };
}

interface WorkerFileEntry {
Expand Down

0 comments on commit 8b9ccad

Please sign in to comment.