2023-08-24 09:47:45 +00:00
|
|
|
import { Model } from '@nocobase/database';
|
2023-12-29 01:40:17 +00:00
|
|
|
import { LoggerOptions } from '@nocobase/logger';
|
2023-09-12 14:39:23 +00:00
|
|
|
import fs from 'fs';
|
2023-12-29 01:40:17 +00:00
|
|
|
import type { TFuncKey, TOptions } from 'i18next';
|
2023-09-12 14:39:23 +00:00
|
|
|
import { resolve } from 'path';
|
2022-06-17 02:25:59 +00:00
|
|
|
import { Application } from './application';
|
2023-09-12 14:39:23 +00:00
|
|
|
import { InstallOptions, getExposeChangelogUrl, getExposeReadmeUrl } from './plugin-manager';
|
|
|
|
import { checkAndGetCompatible } from './plugin-manager/utils';
|
2021-09-22 16:16:04 +00:00
|
|
|
|
2022-01-30 03:11:36 +00:00
|
|
|
export interface PluginInterface {
|
|
|
|
beforeLoad?: () => void;
|
2023-01-08 04:45:02 +00:00
|
|
|
|
2022-01-30 03:11:36 +00:00
|
|
|
load();
|
2023-01-08 04:45:02 +00:00
|
|
|
|
2022-01-30 03:11:36 +00:00
|
|
|
getName(): string;
|
2021-09-22 16:16:04 +00:00
|
|
|
}
|
2021-09-14 03:09:26 +00:00
|
|
|
|
|
|
|
export interface PluginOptions {
|
|
|
|
activate?: boolean;
|
|
|
|
displayName?: string;
|
|
|
|
description?: string;
|
|
|
|
version?: string;
|
2022-09-18 06:10:01 +00:00
|
|
|
enabled?: boolean;
|
2021-09-14 03:09:26 +00:00
|
|
|
install?: (this: Plugin) => void;
|
|
|
|
load?: (this: Plugin) => void;
|
2021-09-22 16:16:04 +00:00
|
|
|
plugin?: typeof Plugin;
|
2023-01-08 04:45:02 +00:00
|
|
|
|
2021-09-22 16:16:04 +00:00
|
|
|
[key: string]: any;
|
2021-09-14 03:09:26 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 03:11:36 +00:00
|
|
|
export abstract class Plugin<O = any> implements PluginInterface {
|
2022-10-27 05:00:16 +00:00
|
|
|
options: any;
|
2021-09-14 03:09:26 +00:00
|
|
|
app: Application;
|
2023-08-24 09:47:45 +00:00
|
|
|
model: Model;
|
|
|
|
state: any = {};
|
2021-09-14 03:09:26 +00:00
|
|
|
|
2022-10-27 05:00:16 +00:00
|
|
|
constructor(app: Application, options?: any) {
|
2022-01-30 03:11:36 +00:00
|
|
|
this.app = app;
|
|
|
|
this.setOptions(options);
|
2023-08-24 09:47:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get log() {
|
2023-12-27 05:56:13 +00:00
|
|
|
return this.app.log.child({
|
|
|
|
reqId: this.app.context.reqId,
|
|
|
|
module: this.name,
|
|
|
|
});
|
2021-09-14 03:09:26 +00:00
|
|
|
}
|
|
|
|
|
2023-01-08 23:35:48 +00:00
|
|
|
get name() {
|
|
|
|
return this.options.name as string;
|
|
|
|
}
|
|
|
|
|
2023-08-24 09:47:45 +00:00
|
|
|
get pm() {
|
|
|
|
return this.app.pm;
|
|
|
|
}
|
|
|
|
|
2022-12-31 02:54:20 +00:00
|
|
|
get db() {
|
|
|
|
return this.app.db;
|
|
|
|
}
|
|
|
|
|
2022-10-27 05:00:16 +00:00
|
|
|
get enabled() {
|
|
|
|
return this.options.enabled;
|
2021-09-14 03:09:26 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 05:00:16 +00:00
|
|
|
set enabled(value) {
|
|
|
|
this.options.enabled = value;
|
2022-09-18 06:10:01 +00:00
|
|
|
}
|
|
|
|
|
2023-08-24 09:47:45 +00:00
|
|
|
get installed() {
|
|
|
|
return this.options.installed;
|
|
|
|
}
|
|
|
|
|
|
|
|
set installed(value) {
|
|
|
|
this.options.installed = value;
|
|
|
|
}
|
|
|
|
|
2022-10-27 05:00:16 +00:00
|
|
|
setOptions(options: any) {
|
|
|
|
this.options = options || {};
|
2022-09-18 06:10:01 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 05:00:16 +00:00
|
|
|
getName() {
|
|
|
|
return (this.options as any).name;
|
|
|
|
}
|
2021-09-14 03:09:26 +00:00
|
|
|
|
2023-12-27 05:56:13 +00:00
|
|
|
createLogger(options: LoggerOptions) {
|
|
|
|
return this.app.createLogger(options);
|
|
|
|
}
|
|
|
|
|
2022-10-27 05:00:16 +00:00
|
|
|
afterAdd() {}
|
2022-09-18 06:10:01 +00:00
|
|
|
|
2022-01-30 03:11:36 +00:00
|
|
|
beforeLoad() {}
|
2021-09-14 03:09:26 +00:00
|
|
|
|
2022-10-27 05:00:16 +00:00
|
|
|
async load() {}
|
2022-02-28 13:49:50 +00:00
|
|
|
|
2022-10-27 05:00:16 +00:00
|
|
|
async install(options?: InstallOptions) {}
|
2022-02-06 17:14:00 +00:00
|
|
|
|
2023-04-04 08:08:10 +00:00
|
|
|
async beforeEnable() {}
|
|
|
|
|
2022-10-27 05:00:16 +00:00
|
|
|
async afterEnable() {}
|
2022-09-18 06:10:01 +00:00
|
|
|
|
2023-08-24 09:47:45 +00:00
|
|
|
async beforeDisable() {}
|
|
|
|
|
2022-10-27 05:00:16 +00:00
|
|
|
async afterDisable() {}
|
2022-03-28 14:01:10 +00:00
|
|
|
|
2023-08-24 09:47:45 +00:00
|
|
|
async beforeRemove() {}
|
|
|
|
|
|
|
|
async afterRemove() {}
|
2023-01-08 04:45:02 +00:00
|
|
|
|
|
|
|
async importCollections(collectionsPath: string) {
|
|
|
|
await this.db.import({
|
|
|
|
directory: collectionsPath,
|
|
|
|
from: this.getName(),
|
|
|
|
});
|
|
|
|
}
|
2023-03-19 15:40:42 +00:00
|
|
|
|
|
|
|
requiredPlugins() {
|
|
|
|
return [];
|
|
|
|
}
|
2023-09-12 14:39:23 +00:00
|
|
|
|
2023-12-29 01:40:17 +00:00
|
|
|
t(text: TFuncKey | TFuncKey[], options: TOptions = {}) {
|
|
|
|
return this.app.i18n.t(text, { ns: this.options['packageName'], ...(options as any) });
|
|
|
|
}
|
|
|
|
|
2023-09-12 14:39:23 +00:00
|
|
|
async toJSON(options: any = {}) {
|
|
|
|
const { locale = 'en-US' } = options;
|
2023-10-26 03:01:45 +00:00
|
|
|
const { name, packageName, packageJson } = this.options;
|
2023-09-12 14:39:23 +00:00
|
|
|
if (!packageName) {
|
|
|
|
return {
|
|
|
|
...this.options,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const file = await fs.promises.realpath(resolve(process.env.NODE_MODULES_PATH, packageName));
|
|
|
|
const lastUpdated = (await fs.promises.stat(file)).ctime;
|
|
|
|
const others = await checkAndGetCompatible(packageName);
|
|
|
|
return {
|
|
|
|
...this.options,
|
|
|
|
...others,
|
|
|
|
readmeUrl: getExposeReadmeUrl(packageName, locale),
|
|
|
|
changelogUrl: getExposeChangelogUrl(packageName),
|
|
|
|
lastUpdated,
|
|
|
|
file,
|
|
|
|
updatable: file.startsWith(process.env.PLUGIN_STORAGE_PATH),
|
2023-10-26 03:01:45 +00:00
|
|
|
displayName: packageJson[`displayName.${locale}`] || packageJson.displayName || name,
|
2023-09-12 14:39:23 +00:00
|
|
|
description: packageJson[`description.${locale}`] || packageJson.description,
|
|
|
|
};
|
|
|
|
}
|
2021-09-14 03:09:26 +00:00
|
|
|
}
|
2022-06-17 02:25:59 +00:00
|
|
|
|
|
|
|
export default Plugin;
|