You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: env.js
+35-76Lines changed: 35 additions & 76 deletions
Original file line number
Diff line number
Diff line change
@@ -2,23 +2,19 @@
2
2
/*
3
3
* Env file to load and validate env variables
4
4
* Be cautious; this file should not be imported into your source folder.
5
-
* We split the env variables into two parts:
6
-
* 1. Client variables: These variables are used in the client-side code (src folder).
7
-
* 2. Build-time variables: These variables are used in the build process (app.config.ts file).
8
5
* Import this file into the `app.config.ts` file to use environment variables during the build process. The client variables can then be passed to the client-side using the extra field in the `app.config.ts` file.
9
6
* To access the client environment variables in your `src` folder, you can import them from `@env`. For example: `import Env from '@env'`.
10
7
*/
11
8
/**
12
9
* 1st part: Import packages and Load your env variables
13
-
* we use dotenv to load the correct variables from the .env file based on the APP_ENV variable (default is development)
14
10
* APP_ENV is passed as an inline variable while executing the command, for example: APP_ENV=staging pnpm build:android
constEAS_PROJECT_ID='c3e1075b-6fe7-4686-aa49-35b46a229044';// eas project id
42
-
constSCHEME='habitstogetherApp';// app scheme
43
-
44
-
/**
45
-
* We declare a function withEnvSuffix that will add a suffix to the variable name based on the APP_ENV
46
-
* Add a suffix to variable env based on APP_ENV
47
-
* @param {string} name
48
-
* @returns {string}
49
-
*/
50
-
51
-
constwithEnvSuffix=(name)=>{
52
-
returnAPP_ENV==='production' ? name : `${name}.${APP_ENV}`;
53
-
};
54
37
55
38
/**
56
39
* 2nd part: Define your env variables schema
57
40
* we use zod to define our env variables schema
58
41
*
59
-
* we split the env variables into two parts:
60
-
* 1. client: These variables are used in the client-side code (`src` folder).
61
-
* 2. buildTime: These variables are used in the build process (app.config.ts file). You can think of them as server-side variables.
62
-
*
63
-
* Main rules:
64
-
* 1. If you need your variable on the client-side, you should add it to the client schema; otherwise, you should add it to the buildTime schema.
65
-
* 2. Whenever you want to add a new variable, you should add it to the correct schema based on the previous rule, then you should add it to the corresponding object (_clientEnv or _buildTimeEnv).
66
-
*
67
42
* Note: `z.string()` means that the variable exists and can be an empty string, but not `undefined`.
68
43
* If you want to make the variable required, you should use `z.string().min(1)` instead.
69
44
* Read more about zod here: https://zod.dev/?id=strings
* We use zod to validate our env variables based on the schema we defined above
124
97
* If the validation fails we throw an error and log the error to the console with a detailed message about missed variables
125
-
* If the validation passes we export the merged and parsed env variables to be used in the app.config.ts file as well as a ClientEnv object to be used in the client-side code
98
+
* If the validation passes we export the merged and parsed env variables to be used in the app.config.ts file (look at under extra)
126
99
**/
127
-
const_env={
128
-
..._clientEnv,
129
-
..._buildTimeEnv,
130
-
};
131
100
132
-
constmerged=buildTime.merge(client);
133
-
constparsed=merged.safeParse(_env);
134
-
135
-
if(parsed.success===false){
101
+
constparsed=envSchema.safeParse(_env);
102
+
if(!parsed.success){
136
103
console.error(
137
104
'❌ Invalid environment variables:',
138
105
parsed.error.flatten().fieldErrors,
139
-
140
-
`\n❌ Missing variables in .env.${APP_ENV} file, Make sure all required variables are defined in the .env.${APP_ENV} file.`,
141
-
`\n💡 Tip: If you recently updated the .env.${APP_ENV} file and the error still persists, try restarting the server with the -c flag to clear the cache.`,
142
-
);
143
-
thrownewError(
144
-
'Invalid environment variables, Check terminal for more details ',
145
106
);
107
+
thrownewError('Invalid environment variables. Check your .env file.');
0 commit comments