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();
|
await this.writeRolesToACL();
|
||||||
});
|
});
|
||||||
|
|
||||||
this.app.on('installing.beforeUsersPlugin', async () => {
|
this.app.on('afterInstallUsersPlugin', async () => {
|
||||||
const repository = this.app.db.getRepository('roles');
|
const repository = this.app.db.getRepository('roles');
|
||||||
await repository.createMany({
|
await repository.createMany({
|
||||||
records: [
|
records: [
|
||||||
|
@ -3,11 +3,8 @@ import { areas, cities, provinces } from 'china-division';
|
|||||||
import { resolve } from 'path';
|
import { resolve } from 'path';
|
||||||
|
|
||||||
export class ChinaRegionPlugin extends Plugin {
|
export class ChinaRegionPlugin extends Plugin {
|
||||||
|
async install() {
|
||||||
async beforeLoad() {
|
|
||||||
this.app.on('installing', async () => {
|
|
||||||
await this.importData();
|
await this.importData();
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async load() {
|
async load() {
|
||||||
|
@ -9,8 +9,7 @@ export default class PluginFileManager extends Plugin {
|
|||||||
return process.env.DEFAULT_STORAGE_TYPE;
|
return process.env.DEFAULT_STORAGE_TYPE;
|
||||||
}
|
}
|
||||||
|
|
||||||
async beforeLoad() {
|
async install() {
|
||||||
this.app.on('installing', async () => {
|
|
||||||
const defaultStorageConfig = getStorageConfig(this.storageType());
|
const defaultStorageConfig = getStorageConfig(this.storageType());
|
||||||
if (defaultStorageConfig) {
|
if (defaultStorageConfig) {
|
||||||
const Storage = this.db.getCollection('storages');
|
const Storage = this.db.getCollection('storages');
|
||||||
@ -22,7 +21,6 @@ export default class PluginFileManager extends Plugin {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async load() {
|
async load() {
|
||||||
|
@ -3,8 +3,7 @@ import { Plugin } from '@nocobase/server';
|
|||||||
import { resolve } from 'path';
|
import { resolve } from 'path';
|
||||||
|
|
||||||
export class SystemSettingsPlugin extends Plugin {
|
export class SystemSettingsPlugin extends Plugin {
|
||||||
async beforeLoad() {
|
async install() {
|
||||||
this.app.on('installing', async () => {
|
|
||||||
await this.db.getRepository('systemSettings').create({
|
await this.db.getRepository('systemSettings').create({
|
||||||
values: {
|
values: {
|
||||||
title: 'NocoBase',
|
title: 'NocoBase',
|
||||||
@ -17,7 +16,6 @@ export class SystemSettingsPlugin extends Plugin {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async load() {
|
async load() {
|
||||||
|
@ -5,8 +5,7 @@ import { resolve } from 'path';
|
|||||||
import { getAccessible } from './actions/getAccessible';
|
import { getAccessible } from './actions/getAccessible';
|
||||||
|
|
||||||
export class UiRoutesStoragePlugin extends Plugin {
|
export class UiRoutesStoragePlugin extends Plugin {
|
||||||
beforeLoad() {
|
async install() {
|
||||||
this.app.on('installing', async () => {
|
|
||||||
const repository = this.app.db.getRepository('uiRoutes');
|
const repository = this.app.db.getRepository('uiRoutes');
|
||||||
const routes = [
|
const routes = [
|
||||||
{
|
{
|
||||||
@ -139,7 +138,6 @@ export class UiRoutesStoragePlugin extends Plugin {
|
|||||||
values,
|
values,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async load() {
|
async load() {
|
||||||
|
@ -8,6 +8,7 @@ describe('role', () => {
|
|||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
api = mockServer();
|
api = mockServer();
|
||||||
|
await api.cleanDb();
|
||||||
api.plugin(require('../server').default);
|
api.plugin(require('../server').default);
|
||||||
api.plugin(PluginACL);
|
api.plugin(PluginACL);
|
||||||
await api.loadAndInstall();
|
await api.loadAndInstall();
|
||||||
|
@ -5,31 +5,28 @@ import * as actions from './actions/users';
|
|||||||
import * as middlewares from './middlewares';
|
import * as middlewares from './middlewares';
|
||||||
|
|
||||||
export default class UsersPlugin extends Plugin {
|
export default class UsersPlugin extends Plugin {
|
||||||
async beforeLoad() {
|
async install() {
|
||||||
const {
|
const {
|
||||||
adminNickname = 'Super Admin',
|
adminNickname = 'Super Admin',
|
||||||
adminEmail = 'admin@nocobase.com',
|
adminEmail = 'admin@nocobase.com',
|
||||||
adminPassword = 'admin123',
|
adminPassword = 'admin123',
|
||||||
} = this.options;
|
} = 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');
|
const User = this.db.getCollection('users');
|
||||||
await User.repository.create({
|
await User.repository.create({
|
||||||
values: {
|
values: {
|
||||||
nickname: adminNickname,
|
nickname: adminNickname,
|
||||||
email: adminEmail,
|
email: adminEmail,
|
||||||
password: adminPassword,
|
password: adminPassword,
|
||||||
roles: ['admin'],
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
await this.app.emitAsync('installing.afterUsersPlugin', ...args);
|
}
|
||||||
});
|
|
||||||
|
|
||||||
|
async beforeLoad() {
|
||||||
this.db.on('users.afterCreateWithAssociations', async (model, options) => {
|
this.db.on('users.afterCreateWithAssociations', async (model, options) => {
|
||||||
const { transaction } = options;
|
const { transaction } = options;
|
||||||
|
|
||||||
|
if (this.app.db.getCollection('roles')) {
|
||||||
const defaultRole = await this.app.db.getRepository('roles').findOne({
|
const defaultRole = await this.app.db.getRepository('roles').findOne({
|
||||||
filter: {
|
filter: {
|
||||||
default: true,
|
default: true,
|
||||||
@ -40,6 +37,7 @@ export default class UsersPlugin extends Plugin {
|
|||||||
if (defaultRole && (await model.countRoles({ transaction })) == 0) {
|
if (defaultRole && (await model.countRoles({ transaction })) == 0) {
|
||||||
await model.addRoles(defaultRole, { transaction });
|
await model.addRoles(defaultRole, { transaction });
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
this.db.on('afterDefineCollection', (collection: Collection) => {
|
this.db.on('afterDefineCollection', (collection: Collection) => {
|
||||||
|
@ -11,7 +11,7 @@ import { isBoolean } from 'lodash';
|
|||||||
import { createACL } from './acl';
|
import { createACL } from './acl';
|
||||||
import { createCli, createDatabase, createI18n, createResourcer, registerMiddlewares } from './helper';
|
import { createCli, createDatabase, createI18n, createResourcer, registerMiddlewares } from './helper';
|
||||||
import { Plugin } from './plugin';
|
import { Plugin } from './plugin';
|
||||||
import { PluginManager } from './plugin-manager';
|
import { PluginManager, InstallOptions } from './plugin-manager';
|
||||||
|
|
||||||
export interface ResourcerOptions {
|
export interface ResourcerOptions {
|
||||||
prefix?: string;
|
prefix?: string;
|
||||||
@ -71,12 +71,6 @@ interface StartOptions {
|
|||||||
listen?: ListenOptions;
|
listen?: ListenOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface InstallOptions {
|
|
||||||
cliArgs?: any[];
|
|
||||||
clean?: CleanOptions | boolean;
|
|
||||||
sync?: SyncOptions;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class Application<StateT = DefaultState, ContextT = DefaultContext> extends Koa implements AsyncEmitter {
|
export class Application<StateT = DefaultState, ContextT = DefaultContext> extends Koa implements AsyncEmitter {
|
||||||
public readonly db: Database;
|
public readonly db: Database;
|
||||||
|
|
||||||
@ -214,11 +208,13 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
|
|||||||
|
|
||||||
async install(options?: InstallOptions) {
|
async install(options?: InstallOptions) {
|
||||||
await this.emitAsync('beforeInstall', this, options);
|
await this.emitAsync('beforeInstall', this, options);
|
||||||
|
|
||||||
if (options?.clean) {
|
if (options?.clean) {
|
||||||
await this.db.clean(isBoolean(options.clean) ? { drop: options.clean } : options.clean);
|
await this.db.clean(isBoolean(options.clean) ? { drop: options.clean } : options.clean);
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.db.sync(options?.sync);
|
await this.db.sync(options?.sync);
|
||||||
await this.emitAsync('installing', this, options);
|
await this.pm.install(options);
|
||||||
await this.emitAsync('afterInstall', this, options);
|
await this.emitAsync('afterInstall', this, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,17 @@
|
|||||||
import Application from './application';
|
import Application from './application';
|
||||||
import { Plugin } from './plugin';
|
import { Plugin } from './plugin';
|
||||||
|
import { CleanOptions, SyncOptions } from '@nocobase/database';
|
||||||
|
|
||||||
interface PluginManagerOptions {
|
interface PluginManagerOptions {
|
||||||
app: Application;
|
app: Application;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface InstallOptions {
|
||||||
|
cliArgs?: any[];
|
||||||
|
clean?: CleanOptions | boolean;
|
||||||
|
sync?: SyncOptions;
|
||||||
|
}
|
||||||
|
|
||||||
export class PluginManager {
|
export class PluginManager {
|
||||||
app: Application;
|
app: Application;
|
||||||
protected plugins = new Map<string, Plugin>();
|
protected plugins = new Map<string, Plugin>();
|
||||||
@ -46,4 +53,12 @@ export class PluginManager {
|
|||||||
|
|
||||||
await this.app.emitAsync('afterLoadAll');
|
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 { Database } from '@nocobase/database';
|
||||||
import { Application } from './application';
|
import { Application } from './application';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import { InstallOptions } from './plugin-manager';
|
||||||
|
|
||||||
export interface PluginInterface {
|
export interface PluginInterface {
|
||||||
beforeLoad?: () => void;
|
beforeLoad?: () => void;
|
||||||
@ -42,6 +43,8 @@ export abstract class Plugin<O = any> implements PluginInterface {
|
|||||||
|
|
||||||
beforeLoad() {}
|
beforeLoad() {}
|
||||||
|
|
||||||
|
async install(options?: InstallOptions) {}
|
||||||
|
|
||||||
async load() {
|
async load() {
|
||||||
const collectionPath = this.collectionPath();
|
const collectionPath = this.collectionPath();
|
||||||
if (collectionPath) {
|
if (collectionPath) {
|
||||||
|
Loading…
Reference in New Issue
Block a user