mirror of
https://gitee.com/nocobase/nocobase.git
synced 2024-12-02 12:18:15 +08:00
feat: plugin install (#211)
* feat: plugin install * fix: install options
This commit is contained in:
parent
78f75f5a2f
commit
5e51973b21
@ -238,7 +238,7 @@ export class PluginACL extends Plugin {
|
||||
await this.writeRolesToACL();
|
||||
});
|
||||
|
||||
this.app.on('installing.beforeUsersPlugin', async () => {
|
||||
this.app.on('afterInstallUsersPlugin', async () => {
|
||||
const repository = this.app.db.getRepository('roles');
|
||||
await repository.createMany({
|
||||
records: [
|
||||
|
@ -3,11 +3,8 @@ import { areas, cities, provinces } from 'china-division';
|
||||
import { resolve } from 'path';
|
||||
|
||||
export class ChinaRegionPlugin extends Plugin {
|
||||
|
||||
async beforeLoad() {
|
||||
this.app.on('installing', async () => {
|
||||
await this.importData();
|
||||
});
|
||||
async install() {
|
||||
await this.importData();
|
||||
}
|
||||
|
||||
async load() {
|
||||
|
@ -9,20 +9,18 @@ export default class PluginFileManager extends Plugin {
|
||||
return process.env.DEFAULT_STORAGE_TYPE;
|
||||
}
|
||||
|
||||
async beforeLoad() {
|
||||
this.app.on('installing', async () => {
|
||||
const defaultStorageConfig = getStorageConfig(this.storageType());
|
||||
if (defaultStorageConfig) {
|
||||
const Storage = this.db.getCollection('storages');
|
||||
await Storage.repository.create({
|
||||
values: {
|
||||
...defaultStorageConfig.defaults(),
|
||||
type: this.storageType(),
|
||||
default: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
});
|
||||
async install() {
|
||||
const defaultStorageConfig = getStorageConfig(this.storageType());
|
||||
if (defaultStorageConfig) {
|
||||
const Storage = this.db.getCollection('storages');
|
||||
await Storage.repository.create({
|
||||
values: {
|
||||
...defaultStorageConfig.defaults(),
|
||||
type: this.storageType(),
|
||||
default: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async load() {
|
||||
|
@ -3,20 +3,18 @@ import { Plugin } from '@nocobase/server';
|
||||
import { resolve } from 'path';
|
||||
|
||||
export class SystemSettingsPlugin extends Plugin {
|
||||
async beforeLoad() {
|
||||
this.app.on('installing', async () => {
|
||||
await this.db.getRepository('systemSettings').create({
|
||||
values: {
|
||||
title: 'NocoBase',
|
||||
logo: {
|
||||
title: 'nocobase-logo',
|
||||
filename: '682e5ad037dd02a0fe4800a3e91c283b.png',
|
||||
extname: '.png',
|
||||
mimetype: 'image/png',
|
||||
url: 'https://nocobase.oss-cn-beijing.aliyuncs.com/682e5ad037dd02a0fe4800a3e91c283b.png',
|
||||
},
|
||||
async install() {
|
||||
await this.db.getRepository('systemSettings').create({
|
||||
values: {
|
||||
title: 'NocoBase',
|
||||
logo: {
|
||||
title: 'nocobase-logo',
|
||||
filename: '682e5ad037dd02a0fe4800a3e91c283b.png',
|
||||
extname: '.png',
|
||||
mimetype: 'image/png',
|
||||
url: 'https://nocobase.oss-cn-beijing.aliyuncs.com/682e5ad037dd02a0fe4800a3e91c283b.png',
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -5,141 +5,139 @@ import { resolve } from 'path';
|
||||
import { getAccessible } from './actions/getAccessible';
|
||||
|
||||
export class UiRoutesStoragePlugin extends Plugin {
|
||||
beforeLoad() {
|
||||
this.app.on('installing', async () => {
|
||||
const repository = this.app.db.getRepository('uiRoutes');
|
||||
const routes = [
|
||||
{
|
||||
type: 'redirect',
|
||||
from: '/',
|
||||
to: '/admin',
|
||||
exact: true,
|
||||
},
|
||||
{
|
||||
type: 'route',
|
||||
uiSchema: {
|
||||
type: 'void',
|
||||
'x-component': 'Menu',
|
||||
'x-designer': 'Menu.Designer',
|
||||
'x-initializer': 'MenuItemInitializers',
|
||||
'x-component-props': {
|
||||
mode: 'mix',
|
||||
theme: 'dark',
|
||||
// defaultSelectedUid: 'u8',
|
||||
onSelect: '{{ onSelect }}',
|
||||
sideMenuRefScopeKey: 'sideMenuRef',
|
||||
},
|
||||
properties: {
|
||||
// item3: {
|
||||
// type: 'void',
|
||||
// title: 'SubMenu u3',
|
||||
// 'x-component': 'Menu.SubMenu',
|
||||
// 'x-component-props': {},
|
||||
// properties: {
|
||||
// item6: {
|
||||
// type: 'void',
|
||||
// title: 'SubMenu u6',
|
||||
// 'x-component': 'Menu.SubMenu',
|
||||
// 'x-component-props': {},
|
||||
// properties: {
|
||||
// item7: {
|
||||
// type: 'void',
|
||||
// title: 'Menu Item u7',
|
||||
// 'x-component': 'Menu.Item',
|
||||
// 'x-component-props': {},
|
||||
// properties: {
|
||||
// page1: {
|
||||
// type: 'void',
|
||||
// 'x-component': 'Page',
|
||||
// 'x-async': true,
|
||||
// properties: {
|
||||
// grid1: {
|
||||
// type: 'void',
|
||||
// 'x-component': 'Grid',
|
||||
// 'x-item-initializer': 'BlockInitializer',
|
||||
// properties: {},
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// item8: {
|
||||
// type: 'void',
|
||||
// title: 'Menu Item u8',
|
||||
// 'x-component': 'Menu.Item',
|
||||
// 'x-component-props': {},
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// item4: {
|
||||
// type: 'void',
|
||||
// title: 'Menu Item u4',
|
||||
// 'x-component': 'Menu.Item',
|
||||
// 'x-component-props': {},
|
||||
// },
|
||||
// item5: {
|
||||
// type: 'void',
|
||||
// title: 'Menu Item u5',
|
||||
// 'x-component': 'Menu.Item',
|
||||
// 'x-component-props': {},
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// item1: {
|
||||
// type: 'void',
|
||||
// title: 'Menu Item u1',
|
||||
// 'x-component': 'Menu.Item',
|
||||
// 'x-component-props': {},
|
||||
// },
|
||||
// item2: {
|
||||
// type: 'void',
|
||||
// title: 'Menu Item u2',
|
||||
// 'x-component': 'Menu.Item',
|
||||
// 'x-component-props': {},
|
||||
// },
|
||||
// item9: {
|
||||
// type: 'void',
|
||||
// title: 'SubMenu u9',
|
||||
// 'x-component': 'Menu.SubMenu',
|
||||
// 'x-component-props': {},
|
||||
// properties: {
|
||||
// item10: {
|
||||
// type: 'void',
|
||||
// title: 'Menu Item u10',
|
||||
// 'x-component': 'Menu.Item',
|
||||
// 'x-component-props': {},
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
},
|
||||
async install() {
|
||||
const repository = this.app.db.getRepository('uiRoutes');
|
||||
const routes = [
|
||||
{
|
||||
type: 'redirect',
|
||||
from: '/',
|
||||
to: '/admin',
|
||||
exact: true,
|
||||
},
|
||||
{
|
||||
type: 'route',
|
||||
uiSchema: {
|
||||
type: 'void',
|
||||
'x-component': 'Menu',
|
||||
'x-designer': 'Menu.Designer',
|
||||
'x-initializer': 'MenuItemInitializers',
|
||||
'x-component-props': {
|
||||
mode: 'mix',
|
||||
theme: 'dark',
|
||||
// defaultSelectedUid: 'u8',
|
||||
onSelect: '{{ onSelect }}',
|
||||
sideMenuRefScopeKey: 'sideMenuRef',
|
||||
},
|
||||
properties: {
|
||||
// item3: {
|
||||
// type: 'void',
|
||||
// title: 'SubMenu u3',
|
||||
// 'x-component': 'Menu.SubMenu',
|
||||
// 'x-component-props': {},
|
||||
// properties: {
|
||||
// item6: {
|
||||
// type: 'void',
|
||||
// title: 'SubMenu u6',
|
||||
// 'x-component': 'Menu.SubMenu',
|
||||
// 'x-component-props': {},
|
||||
// properties: {
|
||||
// item7: {
|
||||
// type: 'void',
|
||||
// title: 'Menu Item u7',
|
||||
// 'x-component': 'Menu.Item',
|
||||
// 'x-component-props': {},
|
||||
// properties: {
|
||||
// page1: {
|
||||
// type: 'void',
|
||||
// 'x-component': 'Page',
|
||||
// 'x-async': true,
|
||||
// properties: {
|
||||
// grid1: {
|
||||
// type: 'void',
|
||||
// 'x-component': 'Grid',
|
||||
// 'x-item-initializer': 'BlockInitializer',
|
||||
// properties: {},
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// item8: {
|
||||
// type: 'void',
|
||||
// title: 'Menu Item u8',
|
||||
// 'x-component': 'Menu.Item',
|
||||
// 'x-component-props': {},
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// item4: {
|
||||
// type: 'void',
|
||||
// title: 'Menu Item u4',
|
||||
// 'x-component': 'Menu.Item',
|
||||
// 'x-component-props': {},
|
||||
// },
|
||||
// item5: {
|
||||
// type: 'void',
|
||||
// title: 'Menu Item u5',
|
||||
// 'x-component': 'Menu.Item',
|
||||
// 'x-component-props': {},
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// item1: {
|
||||
// type: 'void',
|
||||
// title: 'Menu Item u1',
|
||||
// 'x-component': 'Menu.Item',
|
||||
// 'x-component-props': {},
|
||||
// },
|
||||
// item2: {
|
||||
// type: 'void',
|
||||
// title: 'Menu Item u2',
|
||||
// 'x-component': 'Menu.Item',
|
||||
// 'x-component-props': {},
|
||||
// },
|
||||
// item9: {
|
||||
// type: 'void',
|
||||
// title: 'SubMenu u9',
|
||||
// 'x-component': 'Menu.SubMenu',
|
||||
// 'x-component-props': {},
|
||||
// properties: {
|
||||
// item10: {
|
||||
// type: 'void',
|
||||
// title: 'Menu Item u10',
|
||||
// 'x-component': 'Menu.Item',
|
||||
// 'x-component-props': {},
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
},
|
||||
path: '/admin/:name(.+)?',
|
||||
component: 'AdminLayout',
|
||||
title: 'NocoBase Admin',
|
||||
},
|
||||
{
|
||||
type: 'route',
|
||||
component: 'AuthLayout',
|
||||
routes: [
|
||||
{
|
||||
type: 'route',
|
||||
path: '/signin',
|
||||
component: 'SigninPage',
|
||||
},
|
||||
{
|
||||
type: 'route',
|
||||
path: '/signup',
|
||||
component: 'SignupPage',
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
for (const values of routes) {
|
||||
await repository.create({
|
||||
values,
|
||||
});
|
||||
}
|
||||
});
|
||||
path: '/admin/:name(.+)?',
|
||||
component: 'AdminLayout',
|
||||
title: 'NocoBase Admin',
|
||||
},
|
||||
{
|
||||
type: 'route',
|
||||
component: 'AuthLayout',
|
||||
routes: [
|
||||
{
|
||||
type: 'route',
|
||||
path: '/signin',
|
||||
component: 'SigninPage',
|
||||
},
|
||||
{
|
||||
type: 'route',
|
||||
path: '/signup',
|
||||
component: 'SignupPage',
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
for (const values of routes) {
|
||||
await repository.create({
|
||||
values,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async load() {
|
||||
|
@ -8,6 +8,7 @@ describe('role', () => {
|
||||
|
||||
beforeEach(async () => {
|
||||
api = mockServer();
|
||||
await api.cleanDb();
|
||||
api.plugin(require('../server').default);
|
||||
api.plugin(PluginACL);
|
||||
await api.loadAndInstall();
|
||||
|
@ -5,40 +5,38 @@ import * as actions from './actions/users';
|
||||
import * as middlewares from './middlewares';
|
||||
|
||||
export default class UsersPlugin extends Plugin {
|
||||
async beforeLoad() {
|
||||
async install() {
|
||||
const {
|
||||
adminNickname = 'Super Admin',
|
||||
adminEmail = 'admin@nocobase.com',
|
||||
adminPassword = 'admin123',
|
||||
} = this.options;
|
||||
|
||||
this.app.on('installing', async (...args) => {
|
||||
// TODO 暂时先这么写着,理想状态应该由 app.emitAsync('installing') 内部处理
|
||||
await this.app.emitAsync('installing.beforeUsersPlugin', ...args);
|
||||
const User = this.db.getCollection('users');
|
||||
await User.repository.create({
|
||||
values: {
|
||||
nickname: adminNickname,
|
||||
email: adminEmail,
|
||||
password: adminPassword,
|
||||
roles: ['admin'],
|
||||
},
|
||||
});
|
||||
await this.app.emitAsync('installing.afterUsersPlugin', ...args);
|
||||
const User = this.db.getCollection('users');
|
||||
await User.repository.create({
|
||||
values: {
|
||||
nickname: adminNickname,
|
||||
email: adminEmail,
|
||||
password: adminPassword,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async beforeLoad() {
|
||||
this.db.on('users.afterCreateWithAssociations', async (model, options) => {
|
||||
const { transaction } = options;
|
||||
|
||||
const defaultRole = await this.app.db.getRepository('roles').findOne({
|
||||
filter: {
|
||||
default: true,
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
if (this.app.db.getCollection('roles')) {
|
||||
const defaultRole = await this.app.db.getRepository('roles').findOne({
|
||||
filter: {
|
||||
default: true,
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
|
||||
if (defaultRole && (await model.countRoles({ transaction })) == 0) {
|
||||
await model.addRoles(defaultRole, { transaction });
|
||||
if (defaultRole && (await model.countRoles({ transaction })) == 0) {
|
||||
await model.addRoles(defaultRole, { transaction });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -11,7 +11,7 @@ import { isBoolean } from 'lodash';
|
||||
import { createACL } from './acl';
|
||||
import { createCli, createDatabase, createI18n, createResourcer, registerMiddlewares } from './helper';
|
||||
import { Plugin } from './plugin';
|
||||
import { PluginManager } from './plugin-manager';
|
||||
import { PluginManager, InstallOptions } from './plugin-manager';
|
||||
|
||||
export interface ResourcerOptions {
|
||||
prefix?: string;
|
||||
@ -71,12 +71,6 @@ interface StartOptions {
|
||||
listen?: ListenOptions;
|
||||
}
|
||||
|
||||
interface InstallOptions {
|
||||
cliArgs?: any[];
|
||||
clean?: CleanOptions | boolean;
|
||||
sync?: SyncOptions;
|
||||
}
|
||||
|
||||
export class Application<StateT = DefaultState, ContextT = DefaultContext> extends Koa implements AsyncEmitter {
|
||||
public readonly db: Database;
|
||||
|
||||
@ -214,11 +208,13 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
|
||||
|
||||
async install(options?: InstallOptions) {
|
||||
await this.emitAsync('beforeInstall', this, options);
|
||||
|
||||
if (options?.clean) {
|
||||
await this.db.clean(isBoolean(options.clean) ? { drop: options.clean } : options.clean);
|
||||
}
|
||||
|
||||
await this.db.sync(options?.sync);
|
||||
await this.emitAsync('installing', this, options);
|
||||
await this.pm.install(options);
|
||||
await this.emitAsync('afterInstall', this, options);
|
||||
}
|
||||
|
||||
|
@ -1,10 +1,17 @@
|
||||
import Application from './application';
|
||||
import { Plugin } from './plugin';
|
||||
import { CleanOptions, SyncOptions } from '@nocobase/database';
|
||||
|
||||
interface PluginManagerOptions {
|
||||
app: Application;
|
||||
}
|
||||
|
||||
export interface InstallOptions {
|
||||
cliArgs?: any[];
|
||||
clean?: CleanOptions | boolean;
|
||||
sync?: SyncOptions;
|
||||
}
|
||||
|
||||
export class PluginManager {
|
||||
app: Application;
|
||||
protected plugins = new Map<string, Plugin>();
|
||||
@ -46,4 +53,12 @@ export class PluginManager {
|
||||
|
||||
await this.app.emitAsync('afterLoadAll');
|
||||
}
|
||||
|
||||
async install(options?: InstallOptions) {
|
||||
for (const [name, plugin] of this.plugins) {
|
||||
await this.app.emitAsync('beforeInstallPlugin', plugin, options);
|
||||
await plugin.install(options);
|
||||
await this.app.emitAsync('afterInstallPlugin', plugin, options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { Database } from '@nocobase/database';
|
||||
import { Application } from './application';
|
||||
import path from 'path';
|
||||
import { InstallOptions } from './plugin-manager';
|
||||
|
||||
export interface PluginInterface {
|
||||
beforeLoad?: () => void;
|
||||
@ -42,6 +43,8 @@ export abstract class Plugin<O = any> implements PluginInterface {
|
||||
|
||||
beforeLoad() {}
|
||||
|
||||
async install(options?: InstallOptions) {}
|
||||
|
||||
async load() {
|
||||
const collectionPath = this.collectionPath();
|
||||
if (collectionPath) {
|
||||
|
Loading…
Reference in New Issue
Block a user