Skip to content

Commit

Permalink
Merge pull request #425 from mit-27/fix-validate-connector-script
Browse files Browse the repository at this point in the history
Fix validate-connector script
  • Loading branch information
naelob authored May 13, 2024
2 parents c3a2c05 + c206663 commit d481042
Showing 1 changed file with 47 additions and 6 deletions.
53 changes: 47 additions & 6 deletions packages/api/scripts/connectorUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)}';`;
Expand All @@ -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
Expand All @@ -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}`);
Expand All @@ -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 =)`,
);
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d481042

Please sign in to comment.