Skip to content

Commit

Permalink
refactor(backend): Create / delete plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
aXenDeveloper committed Oct 21, 2024
1 parent a562f8b commit eacddcb
Show file tree
Hide file tree
Showing 17 changed files with 178 additions and 115 deletions.
22 changes: 19 additions & 3 deletions packages/backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,28 +66,44 @@ export const ABSOLUTE_PATHS_BACKEND = {
),
},
frontend: {
admin_pages: join(
admin_pages_auth: join(
internalPaths.frontend,
'app',
'[locale]',
'admin',
'(auth)',
code,
),
admin_templates: join(internalPaths.frontend, 'plugins', code, 'admin'),
admin_pages: join(
internalPaths.frontend,
'app',
'[locale]',
'admin',
code,
),
default_page: join(
internalPaths.frontend,
'plugins',
code,
'templates',
'default-page.tsx',
),
pages: join(
pages: join(internalPaths.frontend, 'src', 'app', '[locale]', code),
pages_main: join(
internalPaths.frontend,
'src',
'app',
'[locale]',
'(main)',
code,
),
pages_main_layout: join(
internalPaths.frontend,
'src',
'app',
'[locale]',
'(main)',
'(layout)',
code,
),
templates: join(internalPaths.frontend, 'plugins', code, 'templates'),
Expand Down
100 changes: 60 additions & 40 deletions packages/backend/src/core/admin/plugins/create/create.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { core_plugins } from '@/database/schema/plugins';
import { CustomError } from '@/errors';
import { InternalDatabaseService } from '@/utils/database/internal_database.service';
import { Injectable } from '@nestjs/common';
import * as fs from 'fs';
import { existsSync } from 'fs';
import { mkdir, writeFile } from 'fs/promises';
import { join } from 'path';

import { ABSOLUTE_PATHS_BACKEND } from '../../../..';
import { ChangeFilesAdminPluginsService } from '../helpers/files/change/change.service';
import { ChangeFilesAdminPluginsService } from '../helpers/change-files.service';
import { CreateFilesAdminPluginsService } from '../helpers/files/create/create-files.service';
import { VerifyFilesAdminPluginsService } from '../helpers/verify-files.service';
import { ShowAdminPlugins } from '../show/show.dto';
import { CreateAdminPluginsArgs } from './create.dto';

Expand All @@ -17,8 +19,51 @@ export class CreateAdminPluginsService {
private readonly databaseService: InternalDatabaseService,
private readonly createFilesService: CreateFilesAdminPluginsService,
private readonly changeFilesService: ChangeFilesAdminPluginsService,
private readonly verifyFilesService: VerifyFilesAdminPluginsService,
) {}

private async createLanguageFiles({
code,
name,
}: {
code: string;
name: string;
}) {
const languages =
await this.databaseService.db.query.core_languages.findMany({
orderBy: (table, { asc }) => asc(table.code),
});

await Promise.all(
languages.map(async lang => {
const langPath = join(
ABSOLUTE_PATHS_BACKEND.plugin({ code }).frontend.languages,
);

if (!existsSync(langPath)) {
await mkdir(langPath, { recursive: true });
}

await writeFile(
join(langPath, `${lang.code}.json`),
JSON.stringify(
{
[code]: {},
[`admin_${code}`]: {
nav: {
title: name,
},
},
},
null,
2,
),
'utf-8',
);
}),
);
}

async create({
author,
author_url,
Expand All @@ -38,8 +83,11 @@ export class CreateAdminPluginsService {
});
}

// Modifying / Create files
this.createFilesService.createFiles({
// Verify files and folders to check if they exist
this.verifyFilesService.verifyFiles({ code });

// Create basic files
await this.createFilesService.createFiles({
author,
author_url,
code,
Expand All @@ -51,42 +99,8 @@ export class CreateAdminPluginsService {
version: '0.0.1',
version_code: 1,
});
await this.changeFilesService.changeFiles({ code, action: 'add' });

// Create lang.json file inside the plugin frontend folder
const languages =
await this.databaseService.db.query.core_languages.findMany({
orderBy: (table, { asc }) => asc(table.code),
});

languages.forEach(lang => {
const langPath = join(
ABSOLUTE_PATHS_BACKEND.plugin({ code }).frontend.languages,
);

if (!fs.existsSync(langPath)) {
fs.mkdirSync(langPath, { recursive: true });
}

fs.writeFileSync(
join(langPath, `${lang.code}.json`),
JSON.stringify(
{
[code]: {},
[`admin_${code}`]: {
nav: {
title: name,
},
},
},
null,
2,
),
'utf-8',
);
});

const data = await this.databaseService.db
const [data] = await this.databaseService.db
.insert(core_plugins)
.values({
code,
Expand All @@ -98,6 +112,12 @@ export class CreateAdminPluginsService {
})
.returning();

return data[0];
// Create i18n files
await this.createLanguageFiles({ code, name });

// Modifying / Create files
await this.changeFilesService.changeFiles({ code, action: 'add' });

return data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Injectable } from '@nestjs/common';
import { eq } from 'drizzle-orm';
import * as fs from 'fs';

import { ChangeFilesAdminPluginsService } from '../helpers/files/change/change.service';
import { ChangeFilesAdminPluginsService } from '../helpers/change-files.service';

@Injectable()
export class DeleteAdminPluginsService {
Expand Down Expand Up @@ -51,7 +51,6 @@ export class DeleteAdminPluginsService {
ABSOLUTE_PATHS_BACKEND.plugin({ code }).frontend[path],
);
});
await this.changeFilesService.setServerToRestartConfig();

await this.databaseService.db
.delete(core_plugins)
Expand All @@ -67,6 +66,8 @@ export class DeleteAdminPluginsService {
.delete(core_files_using)
.where(eq(core_files_using.plugin, code));

await this.changeFilesService.setServerToRestartConfig();

return 'Success!';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Injectable } from '@nestjs/common';
import { existsSync, promises as fs } from 'fs';
import { join } from 'path';

import { changeCodePluginToCapitalLetters } from '../../change-code-plugin-to-capital-letters';
import { changeCodePluginToCapitalLetters } from './change-code-plugin-to-capital-letters';

const { readFile, writeFile } = fs;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
PluginInfoJSONType,
} from '@/index';
import { Injectable } from '@nestjs/common';
import * as fs from 'fs';
import { existsSync } from 'fs';
import { mkdir, writeFile } from 'fs/promises';
import { join } from 'path';

import {
Expand All @@ -16,7 +17,7 @@ import {

@Injectable()
export class CreateFilesAdminPluginsService {
createFiles({ code, ...rest }: PluginInfoJSONType): void {
async createFiles({ code, ...rest }: PluginInfoJSONType) {
const folders: {
files: { content: string; name: string }[];
path: string;
Expand Down Expand Up @@ -65,29 +66,34 @@ export class CreateFilesAdminPluginsService {

// Check if folder exists
folders.forEach(folder => {
if (fs.existsSync(folder.path)) {
if (existsSync(folder.path)) {
throw new CustomError({
code: 'PLUGIN_ALREADY_EXISTS',
message: `Plugin already exists in filesystem with "${code}" code!`,
});
}
});

folders.forEach(folder => {
// Create folders
fs.mkdirSync(folder.path, { recursive: true });
await Promise.all(
folders.map(async folder => {
// Create folders
await mkdir(folder.path, { recursive: true });

// Create files
folder.files.forEach(file => {
fs.writeFile(join(folder.path, file.name), file.content, err => {
if (err) {
throw new CustomError({
code: 'ERROR_CREATING_FILE',
message: err.message,
});
}
});
});
});
// Create files
await Promise.all(
folder.files.map(async file => {
try {
await writeFile(join(folder.path, file.name), file.content);
} catch (err) {
const error = err as Error;
throw new CustomError({
code: 'ERROR_CREATING_FILE',
message: error.message,
});
}
}),
);
}),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ABSOLUTE_PATHS_BACKEND, CustomError } from '@/index';
import { Injectable } from '@nestjs/common';
import { existsSync } from 'fs';

@Injectable()
export class VerifyFilesAdminPluginsService {
verifyFiles({
code,
errorHandler,
}: {
code: string;
errorHandler?: (path: string) => void;
}) {
const pathsToFolders = [
// Frontend - pages
ABSOLUTE_PATHS_BACKEND.plugin({ code }).frontend.pages,
ABSOLUTE_PATHS_BACKEND.plugin({ code }).frontend.pages_main,
ABSOLUTE_PATHS_BACKEND.plugin({ code }).frontend.pages_main_layout,
// Frontend - admin pages
ABSOLUTE_PATHS_BACKEND.plugin({ code }).frontend.admin_pages,
ABSOLUTE_PATHS_BACKEND.plugin({ code }).frontend.admin_pages_auth,
// Frontend - plugin
ABSOLUTE_PATHS_BACKEND.plugin({ code }).frontend.plugin,
];

// Check if the folders exist
pathsToFolders.forEach(path => {
if (existsSync(path)) {
errorHandler?.(path);
throw new CustomError({
code: 'CONFLICT_PLUGIN',
message: `Folder ${path} already exists!`,
});
}
});
}
}
4 changes: 3 additions & 1 deletion packages/backend/src/core/admin/plugins/plugins.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import { DownloadAdminPluginsResolver } from './download/download.resolver';
import { DownloadAdminPluginsService } from './download/download.service';
import { EditAdminPluginsResolver } from './edit/edit.resolver';
import { EditAdminPluginsService } from './edit/edit.service';
import { ChangeFilesAdminPluginsService } from './helpers/files/change/change.service';
import { ChangeFilesAdminPluginsService } from './helpers/change-files.service';
import { CreateFilesAdminPluginsService } from './helpers/files/create/create-files.service';
import { VerifyFilesAdminPluginsService } from './helpers/verify-files.service';
import { AdminNavPluginsModule } from './nav/nav-plugins.module';
import { AdminPermissionsAdminPluginsModule } from './permissions-admin/permissions-admin.module';
import { ShowAdminPluginsResolver } from './show/show.resolver';
Expand All @@ -33,6 +34,7 @@ import { UploadAdminPluginsService } from './upload/upload.service';
UploadAdminPluginsService,
EditAdminPluginsResolver,
EditAdminPluginsService,
VerifyFilesAdminPluginsService,
],
imports: [AdminNavPluginsModule, AdminPermissionsAdminPluginsModule],
})
Expand Down
Loading

0 comments on commit eacddcb

Please sign in to comment.