Skip to content

Commit

Permalink
🐛 Update connectorUpdate script and fix doc
Browse files Browse the repository at this point in the history
  • Loading branch information
mit-27 committed Jul 25, 2024
1 parent be34cd6 commit 2ad0a7f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/open-source/contributors/build.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ For the sake of this guide, let's map the common object `contact` under `crm` ca
docker build -t validate_connectors -f ./packages/api/Dockerfile.validate-connectors .
```
```bash
docker run -v $(pwd):/app/ -e VERTICAL=crm -e OBJECT_TYPE=contact validate_connectors
docker run -v "$(pwd):/app/" -e VERTICAL=crm -e OBJECT_TYPE=contact validate_connectors
```

The script will automatically scan the `/crm/contact/services` folder and detect any new service folder so all dependencies and imports are updated across the codebase.
Expand Down
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 2ad0a7f

Please sign in to comment.