UNPKG

@adpt/cloud

Version:
143 lines 4.55 kB
"use strict"; /* * Copyright 2019 Unbounded Systems, LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("@adpt/utils"); /** * Combine multiple {@link Environment} objects into a single array of * {@link EnvPair} objects. Returns `undefined` if there are no `Environment` * objects provided. * @remarks * If more than one `Environment` object specifies the same environment variable * name, the last one present in the array of arguments takes precedence. * @public */ function mergeEnvPairs(...envs) { const vals = new Map(); for (const e of envs) { if (!e) continue; if (Array.isArray(e)) e.forEach((pair) => vals.set(pair.name, pair)); else Object.keys(e).map((name) => vals.set(name, { name, value: e[name] })); } return vals.size ? [...vals.values()] : undefined; } exports.mergeEnvPairs = mergeEnvPairs; /** * Combine multiple {@link Environment} objects into a single * {@link EnvSimple} object. Returns `undefined` if there are no `Environment` * objects provided. * @remarks * If more than one `Environment` object specifies the same environment variable * name, the last one present in the array of arguments takes precedence. * @public */ function mergeEnvSimple(...envs) { let ret; envs.forEach((e) => { if (!e) return; if (!ret) ret = {}; if (Array.isArray(e)) { e.forEach((pair) => ret[pair.name] = pair.value); } else { Object.assign(ret, e); } }); return ret; } exports.mergeEnvSimple = mergeEnvSimple; /** * Renames all variables in `e` based on `mapping` * * @param e - {@link Environment} to rename * @param mapping - Object with `(key, value)` pairs that are `(originalName, newName)` pairs. * * @returns A new {@link Environment} object with all variables renamed according to `mapping` * * @public */ function renameEnvVars(e, mapping) { return updateEnvVars(e, (name, value) => ({ name: mapping[name] || name, value })); } exports.renameEnvVars = renameEnvVars; /** * Find the value of an environment variable in an {@link Environment} * * @param e - {@link Environment} to search * @param name - variable to search for * @returns the value of the variable name in e, or undefined if not found * * @public */ function lookupEnvVar(e, name) { if (Array.isArray(e)) { const pair = e.find((p) => p.name === name); if (pair === undefined) return undefined; return pair.value; } else { return e[name]; } } exports.lookupEnvVar = lookupEnvVar; /** * Updates the names and/or values of variables in an {@link Environment} * * @param e - The source {@link Environment} * @param upd - Updated function that returns an EnvPair with the new name and value of the variable * @returns - A new {@link Environment} that is identical to `e` except for the updates done by `upd` * * @public */ function updateEnvVars(e, upd) { if (Array.isArray(e)) { return e.map((p) => upd(p.name, p.value)).filter(utils_1.notNull); } const ret = {}; for (const k in e) { if (Object.hasOwnProperty.call(e, k)) { const res = upd(k, e[k]); if (utils_1.notNull(res)) { ret[res.name] = res.value; } } } return ret; } exports.updateEnvVars = updateEnvVars; /** * Formats an {@link Environment} for printing in human-readable format. * * @param env - The environment to be printed. * @returns - A string representation of the environment for use in logging * or debugging. * * @public */ function formatEnvVars(env) { const pairs = mergeEnvPairs(env); if (!pairs || pairs.length === 0) return "<empty>"; return pairs.map((p) => `${p.name}: '${p.value}'`).join("\n"); } exports.formatEnvVars = formatEnvVars; //# sourceMappingURL=env.js.map