-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathenv.js
127 lines (104 loc) · 3.61 KB
/
env.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* eslint-disable @typescript-eslint/no-require-imports */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/restrict-template-expressions */
/* eslint-disable @typescript-eslint/naming-convention */
/* eslint-disable import/no-nodejs-modules */
const path = require('path');
const z = require('zod');
const packageJSON = require('./package.json');
const APP_ENV = process.env.APP_ENV ?? 'development';
const envPath = path.resolve(__dirname, `.env.${APP_ENV}`);
require('dotenv').config({
path: envPath,
});
// Default values
const BUNDLE_ID = 'com.tsyirvo.rnstarter';
const PACKAGE = 'com.tsyirvo.rnstarter';
const EXPO_ACCOUNT_OWNER = 'tsyirvo';
const withEnvSuffix = (name) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return APP_ENV === 'production' ? name : `${name}.${APP_ENV}`;
};
// Environment variables validation schemas
const client = z.object({
APP_ENV: z.enum(['development', 'staging', 'production']),
APP_NAME: z.string(),
BUNDLE_ID: z.string(),
PACKAGE: z.string(),
VERSION: z.string(),
// ADD CLIENT ENV VARS HERE
STORYBOOK_ENABLED: z.string().optional(),
API_URL: z.string(),
ITUNES_ITEM_ID: z.string(),
FLAGSMITH_KEY: z.string(),
AMPLITUDE_API_KEY: z.string(),
SENTRY_DSN: z.string(),
ONE_SIGNAL_APP_ID: z.string(),
REVENUE_CAT_APPLE_API_KEY: z.string(),
REVENUE_CAT_ANDROID_API_KEY: z.string(),
APPSFLYER_DEV_KEY: z.string(),
APPSFLYER_APP_ID: z.string(),
});
const buildTime = z.object({
EXPO_ACCOUNT_OWNER: z.string(),
EAS_PROJECT_ID: z.string(),
// ADD BUILD TIME ENV VARS HERE
SENTRY_ORG: z.string(),
SENTRY_PROJECT: z.string(),
SENTRY_AUTH_TOKEN: z.string(),
EXPO_APPLE_TEAM_ID: z.string(),
});
// Environment variables config
const _clientEnv = {
APP_ENV,
APP_NAME: process.env.APP_NAME,
BUNDLE_ID: withEnvSuffix(BUNDLE_ID),
PACKAGE: withEnvSuffix(PACKAGE),
VERSION: packageJSON.version,
// ADD ENV VARS HERE TOO
API_URL: process.env.API_URL,
STORYBOOK_ENABLED: process.env.STORYBOOK_ENABLED,
ITUNES_ITEM_ID: process.env.ITUNES_ITEM_ID,
FLAGSMITH_KEY: process.env.FLAGSMITH_KEY,
AMPLITUDE_API_KEY: process.env.AMPLITUDE_API_KEY,
SENTRY_DSN: process.env.SENTRY_DSN,
ONE_SIGNAL_APP_ID: process.env.ONE_SIGNAL_APP_ID,
REVENUE_CAT_APPLE_API_KEY: process.env.REVENUE_CAT_APPLE_API_KEY,
REVENUE_CAT_ANDROID_API_KEY: process.env.REVENUE_CAT_ANDROID_API_KEY,
APPSFLYER_DEV_KEY: process.env.APPSFLYER_DEV_KEY,
APPSFLYER_APP_ID: process.env.APPSFLYER_APP_ID,
};
const _buildTimeEnv = {
EXPO_ACCOUNT_OWNER,
EAS_PROJECT_ID: process.env.EAS_PROJECT_ID,
// ADD ENV VARS HERE TOO
SENTRY_ORG: process.env.SENTRY_ORG,
SENTRY_PROJECT: process.env.SENTRY_PROJECT,
SENTRY_AUTH_TOKEN: process.env.SENTRY_AUTH_TOKEN,
EXPO_APPLE_TEAM_ID: process.env.EXPO_APPLE_TEAM_ID,
};
const _env = {
..._clientEnv,
..._buildTimeEnv,
};
const merged = buildTime.merge(client);
const parsed = merged.safeParse(_env);
if (!parsed.success) {
// eslint-disable-next-line no-console
console.error(
'❌ Invalid environment variables:',
parsed.error.flatten().fieldErrors,
`\n❌ Missing variables in .env.${APP_ENV} file, Make sure all required variables are defined in the .env.${APP_ENV} file.`,
`\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.`,
);
throw new Error(
'Invalid environment variables, Check terminal for more details ',
);
}
const Env = parsed.data;
const ClientEnv = client.parse(_clientEnv);
module.exports = {
Env,
ClientEnv,
withEnvSuffix,
};