UNPKG

@dbs-portal/core-module-registry

Version:

Core module registry system for automatic module discovery and registration

213 lines 5.64 kB
/** * Environment Detection Utilities * * Provides utilities to detect the current runtime environment * and conditionally load appropriate modules. */ /** * Detect current environment */ export function detectEnvironment() { // Check for browser environment const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof navigator !== 'undefined'; // Check for Node.js environment const isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null && !isBrowser; // Check for Web Worker environment const isWebWorker = typeof importScripts === 'function' && typeof navigator !== 'undefined' && !isBrowser; // Check for test environment const isTest = (typeof process !== 'undefined' && process.env?.['NODE_ENV'] === 'test') || (typeof global !== 'undefined' && global.__TEST__) || typeof jest !== 'undefined' || typeof vitest !== 'undefined'; // Check for development environment const isDevelopment = (typeof process !== 'undefined' && process.env?.['NODE_ENV'] === 'development') || (isBrowser && window.location?.hostname === 'localhost'); // Check for production environment const isProduction = (typeof process !== 'undefined' && process.env?.['NODE_ENV'] === 'production') || (!isDevelopment && !isTest); return { isBrowser, isNode, isWebWorker, isTest, isDevelopment, isProduction }; } // Global environment info export const ENV = detectEnvironment(); /** * Conditional module loader */ export async function loadConditionalModule(nodeModule, browserModule) { if (ENV.isNode) { return await nodeModule(); } else { return await browserModule(); } } /** * Conditional synchronous module loader */ export function loadConditionalModuleSync(nodeModule, browserModule) { if (ENV.isNode) { return nodeModule(); } else { return browserModule(); } } /** * Get platform-specific path separator */ export function getPathSeparator() { return ENV.isNode && process.platform === 'win32' ? '\\' : '/'; } /** * Get current working directory */ export function getCurrentDirectory() { if (ENV.isNode && typeof process !== 'undefined') { return process.cwd(); } else if (ENV.isBrowser && typeof window !== 'undefined') { return window.location.pathname; } else { return '/'; } } /** * Check if running in development mode */ export function isDevelopment() { return ENV.isDevelopment; } /** * Check if running in production mode */ export function isProduction() { return ENV.isProduction; } /** * Check if running in test mode */ export function isTest() { return ENV.isTest; } /** * Check if running in browser */ export function isBrowser() { return ENV.isBrowser; } /** * Check if running in Node.js */ export function isNode() { return ENV.isNode; } /** * Get environment name */ export function getEnvironmentName() { if (ENV.isTest) return 'test'; if (ENV.isDevelopment) return 'development'; if (ENV.isProduction) return 'production'; return 'unknown'; } /** * Get platform name */ export function getPlatformName() { if (ENV.isNode) return 'node'; if (ENV.isBrowser) return 'browser'; if (ENV.isWebWorker) return 'webworker'; return 'unknown'; } /** * Create environment-specific logger */ export function createLogger(prefix = '') { const logPrefix = `[${getPlatformName()}${prefix ? `:${prefix}` : ''}]`; return { debug: (...args) => { if (ENV.isDevelopment) { console.debug(logPrefix, ...args); } }, info: (...args) => { console.info(logPrefix, ...args); }, warn: (...args) => { console.warn(logPrefix, ...args); }, error: (...args) => { console.error(logPrefix, ...args); } }; } /** * Environment-specific feature flags */ export const FEATURES = { // File system operations canReadFiles: ENV.isNode, canWriteFiles: ENV.isNode, canAccessFileSystem: ENV.isNode, // Network operations canFetch: ENV.isBrowser || ENV.isNode, canMakeHttpRequests: true, // Module operations canDynamicImport: true, canRequire: ENV.isNode, // Discovery features canScanPackages: ENV.isNode, canScanFileSystem: ENV.isNode, canUseGlob: ENV.isNode, // Caching canUseLocalStorage: ENV.isBrowser, canUseSessionStorage: ENV.isBrowser, canUseMemoryCache: true, // Development features canHotReload: ENV.isDevelopment, canShowDevTools: ENV.isDevelopment && ENV.isBrowser }; /** * Get feature availability */ export function hasFeature(feature) { return FEATURES[feature]; } /** * Assert feature availability */ export function assertFeature(feature, message) { if (!hasFeature(feature)) { throw new Error(message || `Feature '${feature}' is not available in ${getPlatformName()} environment`); } } /** * Warn about missing features */ export function warnMissingFeature(feature, fallback) { if (!hasFeature(feature)) { const logger = createLogger('feature-check'); logger.warn(`Feature '${feature}' is not available in ${getPlatformName()} environment${fallback ? `, using fallback: ${fallback}` : ''}`); } } //# sourceMappingURL=environment.js.map