UNPKG

pig-dam-cfg

Version:

Pig DAM's configuration information server

86 lines (85 loc) 2.73 kB
"use strict"; /** * Date: 6/12/20 * Time: 11:22 PM * @license MIT (see project's LICENSE file) */ Object.defineProperty(exports, "__esModule", { value: true }); exports.loadManifestConfiguration = exports.getClusterManifest = void 0; const _ = require("lodash"); const path_1 = require("path"); const pig_dam_core_1 = require("pig-dam-core"); const path_2 = require("./path"); /******************** * Public Interface ********************/ /** * Loads the manifest pointed to by `path` * @throws {Error} */ function getClusterManifest(path = path_2.getResourcePath("manifest.json")) { const manifest = loadManifestConfiguration(path); const cluster = normalizeClusterConfiguration(manifest.cluster, manifest.deployment); return { cluster, environment: manifest.environment, settings: manifest.settings }; } exports.getClusterManifest = getClusterManifest; /******************** * Private Interface ********************/ /** * Loads the cluster configuration pointed to by `clusterPath` * @throws {Error} */ function loadClusterConfiguration(path) { try { return require(path); } catch (error) { throw new pig_dam_core_1.PigError({ error, message: `unable to load cluster configuration "${path}" - ${error.message}` }); } } /** * Loads the manifest pointed to by `path`. And will make sure the `cluster` is loaded * if it is referenced by a path. * @throws {Error} */ function loadManifestConfiguration(path) { try { const manifest = require(path); if (typeof manifest.cluster === "string") { // paths should be relative to the manifest const clusterPath = path_1.resolve(path_1.parse(path).dir, manifest.cluster); manifest.cluster = loadClusterConfiguration(clusterPath); } return manifest; } catch (error) { throw new pig_dam_core_1.PigError({ error, message: `unable to load manifest "${path}" - ${error.message}` }); } } exports.loadManifestConfiguration = loadManifestConfiguration; /** * Transforms our internal representation into one that reflects the current deployment. Which means: * - we flatten the server structure so that each service points to its state as per the `deployment` spec. */ function normalizeClusterConfiguration(configuration, deployment) { configuration = _.cloneDeep(configuration); _.forEach(configuration, (value, key) => { if ("server" in value) { value.server = (_.get(deployment, key) === "debug") ? value.server.debug : value.server.docker; } }); return configuration; }