rnr-mcp-server
Version:
A Model Context Protocol (MCP) server for React Native Reusables components, providing AI assistants with access to component source code, demos, and metadata for React Native development.
100 lines (99 loc) • 3.71 kB
JavaScript
/**
* Framework selection utility for React Native Reusables MCP server
*
* This module handles switching between React Native and Expo implementations
* based on environment variables or command line arguments.
*
* Usage:
* - Set FRAMEWORK environment variable to 'react-native' or 'expo'
* - Or use --framework command line argument
* - Defaults to 'react-native' if not specified
*/
import { logInfo, logWarning } from "./logger.js";
// Default framework
const DEFAULT_FRAMEWORK = "react-native";
/**
* Get the current framework from environment or command line arguments
* @returns The selected framework ('react-native' or 'expo')
*/
export function getFramework() {
// Check command line arguments first
const args = process.argv.slice(2);
const frameworkIndex = args.findIndex((arg) => arg === "--framework" || arg === "-f");
if (frameworkIndex !== -1 && args[frameworkIndex + 1]) {
const framework = args[frameworkIndex + 1].toLowerCase();
if (framework === "react-native" || framework === "expo") {
logInfo(`Framework set to '${framework}' via command line argument`);
return framework;
}
else {
logWarning(`Invalid framework '${framework}' specified. Using default '${DEFAULT_FRAMEWORK}'`);
}
}
// Check environment variable
const envFramework = process.env.FRAMEWORK?.toLowerCase();
if (envFramework === "react-native" || envFramework === "expo") {
logInfo(`Framework set to '${envFramework}' via environment variable`);
return envFramework;
}
// Return default
logInfo(`Using default framework: '${DEFAULT_FRAMEWORK}'`);
return DEFAULT_FRAMEWORK;
}
/**
* Get the axios implementation based on the current framework
* @returns The appropriate axios implementation
*/
export async function getAxiosImplementation() {
const framework = getFramework();
try {
if (framework === "expo") {
// Dynamic import for Expo implementation
const module = await import("./axios-expo.js");
return module.axios;
}
else {
// Dynamic import for React Native implementation (default)
const module = await import("./axios.js");
return module.axios;
}
}
catch (error) {
// Fallback to default implementation if import fails
const module = await import("./axios.js");
return module.axios;
}
}
/**
* Get framework-specific information for help text
* @returns Framework information object
*/
export function getFrameworkInfo() {
const framework = getFramework();
return {
current: framework,
repository: "mrzachnugent/react-native-reusables",
fileExtension: ".tsx",
description: framework === "expo"
? "Expo components from React Native Reusables"
: "React Native components from React Native Reusables",
};
}
/**
* Validate framework selection and provide helpful feedback
*/
export function validateFrameworkSelection() {
const framework = getFramework();
const info = getFrameworkInfo();
logInfo(`MCP Server configured for ${framework.toUpperCase()} framework`);
logInfo(`Repository: ${info.repository}`);
logInfo(`File extension: ${info.fileExtension}`);
logInfo(`Description: ${info.description}`);
// Provide helpful information about switching frameworks
if (framework === "react-native") {
logInfo("To switch to Expo: set FRAMEWORK=expo or use --framework expo");
}
else {
logInfo("To switch to React Native: set FRAMEWORK=react-native or use --framework react-native");
}
}