UNPKG

@sap/cli-core

Version:

Command-Line Interface (CLI) Core Module

91 lines (90 loc) 3.58 kB
import { createRequire } from "module"; import { isEqual, merge } from "lodash-es"; import { get as getLoggerExt } from "../logger/index.js"; import { getInfoFromTenant, parseTenant } from "../utils/utils.js"; import { CLI_DEPRECATED, CLI_DEPRECATION_MESSAGE, CLI_DESCRIPTION, CLI_DISCOVERY_PATHS, CLI_GENERIC_OPTIONS_HELP, CLI_NAME, CLI_PACKAGE_NAME, CLI_SAP_HELP, CLI_SUPPORTED_AUTHENTICATION_METHODS, CLI_VERSION, CONFIG_PASSCODE_FUNCTION, } from "../constants.js"; const CUSTOM_HOST = "tenant.host"; const CUSTOM_PASSCODE_URL = "tenant.passcodeUrl"; const CUSTOM_PUBLIC_FQDN = "tenant.publicfqdn"; const require = createRequire(import.meta.url); let config; const CONFIGS_TO_KEEP = [ CONFIG_PASSCODE_FUNCTION, CLI_DESCRIPTION, CLI_DISCOVERY_PATHS, CLI_NAME, CLI_PACKAGE_NAME, CLI_SAP_HELP, CLI_SUPPORTED_AUTHENTICATION_METHODS, CLI_VERSION, CLI_DEPRECATED, CLI_DEPRECATION_MESSAGE, CLI_GENERIC_OPTIONS_HELP, ]; export const initialize = () => { const keep = {}; for (const keepConfig of CONFIGS_TO_KEEP) { keep[keepConfig] = config?.[keepConfig]; } config = {}; for (const keepConfig of CONFIGS_TO_KEEP) { if (keep[keepConfig]) { config[keepConfig] = keep[keepConfig]; } } }; initialize(); const getLogger = () => getLoggerExt("config"); export const setCustomConfig = () => { if (process.env.SUPPRESS_NO_CONFIG_WARNING === undefined) { process.env.SUPPRESS_NO_CONFIG_WARNING = "true"; } // This has to be loaded via dynamic import to ensure that the env variable is set before the config is loaded. const customConfig = require("config"); const { trace, debug } = getLogger(); debug("setting custom configuration"); const sources = customConfig.util.getConfigSources(); trace("config.custom.getConfigSources", JSON.stringify(sources, null, 2)); trace("config.custom.NODE_CONFIG_DIR", customConfig.util.getEnv("NODE_CONFIG_DIR")); trace("config.custom.NODE_ENV", customConfig.util.getEnv("NODE_ENV")); trace("config.custom.NODE_CONFIG", JSON.stringify(customConfig.util.getEnv("NODE_CONFIG"), null, 2)); trace("config.custom.HOSTNAME", customConfig.util.getEnv("HOSTNAME")); trace("config.custom.NODE_APP_INSTANCE", customConfig.util.getEnv("NODE_APP_INSTANCE")); if (customConfig.has(CUSTOM_HOST)) { config.host = customConfig.get(CUSTOM_HOST); config.hostSetFromCustomConfig = true; } if (customConfig.has(CUSTOM_PASSCODE_URL)) { config.passcodeUrl = customConfig.get(CUSTOM_PASSCODE_URL); } if (customConfig.has(CUSTOM_PUBLIC_FQDN)) { config.publicfqdn = parseTenant(customConfig.get(CUSTOM_PUBLIC_FQDN)); } }; setCustomConfig(); const setTenant = (tenant) => { const info = getInfoFromTenant(tenant, config.verbose); // eslint-disable-next-line @typescript-eslint/no-use-before-define set({ ...info, newTenant: tenant }); }; export const set = (c) => { const { trace } = getLogger(); const prev = JSON.parse(JSON.stringify(config)); if (c.tenant) { setTenant(c.tenant); } else { config = merge(config, c); if (config.newTenant) { config.tenant = config.newTenant; delete config.newTenant; } if (c.data) { config.data = c.data; } if (!isEqual(prev, config)) { trace("configuration changed. previous: %s, current: %s", JSON.stringify(prev), JSON.stringify(config)); } } }; export const get = () => config;