tachybase_todo/packages/presets/nocobase/src/server/index.ts
YANG QIA 8633ec3735
refactor(logger): improve logger format (#2664)
* refactor(logger): improve logger format

* chore: improve log format

* feat(logger): plugin-logger

* feat: allow to download log files, close T-1917

* chore: update yarn.lock

* chore: improve log format

* fix: add maxsize params

* chore: add userId field to request

* chore: remove userId from request

* chore: change userId in response

* chore: change action in response

* chore: add database logger

* fix: build

* fix: test

* chore: solve conflicts

* fix: escape delimiter in message

* refactor: improve create logger api

* chore: update app logger options

* chore: remove colorize for json

* fix: bug of data2tree

* fix: test

* chore: log

* chore: remove GITHUB_ACTION check

* fix: bug

* chore: change version

* fix: transports

* fix: mockServer

* chore: use new plugin settings api

* fix: version

* fix: build

* feat: support logfmt

* fix: build

* fix: build

* fix: test

* chore: update config

* fix: test

* fix: bug

* fix: test

* fix: format

* chore: update path

* fix: build

* fix: bug

* chore: update comment

* fix: allow to custom format

* fix: package.json

* fix: version

* fix: bug
2023-12-27 13:56:13 +08:00

181 lines
5.4 KiB
TypeScript

import { Repository } from '@nocobase/database';
import { Plugin, PluginManager } from '@nocobase/server';
import _ from 'lodash';
import path from 'path';
export class PresetNocoBase extends Plugin {
builtInPlugins = [
'error-handler',
'collection-manager',
'ui-schema-storage',
// 'ui-routes-storage',
'file-manager',
'system-settings',
'sequence-field',
'verification',
'users',
'acl',
'china-region',
'workflow',
'workflow-aggregate',
'workflow-delay',
'workflow-dynamic-calculation',
'workflow-form-trigger',
'workflow-loop',
'workflow-manual',
'workflow-parallel',
'workflow-request',
'workflow-sql',
'client',
'export',
'import',
'duplicator',
'iframe-block',
'formula-field',
'data-visualization',
'auth',
'sms-auth',
'logger',
'custom-request',
'calendar',
'action-bulk-update',
'action-bulk-edit',
'gantt',
'kanban',
'action-duplicate',
'action-print',
];
localPlugins = [
'audit-logs@0.7.1-alpha.4',
'sample-hello@0.8.0-alpha.4',
'multi-app-manager@0.7.0-alpha.1',
'multi-app-share-collection@0.9.2-alpha.1',
'oidc@0.9.2-alpha.1',
'saml@0.8.1-alpha.3',
'cas@0.13.0-alpha.5',
'map@0.8.1-alpha.3',
'snapshot-field@0.8.1-alpha.3',
'graph-collection-manager@0.9.0-alpha.1',
'mobile-client@0.10.0-alpha.2',
'api-keys@0.10.1-alpha.1',
'localization-management@0.11.1-alpha.1',
'theme-editor@0.11.1-alpha.1',
'api-doc@0.13.0-alpha.1',
];
splitNames(name: string) {
return (name || '').split(',').filter(Boolean);
}
getBuiltInPlugins() {
const { PRESET_NOCOBASE_PLUGINS, APPEND_PRESET_BUILT_IN_PLUGINS } = process.env;
return _.uniq(
this.splitNames(APPEND_PRESET_BUILT_IN_PLUGINS || PRESET_NOCOBASE_PLUGINS).concat(this.builtInPlugins),
);
}
getLocalPlugins() {
const { APPEND_PRESET_LOCAL_PLUGINS } = process.env;
const plugins = this.splitNames(APPEND_PRESET_LOCAL_PLUGINS)
.concat(this.localPlugins)
.map((name) => name.split('@'));
return plugins;
}
beforeLoad() {
this.db.addMigrations({
namespace: this.getName(),
directory: path.resolve(__dirname, './migrations'),
context: {
plugin: this,
},
});
this.app.on('beforeUpgrade', async () => {
await this.updateOrCreatePlugins();
});
}
async allPlugins() {
return (
await Promise.all(
this.getBuiltInPlugins().map(async (name) => {
const packageName = await PluginManager.getPackageName(name);
const packageJson = await PluginManager.getPackageJson(packageName);
return { name, packageName, enabled: true, builtIn: true, version: packageJson.version } as any;
}),
)
).concat(
await Promise.all(
this.getLocalPlugins().map(async (plugin) => {
const name = plugin[0];
const packageName = await PluginManager.getPackageName(name);
const packageJson = await PluginManager.getPackageJson(packageName);
return { name, packageName, version: packageJson.version };
}),
),
);
}
async getPluginToBeUpgraded() {
const repository = this.app.db.getRepository<any>('applicationPlugins');
const items = (await repository.find()).map((item) => item.name);
const plugins = await Promise.all(
this.getBuiltInPlugins().map(async (name) => {
const packageName = await PluginManager.getPackageName(name);
const packageJson = await PluginManager.getPackageJson(packageName);
return { name, packageName, enabled: true, builtIn: true, version: packageJson.version } as any;
}),
);
for (const plugin of this.getLocalPlugins()) {
if (plugin[1]) {
// 不在插件列表,并且插件最低版本小于当前应用版本,跳过不处理
if (!items.includes(plugin[0]) && (await this.app.version.satisfies(`>${plugin[1]}`))) {
continue;
}
}
const name = plugin[0];
const packageName = await PluginManager.getPackageName(name);
const packageJson = await PluginManager.getPackageJson(packageName);
plugins.push({ name, packageName, version: packageJson.version });
}
return plugins;
}
async updateOrCreatePlugins() {
const repository = this.app.db.getRepository<any>('applicationPlugins');
const plugins = await this.getPluginToBeUpgraded();
await this.db.sequelize.transaction((transaction) => {
return Promise.all(
plugins.map((values) =>
repository.updateOrCreate({
transaction,
values,
filterKeys: ['name'],
}),
),
);
});
await this.app.reload();
}
async createIfNotExists() {
const repository = this.app.db.getRepository<Repository>('applicationPlugins');
const existPlugins = await repository.find();
const existPluginNames = existPlugins.map((item) => item.name);
const plugins = (await this.allPlugins()).filter((item) => !existPluginNames.includes(item.name));
await repository.create({ values: plugins });
}
async install() {
const repository = this.db.getRepository<any>('applicationPlugins');
await this.createIfNotExists();
this.log.debug('install preset plugins');
await repository.init();
await this.app.pm.load();
await this.app.pm.install();
}
}
export default PresetNocoBase;