forked from ToolJet/ToolJet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugins-uninstall.ts
More file actions
89 lines (72 loc) · 3.29 KB
/
plugins-uninstall.ts
File metadata and controls
89 lines (72 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import * as availablePlugins from 'src/assets/marketplace/plugins.json';
import { AppModule } from '@modules/app/module';
import { CreatePluginDto } from '@modules/plugins/dto';
import { EntityManager } from 'typeorm';
import { INestApplicationContext } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { Plugin } from 'src/entities/plugin.entity';
import { PluginsService } from '@modules/plugins/service';
import { getEnvVars } from './database-config-utils';
import { validateSync } from 'class-validator';
import { getImportPath, TOOLJET_EDITIONS } from '@modules/app/constants';
import { getTooljetEdition } from '@helpers/utils.helper';
const ENV_VARS = getEnvVars();
async function bootstrap() {
const nestApp = await NestFactory.createApplicationContext(await AppModule.register({ IS_GET_CONTEXT: true }), {
logger: ['error', 'warn'],
});
await validateAndUninstallPlugins(nestApp);
await nestApp.close();
process.exit(0);
}
async function validateAndUninstallPlugins(nestApp: INestApplicationContext) {
const edition: TOOLJET_EDITIONS = getTooljetEdition() as TOOLJET_EDITIONS;
const { PluginsService } = await import(`${await getImportPath(true, edition)}/plugins/service`);
const pluginsService = nestApp.get(PluginsService);
const pluginsToUninstall = fetchPluginsToUninstall();
const validPluginDtos: CreatePluginDto[] = [];
const invalidPluginDtos: CreatePluginDto[] = [];
console.log('Plugins to uninstall:', pluginsToUninstall);
for (const pluginId of pluginsToUninstall) {
const pluginDto = Object.assign(new CreatePluginDto(), findPluginDetails(pluginId));
const validationErrors = validateSync(pluginDto);
if (validationErrors.length === 0) {
validPluginDtos.push(pluginDto);
} else {
console.log(`Plugin with ID '${pluginId}' has validation errors:`, validationErrors);
invalidPluginDtos.push(pluginDto);
}
}
invalidPluginDtos.length > 0 &&
console.log(
'Skipping invalid plugins:',
invalidPluginDtos.map((dto) => dto.id),
'\n'
);
for (const dto of validPluginDtos) {
const entityManager = nestApp.get(EntityManager);
const plugins = await entityManager.find(Plugin, { where: { pluginId: dto.id } });
const pluginDbIds = [];
// Note: Plugins are installed at instance level. But there is no uniqueness check for the plugin.
// This means that same plugin can be installed multiple times but this is restricted at UI.
// Hence when removing, we are removing all the plugins installed of the same name.
// If in future we support installing different versions of the same plugin, we should remove it selectively.
for (const plugin of plugins) {
await pluginsService.remove(plugin.id);
pluginDbIds.push(plugin.id);
}
console.log('Uninstalled:', dto.id, pluginDbIds);
}
}
function findPluginDetails(pluginId: string) {
return availablePlugins.find((p: { id: string }) => p.id === pluginId);
}
function fetchPluginsToUninstall(): string[] {
if (!ENV_VARS.PLUGINS_TO_UNINSTALL) return [];
return sanitizedArray(ENV_VARS.PLUGINS_TO_UNINSTALL);
}
function sanitizedArray(string: string): string[] {
return [...new Set(string.split(',').map((p: string) => p.trim()))];
}
// eslint-disable-next-line @typescript-eslint/no-floating-promises
bootstrap();