UNPKG

@constructor-io/constructorio-connect-cli

Version:

CLI tool to enable users to interface with the Constructor Connect Ecosystem

111 lines (110 loc) 4.36 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildConfigFromConnections = buildConfigFromConnections; exports.buildConnectionsDictFromList = buildConnectionsDictFromList; exports.buildConfigString = buildConfigString; /** * 1. Receive connections list, and transform it into the connections dictionary. * - Store only connectionId, name, and slug. * 2. Create the config object from a default config. * 3. Create a connections environments array by making a filtered array of connections for each environment. * 4. Map each object in the environments array to expand out */ const path_1 = __importDefault(require("path")); const environmentOrder = { development: 1, qa: 2, production: 3, }; /** * Builds the config object from the connections list. * @param connections * @param shouldCreateMappingTemplates * @returns the config object */ function buildConfigFromConnections(connections, shouldCreateMappingTemplates) { const environments = getUniqueConnectionEnvs(connections); return { helpers: path_1.default.join("src", "templates", "helpers.jsonata"), environments: buildTemplateEnvironments(environments, connections, shouldCreateMappingTemplates), }; } /** * Builds a dictionary of connections from array of connections sorted by environment and name. * @param connections Array of connections * @returns Dictionary of connections */ function buildConnectionsDictFromList(connections) { const sortedConnections = connections .filter((conn) => conn.name) .sort((a, b) => { // MAX_SAFE_INTEGER used just in case there are other environments not accounted for const envOrderA = environmentOrder[a.environment] ?? Number.MAX_SAFE_INTEGER; const envOrderB = environmentOrder[b.environment] ?? Number.MAX_SAFE_INTEGER; return ( /** * Sorts connections first by environment order, then by name. * * Example result: * * Dev A integration * Dev B integration * QA integration * Prod integration **/ envOrderA - envOrderB || a.name.localeCompare(b.name)); }); return sortedConnections.reduce((dict, conn) => { dict[conn.name] = { id: conn.id, slug: conn.slug, environment: conn.environment, }; return dict; }, {}); } function getUniqueConnectionEnvs(connections) { return connections.reduce((envs, conn) => { if (!envs.includes(conn.environment)) envs.push(conn.environment); return envs; }, []); } function buildTemplateEnvironments(environments, connections, shouldCreateMappingTemplates) { return environments.map((env) => { return { environment: env, templates: [ { connection_ids: connections .filter((conn) => conn.environment === env) .map((conn) => conn.id), paths: { item: path_1.default.join("src", "templates", "item", "item.jsonata"), variation: path_1.default.join("src", "templates", "variation", "variation.jsonata"), item_group: path_1.default.join("src", "templates", "item_group", "item_group.jsonata"), ...(shouldCreateMappingTemplates && { mapping: path_1.default.join("src", "templates", "mapping", "mapping.jsonata"), }), }, }, ], }; }); } function buildConfigString(config, connections) { let configString = JSON.stringify(config, null, 2); for (const connectionName in connections) { const conn = connections[connectionName]; /** * We need to escape the name to ensure the JSON won't break, in case * there are special characters in the connection name. */ const escapedName = JSON.stringify(connectionName); configString = configString.replace(`"${conn.id}"`, `connections[${escapedName}].id`); } return configString; }