-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconfig.ts
96 lines (88 loc) · 2.84 KB
/
config.ts
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
const debugLog = console.log; // eslint-disable-line no-console
/*
Note: In order to keep server-only secrets safe, Next.js replaces process.env.foo with the correct
value at build time. This means that process.env is not a standard JavaScript object, so you’re
not able to use object destructuring. Environment variables must be referenced as e.g.
process.env.NEXT_PUBLIC_PUBLISHABLE_KEY, not const { NEXT_PUBLIC_PUBLISHABLE_KEY } = process.env.
*/
const env = {
DEBUG_CONFIG: process.env.DEBUG_CONFIG,
HUB_AUTH_TOKEN: process.env.HUB_AUTH_TOKEN,
HUB_BASE_URL: process.env.HUB_BASE_URL,
HUB_GUI_URL: process.env.HUB_GUI_URL,
HUB_PROJECTUID: process.env.HUB_PROJECTUID,
NEXT_PUBLIC_BUILD_VERSION: process.env.NEXT_PUBLIC_BUILD_VERSION,
NEXT_PUBLIC_COMPANY_NAME: process.env.NEXT_PUBLIC_COMPANY_NAME,
DATABASE_URL: process.env.DATABASE_URL,
READ_ONLY: process.env.READ_ONLY,
NOTEHUB_PROVIDER: process.env.NOTEHUB_PROVIDER,
};
const optionalEnvVar = (varName: keyof typeof env, defaultValue: string) => {
const val = env[varName];
if (val === undefined) {
return defaultValue;
}
return val;
};
const requiredEnvVar = (varName: keyof typeof env) => {
const val = env[varName];
if (!val) {
throw new Error(
`${varName} is not set in the environment. See .env.example for help.`
);
}
return val;
};
const Config = {
isBuildVersionSet() {
return !!optionalEnvVar("NEXT_PUBLIC_BUILD_VERSION", "");
},
// These are getters so undefined required variables do not throw errors at build time.
get buildVersion() {
return optionalEnvVar("NEXT_PUBLIC_BUILD_VERSION", "ver n/a");
},
get companyName() {
return optionalEnvVar("NEXT_PUBLIC_COMPANY_NAME", "Nada Company");
},
get debugConfig() {
return Boolean(optionalEnvVar("DEBUG_CONFIG", ""));
},
get hubProjectUID() {
return requiredEnvVar("HUB_PROJECTUID");
},
get hubAuthToken() {
return requiredEnvVar("HUB_AUTH_TOKEN");
},
get hubBaseURL() {
return optionalEnvVar("HUB_BASE_URL", "https://api.notefile.net");
},
get hubGuiURL() {
return optionalEnvVar("HUB_GUI_URL", "https://notehub.io");
},
get databaseURL() {
const getVar = this.notehubProvider ? optionalEnvVar : requiredEnvVar;
return getVar("DATABASE_URL", "");
},
get readOnly() {
return Boolean(optionalEnvVar("READ_ONLY", ""));
},
get notehubProvider() {
return Boolean(optionalEnvVar("NOTEHUB_PROVIDER", ""));
},
};
const toString = (c: typeof Config | typeof env) => {
const indent = 2;
return JSON.stringify(c, undefined, indent);
};
if (Config.debugConfig) {
try {
debugLog(`Environment: ${toString(env)}`);
debugLog(`Derived config: ${toString(Config)}`);
} catch (error) {
debugLog(error);
debugLog(
`Program isn't configured fully and likely won't work until it is.`
);
}
}
export default Config;