2022-05-19 00:40:55 +08:00
|
|
|
import { Plugin } from '@nocobase/server';
|
2022-12-18 23:27:03 +08:00
|
|
|
import _ from 'lodash';
|
2022-11-01 18:00:00 +08:00
|
|
|
import path from 'path';
|
2022-05-19 00:40:55 +08:00
|
|
|
|
2022-10-27 13:00:16 +08:00
|
|
|
export class PresetNocoBase extends Plugin {
|
2023-02-23 21:09:20 +08:00
|
|
|
builtInPlugins = [
|
|
|
|
'error-handler',
|
|
|
|
'collection-manager',
|
|
|
|
'ui-schema-storage',
|
|
|
|
'ui-routes-storage',
|
|
|
|
'file-manager',
|
|
|
|
'system-settings',
|
|
|
|
'sequence-field',
|
|
|
|
'verification',
|
|
|
|
'users',
|
|
|
|
'acl',
|
|
|
|
'china-region',
|
|
|
|
'workflow',
|
|
|
|
'client',
|
|
|
|
'export',
|
|
|
|
'import',
|
|
|
|
'audit-logs',
|
|
|
|
'duplicator',
|
|
|
|
'iframe-block',
|
|
|
|
'formula-field',
|
2023-03-04 19:36:58 +08:00
|
|
|
'charts',
|
2023-02-23 21:09:20 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
localPlugins = [
|
|
|
|
'sample-hello',
|
|
|
|
'multi-app-manager',
|
2023-03-13 20:43:57 +08:00
|
|
|
'multi-app-share-collection',
|
2023-02-23 21:09:20 +08:00
|
|
|
'oidc',
|
|
|
|
'saml',
|
|
|
|
'map',
|
|
|
|
'snapshot-field',
|
|
|
|
'graph-collection-manager',
|
|
|
|
];
|
|
|
|
|
|
|
|
splitNames(name: string) {
|
|
|
|
return (name || '').split(',').filter(Boolean);
|
|
|
|
}
|
|
|
|
|
2022-12-14 22:33:15 +08:00
|
|
|
getBuiltInPlugins() {
|
2023-02-23 21:09:20 +08:00
|
|
|
const { PRESET_NOCOBASE_PLUGINS, APPEND_PRESET_BUILT_IN_PLUGINS } = process.env;
|
2022-12-18 23:27:03 +08:00
|
|
|
return _.uniq(
|
2023-02-23 21:09:20 +08:00
|
|
|
this.splitNames(APPEND_PRESET_BUILT_IN_PLUGINS || PRESET_NOCOBASE_PLUGINS).concat(this.builtInPlugins),
|
2022-12-18 23:27:03 +08:00
|
|
|
);
|
2022-12-14 22:33:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
getLocalPlugins() {
|
2023-02-23 21:09:20 +08:00
|
|
|
const { APPEND_PRESET_LOCAL_PLUGINS } = process.env;
|
|
|
|
return _.uniq(this.splitNames(APPEND_PRESET_LOCAL_PLUGINS).concat(this.localPlugins));
|
2022-12-14 22:33:15 +08:00
|
|
|
}
|
|
|
|
|
2023-01-09 07:35:48 +08:00
|
|
|
async addBuiltInPlugins(options?: any) {
|
2022-12-14 22:33:15 +08:00
|
|
|
const builtInPlugins = this.getBuiltInPlugins();
|
2023-03-10 19:16:00 +08:00
|
|
|
|
2022-11-29 23:39:12 +08:00
|
|
|
await this.app.pm.add(builtInPlugins, {
|
2022-10-27 13:00:16 +08:00
|
|
|
enabled: true,
|
|
|
|
builtIn: true,
|
|
|
|
installed: true,
|
|
|
|
});
|
2023-03-10 19:16:00 +08:00
|
|
|
|
2022-12-14 22:33:15 +08:00
|
|
|
const localPlugins = this.getLocalPlugins();
|
2022-11-29 23:39:12 +08:00
|
|
|
await this.app.pm.add(localPlugins, {});
|
2023-01-09 07:35:48 +08:00
|
|
|
await this.app.reload({ method: options.method });
|
2022-05-19 00:40:55 +08:00
|
|
|
}
|
2022-08-20 18:06:12 +08:00
|
|
|
|
2022-10-27 13:00:16 +08:00
|
|
|
afterAdd() {
|
2022-10-28 15:09:14 +08:00
|
|
|
this.app.on('beforeLoad', async (app, options) => {
|
|
|
|
if (options?.method !== 'upgrade') {
|
|
|
|
return;
|
|
|
|
}
|
2022-11-04 00:32:25 +08:00
|
|
|
const version = await this.app.version.get();
|
|
|
|
console.log(`The version number before upgrade is ${version}`);
|
2022-10-28 15:09:14 +08:00
|
|
|
});
|
2023-02-27 13:59:17 +08:00
|
|
|
|
2023-01-09 07:35:48 +08:00
|
|
|
this.app.on('beforeUpgrade', async (options) => {
|
2022-10-27 13:00:16 +08:00
|
|
|
const result = await this.app.version.satisfies('<0.8.0-alpha.1');
|
2023-03-19 23:40:42 +08:00
|
|
|
|
2022-10-27 13:00:16 +08:00
|
|
|
if (result) {
|
2023-02-26 08:04:29 +08:00
|
|
|
console.log(`Initialize all built-in plugins beforeUpgrade`);
|
2023-01-09 07:35:48 +08:00
|
|
|
await this.addBuiltInPlugins({ method: 'upgrade' });
|
2022-09-18 14:10:01 +08:00
|
|
|
}
|
2023-03-19 23:40:42 +08:00
|
|
|
|
2022-12-14 22:33:15 +08:00
|
|
|
const builtInPlugins = this.getBuiltInPlugins();
|
2022-12-31 10:54:20 +08:00
|
|
|
const plugins = await this.app.db.getRepository('applicationPlugins').find();
|
2022-12-17 10:35:10 +08:00
|
|
|
const pluginNames = plugins.map((p) => p.name);
|
2022-12-14 22:33:15 +08:00
|
|
|
await this.app.pm.add(
|
2022-12-17 10:35:10 +08:00
|
|
|
builtInPlugins.filter((plugin) => !pluginNames.includes(plugin)),
|
2022-12-14 22:33:15 +08:00
|
|
|
{
|
|
|
|
enabled: true,
|
|
|
|
builtIn: true,
|
|
|
|
installed: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
const localPlugins = this.getLocalPlugins();
|
|
|
|
await this.app.pm.add(
|
2022-12-17 10:35:10 +08:00
|
|
|
localPlugins.filter((plugin) => !pluginNames.includes(plugin)),
|
2022-12-14 22:33:15 +08:00
|
|
|
{},
|
|
|
|
);
|
2023-01-09 07:35:48 +08:00
|
|
|
await this.app.reload({ method: 'upgrade' });
|
2022-12-19 00:00:58 +08:00
|
|
|
await this.app.db.sync();
|
2022-09-18 14:10:01 +08:00
|
|
|
});
|
2023-02-26 08:04:29 +08:00
|
|
|
|
2023-01-09 07:35:48 +08:00
|
|
|
this.app.on('beforeInstall', async (options) => {
|
2023-03-10 19:16:00 +08:00
|
|
|
console.log(`Initialize all built-in plugins beforeInstall in ${this.app.name}`);
|
2023-01-09 07:35:48 +08:00
|
|
|
await this.addBuiltInPlugins({ method: 'install' });
|
2022-10-27 13:00:16 +08:00
|
|
|
});
|
2022-08-20 18:06:12 +08:00
|
|
|
}
|
2023-01-09 07:35:48 +08:00
|
|
|
|
2022-11-01 18:00:00 +08:00
|
|
|
beforeLoad() {
|
|
|
|
this.db.addMigrations({
|
|
|
|
namespace: this.getName(),
|
|
|
|
directory: path.resolve(__dirname, './migrations'),
|
|
|
|
context: {
|
|
|
|
plugin: this,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2022-05-19 00:40:55 +08:00
|
|
|
}
|
|
|
|
|
2022-08-20 18:06:12 +08:00
|
|
|
export default PresetNocoBase;
|