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';
|
2024-01-08 11:05:14 +00:00
|
|
|
import { fsExists, importModule } from '@nocobase/utils';
|
2023-09-12 14:39:23 +00:00
|
|
|
import fs from 'fs';
|
2024-01-08 11:05:14 +00:00
|
|
|
import glob from 'glob';
|
2023-12-29 01:40:17 +00:00
|
|
|
import type { TFuncKey, TOptions } from 'i18next';
|
2024-01-08 11:05:14 +00:00
|
|
|
import { basename, resolve } from 'path';
|
2022-06-17 02:25:59 +00:00
|
|
|
import { Application } from './application';
|
2024-01-08 11:05:14 +00:00
|
|
|
import { InstallOptions, getExposeChangelogUrl, getExposeReadmeUrl } from './plugin-manager';
|
2023-09-12 14:39:23 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-01-08 11:05:14 +00:00
|
|
|
get isPreset() {
|
|
|
|
return this.options.isPreset;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-01-08 11:05:14 +00:00
|
|
|
get _sourceDir() {
|
|
|
|
if (basename(__dirname) === 'src') {
|
|
|
|
return 'src';
|
|
|
|
}
|
|
|
|
return this.isPreset ? 'lib' : 'dist';
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadCommands() {
|
|
|
|
const extensions = ['js', 'ts'];
|
|
|
|
const directory = resolve(
|
|
|
|
process.env.NODE_MODULES_PATH,
|
|
|
|
this.options.packageName,
|
|
|
|
this._sourceDir,
|
|
|
|
'server/commands',
|
|
|
|
);
|
|
|
|
const patten = `${directory}/*.{${extensions.join(',')}}`;
|
|
|
|
const files = glob.sync(patten, {
|
|
|
|
ignore: ['**/*.d.ts'],
|
|
|
|
});
|
|
|
|
for (const file of files) {
|
|
|
|
let filename = basename(file);
|
|
|
|
filename = filename.substring(0, filename.lastIndexOf('.')) || filename;
|
|
|
|
const callback = await importModule(file);
|
|
|
|
callback(this.app);
|
|
|
|
}
|
|
|
|
if (files.length) {
|
|
|
|
this.app.log.debug(`load commands [${this.name}]`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadMigrations() {
|
|
|
|
this.app.log.debug(`load plugin migrations [${this.name}]`);
|
|
|
|
if (!this.options.packageName) {
|
|
|
|
return { beforeLoad: [], afterSync: [], afterLoad: [] };
|
|
|
|
}
|
|
|
|
const directory = resolve(
|
|
|
|
process.env.NODE_MODULES_PATH,
|
|
|
|
this.options.packageName,
|
|
|
|
this._sourceDir,
|
|
|
|
'server/migrations',
|
|
|
|
);
|
|
|
|
return await this.app.loadMigrations({
|
|
|
|
directory,
|
|
|
|
namespace: this.options.packageName,
|
|
|
|
context: {
|
|
|
|
plugin: this,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2024-01-08 11:05:14 +00:00
|
|
|
async upgrade() {}
|
|
|
|
|
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) {
|
2024-01-08 11:05:14 +00:00
|
|
|
// await this.db.import({
|
|
|
|
// directory: collectionsPath,
|
|
|
|
// from: `plugin:${this.getName()}`,
|
|
|
|
// });
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadCollections() {
|
|
|
|
if (!this.options.packageName) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const directory = resolve(
|
|
|
|
process.env.NODE_MODULES_PATH,
|
|
|
|
this.options.packageName,
|
|
|
|
this._sourceDir,
|
|
|
|
'server/collections',
|
|
|
|
);
|
|
|
|
if (await fsExists(directory)) {
|
|
|
|
await this.db.import({
|
|
|
|
directory,
|
|
|
|
from: this.options.packageName,
|
|
|
|
});
|
|
|
|
}
|
2023-01-08 04:45:02 +00:00
|
|
|
}
|
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,
|
|
|
|
};
|
|
|
|
}
|
2024-01-08 10:59:56 +00:00
|
|
|
|
|
|
|
const results = {
|
2023-09-12 14:39:23 +00:00
|
|
|
...this.options,
|
|
|
|
readmeUrl: getExposeReadmeUrl(packageName, locale),
|
|
|
|
changelogUrl: getExposeChangelogUrl(packageName),
|
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,
|
|
|
|
};
|
2024-01-08 10:59:56 +00:00
|
|
|
|
|
|
|
if (!options.withOutOpenFile) {
|
|
|
|
const file = await fs.promises.realpath(
|
|
|
|
resolve(process.env.NODE_MODULES_PATH || resolve(process.cwd(), 'node_modules'), packageName),
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...results,
|
|
|
|
...(await checkAndGetCompatible(packageName)),
|
|
|
|
lastUpdated: (await fs.promises.stat(file)).ctime,
|
|
|
|
file,
|
|
|
|
updatable: file.startsWith(process.env.PLUGIN_STORAGE_PATH),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
2023-09-12 14:39:23 +00:00
|
|
|
}
|
2021-09-14 03:09:26 +00:00
|
|
|
}
|
2022-06-17 02:25:59 +00:00
|
|
|
|
|
|
|
export default Plugin;
|