-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsSubmoduleLog.ts
80 lines (76 loc) · 3.08 KB
/
tsSubmoduleLog.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
import {
currentConfig,
getPrintFn,
LogLevel,
withConfigProvideLogFn,
} from "./genericLogger";
// because this file is referenced from cloud, webapp, cypress main, cypress
// task, and device contexts, it is ignored for linting and formatting from all
// contexts, since each will have different complaints
function logOrInformUndefined(s: string | undefined) {
if (s === undefined) {
console.log("Submodule logging config: undefined");
} else {
console.log("Submodule logging config: " + s);
}
}
// because this fn is used only from within scorebridge-ts-submodule, we must
// guess-and-check as to whether we are in node, webapp prod, a webapp Cypress
// test, a webapp Cypress task, or device, and pull the env var appropriately
// for each context.
//
// cloud: node, process.env, no prefix
// webapp prod: vite, import.meta.env, VITE_
// webapp Cypress test: cypress, Cypress.env(), no prefix
// webapp Cypress task: cypress, process.env, no prefix
// device: expo, process.env, EXPO_PUBLIC_
//
// Unfortunately in expo, any reference to Cypress or to import.meta causes an
// uncatchable error, so that means that for ts-submodule code logging in the
// context of webapp prod or a webapp Cypress test, the only way to specify
// logging configuration will be via the file loggingConfig.json.
function localCurrentConfig() {
const submoduleLoggingConfigKey = "TS_SUBMODULE_SB_LOGGING_CONFIG";
let foundProcess = true;
try {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
process;
} catch (e) {
if (e instanceof ReferenceError) {
foundProcess = false;
} else {
throw e;
}
}
// Cypress test guess removed, since it relies on Cypress object which breaks expo
if (foundProcess && process.env.AWS_LAMBDA_FUNCTION_NAME) {
console.log("Submodule logging config: using process.env with no prefix:");
const processEnvNoPrefix = process.env[submoduleLoggingConfigKey];
logOrInformUndefined(processEnvNoPrefix);
return currentConfig(processEnvNoPrefix);
} else if (foundProcess && process.env.CYPRESS) {
console.log(
"Submodule logging config: using process.env with CYPRESS_TASK_ prefix:",
);
const processEnvCypressTaskPrefix =
process.env[`CYPRESS_TASK_${submoduleLoggingConfigKey}`];
logOrInformUndefined(processEnvCypressTaskPrefix);
return currentConfig(processEnvCypressTaskPrefix);
} else if (foundProcess && process.env.EXPO_PUBLIC_SB_EXPO) {
console.log(
"Submodule logging config: using process.env with EXPO_PUBLIC_ prefix:",
);
const processEnvExpoPublicPrefix =
process.env[`EXPO_PUBLIC_${submoduleLoggingConfigKey}`];
logOrInformUndefined(processEnvExpoPublicPrefix);
return currentConfig(processEnvExpoPublicPrefix);
}
// VITE guess removed since it also breaks expo
return currentConfig(undefined);
}
const config = localCurrentConfig();
export function tsSubmoduleLogFn(
catPrefix: string,
): (catSuffix: string, logLevel: LogLevel, ...addlParams: unknown[]) => void {
return withConfigProvideLogFn(config, getPrintFn)(`tsSubmodule.${catPrefix}`);
}