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 {
|
2022-12-14 22:33:15 +08:00
|
|
|
getBuiltInPlugins() {
|
2022-12-18 23:27:03 +08:00
|
|
|
const plugins = (process.env.PRESET_NOCOBASE_PLUGINS || '').split(',').filter(Boolean);
|
|
|
|
return _.uniq(
|
|
|
|
[
|
|
|
|
'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',
|
|
|
|
].concat(plugins),
|
|
|
|
);
|
2022-12-14 22:33:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
getLocalPlugins() {
|
|
|
|
const localPlugins = ['sample-hello', 'oidc', 'saml', 'map'];
|
|
|
|
return localPlugins;
|
|
|
|
}
|
|
|
|
|
|
|
|
async addBuiltInPlugins() {
|
|
|
|
const builtInPlugins = this.getBuiltInPlugins();
|
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,
|
|
|
|
});
|
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, {});
|
2022-10-27 13:00:16 +08:00
|
|
|
await this.app.reload();
|
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
|
|
|
const result = await this.app.version.satisfies('<0.8.0-alpha.1');
|
|
|
|
if (result) {
|
|
|
|
const r = await this.db.collectionExistsInDb('applicationPlugins');
|
|
|
|
if (r) {
|
2022-11-04 00:32:25 +08:00
|
|
|
console.log(`Clear the installed application plugins`);
|
2022-10-28 15:09:14 +08:00
|
|
|
await this.db.getRepository('applicationPlugins').destroy({ truncate: true });
|
|
|
|
await this.app.reload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-10-27 13:00:16 +08:00
|
|
|
this.app.on('beforeUpgrade', async () => {
|
|
|
|
const result = await this.app.version.satisfies('<0.8.0-alpha.1');
|
|
|
|
if (result) {
|
2022-11-04 00:32:25 +08:00
|
|
|
console.log(`Initialize all built-in plugins`);
|
2022-10-27 13:00:16 +08:00
|
|
|
await this.addBuiltInPlugins();
|
2022-09-18 14:10:01 +08:00
|
|
|
}
|
2022-12-14 22:33:15 +08:00
|
|
|
const builtInPlugins = this.getBuiltInPlugins();
|
2022-12-17 10:35:10 +08:00
|
|
|
const plugins = await this.db.getRepository('applicationPlugins').find();
|
|
|
|
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
|
|
|
{},
|
|
|
|
);
|
2022-12-17 10:35:10 +08:00
|
|
|
await this.app.reload();
|
2022-12-19 00:00:58 +08:00
|
|
|
await this.app.db.sync();
|
2022-09-18 14:10:01 +08:00
|
|
|
});
|
2022-10-27 13:00:16 +08:00
|
|
|
this.app.on('beforeInstall', async () => {
|
2022-11-04 00:32:25 +08:00
|
|
|
console.log(`Initialize all built-in plugins`);
|
2022-10-27 13:00:16 +08:00
|
|
|
await this.addBuiltInPlugins();
|
|
|
|
});
|
2022-08-20 18:06:12 +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;
|