Skip to content

Commit d23caaf

Browse files
committed
feat(eas): read environment file from EAS Secrets
1 parent 9f0cd1a commit d23caaf

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

env.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,27 @@ const APP_ENV =
2121
/** @type {z.infer<typeof clientEnvSchema>['APP_ENV']} */
2222
(process.env.APP_ENV) ?? 'development';
2323

24+
const isEASBuild = process.env.EAS_BUILD === 'true';
25+
2426
const ENVIRONMENT_DEPENDANT_SCRIPTS = [
2527
'expo start',
2628
'expo prebuild',
2729
'eas build',
2830
'expo run',
2931
];
3032

31-
// Check if the environment file has to be validated for the current running script
32-
const shouldValidateEnv = ENVIRONMENT_DEPENDANT_SCRIPTS.some((script) =>
33-
process.env.npm_lifecycle_script?.includes(script)
33+
const scriptIsEnvironmentDependant = ENVIRONMENT_DEPENDANT_SCRIPTS.some(
34+
(script) => process.env.npm_lifecycle_script?.includes(script)
3435
);
3536

36-
const envPath = path.resolve(__dirname, `.env.${APP_ENV}`);
37+
// Check if the environment file has to be validated for the current running script and build method
38+
const shouldValidateEnv = isEASBuild && scriptIsEnvironmentDependant;
39+
40+
const easEnvironmentFileVariable = `ENVIRONMENT_FILE_${APP_ENV.toUpperCase()}`;
41+
const easEnvironmentFilePath = process.env[easEnvironmentFileVariable];
42+
const localEnvironmentFilePath = path.resolve(__dirname, `.env.${APP_ENV}`);
43+
44+
const envPath = isEASBuild ? easEnvironmentFilePath : localEnvironmentFilePath;
3745

3846
require('dotenv').config({
3947
path: envPath,
@@ -166,13 +174,24 @@ if (shouldValidateEnv) {
166174
const parsedWholeEnv = wholeEnvSchema.safeParse(_wholeEnv);
167175

168176
if (parsedWholeEnv.success === false) {
169-
console.error(
177+
const envFile = isEASBuild ? easEnvironmentFileVariable : `.env.${APP_ENV}`;
178+
179+
const messages = [
170180
'❌ Invalid environment variables:',
171181
parsedWholeEnv.error.flatten().fieldErrors,
172182

173-
`\n❌ Missing variables in \x1b[1m\x1b[4m\x1b[31m.env.${APP_ENV}\x1b[0m file, Make sure all required variables are defined in the \x1b[1m\x1b[4m\x1b[31m.env.${APP_ENV}\x1b[0m file.`,
174-
`\n💡 Tip: If you recently updated the .env.${APP_ENV} file and the error still persists, try restarting the server with the -cc flag to clear the cache.`
175-
);
183+
`\n❌ Missing variables in \x1b[1m\x1b[4m\x1b[31m${envFile}\x1b[0m file. Make sure all required variables are defined in the \x1b[1m\x1b[4m\x1b[31m${envFile}\x1b[0m file.`,
184+
`\n💡 Tip: If you recently updated the \x1b[1m\x1b[4m\x1b[31m${envFile}\x1b[0m file and the error still persists, try restarting the server with the -cc flag to clear the cache.`,
185+
];
186+
187+
if (isEASBuild) {
188+
messages.push(
189+
`\n☁️ For \x1b[1m\x1b[32mEAS Build\x1b[0m deployments, ensure the secret\x1b[1m\x1b[4m\x1b[31m${easEnvironmentFileVariable} \x1b[0m is defined in Project Secrets and has the proper environment file attached.`
190+
);
191+
}
192+
193+
console.error(...messages);
194+
176195
throw new Error(
177196
'Invalid environment variables, Check terminal for more details '
178197
);

0 commit comments

Comments
 (0)