import { IERPConnector } from "../types/erp-connector.js"; import { sdkLogger as logger } from "../services/reporting-service/logger.js"; import { pathToFileURL } from "url"; /** * Helper function to dynamically import and create connector instance from a file path * @param connectorPath - The file path to the connector module * @returns A new instance of the IERPConnector */ export const createConnectorFromPath = async ( connectorPath: string ): Promise => { try { // Some detailed debug logging to help with troubleshooting multi-platform connector paths const pathParts = connectorPath.split('/'); const filename = pathParts[pathParts.length - 1]; // Use silly for development-time only logging logger.silly("createConnectorFromPath:", { connectorPath, pathParts, filename, finalImport: pathToFileURL(connectorPath).href, }); const connectorModule = await import(pathToFileURL(connectorPath).href); // Get the default export or named export const ConnectorClass = connectorModule.default || connectorModule[Object.keys(connectorModule)[0]]; if (!ConnectorClass) { throw new Error(`No connector class found in module: ${connectorPath}`); } // Create new instance of the connector return new ConnectorClass(); } catch (error) { logger.error( `Failed to create connector instance from path: ${connectorPath}`, { error } ); throw error; } };