-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(backend): Change plugin.module.ts while create/delete plugin
- Loading branch information
1 parent
3c15e82
commit 2fc2d6d
Showing
9 changed files
with
91 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,8 @@ | ||
// ! DO NOT TOUCH THIS FILE!!! IT IS GENERATED BY VITNODE-CLI | ||
|
||
import { Module } from '@nestjs/common'; | ||
|
||
import { WelcomeModule } from './welcome/welcome.module'; | ||
// ! === IMPORT === | ||
|
||
@Module({ | ||
imports: [ | ||
WelcomeModule, | ||
// ! === MODULE === | ||
], | ||
imports: [WelcomeModule], | ||
}) | ||
export class PluginsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
packages/backend/src/core/admin/plugins/delete/contents.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 0 additions & 22 deletions
22
packages/backend/src/core/admin/plugins/helpers/files/change/contents.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
packages/backend/src/core/admin/plugins/helpers/files/change/update-module-file.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { ABSOLUTE_PATHS_BACKEND } from '@/index'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { existsSync } from 'fs'; | ||
import { readFile, writeFile } from 'fs/promises'; | ||
import { join } from 'path'; | ||
|
||
import { changeCodePluginToCapitalLetters } from '../../change-code-plugin-to-capital-letters'; | ||
|
||
@Injectable() | ||
export class UpdateModuleFileAdminPluginsService { | ||
async updatePluginModuleFile(code: string, action: 'add' | 'delete') { | ||
const filePath = join(ABSOLUTE_PATHS_BACKEND.plugins, 'plugins.module.ts'); | ||
if (!existsSync(filePath)) { | ||
throw new Error(`File not found: ${filePath}`); | ||
} | ||
|
||
const fileContent = await readFile(filePath, 'utf8'); | ||
|
||
// Regular expressions to extract import statements and imports array | ||
const importRegex = /import { (\w+) } from ['"](.*)['"];/g; | ||
const importsArrayRegex = /imports:\s*\[([^\]]*)\]/; | ||
|
||
const existingImports = new Map<string, string>(); | ||
const existingModuleNames = new Set<string>(); | ||
|
||
// Extract existing import statements | ||
let match: null | RegExpExecArray; | ||
while ((match = importRegex.exec(fileContent)) !== null) { | ||
const moduleName = match[1]; | ||
const modulePath = match[2]; | ||
|
||
if (moduleName === 'Module' && modulePath === '@nestjs/common') { | ||
continue; // Skip the NestJS Module import | ||
} | ||
|
||
existingImports.set(moduleName, modulePath); | ||
existingModuleNames.add(moduleName); | ||
} | ||
|
||
// Extract existing module names from the imports array | ||
const importsArrayMatch = importsArrayRegex.exec(fileContent); | ||
if (importsArrayMatch?.[1]) { | ||
const modulesList = importsArrayMatch[1] | ||
.split(',') | ||
.map(m => m.trim()) | ||
.filter(m => m.length > 0); | ||
|
||
modulesList.forEach(moduleName => existingModuleNames.add(moduleName)); | ||
} | ||
|
||
// Determine the module name and path | ||
const moduleName = `${changeCodePluginToCapitalLetters(code)}Module`; | ||
const modulePath = `./${code}/${code}.module`; | ||
|
||
// Add or remove the module based on the action | ||
if (action === 'add') { | ||
existingImports.set(moduleName, modulePath); | ||
existingModuleNames.add(moduleName); | ||
} else if (action === 'delete') { | ||
existingImports.delete(moduleName); | ||
existingModuleNames.delete(moduleName); | ||
} else { | ||
throw new Error(`Invalid action: ${action}`); | ||
} | ||
|
||
// Reconstruct import statements | ||
let newFileContent = `import { Module } from '@nestjs/common';\n\n`; | ||
|
||
existingImports.forEach((modulePath, moduleName) => { | ||
newFileContent += `import { ${moduleName} } from '${modulePath}';\n`; | ||
}); | ||
|
||
// Reconstruct the @Module decorator | ||
const allModuleNames = Array.from(existingModuleNames).join(', '); | ||
newFileContent += `\n@Module({\n imports: [${allModuleNames}],\n})\nexport class PluginsModule {}\n`; | ||
|
||
// Write the updated content back to the file | ||
await writeFile(filePath, newFileContent, 'utf8'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 1 addition & 7 deletions
8
packages/create-vitnode-app/templates/basic/apps/backend/src/plugins/plugins.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,8 @@ | ||
// ! DO NOT TOUCH THIS FILE!!! IT IS GENERATED BY VITNODE-CLI | ||
|
||
import { Module } from '@nestjs/common'; | ||
|
||
import { WelcomeModule } from './welcome/welcome.module'; | ||
// ! === IMPORT === | ||
|
||
@Module({ | ||
imports: [ | ||
WelcomeModule, | ||
// ! === MODULE === | ||
], | ||
imports: [WelcomeModule], | ||
}) | ||
export class PluginsModule {} |