-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathconfig.ts
More file actions
80 lines (67 loc) · 2.18 KB
/
config.ts
File metadata and controls
80 lines (67 loc) · 2.18 KB
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 Third-party Dependencies
import { warnings, type WarningName } from "@nodesecure/js-x-ray";
import { appCache, type AppConfig } from "@nodesecure/cache";
import * as i18n from "@nodesecure/i18n";
// Import Internal Dependencies
import { logger } from "./logger.js";
const experimentalWarnings = Object.entries(warnings)
.flatMap(([warning, metadata]) => ("experimental" in metadata && metadata.experimental ? [warning] : [])) as WarningName[];
// CONSTANTS
const kDefaultConfig = {
defaultPackageMenu: "info",
ignore: { flags: [], warnings: experimentalWarnings },
disableExternalRequests: false
};
export async function get(): Promise<AppConfig> {
const localLang = await i18n.getLocalLang();
try {
const config = await appCache.getConfig();
const {
defaultPackageMenu,
ignore: {
flags = [],
warnings = []
} = {},
theme,
disableExternalRequests = false,
lang = localLang
} = config;
logger.info(
// eslint-disable-next-line @stylistic/max-len
`[config|get](defaultPackageMenu: ${defaultPackageMenu}|ignore-flag: ${flags}|ignore-warnings: ${warnings}|theme: ${theme}|disableExternalRequests${disableExternalRequests})`
);
return {
defaultPackageMenu,
ignore: {
flags,
warnings
},
theme,
disableExternalRequests,
lang
};
}
catch (err: any) {
logger.error(`[config|get](error: ${err.message})`);
await appCache.updateConfig(kDefaultConfig);
logger.info(`[config|get](fallback to default: ${JSON.stringify(kDefaultConfig)})`);
return { ...kDefaultConfig, lang: localLang };
}
}
export async function set(newValue: AppConfig) {
logger.info(`[config|set](config: ${JSON.stringify(newValue)})`);
try {
await appCache.updateConfig(newValue);
logger.info("[config|set](sucess)");
}
catch (err: any) {
logger.error(`[config|set](error: ${err.message})`);
throw err;
}
const i18nLocalLang = await i18n.getLocalLang();
if (i18nLocalLang !== newValue.lang) {
logger.info(`[config|set](updating i18n lang to: ${newValue.lang})`);
await i18n.setLocalLang(newValue.lang!);
await i18n.getLanguages();
}
}