UNPKG

@posthog/wizard

Version:

The PostHog wizard helps you to configure your project

138 lines 5.68 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.DefaultMCPClient = exports.MCPClient = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const jsonc = __importStar(require("jsonc-parser")); const defaults_1 = require("./defaults"); class MCPClient { name; } exports.MCPClient = MCPClient; class DefaultMCPClient extends MCPClient { name = 'Default'; constructor() { super(); } getServerPropertyName() { return 'mcpServers'; } getServerConfig(apiKey, type, selectedFeatures, local, region) { return (0, defaults_1.getDefaultServerConfig)(apiKey, type, selectedFeatures, local, region); } async isServerInstalled(local) { try { const configPath = await this.getConfigPath(); if (!fs.existsSync(configPath)) { return false; } const configContent = await fs.promises.readFile(configPath, 'utf8'); const config = jsonc.parse(configContent); const serverPropertyName = this.getServerPropertyName(); const serverName = local ? 'posthog-local' : 'posthog'; return (serverPropertyName in config && serverName in config[serverPropertyName]); } catch { return false; } } async addServer(apiKey, selectedFeatures, local, region) { return this._addServerType(apiKey, 'sse', selectedFeatures, local, region); } async _addServerType(apiKey, type, selectedFeatures, local, region) { try { const configPath = await this.getConfigPath(); const configDir = path.dirname(configPath); await fs.promises.mkdir(configDir, { recursive: true }); const serverPropertyName = this.getServerPropertyName(); let configContent = ''; let existingConfig = {}; if (fs.existsSync(configPath)) { configContent = await fs.promises.readFile(configPath, 'utf8'); existingConfig = jsonc.parse(configContent) || {}; } const newServerConfig = this.getServerConfig(apiKey, type, selectedFeatures, local, region); const typedConfig = existingConfig; if (!typedConfig[serverPropertyName]) { typedConfig[serverPropertyName] = {}; } const serverName = local ? 'posthog-local' : 'posthog'; typedConfig[serverPropertyName][serverName] = newServerConfig; const edits = jsonc.modify(configContent, [serverPropertyName, serverName], newServerConfig, { formattingOptions: { tabSize: 2, insertSpaces: true, }, }); const modifiedContent = jsonc.applyEdits(configContent, edits); await fs.promises.writeFile(configPath, modifiedContent, 'utf8'); return { success: true }; } catch { return { success: false }; } } async removeServer(local) { try { const configPath = await this.getConfigPath(); if (!fs.existsSync(configPath)) { return { success: false }; } const configContent = await fs.promises.readFile(configPath, 'utf8'); const config = jsonc.parse(configContent); const serverPropertyName = this.getServerPropertyName(); const serverName = local ? 'posthog-local' : 'posthog'; if (serverPropertyName in config && serverName in config[serverPropertyName]) { const edits = jsonc.modify(configContent, [serverPropertyName, serverName], undefined, { formattingOptions: { tabSize: 2, insertSpaces: true, }, }); const modifiedContent = jsonc.applyEdits(configContent, edits); await fs.promises.writeFile(configPath, modifiedContent, 'utf8'); return { success: true }; } } catch { // } return { success: false }; } } exports.DefaultMCPClient = DefaultMCPClient; //# sourceMappingURL=MCPClient.js.map