-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathbasePlugin.ts
More file actions
78 lines (61 loc) · 2.91 KB
/
basePlugin.ts
File metadata and controls
78 lines (61 loc) · 2.91 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
import { AdminForthResource, IAdminForthPlugin, IAdminForth } from './types/Back.js';
import { getComponentNameFromPath, md5hash } from './modules/utils.js';
import { currentFileDir } from './modules/utils.js';
import path from 'path';
import fs from 'fs';
import crypto from 'crypto';
import { afLogger } from './modules/logger.js';
export default class AdminForthPlugin implements IAdminForthPlugin {
adminforth: IAdminForth;
pluginDir: string;
customFolderName: string = 'custom';
pluginInstanceId: string;
customFolderPath: string;
pluginOptions: any;
resourceConfig: AdminForthResource;
className: string;
activationOrder: number = 0;
shouldHaveSingleInstancePerWholeApp?: () => boolean;
constructor(pluginOptions: any, metaUrl: string) {
// set up plugin here
this.pluginDir = currentFileDir(metaUrl);
this.customFolderPath = path.join(this.pluginDir, this.customFolderName);
this.pluginOptions = pluginOptions;
afLogger.trace(`🪲 🪲 AdminForthPlugin.constructor ${this.constructor.name}`);
this.className = this.constructor.name;
this.shouldHaveSingleInstancePerWholeApp = () => false;
}
setupEndpoints(server: any) {
}
instanceUniqueRepresentation(pluginOptions: any) : string {
return 'non-uniquely-identified';
}
modifyResourceConfig(adminforth: IAdminForth, resourceConfig: AdminForthResource, allPluginInstances?: {pi: AdminForthPlugin, resource: AdminForthResource}[]) {
this.resourceConfig = resourceConfig;
const uniqueness = this.instanceUniqueRepresentation(this.pluginOptions);
const seed = `af_pl_${this.constructor.name}_${resourceConfig.resourceId}_${uniqueness}`;
this.pluginInstanceId = md5hash(seed);
afLogger.trace({seed, pluginInstanceId: this.pluginInstanceId}, `🪲 AdminForthPlugin.modifyResourceConfig`);
this.adminforth = adminforth;
}
/**
* This method des next:
* - fills this.adminforth.codeInjector.srcFoldersToSync with the custom folder path mapped to `./plugins/${this.constructor.name}/`
* - fills this.adminforth.codeInjector.allComponentNames with the component name mapped to the component path
*/
componentPath(componentFile: string) {
const key = `@@/plugins/${this.constructor.name}/${componentFile}`;
const componentName = getComponentNameFromPath(key);
if (!this.adminforth.codeInjector.srcFoldersToSync[this.customFolderPath]) {
this.adminforth.codeInjector.srcFoldersToSync[this.customFolderPath] = `./plugins/${this.constructor.name}/`;
}
if (!this.adminforth.codeInjector.allComponentNames[key]) {
const absSrcPath = path.join(this.customFolderPath, componentFile);
if (!fs.existsSync(absSrcPath)) {
throw new Error(`Plugin "${this.constructor.name}" tried to use file as component which does not exist at "${absSrcPath}"`);
}
this.adminforth.codeInjector.allComponentNames[key] = componentName;
}
return key;
}
}