From c2066633da49ea60105ae7fb280f1d5c03fbf037 Mon Sep 17 00:00:00 2001 From: mit-27 Date: Sat, 11 May 2024 01:47:12 -0400 Subject: [PATCH] Fix connectorUpdate script --- packages/api/scripts/connectorUpdate.js | 53 ++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/packages/api/scripts/connectorUpdate.js b/packages/api/scripts/connectorUpdate.js index ceae4abbf..97943a4be 100755 --- a/packages/api/scripts/connectorUpdate.js +++ b/packages/api/scripts/connectorUpdate.js @@ -30,14 +30,36 @@ function replaceRelativePaths(path) { return path; } +// Function to extract possible provider to generate import statements for new service types +function getProvidersForImportStatements(file, allPossibleProviders, objectType) { + let fileContent = fs.readFileSync(file, 'utf8'); + const possibleImports = allPossibleProviders.filter((provider) => { + const name = + provider.substring(0, 1).toUpperCase() + + provider.substring(1) + + objectType.substring(0, 1).toUpperCase() + objectType.substring(1); + const inputObjPattern = new RegExp(`(${name}Input)`); + const outputObjPattern = new RegExp(`(${name}Output)`); + + if (inputObjPattern.test(fileContent) || outputObjPattern.test(fileContent)) { + return false + } + return true; + }); + + return possibleImports; + +} + // Function to generate import statements for new service types -function generateImportStatements(serviceNames, basePath, objectType) { - return serviceNames.map((serviceName) => { +function generateImportStatements(possibleProviders, basePath, objectType) { + + return possibleProviders.map((serviceName) => { const importPath = `${basePath}/${serviceName}/types`; const name = serviceName.substring(0, 1).toUpperCase() + serviceName.substring(1) + - objectType; + objectType.substring(0, 1).toUpperCase() + objectType.substring(1); return `import { ${name}Input, ${name}Output } from '${replaceRelativePaths( importPath, )}';`; @@ -46,6 +68,7 @@ function generateImportStatements(serviceNames, basePath, objectType) { function updateTargetFile(file, importStatements, serviceNames, objectType) { let fileContent = fs.readFileSync(file, 'utf8'); + objectType = objectType.charAt(0).toUpperCase() + objectType.slice(1); if (importStatements.length > 0) { // Append the import statements @@ -60,6 +83,12 @@ function updateTargetFile(file, importStatements, serviceNames, objectType) { const outputTypeName = `${typeName}Output`; // Update OriginalObjectTypeInput + + // checking whether OriginalObjectTypeInput assigns null + const inputNullRegex = new RegExp(`(export type Original${objectType}Input = null)`); + if (inputNullRegex.test(fileContent)) { + fileContent = fileContent.replace(inputNullRegex, `export type Original${objectType}Input =`) + } const inputRegex = new RegExp(`(export type Original${objectType}Input =)`); if (inputRegex.test(fileContent)) { fileContent = fileContent.replace(inputRegex, `$1\n | ${inputTypeName}`); @@ -69,6 +98,13 @@ function updateTargetFile(file, importStatements, serviceNames, objectType) { } // Update OriginalObjectTypeOutput + + // checking whether OriginalObjectTypeInput assigns null + const outputNullRegex = new RegExp(`(export type Original${objectType}Input = null)`); + if (outputNullRegex.test(fileContent)) { + fileContent = fileContent.replace(outputNullRegex, `export type Original${objectType}Output =`) + } + const outputRegex = new RegExp( `(export type Original${objectType}Output =)`, ); @@ -142,7 +178,7 @@ function updateMappingsFile( // Prepare the import statement and instance declaration const importStatement = `import { ${mapperClassName} } from '../services/${newServiceName}/mappers';\n`; const instanceDeclaration = `const ${mapperInstanceName} = new ${mapperClassName}();\n`; - const mappingEntry = ` ${newServiceName.toLowerCase()}: {\n unify: ${mapperInstanceName}.unify.bind(${mapperInstanceName}),\n desunify: ${mapperInstanceName}.desunify,\n },\n`; + const mappingEntry = ` ${newServiceName.toLowerCase()}: {\n unify: ${mapperInstanceName}.unify.bind(${mapperInstanceName}),\n desunify: ${mapperInstanceName}.desunify.bind(${mapperInstanceName}),\n },\n`; // Check and append new import if it's not already present if (!fileContent.includes(importStatement)) { @@ -300,6 +336,8 @@ function updateObjectTypes(baseDir, objectType, vertical) { `${vertical.toUpperCase()}_PROVIDERS`, ); + + // Compare the extracted arrays with the new service names const newProviders = newServiceDirs.filter( (service) => !currentProviders.includes(service), @@ -331,13 +369,16 @@ function updateObjectTypes(baseDir, objectType, vertical) { // Call updateMappingsFile to update the mappings file with new services updateMappingsFile(mappingsFile, newServiceDirs, objectType, vertical); + const possibleProviderForImportStatements = getProvidersForImportStatements(targetFile, newServiceDirs, objectType); + // Continue with the rest of the updateObjectTypes function... const importStatements = generateImportStatements( - newProviders, + possibleProviderForImportStatements, baseDir, objectType, ); - updateTargetFile(targetFile, importStatements, newProviders, objectType); + console.log(importStatements) + updateTargetFile(targetFile, importStatements, possibleProviderForImportStatements, objectType); } // Example usage for ticketing/team