UNPKG

@constructor-io/constructorio-connect-cli

Version:

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

105 lines (104 loc) 4.34 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.refreshConnectionsList = refreshConnectionsList; const fs = __importStar(require("fs/promises")); const core_1 = require("@oclif/core"); const config_1 = require("../customer/config"); const path_1 = require("../customer/path"); const get_connections_request_1 = require("../http/get-connections-request"); const render_prompt_1 = require("../prompt-data/render-prompt"); const build_config_file_1 = require("./build-config-file"); const lint_config_file_1 = require("./lint-config-file"); const should_update_connections_list_1 = require("./should-update-connections-list"); const ux_action_1 = require("./ux-action"); const fs_1 = require("fs"); async function refreshConnectionsList() { try { const shouldUpdate = await checkConfigFileState(); if (!shouldUpdate) { return; } await updateConfigFile(); if ((0, fs_1.existsSync)("eslint.config.js") || (0, fs_1.existsSync)("eslint.config.mjs")) { await (0, lint_config_file_1.lintConfigFile)(); } } catch (error) { core_1.ux.stdout(`Error refreshing connections list: ${error.message}`); } } async function fetchNewConnections() { return (0, build_config_file_1.buildConnectionsDictFromList)(await (0, get_connections_request_1.getConnections)({ showLogs: false, })); } async function promptForUpdate() { return await (0, render_prompt_1.renderPrompt)({ promptMessage: "💡 Your connections in connectrc.js are out of date. Do you want to update it?", choices: [ { name: "Yes", value: true }, { name: "No", value: false }, ], }); } async function checkConfigFileState() { const newConnections = await fetchNewConnections(); const { fileContent, existingConnections } = await (0, config_1.getRepositoryConnectionsAndConfig)(); if (!fileContent) { return false; } if (!(0, should_update_connections_list_1.shouldUpdateConnectionList)(existingConnections, newConnections)) { return false; } return await promptForUpdate(); } async function updateConfigFile() { const formattedConnections = await fetchNewConnections(); const rcPath = (0, path_1.getCustomerOSSpecificPath)("connectrc.js"); const { fileContent } = await (0, config_1.getRepositoryConnectionsAndConfig)({ shouldPrintExecutionWarnings: false, }); if (!fileContent) { return; } await (0, ux_action_1.uxAction)("Updating connectrc.js", async () => { const updatedContent = fileContent.replace( // Match the connections object in connectrc.js /const\s+connections\s*=\s*({[\s\S]*?});/, `const connections = ${JSON.stringify(formattedConnections, null, 2)};`); await fs.writeFile(rcPath, updatedContent, "utf-8"); })(); core_1.ux.stdout("🎉 Successfully updated connections in connectrc.js"); }