Skip to content

Commit

Permalink
Merge pull request #570 from mit-27/update-with-mapper-connectorUpdate
Browse files Browse the repository at this point in the history
Update connectorUpdate script and fix doc
  • Loading branch information
naelob authored Jul 27, 2024
2 parents 14d515d + ad63865 commit 5bd577b
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions packages/api/scripts/connectorUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ function updateArrayInFile(filePath, arrayName, newArray) {
updateFileContents(filePath, newContents);
}

function updateModuleFile(moduleFile, newServiceDirs) {
function updateModuleFileForService(moduleFile, newServiceDirs) {
let moduleFileContent = fs.readFileSync(moduleFile, 'utf8');

// Generate and insert new service imports
Expand All @@ -258,6 +258,34 @@ function updateModuleFile(moduleFile, newServiceDirs) {
fs.writeFileSync(moduleFile, moduleFileContent);
}

function updateModuleFileForMapper(moduleFile, newServiceDirs, objectType) {
let moduleFileContent = fs.readFileSync(moduleFile, 'utf8');
objectType = objectType.charAt(0).toUpperCase() + objectType.slice(1);

// Generate and insert new service imports
newServiceDirs.forEach((serviceName) => {
const mapperClass =
serviceName.charAt(0).toUpperCase() + serviceName.slice(1) + objectType + 'Mapper';
const importStatement = `import { ${mapperClass} } from './services/${serviceName}/mappers';\n`;
if (!moduleFileContent.includes(importStatement)) {
moduleFileContent = importStatement + moduleFileContent;
}

// Add new service to the providers array if not already present
const providerRegex = /providers: \[\n([\s\S]*?)\n \],/;
const match = moduleFileContent.match(providerRegex);
if (match && !match[1].includes(mapperClass)) {
const updatedProviders = match[1] + `\n ${mapperClass},\n`;
moduleFileContent = moduleFileContent.replace(
providerRegex,
`providers: [\n${updatedProviders} ],`,
);
}
});

fs.writeFileSync(moduleFile, moduleFileContent);
}

function updateEnumFile(enumFilePath, newServiceDirs, vertical) {
let fileContent = fs.readFileSync(enumFilePath, 'utf8');
const base = vertical.substring(0, 1).toUpperCase() + vertical.substring(1);
Expand Down Expand Up @@ -328,7 +356,7 @@ function updateInitSQLFile(initSQLFile, newServiceDirs, vertical) {
let newLines = newServiceDirs
.map((serviceName) => {
const columnName = `${vertical.toLowerCase()}_${serviceName.toLowerCase()}`;
return ` ${columnName} boolean NOT NULL,\n`;
return ` ${columnName} boolean NULL,\n`;
})
.join('');

Expand Down Expand Up @@ -439,7 +467,8 @@ function updateObjectTypes(baseDir, objectType, vertical) {
`../src/${vertical}/${objectType.toLowerCase()}/${objectType.toLowerCase()}.module.ts`,
);

updateModuleFile(moduleFile, newServiceDirs, servicesDir);
updateModuleFileForService(moduleFile, newServiceDirs);
updateModuleFileForMapper(moduleFile, newServiceDirs, objectType)

// Path to the mappings file
// const mappingsFile = path.join(
Expand Down

0 comments on commit 5bd577b

Please sign in to comment.