From 249dff16d318291e9f84619d23bad97aae6a6562 Mon Sep 17 00:00:00 2001 From: chenos Date: Thu, 27 Oct 2022 13:00:16 +0800 Subject: [PATCH] refactor: plugin manager (#965) * feat: improve code * chore: update version * feat: api service * fix: api services * feat: improve code * feat: improve code * feat: improve code * feat: pm socket * fix: test errors * feat: add built-in plugins before upgrade * feat: update docs * feat: improve code * fix: after load --- docs/menus.ts | 9 +- docs/zh-CN/api/server/application.md | 31 +- docs/zh-CN/api/server/plugin-manager.md | 44 ++- docs/zh-CN/api/server/plugin.md | 44 ++- lerna.json | 2 +- packages/app/client/package.json | 4 +- packages/app/server/package.json | 4 +- packages/app/server/src/config/plugins.ts | 2 +- packages/core/acl/package.json | 4 +- packages/core/actions/package.json | 8 +- packages/core/build/package.json | 2 +- packages/core/cache/package.json | 2 +- packages/core/cli/package.json | 4 +- .../templates/plugin/src/server/index.ts.tpl | 3 - packages/core/client/package.json | 6 +- .../core/client/src/api-client/APIClient.ts | 6 +- .../src/block-provider/BlockProvider.tsx | 12 +- .../core/create-nocobase-app/package.json | 2 +- packages/core/database/package.json | 4 +- packages/core/devtools/package.json | 4 +- .../core/dumi-theme-nocobase/package.json | 2 +- packages/core/resourcer/package.json | 4 +- packages/core/sdk/package.json | 2 +- packages/core/server/package.json | 10 +- packages/core/server/src/application.ts | 48 ++- packages/core/server/src/commands/pm.ts | 99 +----- packages/core/server/src/plugin-manager.ts | 289 ---------------- .../src/plugin-manager/PluginManager.ts | 310 ++++++++++++++++++ .../plugin-manager/PluginManagerRepository.ts | 58 ++++ .../core/server/src/plugin-manager/index.ts | 2 + .../src/plugin-manager/options/collection.ts | 12 + .../src/plugin-manager/options/resource.ts | 49 +++ packages/core/server/src/plugin.ts | 60 ++-- packages/core/test/package.json | 4 +- packages/core/test/src/mockServer.ts | 1 + packages/core/utils/package.json | 2 +- packages/plugins/acl/package.json | 10 +- .../plugins/acl/src/__tests__/acl.test.ts | 4 +- .../src/__tests__/association-field.test.ts | 2 +- .../acl/src/__tests__/configuration.test.ts | 2 +- .../acl/src/__tests__/middleware.test.ts | 4 +- .../plugins/acl/src/__tests__/own.test.ts | 2 +- packages/plugins/acl/src/__tests__/prepare.ts | 13 +- .../acl/src/__tests__/role-check.test.ts | 4 +- .../acl/src/__tests__/role-resource.test.ts | 4 +- .../acl/src/__tests__/role-user.test.ts | 6 +- .../plugins/acl/src/__tests__/role.test.ts | 4 +- .../plugins/acl/src/__tests__/scope.test.ts | 6 +- .../acl/src/__tests__/setCurrentRole.test.ts | 2 +- .../plugins/acl/src/__tests__/users.test.ts | 2 +- packages/plugins/acl/src/server.ts | 3 - packages/plugins/audit-logs/package.json | 8 +- .../src/server/__tests__/hook.test.ts | 3 +- .../plugins/audit-logs/src/server/index.ts | 4 - packages/plugins/china-region/package.json | 8 +- .../plugins/china-region/src/server/index.ts | 3 - packages/plugins/client/package.json | 6 +- packages/plugins/client/src/server.ts | 4 - .../plugins/collection-manager/package.json | 10 +- .../__tests__/field-options/indexes.test.ts | 8 +- .../collection-manager/src/__tests__/index.ts | 9 +- .../src/__tests__/through.test.ts | 14 +- .../plugins/collection-manager/src/server.ts | 29 +- packages/plugins/error-handler/package.json | 4 +- .../src/__tests__/render-error.test.ts | 4 +- packages/plugins/error-handler/src/server.ts | 3 - packages/plugins/export/package.json | 6 +- packages/plugins/export/src/server/index.ts | 4 - packages/plugins/file-manager/package.json | 6 +- packages/plugins/file-manager/src/server.ts | 3 - .../plugins/multi-app-manager/package.json | 4 +- .../src/__tests__/mock-get-schema.test.ts | 2 +- .../src/__tests__/multiple-apps.test.ts | 8 +- .../src/models/application.ts | 3 +- .../plugins/multi-app-manager/src/server.ts | 3 - packages/plugins/notifications/package.json | 4 +- packages/plugins/notifications/src/server.ts | 6 +- packages/plugins/system-settings/package.json | 4 +- .../plugins/system-settings/src/server.ts | 4 - .../plugins/ui-routes-storage/package.json | 4 +- .../plugins/ui-routes-storage/src/server.ts | 3 - .../plugins/ui-schema-storage/package.json | 4 +- .../src/__tests__/action.test.ts | 2 +- .../src/__tests__/server-hook-impl.test.ts | 16 +- .../src/__tests__/server-hook.test.ts | 10 +- .../src/__tests__/ui-schema-model.test.ts | 2 +- .../ui-schema-repository-with-cache.test.ts | 4 +- .../__tests__/ui-schema-repository.test.ts | 2 +- .../src/__tests__/ui-schema.test.ts | 2 +- .../plugins/ui-schema-storage/src/server.ts | 4 - packages/plugins/users/package.json | 18 +- .../users/src/__tests__/actions.test.ts | 2 +- .../users/src/__tests__/fields.test.ts | 7 +- packages/plugins/users/src/__tests__/utils.ts | 1 + packages/plugins/users/src/actions/users.ts | 2 +- .../users/src/middlewares/parseToken.ts | 2 +- packages/plugins/users/src/server.ts | 7 +- packages/plugins/verification/package.json | 12 +- packages/plugins/verification/src/Plugin.ts | 4 - .../verification/src/__tests__/Plugin.test.ts | 4 +- .../verification/src/__tests__/index.ts | 6 +- .../verification/src/actions/verifications.ts | 8 +- packages/plugins/workflow/package.json | 14 +- .../plugins/workflow/src/server/Plugin.ts | 18 +- .../src/server/__tests__/Processor.test.ts | 4 +- .../workflow/src/server/__tests__/index.ts | 9 +- .../__tests__/instructions/parallel.test.ts | 6 +- .../__tests__/instructions/prompt.test.ts | 6 +- .../workflow/src/server/actions/jobs.ts | 2 +- .../server/extensions/assignees/actions.ts | 2 +- packages/presets/nocobase/package.json | 32 +- packages/presets/nocobase/src/index.ts | 62 ++-- packages/samples/command/package.json | 9 +- packages/samples/command/src/server/index.ts | 5 +- packages/samples/custom-block/package.json | 9 +- .../samples/custom-block/src/server/index.ts | 3 - packages/samples/custom-page/package.json | 9 +- .../samples/custom-page/src/server/index.ts | 9 +- .../samples/custom-signup-page/package.json | 9 +- .../custom-signup-page/src/server/index.ts | 4 - packages/samples/hello/package.json | 9 +- packages/samples/hello/src/server/index.ts | 3 - packages/samples/ratelimit/package.json | 8 +- .../samples/ratelimit/src/server/index.ts | 3 - packages/samples/shop-actions/package.json | 10 +- .../samples/shop-actions/src/server/index.ts | 3 - packages/samples/shop-events/package.json | 10 +- .../samples/shop-events/src/server/index.ts | 4 - packages/samples/shop-i18n/package.json | 8 +- .../samples/shop-i18n/src/server/index.ts | 3 - packages/samples/shop-modeling/package.json | 8 +- .../samples/shop-modeling/src/server/index.ts | 5 +- 132 files changed, 893 insertions(+), 910 deletions(-) delete mode 100644 packages/core/server/src/plugin-manager.ts create mode 100644 packages/core/server/src/plugin-manager/PluginManager.ts create mode 100644 packages/core/server/src/plugin-manager/PluginManagerRepository.ts create mode 100644 packages/core/server/src/plugin-manager/index.ts create mode 100644 packages/core/server/src/plugin-manager/options/collection.ts create mode 100644 packages/core/server/src/plugin-manager/options/resource.ts diff --git a/docs/menus.ts b/docs/menus.ts index a4576fe7c..231035caf 100644 --- a/docs/menus.ts +++ b/docs/menus.ts @@ -60,10 +60,7 @@ export default { title: 'Getting started', 'title.zh-CN': '快速开始', type: 'group', - children: [ - '/development/index', - '/development/your-fisrt-plugin', - ], + children: ['/development/index', '/development/your-fisrt-plugin'], }, { title: 'Extension Guides', @@ -191,8 +188,8 @@ export default { title: '@nocobase/server', type: 'subMenu', children: [ - '/api/server/application', - //'/api/server/plugin-manager', + '/api/server/application', + // '/api/server/plugin-manager', '/api/server/plugin', ], }, diff --git a/docs/zh-CN/api/server/application.md b/docs/zh-CN/api/server/application.md index 6bca83575..8aac7a008 100644 --- a/docs/zh-CN/api/server/application.md +++ b/docs/zh-CN/api/server/application.md @@ -180,28 +180,9 @@ NocoBase 默认对 context 注入了以下成员,可以在请求处理函数 ## 事件 - -**示例** - -```ts -app.on('beforeStart', async () => { - console.log('app before start'); -}); -``` - -### beforeLoadAll -### afterLoadAll -### beforeLoadPlugin -### afterLoadPlugin -### beforeInstallPlugin -### afterInstallPlugin -### beforeInstall -### afterInstall -### beforeUpgrade -### afterUpgrade -### beforeStart -### afterStart -### beforeStop -### afterStop -### beforeDestroy -### afterDestroy +### `'beforeLoad'` / `'afterLoad'` +### `'beforeInstall'` / `'afterInstall'` +### `'beforeUpgrade'` / `'afterUpgrade'` +### `'beforeStart'` / `'afterStart'` +### `'beforeStop'` / `'afterStop'` +### `'beforeDestroy'` / `'afterDestroy'` diff --git a/docs/zh-CN/api/server/plugin-manager.md b/docs/zh-CN/api/server/plugin-manager.md index ba1223ef0..e161d37f0 100644 --- a/docs/zh-CN/api/server/plugin-manager.md +++ b/docs/zh-CN/api/server/plugin-manager.md @@ -6,16 +6,54 @@ ### `create()` +在本地创建一个插件脚手架 + +**签名** + +```ts +create(name, options): void; +``` + +### `addStatic()` + +**签名** + +```ts +addStatic(plugin: any, options?: PluginOptions): Plugin; +``` + +**示例** + +```ts +pm.addStatic('nocobase'); +``` + ### `add()` +**签名** + +```ts +async add(plugin: any, options?: PluginOptions): Promise; +async add(plugin: string[], options?: PluginOptions): Promise; +``` + +**示例** + +```ts +await pm.add(['test'], { + builtIn: true, + enabled: true, +}); +``` + ### `get()` -### `install()` - -### `upgrade()` +获取插件实例 ### `enable()` ### `disable()` ### `remove()` + +### `upgrade()` diff --git a/docs/zh-CN/api/server/plugin.md b/docs/zh-CN/api/server/plugin.md index e8cb413f6..2f7a9705c 100644 --- a/docs/zh-CN/api/server/plugin.md +++ b/docs/zh-CN/api/server/plugin.md @@ -1,5 +1,23 @@ # Plugin +## 示例 + +```ts +const app = new Application(); + +class MyPlugin extends Plugin { + afterAdd() {} + beforeLoad() {} + load() {} + install() {} + afterEnable() {} + afterDisable() {} + remove() {} +} + +app.plugin(MyPlugin, { name: 'my-plugin' }); +``` + ## 属性 ### `options` @@ -10,20 +28,32 @@ 插件标识,只读 -### `model` - -插件模型数据 - ## 实例方法 +### `afterAdd()` + +插件 add/addStatic 之后 + ### `beforeLoad()` -加载前,如事件或类注册 +插件加载前,如事件或类注册 ### `load()` -加载配置 +加载插件,配置之类 ### `install()` -插件安装逻辑 +插件安装逻辑,如初始化数据 + +### `afterEnable()` + +插件激活之后的逻辑 + +### `afterDisable()` + +插件禁用之后的逻辑 + +### `remove()` + +用于实现插件删除逻辑 \ No newline at end of file diff --git a/lerna.json b/lerna.json index 1007a1f86..d66c1f961 100644 --- a/lerna.json +++ b/lerna.json @@ -1,5 +1,5 @@ { - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "npmClient": "yarn", "useWorkspaces": true, "npmClientArgs": [ diff --git a/packages/app/client/package.json b/packages/app/client/package.json index 672790616..b15bd4040 100644 --- a/packages/app/client/package.json +++ b/packages/app/client/package.json @@ -1,9 +1,9 @@ { "name": "@nocobase/app-client", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "license": "MIT", "devDependencies": { - "@nocobase/client": "0.7.4-alpha.7" + "@nocobase/client": "0.8.0-alpha.1" }, "repository": { "type": "git", diff --git a/packages/app/server/package.json b/packages/app/server/package.json index 06915f82a..6e4b3dd85 100644 --- a/packages/app/server/package.json +++ b/packages/app/server/package.json @@ -1,12 +1,12 @@ { "name": "@nocobase/app-server", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "description": "", "license": "MIT", "main": "./lib/index.js", "types": "./lib/index.d.ts", "dependencies": { - "@nocobase/preset-nocobase": "0.7.4-alpha.7" + "@nocobase/preset-nocobase": "0.8.0-alpha.1" }, "repository": { "type": "git", diff --git a/packages/app/server/src/config/plugins.ts b/packages/app/server/src/config/plugins.ts index 1d5be4c4b..ea227ae77 100644 --- a/packages/app/server/src/config/plugins.ts +++ b/packages/app/server/src/config/plugins.ts @@ -1,3 +1,3 @@ import { PluginConfiguration } from '@nocobase/server'; -export default ['@nocobase/preset-nocobase'] as PluginConfiguration[]; +export default ['nocobase'] as PluginConfiguration[]; diff --git a/packages/core/acl/package.json b/packages/core/acl/package.json index 987f91a9c..515e74fa9 100644 --- a/packages/core/acl/package.json +++ b/packages/core/acl/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/acl", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "description": "", "license": "Apache-2.0", "licenses": [ @@ -12,7 +12,7 @@ "main": "./lib/index.js", "types": "./lib/index.d.ts", "dependencies": { - "@nocobase/resourcer": "0.7.4-alpha.7", + "@nocobase/resourcer": "0.8.0-alpha.1", "json-templates": "^4.2.0" }, "repository": { diff --git a/packages/core/actions/package.json b/packages/core/actions/package.json index 1e7273a6d..34bc670ec 100644 --- a/packages/core/actions/package.json +++ b/packages/core/actions/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/actions", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "description": "", "license": "Apache-2.0", "licenses": [ @@ -12,9 +12,9 @@ "main": "./lib/index.js", "types": "./lib/index.d.ts", "dependencies": { - "@nocobase/database": "0.7.4-alpha.7", - "@nocobase/resourcer": "0.7.4-alpha.7", - "@nocobase/cache": "0.7.4-alpha.7" + "@nocobase/cache": "0.8.0-alpha.1", + "@nocobase/database": "0.8.0-alpha.1", + "@nocobase/resourcer": "0.8.0-alpha.1" }, "repository": { "type": "git", diff --git a/packages/core/build/package.json b/packages/core/build/package.json index fbf0c89be..351c8b6a0 100755 --- a/packages/core/build/package.json +++ b/packages/core/build/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/build", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "description": "Library build tool based on rollup.", "main": "lib/index.js", "bin": { diff --git a/packages/core/cache/package.json b/packages/core/cache/package.json index b9044e626..5db8f47f0 100644 --- a/packages/core/cache/package.json +++ b/packages/core/cache/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/cache", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "description": "", "license": "Apache-2.0", "licenses": [ diff --git a/packages/core/cli/package.json b/packages/core/cli/package.json index daf0e3148..d63cde62a 100644 --- a/packages/core/cli/package.json +++ b/packages/core/cli/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/cli", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "description": "", "license": "Apache-2.0", "licenses": [ @@ -23,7 +23,7 @@ "serve": "^13.0.2" }, "devDependencies": { - "@nocobase/devtools": "0.7.4-alpha.7" + "@nocobase/devtools": "0.8.0-alpha.1" }, "repository": { "type": "git", diff --git a/packages/core/cli/templates/plugin/src/server/index.ts.tpl b/packages/core/cli/templates/plugin/src/server/index.ts.tpl index 51e10fdd1..8635649e6 100644 --- a/packages/core/cli/templates/plugin/src/server/index.ts.tpl +++ b/packages/core/cli/templates/plugin/src/server/index.ts.tpl @@ -1,9 +1,6 @@ import { InstallOptions, Plugin } from '@nocobase/server'; export class {{{pascalCaseName}}}Plugin extends Plugin { - getName(): string { - return this.getPackageName(__dirname); - } beforeLoad() { // TODO diff --git a/packages/core/client/package.json b/packages/core/client/package.json index 10dc172f8..7fae4ec12 100644 --- a/packages/core/client/package.json +++ b/packages/core/client/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/client", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "license": "Apache-2.0", "licenses": [ { @@ -19,8 +19,8 @@ "@formily/antd": "2.0.20", "@formily/core": "2.0.20", "@formily/react": "2.0.20", - "@nocobase/sdk": "0.7.4-alpha.7", - "@nocobase/utils": "0.7.4-alpha.7", + "@nocobase/sdk": "0.8.0-alpha.1", + "@nocobase/utils": "0.8.0-alpha.1", "ahooks": "^3.0.5", "antd": "~4.19.5", "axios": "^0.26.1", diff --git a/packages/core/client/src/api-client/APIClient.ts b/packages/core/client/src/api-client/APIClient.ts index 58045f6a2..503195645 100644 --- a/packages/core/client/src/api-client/APIClient.ts +++ b/packages/core/client/src/api-client/APIClient.ts @@ -4,7 +4,11 @@ import { notification } from 'antd'; import React from 'react'; export class APIClient extends APIClientSDK { - services: Record>; + services: Record> = {}; + + service(uid: string) { + return this.services[uid]; + } interceptors() { super.interceptors(); diff --git a/packages/core/client/src/block-provider/BlockProvider.tsx b/packages/core/client/src/block-provider/BlockProvider.tsx index 3480e7566..1eba60655 100644 --- a/packages/core/client/src/block-provider/BlockProvider.tsx +++ b/packages/core/client/src/block-provider/BlockProvider.tsx @@ -1,5 +1,5 @@ import { Field } from '@formily/core'; -import { useField } from '@formily/react'; +import { useField, useFieldSchema } from '@formily/react'; import { useRequest } from 'ahooks'; import template from 'lodash/template'; import React, { createContext, useContext } from 'react'; @@ -10,7 +10,7 @@ import { useRecordIndex } from '../record-provider'; export const BlockResourceContext = createContext(null); export const BlockAssociationContext = createContext(null); -const BlockRequestContext = createContext(null); +const BlockRequestContext = createContext(null); export const useBlockResource = () => { return useContext(BlockResourceContext); @@ -80,6 +80,8 @@ export const useResourceAction = (props, opts = {}) => { const { fields } = useCollection(); const appends = fields?.filter((field) => field.target).map((field) => field.name); const params = useActionParams(props); + const api = useAPIClient(); + const fieldSchema = useFieldSchema(); if (!Object.keys(params).includes('appends') && appends?.length) { params['appends'] = appends; } @@ -96,6 +98,12 @@ export const useResourceAction = (props, opts = {}) => { }, { ...opts, + onSuccess(data, params) { + opts?.['onSuccess']?.(data, params); + if (fieldSchema['x-uid']) { + api.services[fieldSchema['x-uid']] = result; + } + }, defaultParams: [params], refreshDeps: [JSON.stringify(params.appends)], }, diff --git a/packages/core/create-nocobase-app/package.json b/packages/core/create-nocobase-app/package.json index 8bb615a91..283f1725f 100755 --- a/packages/core/create-nocobase-app/package.json +++ b/packages/core/create-nocobase-app/package.json @@ -1,6 +1,6 @@ { "name": "create-nocobase-app", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "src/index.js", "license": "Apache-2.0", "licenses": [ diff --git a/packages/core/database/package.json b/packages/core/database/package.json index 3bd787d5b..8aac54d9b 100644 --- a/packages/core/database/package.json +++ b/packages/core/database/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/database", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "description": "", "main": "./lib/index.js", "types": "./lib/index.d.ts", @@ -12,7 +12,7 @@ } ], "dependencies": { - "@nocobase/utils": "0.7.4-alpha.7", + "@nocobase/utils": "0.8.0-alpha.1", "async-mutex": "^0.3.2", "cron-parser": "4.4.0", "deepmerge": "^4.2.2", diff --git a/packages/core/devtools/package.json b/packages/core/devtools/package.json index c9f6defae..92d0b6ff8 100644 --- a/packages/core/devtools/package.json +++ b/packages/core/devtools/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/devtools", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "description": "", "license": "Apache-2.0", "licenses": [ @@ -11,7 +11,7 @@ ], "main": "./src/index.js", "dependencies": { - "@nocobase/build": "0.7.4-alpha.7", + "@nocobase/build": "0.8.0-alpha.1", "@testing-library/react": "^12.1.2", "@types/jest": "^26.0.0", "@types/koa": "^2.13.4", diff --git a/packages/core/dumi-theme-nocobase/package.json b/packages/core/dumi-theme-nocobase/package.json index 89440af31..82ab76a5a 100755 --- a/packages/core/dumi-theme-nocobase/package.json +++ b/packages/core/dumi-theme-nocobase/package.json @@ -1,6 +1,6 @@ { "name": "dumi-theme-nocobase", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "private": true, "files": [ "es", diff --git a/packages/core/resourcer/package.json b/packages/core/resourcer/package.json index 7fd3627c0..8a4b47a19 100644 --- a/packages/core/resourcer/package.json +++ b/packages/core/resourcer/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/resourcer", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "description": "", "main": "./lib/index.js", "types": "./lib/index.d.ts", @@ -12,7 +12,7 @@ } ], "dependencies": { - "@nocobase/utils": "0.7.4-alpha.7", + "@nocobase/utils": "0.8.0-alpha.1", "deepmerge": "^4.2.2", "koa-compose": "^4.1.0", "lodash": "^4.17.21", diff --git a/packages/core/sdk/package.json b/packages/core/sdk/package.json index b4be332f3..f34f0e98d 100644 --- a/packages/core/sdk/package.json +++ b/packages/core/sdk/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/sdk", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "license": "Apache-2.0", "licenses": [ { diff --git a/packages/core/server/package.json b/packages/core/server/package.json index 595217e3f..ee23f7fca 100644 --- a/packages/core/server/package.json +++ b/packages/core/server/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/server", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/index.js", "types": "./lib/index.d.ts", "license": "Apache-2.0", @@ -14,10 +14,10 @@ "@hapi/topo": "^6.0.0", "@koa/cors": "^3.1.0", "@koa/router": "^9.4.0", - "@nocobase/acl": "0.7.4-alpha.7", - "@nocobase/actions": "0.7.4-alpha.7", - "@nocobase/database": "0.7.4-alpha.7", - "@nocobase/resourcer": "0.7.4-alpha.7", + "@nocobase/acl": "0.8.0-alpha.1", + "@nocobase/actions": "0.8.0-alpha.1", + "@nocobase/database": "0.8.0-alpha.1", + "@nocobase/resourcer": "0.8.0-alpha.1", "chalk": "^4.1.1", "commander": "^9.2.0", "find-package-json": "^1.2.0", diff --git a/packages/core/server/src/application.ts b/packages/core/server/src/application.ts index 1f18d834a..734c50121 100644 --- a/packages/core/server/src/application.ts +++ b/packages/core/server/src/application.ts @@ -38,6 +38,7 @@ export interface ApplicationOptions { i18n?: i18n | InitOptions; plugins?: PluginConfiguration[]; acl?: boolean; + pmSock?: string; } export interface DefaultState extends KoaDefaultState { @@ -259,20 +260,17 @@ export class Application exten return packageJson.version; } - plugin(pluginClass: any, options?: O): Plugin { - return this.pm.add(pluginClass, options); + plugin(pluginClass: any, options?: O): Plugin { + return this.pm.addStatic(pluginClass, options); } loadPluginConfig(pluginsConfigurations: PluginConfiguration[]) { for (let pluginConfiguration of pluginsConfigurations) { if (typeof pluginConfiguration == 'string') { - pluginConfiguration = [pluginConfiguration, {}]; + this.plugin(pluginConfiguration); + } else { + this.plugin(...pluginConfiguration); } - - const plugin = PluginManager.resolvePlugin(pluginConfiguration[0]); - const pluginOptions = pluginConfiguration[1]; - - this.plugin(plugin, pluginOptions); } } @@ -319,13 +317,20 @@ export class Application exten return (this.cli)._findCommand(name); } - async load() { - await this.pm.load(); + async load(options?: any) { + if (options?.reload) { + this.init(); + } + await this.emitAsync('beforeLoad', this, options); + await this.pm.load(options); + await this.emitAsync('afterLoad', this, options); } - async reload() { - this.init(); - await this.pm.load(); + async reload(options?: any) { + await this.load({ + ...options, + reload: true, + }); } getPlugin

(name: string) { @@ -337,9 +342,9 @@ export class Application exten } async runAsCLI(argv = process.argv, options?: ParseOptions) { - if (argv?.[2] !== 'install') { - await this.load(); - } + await this.load({ + method: argv?.[2], + }); return this.cli.parseAsync(argv, options); } @@ -352,6 +357,7 @@ export class Application exten await this.emitAsync('beforeStart', this, options); if (options?.listen?.port) { + const pmServer = await this.pm.listen(); const listen = () => new Promise((resolve, reject) => { const Server = this.listen(options?.listen, () => { @@ -361,6 +367,10 @@ export class Application exten Server.on('error', (err) => { reject(err); }); + + Server.on('close', () => { + pmServer.close(); + }); }); try { @@ -385,7 +395,9 @@ export class Application exten try { // close database connection // silent if database already closed - await this.db.close(); + if (!this.db.closed()) { + await this.db.close(); + } } catch (e) { console.log(e); } @@ -419,11 +431,11 @@ export class Application exten if (options?.clean) { await this.db.clean(isBoolean(options.clean) ? { drop: options.clean } : options.clean); + await this.reload({ method: 'install' }); } await this.emitAsync('beforeInstall', this, options); - await this.load(); await this.db.sync(options?.sync); await this.pm.install(options); await this.version.update(); diff --git a/packages/core/server/src/commands/pm.ts b/packages/core/server/src/commands/pm.ts index 149faa897..5eb15bb20 100644 --- a/packages/core/server/src/commands/pm.ts +++ b/packages/core/server/src/commands/pm.ts @@ -1,5 +1,3 @@ -import axios from 'axios'; -import { resolve } from 'path'; import Application from '../application'; export default (app: Application) => { @@ -8,101 +6,6 @@ export default (app: Application) => { .argument('') .arguments('') .action(async (method, plugins, options, ...args) => { - const { APP_PORT, API_BASE_PATH = '/api/', API_BASE_URL } = process.env; - const baseURL = API_BASE_URL || `http://localhost:${APP_PORT}${API_BASE_PATH}`; - let started = true; - try { - await axios.get(`${baseURL}app:getLang`); - } catch (error) { - started = false; - } - const pm = { - async create() { - const name = plugins[0]; - const { run } = require('@nocobase/cli/src/util'); - const { PluginGenerator } = require('@nocobase/cli/src/plugin-generator'); - const generator = new PluginGenerator({ - cwd: resolve(process.cwd(), name), - args: options, - context: { - name, - }, - }); - await generator.run(); - await run('yarn', ['install']); - }, - async add() { - try { - if (started) { - const res = await axios.get(`${baseURL}pm:add/${plugins.join(',')}`); - } else { - await app.pm.add(plugins); - } - } catch (error) {} - const fs = require('fs/promises'); - await Promise.all( - plugins.map((plugin) => { - const file = resolve( - process.cwd(), - 'packages', - process.env.APP_PACKAGE_ROOT || 'app', - 'client/src/plugins', - `${plugin}.ts`, - ); - console.log(file); - return fs.writeFile(file, `export { default } from '@nocobase/plugin-${plugin}/client';`); - }), - ); - const { run } = require('@nocobase/cli/src/util'); - await run('yarn', ['nocobase', 'postinstall']); - }, - async enable() { - if (started) { - const res = await axios.get(`${baseURL}pm:enable/${plugins.join(',')}`); - console.log(res.data); - return; - } - const repository = app.db.getRepository('applicationPlugins'); - await repository.update({ - filter: { - 'name.$in': plugins, - }, - values: { - enabled: true, - }, - }); - }, - async disable() { - if (started) { - const res = await axios.get(`${baseURL}pm:disable/${plugins.join(',')}`); - console.log(res.data); - return; - } - const repository = app.db.getRepository('applicationPlugins'); - await repository.update({ - filter: { - 'name.$in': plugins, - }, - values: { - enabled: false, - }, - }); - }, - async remove() { - if (started) { - const res = await axios.get(`${baseURL}pm:disable/${plugins.join(',')}`); - console.log(res.data); - return; - } - const repository = app.db.getRepository('applicationPlugins'); - await repository.destroy({ - filter: { - 'name.$in': plugins, - }, - }); - plugins.map((name) => app.pm.remove(name)); - }, - }; - await pm[method](); + app.pm.clientWrite({ method, plugins }); }); }; diff --git a/packages/core/server/src/plugin-manager.ts b/packages/core/server/src/plugin-manager.ts deleted file mode 100644 index 4169ed6d0..000000000 --- a/packages/core/server/src/plugin-manager.ts +++ /dev/null @@ -1,289 +0,0 @@ -import { CleanOptions, Collection, Model, Repository, SyncOptions } from '@nocobase/database'; -import Application from './application'; -import { Plugin } from './plugin'; - -interface PluginManagerOptions { - app: Application; -} - -export interface InstallOptions { - cliArgs?: any[]; - clean?: CleanOptions | boolean; - sync?: SyncOptions; -} - -class PluginManagerRepository extends Repository { - getInstance(): Plugin { - return; - } - async add(name: string | string[], options) {} - async enable(name: string | string[], options) {} - async disable(name: string | string[], options) {} - async remove(name: string | string[], options) {} - async upgrade(name: string | string[], options) {} -} - -export class PluginManager { - app: Application; - collection: Collection; - repository: PluginManagerRepository; - plugins = new Map(); - - constructor(options: PluginManagerOptions) { - this.app = options.app; - this.collection = this.app.db.collection({ - name: 'applicationPlugins', - fields: [ - { type: 'string', name: 'name', unique: true }, - { type: 'string', name: 'version' }, - { type: 'boolean', name: 'enabled' }, - { type: 'boolean', name: 'installed' }, - { type: 'boolean', name: 'builtIn' }, - { type: 'json', name: 'options' }, - ], - }); - const app = this.app; - const pm = this; - this.repository = this.collection.repository as PluginManagerRepository; - this.app.resourcer.define({ - name: 'pm', - actions: { - async add(ctx, next) { - const { filterByTk } = ctx.action.params; - if (!filterByTk) { - ctx.throw(400, 'null'); - } - await pm.add(filterByTk); - ctx.body = filterByTk; - await next(); - }, - async enable(ctx, next) { - const { filterByTk } = ctx.action.params; - if (!filterByTk) { - ctx.throw(400, 'filterByTk invalid'); - } - const name = pm.getPackageName(filterByTk); - const plugin = pm.get(name); - if (plugin.model) { - plugin.model.set('enabled', true); - await plugin.model.save(); - } - if (!plugin) { - ctx.throw(400, 'plugin invalid'); - } - await app.reload(); - if (plugin.model && !plugin.model.get('installed')) { - await app.db.sync(); - await plugin.install(); - plugin.model.set('installed', true); - await plugin.model.save(); - } - await app.start(); - ctx.body = 'ok'; - await next(); - }, - async disable(ctx, next) { - const { filterByTk } = ctx.action.params; - if (!filterByTk) { - ctx.throw(400, 'filterByTk invalid'); - } - const name = pm.getPackageName(filterByTk); - const plugin = pm.get(name); - if (plugin.model) { - plugin.model.set('enabled', false); - await plugin.model.save(); - } - if (!plugin) { - ctx.throw(400, 'plugin invalid'); - } - await app.reload(); - await app.start(); - ctx.body = 'ok'; - await next(); - }, - async upgrade(ctx, next) { - ctx.body = 'ok'; - await next(); - }, - async remove(ctx, next) { - const { filterByTk } = ctx.action.params; - if (!filterByTk) { - ctx.throw(400, 'filterByTk invalid'); - } - const name = pm.getPackageName(filterByTk); - console.log(name, pm.plugins.keys()); - const plugin = pm.get(name); - if (plugin?.model) { - await plugin.model.destroy(); - pm.remove(name); - } else { - await pm.repository.destroy({ - filter: { - name: filterByTk, - }, - }); - } - await app.reload(); - await app.start(); - ctx.body = 'ok'; - await next(); - }, - }, - }); - this.app.acl.use(async (ctx, next) => { - if (ctx.action.resourceName === 'pm') { - ctx.permission = { - skip: true, - }; - } - await next(); - }); - this.app.on('beforeInstall', async () => { - await this.collection.sync(); - }); - this.app.on('beforeLoadAll', async (options) => { - await this.collection.sync(); - const exists = await this.app.db.collectionExistsInDb('applicationPlugins'); - if (!exists) { - return; - } - const items = await this.repository.find(); - for (const item of items) { - await this.add(item); - } - }); - } - - getPackageName(name: string) { - if (name.includes('@nocobase/plugin-')) { - return name; - } - if (name.includes('/')) { - return `@${name}`; - } - return `@nocobase/plugin-${name}`; - } - - private addByModel(model) { - try { - const packageName = this.getPackageName(model.get('name')); - require.resolve(packageName); - const cls = require(packageName).default; - const instance = new cls(this.app, { - ...model.get('options'), - name: model.get('name'), - version: model.get('version'), - enabled: model.get('enabled'), - }); - instance.setModel(model); - this.plugins.set(packageName, instance); - return instance; - } catch (error) {} - } - - getPlugins() { - return this.plugins; - } - - get(name: string) { - return this.plugins.get(name); - } - - remove(name: string) { - return this.plugins.delete(name); - } - - add

(pluginClass: any, options?: O) { - if (Array.isArray(pluginClass)) { - const addMultiple = async () => { - for (const plugin of pluginClass) { - await this.add(plugin); - } - }; - return addMultiple(); - } - if (typeof pluginClass === 'string') { - const packageName = this.getPackageName(pluginClass); - try { - require.resolve(packageName); - } catch (error) { - throw new Error(`${pluginClass} plugin does not exist`); - } - const packageJson = require(`${packageName}/package.json`); - const addNew = async () => { - let model = await this.repository.findOne({ - filter: { name: pluginClass }, - }); - if (model) { - throw new Error(`${pluginClass} plugin already exists`); - } - model = await this.repository.create({ - values: { - name: pluginClass, - version: packageJson.version, - enabled: false, - options: {}, - }, - }); - return this.addByModel(model); - }; - return addNew(); - } - - if (pluginClass instanceof Model) { - return this.addByModel(pluginClass); - } - - const instance = new pluginClass(this.app, { - ...options, - enabled: true, - }); - - const name = instance.getName(); - - if (this.plugins.has(name)) { - throw new Error(`plugin name [${name}] exists`); - } - - this.plugins.set(name, instance); - - return instance; - } - - async load() { - await this.app.emitAsync('beforeLoadAll'); - - for (const [name, plugin] of this.plugins) { - if (!plugin.enabled) { - continue; - } - await plugin.beforeLoad(); - } - - for (const [name, plugin] of this.plugins) { - if (!plugin.enabled) { - continue; - } - await this.app.emitAsync('beforeLoadPlugin', plugin); - await plugin.load(); - await this.app.emitAsync('afterLoadPlugin', plugin); - } - - await this.app.emitAsync('afterLoadAll'); - } - - async install(options: InstallOptions = {}) { - for (const [name, plugin] of this.plugins) { - if (!plugin.enabled) { - continue; - } - await this.app.emitAsync('beforeInstallPlugin', plugin, options); - await plugin.install(options); - await this.app.emitAsync('afterInstallPlugin', plugin, options); - } - } - - static resolvePlugin(pluginName: string) { - return require(pluginName).default; - } -} diff --git a/packages/core/server/src/plugin-manager/PluginManager.ts b/packages/core/server/src/plugin-manager/PluginManager.ts new file mode 100644 index 000000000..9b5a67491 --- /dev/null +++ b/packages/core/server/src/plugin-manager/PluginManager.ts @@ -0,0 +1,310 @@ +import { CleanOptions, Collection, SyncOptions } from '@nocobase/database'; +import { requireModule } from '@nocobase/utils'; +import execa from 'execa'; +import fs from 'fs'; +import net from 'net'; +import { resolve } from 'path'; +import Application from '../application'; +import { Plugin } from '../plugin'; +import collectionOptions from './options/collection'; +import resourceOptions from './options/resource'; +import { PluginManagerRepository } from './PluginManagerRepository'; + +export interface PluginManagerOptions { + app: Application; +} + +export interface InstallOptions { + cliArgs?: any[]; + clean?: CleanOptions | boolean; + sync?: SyncOptions; +} + +export class PluginManager { + app: Application; + collection: Collection; + repository: PluginManagerRepository; + plugins = new Map(); + server: net.Server; + pmSock: string; + + constructor(options: PluginManagerOptions) { + this.app = options.app; + const f = resolve(process.cwd(), 'storage', 'pm.sock'); + this.pmSock = this.app.options.pmSock || f; + this.app.db.registerRepositories({ + PluginManagerRepository, + }); + this.collection = this.app.db.collection(collectionOptions); + this.repository = this.collection.repository as PluginManagerRepository; + this.repository.setPluginManager(this); + this.app.resourcer.define(resourceOptions); + this.app.acl.use(async (ctx, next) => { + if (ctx.action.resourceName === 'pm') { + ctx.permission = { + skip: true, + }; + } + await next(); + }); + this.server = net.createServer((socket) => { + socket.on('data', async (data) => { + const { method, plugins } = JSON.parse(data.toString()); + try { + console.log(method, plugins); + await this[method](plugins); + } catch (error) { + console.error(error.message); + } + }); + socket.pipe(socket); + }); + this.app.on('beforeLoad', async (app, options) => { + if (options?.method && ['install', 'upgrade'].includes(options.method)) { + await this.collection.sync(); + } + const exists = await this.app.db.collectionExistsInDb('applicationPlugins'); + if (!exists) { + return; + } + if (options?.method !== 'install' || options.reload) { + await this.repository.load(); + } + }); + } + + getPlugins() { + return this.plugins; + } + + get(name: string) { + return this.plugins.get(name); + } + + clientWrite(data: any) { + const { method, plugins } = data; + const client = new net.Socket(); + client.connect(this.pmSock, () => { + client.write(JSON.stringify(data)); + }); + client.on('error', async () => { + try { + console.log(method, plugins); + await this[method](plugins); + } catch (error) { + console.error(error.message); + } + }); + } + + async listen(): Promise { + if (fs.existsSync(this.pmSock)) { + await fs.promises.unlink(this.pmSock); + } + return new Promise((resolve) => { + this.server.listen(this.pmSock, () => { + resolve(this.server); + }); + }); + } + + async create(name: string) { + const { run } = require('@nocobase/cli/src/util'); + const { PluginGenerator } = require('@nocobase/cli/src/plugin-generator'); + const generator = new PluginGenerator({ + cwd: resolve(process.cwd(), name), + args: {}, + context: { + name, + }, + }); + await generator.run(); + await run('yarn', ['install']); + } + + addStatic(plugin?: any, options?: any) { + let name: string; + if (typeof plugin === 'string') { + name = plugin; + plugin = PluginManager.resolvePlugin(plugin); + } else { + name = plugin.name; + if (!name) { + throw new Error(`plugin name invalid`); + } + } + const instance = new plugin(this.app, { + name, + enabled: true, + ...options, + }); + const pluginName = instance.getName(); + if (this.plugins.has(pluginName)) { + throw new Error(`plugin name [${pluginName}] exists`); + } + this.plugins.set(pluginName, instance); + return instance; + } + + async add(plugin: any, options: any = {}) { + if (Array.isArray(plugin)) { + return Promise.all(plugin.map((p) => this.add(p, options))); + } + // console.log(`adding ${plugin} plugin`); + const packageName = await PluginManager.findPackage(plugin); + const packageJson = require(`${packageName}/package.json`); + const instance = this.addStatic(plugin, options); + let model = await this.repository.findOne({ + filter: { name: plugin }, + }); + if (model) { + throw new Error(`${plugin} plugin already exists`); + } + const { enabled, builtIn, installed, ...others } = options; + await this.repository.create({ + values: { + name: plugin, + version: packageJson.version, + enabled: !!enabled, + builtIn: !!builtIn, + installed: !!installed, + options: { + ...others, + }, + }, + }); + const file = resolve( + process.cwd(), + 'packages', + process.env.APP_PACKAGE_ROOT || 'app', + 'client/src/plugins', + `${plugin}.ts`, + ); + if (!fs.existsSync(file)) { + try { + require.resolve(`${packageName}/client`); + await fs.promises.writeFile(file, `export { default } from '${packageName}/client';`); + const { run } = require('@nocobase/cli/src/util'); + await run('yarn', ['nocobase', 'postinstall']); + } catch (error) {} + } + return instance; + } + + async load(options: any = {}) { + for (const [name, plugin] of this.plugins) { + if (!plugin.enabled) { + continue; + } + await plugin.beforeLoad(); + } + + for (const [name, plugin] of this.plugins) { + if (!plugin.enabled) { + continue; + } + await this.app.emitAsync('beforeLoadPlugin', plugin, options); + await plugin.load(); + await this.app.emitAsync('afterLoadPlugin', plugin, options); + } + } + + async install(options: InstallOptions = {}) { + for (const [name, plugin] of this.plugins) { + if (!plugin.enabled) { + continue; + } + await this.app.emitAsync('beforeInstallPlugin', plugin, options); + await plugin.install(options); + await this.app.emitAsync('afterInstallPlugin', plugin, options); + } + } + + async enable(name: string | string[]) { + try { + const pluginNames = await this.repository.enable(name); + await this.app.reload(); + await this.app.db.sync(); + for (const pluginName of pluginNames) { + const plugin = this.app.getPlugin(pluginName); + if (!plugin) { + throw new Error(`${name} plugin does not exist`); + } + await plugin.install(); + await plugin.afterEnable(); + } + } catch (error) { + throw error; + } + } + + async disable(name: string | string[]) { + try { + const pluginNames = await this.repository.disable(name); + await this.app.reload(); + for (const pluginName of pluginNames) { + const plugin = this.app.getPlugin(pluginName); + if (!plugin) { + throw new Error(`${name} plugin does not exist`); + } + await plugin.afterDisable(); + } + } catch (error) { + throw error; + } + } + + async remove(name: string | string[]) { + const pluginNames = typeof name === 'string' ? [name] : name; + for (const pluginName of pluginNames) { + const plugin = this.app.getPlugin(pluginName); + if (!plugin) { + throw new Error(`${name} plugin does not exist`); + } + await plugin.remove(); + } + await this.repository.remove(name); + this.app.reload(); + } + + static getPackageName(name: string) { + const prefixes = (process.env.PLUGIN_PACKAGE_PREFIX || '@nocobase/plugin-,@nocobase/preset-').split(','); + for (const prefix of prefixes) { + try { + require.resolve(`${prefix}${name}`); + return `${prefix}${name}`; + } catch (error) { + continue; + } + } + throw new Error(`${name} plugin does not exist`); + } + + static async findPackage(name: string) { + try { + const packageName = this.getPackageName(name); + return packageName; + } catch (error) { + const prefixes = (process.env.PLUGIN_PACKAGE_PREFIX || '@nocobase/plugin-,@nocobase/preset-').split(','); + for (const prefix of prefixes) { + try { + const packageName = `${prefix}${name}`; + await execa('npm', ['v', packageName, 'versions']); + console.log(`${packageName} is downloading...`); + await execa('yarn', ['add', packageName, '-W']); + return packageName; + } catch (error) { + continue; + } + } + } + throw new Error(`${name} plugin does not exist`); + } + + static resolvePlugin(pluginName: string) { + const packageName = this.getPackageName(pluginName); + return requireModule(packageName); + } +} + +export default PluginManager; diff --git a/packages/core/server/src/plugin-manager/PluginManagerRepository.ts b/packages/core/server/src/plugin-manager/PluginManagerRepository.ts new file mode 100644 index 000000000..dde22197c --- /dev/null +++ b/packages/core/server/src/plugin-manager/PluginManagerRepository.ts @@ -0,0 +1,58 @@ +import { Repository } from '@nocobase/database'; +import { PluginManager } from './PluginManager'; + +export class PluginManagerRepository extends Repository { + pm: PluginManager; + + setPluginManager(pm: PluginManager) { + this.pm = pm; + } + + async remove(name: string | string[]) { + await this.destroy({ + filter: { + name, + }, + }); + } + + async enable(name: string | string[]) { + const pluginNames = typeof name === 'string' ? [name] : name; + await this.update({ + filter: { + name, + }, + values: { + enabled: true, + installed: true, + }, + }); + return pluginNames; + } + + async disable(name: string | string[]) { + const pluginNames = typeof name === 'string' ? [name] : name; + await this.update({ + filter: { + name, + }, + values: { + enabled: false, + installed: false, + }, + }); + return pluginNames; + } + + async load() { + const items = await this.find(); + for (const item of items) { + await this.pm.addStatic(item.get('name'), { + ...item.get('options'), + name: item.get('name'), + version: item.get('version'), + enabled: item.get('enabled'), + }); + } + } +} diff --git a/packages/core/server/src/plugin-manager/index.ts b/packages/core/server/src/plugin-manager/index.ts new file mode 100644 index 000000000..9fd3174d6 --- /dev/null +++ b/packages/core/server/src/plugin-manager/index.ts @@ -0,0 +1,2 @@ +export * from './PluginManager'; + diff --git a/packages/core/server/src/plugin-manager/options/collection.ts b/packages/core/server/src/plugin-manager/options/collection.ts new file mode 100644 index 000000000..3b9cffb93 --- /dev/null +++ b/packages/core/server/src/plugin-manager/options/collection.ts @@ -0,0 +1,12 @@ +export default { + name: 'applicationPlugins', + repository: 'PluginManagerRepository', + fields: [ + { type: 'string', name: 'name', unique: true }, + { type: 'string', name: 'version' }, + { type: 'boolean', name: 'enabled' }, + { type: 'boolean', name: 'installed' }, + { type: 'boolean', name: 'builtIn' }, + { type: 'json', name: 'options' }, + ], +}; diff --git a/packages/core/server/src/plugin-manager/options/resource.ts b/packages/core/server/src/plugin-manager/options/resource.ts new file mode 100644 index 000000000..6f1295715 --- /dev/null +++ b/packages/core/server/src/plugin-manager/options/resource.ts @@ -0,0 +1,49 @@ +export default { + name: 'pm', + actions: { + async add(ctx, next) { + const pm = ctx.app.pm; + const { filterByTk } = ctx.action.params; + if (!filterByTk) { + ctx.throw(400, 'plugin name invalid'); + } + await pm.add(filterByTk); + ctx.body = filterByTk; + await next(); + }, + async enable(ctx, next) { + const pm = ctx.app.pm; + const { filterByTk } = ctx.action.params; + if (!filterByTk) { + ctx.throw(400, 'plugin name invalid'); + } + await pm.enable(filterByTk); + ctx.body = filterByTk; + await next(); + }, + async disable(ctx, next) { + const pm = ctx.app.pm; + const { filterByTk } = ctx.action.params; + if (!filterByTk) { + ctx.throw(400, 'plugin name invalid'); + } + await pm.disable(filterByTk); + ctx.body = filterByTk; + await next(); + }, + async upgrade(ctx, next) { + ctx.body = 'ok'; + await next(); + }, + async remove(ctx, next) { + const pm = ctx.app.pm; + const { filterByTk } = ctx.action.params; + if (!filterByTk) { + ctx.throw(400, 'plugin name invalid'); + } + await pm.remove(filterByTk); + ctx.body = filterByTk; + await next(); + }, + }, +}; diff --git a/packages/core/server/src/plugin.ts b/packages/core/server/src/plugin.ts index 0c26391f4..3af567d7b 100644 --- a/packages/core/server/src/plugin.ts +++ b/packages/core/server/src/plugin.ts @@ -1,9 +1,7 @@ -import { Database, Model } from '@nocobase/database'; -import finder from 'find-package-json'; +import { Database } from '@nocobase/database'; import { Application } from './application'; import { InstallOptions } from './plugin-manager'; - export interface PluginInterface { beforeLoad?: () => void; load(); @@ -25,60 +23,46 @@ export interface PluginOptions { export type PluginType = typeof Plugin; export abstract class Plugin implements PluginInterface { - options: O; + options: any; app: Application; db: Database; - model: Model; - constructor(app: Application, options?: O) { + constructor(app: Application, options?: any) { this.app = app; this.db = app.db; this.setOptions(options); - this.initialize(); - } - - setOptions(options: O) { - this.options = options || ({} as any); - } - - setModel(model) { - this.model = model; + this.afterAdd(); } get enabled() { - return (this.options as any).enabled; + return this.options.enabled; } - public abstract getName(): string; + set enabled(value) { + this.options.enabled = value; + } - initialize() {} + setOptions(options: any) { + this.options = options || {}; + } + + getName() { + return (this.options as any).name; + } + + afterAdd() {} beforeLoad() {} + async load() {} + async install(options?: InstallOptions) {} - async load() { - const collectionPath = this.collectionPath(); - if (collectionPath) { - await this.db.import({ - directory: collectionPath, - }); - } - } + async afterEnable() {} - async disable() { - - } + async afterDisable() {} - collectionPath() { - return null; - } - - protected getPackageName(dirname: string) { - const f = finder(dirname); - const packageObj = f.next().value; - return packageObj['name']; - } + async remove() {} } export default Plugin; diff --git a/packages/core/test/package.json b/packages/core/test/package.json index 5458a2683..9b4525d3f 100644 --- a/packages/core/test/package.json +++ b/packages/core/test/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/test", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/index.js", "types": "./lib/index.d.ts", "license": "Apache-2.0", @@ -11,7 +11,7 @@ } ], "dependencies": { - "@nocobase/server": "0.7.4-alpha.7", + "@nocobase/server": "0.8.0-alpha.1", "@types/supertest": "^2.0.11", "mockjs": "^1.1.0", "mysql2": "^2.3.3", diff --git a/packages/core/test/src/mockServer.ts b/packages/core/test/src/mockServer.ts index d5e2c9c8a..d9fe5f83f 100644 --- a/packages/core/test/src/mockServer.ts +++ b/packages/core/test/src/mockServer.ts @@ -56,6 +56,7 @@ interface Resource { export class MockServer extends Application { async loadAndInstall(options: any = {}) { + await this.load(); await this.install({ ...options, sync: { diff --git a/packages/core/utils/package.json b/packages/core/utils/package.json index 37c353b1e..5a672c49f 100644 --- a/packages/core/utils/package.json +++ b/packages/core/utils/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/utils", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/index.js", "types": "./lib/index.d.ts", "license": "Apache-2.0", diff --git a/packages/plugins/acl/package.json b/packages/plugins/acl/package.json index 65986adb5..f1003239b 100644 --- a/packages/plugins/acl/package.json +++ b/packages/plugins/acl/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/plugin-acl", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "description": "", "license": "Apache-2.0", "licenses": [ @@ -12,10 +12,10 @@ "main": "./lib/index.js", "types": "./lib/index.d.ts", "dependencies": { - "@nocobase/acl": "0.7.4-alpha.7", - "@nocobase/database": "0.7.4-alpha.7", - "@nocobase/plugin-users": "0.7.4-alpha.7", - "@nocobase/server": "0.7.4-alpha.7" + "@nocobase/acl": "0.8.0-alpha.1", + "@nocobase/database": "0.8.0-alpha.1", + "@nocobase/plugin-users": "0.8.0-alpha.1", + "@nocobase/server": "0.8.0-alpha.1" }, "repository": { "type": "git", diff --git a/packages/plugins/acl/src/__tests__/acl.test.ts b/packages/plugins/acl/src/__tests__/acl.test.ts index f10fe6460..51f56205b 100644 --- a/packages/plugins/acl/src/__tests__/acl.test.ts +++ b/packages/plugins/acl/src/__tests__/acl.test.ts @@ -30,7 +30,7 @@ describe('acl', () => { }, }); - const userPlugin = app.getPlugin('@nocobase/plugin-users') as UsersPlugin; + const userPlugin = app.getPlugin('users') as UsersPlugin; adminAgent = app.agent().auth( userPlugin.jwtService.sign({ @@ -483,7 +483,7 @@ describe('acl', () => { }, }); - const userPlugin = app.getPlugin('@nocobase/plugin-users') as UsersPlugin; + const userPlugin = app.getPlugin('users') as UsersPlugin; const userAgent = app.agent().auth( userPlugin.jwtService.sign({ userId: user.get('id'), diff --git a/packages/plugins/acl/src/__tests__/association-field.test.ts b/packages/plugins/acl/src/__tests__/association-field.test.ts index c4937dbe0..4cb35277f 100644 --- a/packages/plugins/acl/src/__tests__/association-field.test.ts +++ b/packages/plugins/acl/src/__tests__/association-field.test.ts @@ -48,7 +48,7 @@ describe('association field acl', () => { }, }); - const userPlugin = app.getPlugin('@nocobase/plugin-users') as UsersPlugin; + const userPlugin = app.getPlugin('users') as UsersPlugin; userAgent = app.agent().auth( userPlugin.jwtService.sign({ userId: user.get('id'), diff --git a/packages/plugins/acl/src/__tests__/configuration.test.ts b/packages/plugins/acl/src/__tests__/configuration.test.ts index d7066d2dc..0555164be 100644 --- a/packages/plugins/acl/src/__tests__/configuration.test.ts +++ b/packages/plugins/acl/src/__tests__/configuration.test.ts @@ -45,7 +45,7 @@ describe('configuration', () => { } }); - const userPlugin = app.getPlugin('@nocobase/plugin-users') as UsersPlugin; + const userPlugin = app.getPlugin('users') as UsersPlugin; adminAgent = app.agent().auth(userPlugin.jwtService.sign({ userId: admin.get('id'), }), { type: 'bearer' }); diff --git a/packages/plugins/acl/src/__tests__/middleware.test.ts b/packages/plugins/acl/src/__tests__/middleware.test.ts index 520aa045b..c2a0fad73 100644 --- a/packages/plugins/acl/src/__tests__/middleware.test.ts +++ b/packages/plugins/acl/src/__tests__/middleware.test.ts @@ -1,7 +1,7 @@ import { ACL } from '@nocobase/acl'; import { Database, Model } from '@nocobase/database'; -import { MockServer } from '@nocobase/test'; import UsersPlugin from '@nocobase/plugin-users'; +import { MockServer } from '@nocobase/test'; import { prepareApp } from './prepare'; describe('middleware', () => { @@ -30,7 +30,7 @@ describe('middleware', () => { } }); - const userPlugin = app.getPlugin('@nocobase/plugin-users') as UsersPlugin; + const userPlugin = app.getPlugin('users') as UsersPlugin; adminAgent = app.agent().auth(userPlugin.jwtService.sign({ userId: admin.get('id'), }), { type: 'bearer' }); diff --git a/packages/plugins/acl/src/__tests__/own.test.ts b/packages/plugins/acl/src/__tests__/own.test.ts index 0d5f76b41..fbdd0918c 100644 --- a/packages/plugins/acl/src/__tests__/own.test.ts +++ b/packages/plugins/acl/src/__tests__/own.test.ts @@ -67,7 +67,7 @@ describe('own test', () => { admin = await db.getRepository('users').findOne(); - pluginUser = app.getPlugin('@nocobase/plugin-users'); + pluginUser = app.getPlugin('users'); adminToken = pluginUser.jwtService.sign({ userId: admin.get('id') }); diff --git a/packages/plugins/acl/src/__tests__/prepare.ts b/packages/plugins/acl/src/__tests__/prepare.ts index 0bbf480cf..e8ed109c8 100644 --- a/packages/plugins/acl/src/__tests__/prepare.ts +++ b/packages/plugins/acl/src/__tests__/prepare.ts @@ -1,7 +1,3 @@ -import PluginCollectionManager from '@nocobase/plugin-collection-manager'; -import PluginErrorHandler from '@nocobase/plugin-error-handler'; -import PluginUiSchema from '@nocobase/plugin-ui-schema-storage'; -import PluginUsers from '@nocobase/plugin-users'; import { mockServer } from '@nocobase/test'; import PluginACL from '../server'; @@ -9,15 +5,14 @@ export async function prepareApp() { const app = mockServer({ registerActions: true, acl: true, + plugins: ['error-handler', 'users', 'ui-schema-storage', 'collection-manager'], }); await app.cleanDb(); - app.plugin(PluginUsers); - app.plugin(PluginUiSchema); - app.plugin(PluginErrorHandler); - app.plugin(PluginCollectionManager); - app.plugin(PluginACL); + app.plugin(PluginACL, { + name: 'acl', + }); await app.loadAndInstall(); await app.db.sync(); diff --git a/packages/plugins/acl/src/__tests__/role-check.test.ts b/packages/plugins/acl/src/__tests__/role-check.test.ts index 5c43cf8c7..8c1057e50 100644 --- a/packages/plugins/acl/src/__tests__/role-check.test.ts +++ b/packages/plugins/acl/src/__tests__/role-check.test.ts @@ -1,6 +1,6 @@ -import { MockServer } from '@nocobase/test'; import { Database } from '@nocobase/database'; import UsersPlugin from '@nocobase/plugin-users'; +import { MockServer } from '@nocobase/test'; import { prepareApp } from './prepare'; @@ -28,7 +28,7 @@ describe('role check action', () => { roles: ['test'] } }); - const userPlugin = app.getPlugin('@nocobase/plugin-users') as UsersPlugin; + const userPlugin = app.getPlugin('users') as UsersPlugin; const agent = app.agent().auth(userPlugin.jwtService.sign({ userId: user.get('id'), }), { type: 'bearer' }); diff --git a/packages/plugins/acl/src/__tests__/role-resource.test.ts b/packages/plugins/acl/src/__tests__/role-resource.test.ts index 63a66eaac..6d96b5569 100644 --- a/packages/plugins/acl/src/__tests__/role-resource.test.ts +++ b/packages/plugins/acl/src/__tests__/role-resource.test.ts @@ -1,6 +1,6 @@ import { Database, Model } from '@nocobase/database'; -import UsersPlugin from '@nocobase/plugin-users'; import { CollectionRepository } from '@nocobase/plugin-collection-manager'; +import UsersPlugin from '@nocobase/plugin-users'; import { MockServer } from '@nocobase/test'; import { prepareApp } from './prepare'; @@ -32,7 +32,7 @@ describe('role resource api', () => { } }); - const userPlugin = app.getPlugin('@nocobase/plugin-users') as UsersPlugin; + const userPlugin = app.getPlugin('users') as UsersPlugin; adminAgent = app.agent().auth(userPlugin.jwtService.sign({ userId: admin.get('id'), }), { type: 'bearer' }); diff --git a/packages/plugins/acl/src/__tests__/role-user.test.ts b/packages/plugins/acl/src/__tests__/role-user.test.ts index 6d14a5326..c48527554 100644 --- a/packages/plugins/acl/src/__tests__/role-user.test.ts +++ b/packages/plugins/acl/src/__tests__/role-user.test.ts @@ -12,12 +12,12 @@ describe('role', () => { beforeEach(async () => { api = mockServer(); await api.cleanDb(); - api.plugin(UsersPlugin); - api.plugin(PluginACL); + api.plugin(UsersPlugin, { name: 'users' }); + api.plugin(PluginACL, { name: 'acl' }); await api.loadAndInstall(); db = api.db; - usersPlugin = api.getPlugin('@nocobase/plugin-users'); + usersPlugin = api.getPlugin('users'); }); afterEach(async () => { diff --git a/packages/plugins/acl/src/__tests__/role.test.ts b/packages/plugins/acl/src/__tests__/role.test.ts index a28a51019..2d549c3b8 100644 --- a/packages/plugins/acl/src/__tests__/role.test.ts +++ b/packages/plugins/acl/src/__tests__/role.test.ts @@ -1,6 +1,6 @@ -import { MockServer } from '@nocobase/test'; import { Database, Model } from '@nocobase/database'; import UsersPlugin from '@nocobase/plugin-users'; +import { MockServer } from '@nocobase/test'; import { prepareApp } from './prepare'; @@ -36,7 +36,7 @@ describe('role api', () => { }, }); - const userPlugin = app.getPlugin('@nocobase/plugin-users') as UsersPlugin; + const userPlugin = app.getPlugin('users') as UsersPlugin; adminAgent = app.agent().auth( userPlugin.jwtService.sign({ userId: admin.get('id'), diff --git a/packages/plugins/acl/src/__tests__/scope.test.ts b/packages/plugins/acl/src/__tests__/scope.test.ts index d35b096de..c010ced6c 100644 --- a/packages/plugins/acl/src/__tests__/scope.test.ts +++ b/packages/plugins/acl/src/__tests__/scope.test.ts @@ -1,7 +1,7 @@ -import { prepareApp } from './prepare'; -import { MockServer } from '@nocobase/test'; import { Database } from '@nocobase/database'; import UsersPlugin from '@nocobase/plugin-users'; +import { MockServer } from '@nocobase/test'; +import { prepareApp } from './prepare'; describe('scope api', () => { let app: MockServer; @@ -25,7 +25,7 @@ describe('scope api', () => { } }); - const userPlugin = app.getPlugin('@nocobase/plugin-users') as UsersPlugin; + const userPlugin = app.getPlugin('users') as UsersPlugin; adminAgent = app.agent().auth(userPlugin.jwtService.sign({ userId: admin.get('id'), }), { type: 'bearer' }); diff --git a/packages/plugins/acl/src/__tests__/setCurrentRole.test.ts b/packages/plugins/acl/src/__tests__/setCurrentRole.test.ts index 21cf2aef3..58e756155 100644 --- a/packages/plugins/acl/src/__tests__/setCurrentRole.test.ts +++ b/packages/plugins/acl/src/__tests__/setCurrentRole.test.ts @@ -15,7 +15,7 @@ describe('role', () => { api = await prepareApp(); db = api.db; - usersPlugin = api.getPlugin('@nocobase/plugin-users'); + usersPlugin = api.getPlugin('users'); ctx = { db, diff --git a/packages/plugins/acl/src/__tests__/users.test.ts b/packages/plugins/acl/src/__tests__/users.test.ts index da2a16495..d5db0d5c3 100644 --- a/packages/plugins/acl/src/__tests__/users.test.ts +++ b/packages/plugins/acl/src/__tests__/users.test.ts @@ -18,7 +18,7 @@ describe('actions', () => { app = await prepareApp(); db = app.db; - pluginUser = app.getPlugin('@nocobase/plugin-users'); + pluginUser = app.getPlugin('users'); adminUser = await db.getRepository('users').findOne({ filter: { email: process.env.INIT_ROOT_EMAIL diff --git a/packages/plugins/acl/src/server.ts b/packages/plugins/acl/src/server.ts index 6ab2ba0e8..ce4d515f4 100644 --- a/packages/plugins/acl/src/server.ts +++ b/packages/plugins/acl/src/server.ts @@ -442,9 +442,6 @@ export class PluginACL extends Plugin { }); } - getName(): string { - return this.getPackageName(__dirname); - } } export default PluginACL; diff --git a/packages/plugins/audit-logs/package.json b/packages/plugins/audit-logs/package.json index 04a56119a..5d1574a1c 100644 --- a/packages/plugins/audit-logs/package.json +++ b/packages/plugins/audit-logs/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/plugin-audit-logs", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "./lib/server/index.js", "types": "./lib/server/index.d.ts", "license": "Apache-2.0", @@ -11,11 +11,11 @@ } ], "dependencies": { - "@nocobase/client": "0.7.4-alpha.7", - "@nocobase/server": "0.7.4-alpha.7" + "@nocobase/client": "0.8.0-alpha.1", + "@nocobase/server": "0.8.0-alpha.1" }, "devDependencies": { - "@nocobase/test": "0.7.4-alpha.7" + "@nocobase/test": "0.8.0-alpha.1" }, "gitHead": "b17d1ecae5dd0b9d4e7170dcf9663bb225598990" } diff --git a/packages/plugins/audit-logs/src/server/__tests__/hook.test.ts b/packages/plugins/audit-logs/src/server/__tests__/hook.test.ts index b9d27882a..b4218c086 100644 --- a/packages/plugins/audit-logs/src/server/__tests__/hook.test.ts +++ b/packages/plugins/audit-logs/src/server/__tests__/hook.test.ts @@ -8,8 +8,7 @@ describe('hook', () => { beforeEach(async () => { api = mockServer(); - // api.plugin(require('@nocobase/plugin-users').default); - api.plugin(logPlugin); + api.plugin(logPlugin, { name: 'audit-logs' }); await api.load(); db = api.db; db.collection({ diff --git a/packages/plugins/audit-logs/src/server/index.ts b/packages/plugins/audit-logs/src/server/index.ts index 2f89b9e24..9b91143fa 100644 --- a/packages/plugins/audit-logs/src/server/index.ts +++ b/packages/plugins/audit-logs/src/server/index.ts @@ -21,8 +21,4 @@ export default class PluginActionLogs extends Plugin { }, }); } - - getName(): string { - return this.getPackageName(__dirname); - } } diff --git a/packages/plugins/china-region/package.json b/packages/plugins/china-region/package.json index 1a5d09642..2bbc48538 100644 --- a/packages/plugins/china-region/package.json +++ b/packages/plugins/china-region/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/plugin-china-region", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/index.js", "license": "Apache-2.0", "licenses": [ @@ -10,12 +10,12 @@ } ], "dependencies": { - "@nocobase/client": "0.7.4-alpha.7", - "@nocobase/server": "0.7.4-alpha.7", + "@nocobase/client": "0.8.0-alpha.1", + "@nocobase/server": "0.8.0-alpha.1", "china-division": "^2.4.0" }, "devDependencies": { - "@nocobase/test": "0.7.4-alpha.7" + "@nocobase/test": "0.8.0-alpha.1" }, "gitHead": "b17d1ecae5dd0b9d4e7170dcf9663bb225598990" } diff --git a/packages/plugins/china-region/src/server/index.ts b/packages/plugins/china-region/src/server/index.ts index aea022720..2972934eb 100644 --- a/packages/plugins/china-region/src/server/index.ts +++ b/packages/plugins/china-region/src/server/index.ts @@ -64,9 +64,6 @@ export class PluginChinaRegion extends Plugin { // console.log(`${count} rows of region data imported in ${(Date.now() - timer) / 1000}s`); } - getName(): string { - return this.getPackageName(__dirname); - } } export default PluginChinaRegion; diff --git a/packages/plugins/client/package.json b/packages/plugins/client/package.json index 7b12ddcc0..5d114ee8a 100644 --- a/packages/plugins/client/package.json +++ b/packages/plugins/client/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/plugin-client", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/index.js", "license": "Apache-2.0", "licenses": [ @@ -10,10 +10,10 @@ } ], "dependencies": { - "@nocobase/server": "0.7.4-alpha.7" + "@nocobase/server": "0.8.0-alpha.1" }, "devDependencies": { - "@nocobase/test": "0.7.4-alpha.7" + "@nocobase/test": "0.8.0-alpha.1" }, "gitHead": "b17d1ecae5dd0b9d4e7170dcf9663bb225598990" } diff --git a/packages/plugins/client/src/server.ts b/packages/plugins/client/src/server.ts index 25eed8b10..acd6490fc 100644 --- a/packages/plugins/client/src/server.ts +++ b/packages/plugins/client/src/server.ts @@ -108,10 +108,6 @@ export class ClientPlugin extends Plugin { }); } } - - getName(): string { - return this.getPackageName(__dirname); - } } export default ClientPlugin; diff --git a/packages/plugins/collection-manager/package.json b/packages/plugins/collection-manager/package.json index 998a96ad8..de34dd111 100644 --- a/packages/plugins/collection-manager/package.json +++ b/packages/plugins/collection-manager/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/plugin-collection-manager", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/index.js", "license": "Apache-2.0", "licenses": [ @@ -10,13 +10,13 @@ } ], "dependencies": { - "@nocobase/database": "0.7.4-alpha.7", - "@nocobase/plugin-error-handler": "0.7.4-alpha.7", - "@nocobase/server": "0.7.4-alpha.7", + "@nocobase/database": "0.8.0-alpha.1", + "@nocobase/plugin-error-handler": "0.8.0-alpha.1", + "@nocobase/server": "0.8.0-alpha.1", "sequelize": "^6.9.0" }, "devDependencies": { - "@nocobase/test": "0.7.4-alpha.7" + "@nocobase/test": "0.8.0-alpha.1" }, "gitHead": "b17d1ecae5dd0b9d4e7170dcf9663bb225598990" } diff --git a/packages/plugins/collection-manager/src/__tests__/field-options/indexes.test.ts b/packages/plugins/collection-manager/src/__tests__/field-options/indexes.test.ts index f82659570..94b88bae0 100644 --- a/packages/plugins/collection-manager/src/__tests__/field-options/indexes.test.ts +++ b/packages/plugins/collection-manager/src/__tests__/field-options/indexes.test.ts @@ -46,7 +46,9 @@ describe('field indexes', () => { }, }); - expect(response3.status).toBe(400); + console.log(response3.body); + + expect(response3.status).toBe(500); const response4 = await agent.resource(tableName).create({ values: { title: 't1' }, @@ -82,7 +84,7 @@ describe('field indexes', () => { title: 't1', }, }); - expect(response2.status).toBe(400); + expect(response2.status).toBe(500); // update field to remove unique constraint await agent.resource('fields').update({ @@ -109,7 +111,7 @@ describe('field indexes', () => { }, }); - expect(response4.status).toBe(400); + expect(response4.status).toBe(500); // remove a duplicated record await agent.resource(tableName).destroy({ diff --git a/packages/plugins/collection-manager/src/__tests__/index.ts b/packages/plugins/collection-manager/src/__tests__/index.ts index 3d088f08a..ad86091d6 100644 --- a/packages/plugins/collection-manager/src/__tests__/index.ts +++ b/packages/plugins/collection-manager/src/__tests__/index.ts @@ -13,12 +13,11 @@ export async function createApp(options = {}) { await app.cleanDb(); } - app.plugin(PluginErrorHandler); - app.plugin(Plugin); - app.plugin(PluginUiSchema); + app.plugin(PluginErrorHandler, { name: 'error-handler' }); + app.plugin(Plugin, { name: 'collection-manager' }); + app.plugin(PluginUiSchema, { name: 'ui-schema-storage' }); - // await app.load(); - await app.install({ clean: true }); + await app.loadAndInstall({ clean: true }); await app.start(); return app; } diff --git a/packages/plugins/collection-manager/src/__tests__/through.test.ts b/packages/plugins/collection-manager/src/__tests__/through.test.ts index cf6f4a95b..961acc632 100644 --- a/packages/plugins/collection-manager/src/__tests__/through.test.ts +++ b/packages/plugins/collection-manager/src/__tests__/through.test.ts @@ -10,11 +10,10 @@ describe('collections repository', () => { }, acl: false, }); - await app1.cleanDb(); - app1.plugin(PluginErrorHandler); - app1.plugin(Plugin); - await app1.install({ clean: true }); - await app1.start(); + app1.plugin(PluginErrorHandler, { name: 'error-handler' }); + app1.plugin(Plugin, { name: 'collection-manager' }); + await app1.loadAndInstall({ clean: true }); + await app1 .agent() .resource('collections') @@ -121,10 +120,9 @@ describe('collections repository', () => { tablePrefix: 'through_', }, }); - app2.plugin(PluginErrorHandler); - app2.plugin(Plugin); + app2.plugin(PluginErrorHandler, { name: 'error-handler' }); + app2.plugin(Plugin, { name: 'collection-manager' }); await app2.load(); - await app2.start(); await app2.db.sync({ force: true, diff --git a/packages/plugins/collection-manager/src/server.ts b/packages/plugins/collection-manager/src/server.ts index 67e8f5cb6..db11c133e 100644 --- a/packages/plugins/collection-manager/src/server.ts +++ b/packages/plugins/collection-manager/src/server.ts @@ -11,11 +11,12 @@ import { afterCreateForReverseField, beforeCreateForChildrenCollection, beforeCreateForReverseField, - beforeInitOptions, + beforeInitOptions } from './hooks'; import { CollectionModel, FieldModel } from './models'; export class CollectionManagerPlugin extends Plugin { + async beforeLoad() { this.app.db.registerModels({ CollectionModel, @@ -60,7 +61,7 @@ export class CollectionManagerPlugin extends Plugin { await fn(model, { database: this.app.db }); } }); - + this.app.db.on('fields.afterCreate', afterCreateForReverseField(this.app.db)); this.app.db.on('collections.afterCreateWithAssociations', async (model, { context, transaction }) => { @@ -116,22 +117,10 @@ export class CollectionManagerPlugin extends Plugin { await model.remove(options); }); - this.app.on('beforeStart', async () => { - await this.app.db.getRepository('collections').load(); - }); - - this.app.on('beforeUpgrade', async () => { - await this.app.db.getRepository('collections').load(); - }); - - this.app.on('cli.beforeMigrator', async () => { - const exists = await this.app.db.collectionExistsInDb('collections'); - if (exists) { - await this.app.db.getRepository('collections').load(); + this.app.on('afterLoad', async (app, options) => { + if (options?.method === 'install') { + return; } - }); - - this.app.on('cli.beforeDbSync', async () => { const exists = await this.app.db.collectionExistsInDb('collections'); if (exists) { await this.app.db.getRepository('collections').load(); @@ -195,7 +184,7 @@ export class CollectionManagerPlugin extends Plugin { directory: path.resolve(__dirname, './collections'), }); - const errorHandlerPlugin = this.app.getPlugin('@nocobase/plugin-error-handler'); + const errorHandlerPlugin = this.app.getPlugin('error-handler'); errorHandlerPlugin.errorHandler.register( (err) => { return err instanceof UniqueConstraintError; @@ -205,10 +194,6 @@ export class CollectionManagerPlugin extends Plugin { }, ); } - - getName(): string { - return this.getPackageName(__dirname); - } } export default CollectionManagerPlugin; diff --git a/packages/plugins/error-handler/package.json b/packages/plugins/error-handler/package.json index 9dd04a6dc..618e24afd 100644 --- a/packages/plugins/error-handler/package.json +++ b/packages/plugins/error-handler/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/plugin-error-handler", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "description": "", "license": "Apache-2.0", "licenses": [ @@ -13,7 +13,7 @@ "types": "./lib/index.d.ts", "dependencies": { "@formily/json-schema": "^2.0.15", - "@nocobase/server": "0.7.4-alpha.7" + "@nocobase/server": "0.8.0-alpha.1" }, "repository": { "type": "git", diff --git a/packages/plugins/error-handler/src/__tests__/render-error.test.ts b/packages/plugins/error-handler/src/__tests__/render-error.test.ts index 113964960..b653c43db 100644 --- a/packages/plugins/error-handler/src/__tests__/render-error.test.ts +++ b/packages/plugins/error-handler/src/__tests__/render-error.test.ts @@ -9,14 +9,14 @@ describe('create with exception', () => { acl: false, }); await app.cleanDb(); - app.plugin(PluginErrorHandler); + app.plugin(PluginErrorHandler, { name: 'error-handler' }); }); afterEach(async () => { await app.destroy(); }); - it.only('should handle not null error', async () => { + it('should handle not null error', async () => { app.collection({ name: 'users', fields: [ diff --git a/packages/plugins/error-handler/src/server.ts b/packages/plugins/error-handler/src/server.ts index a2933cd6b..d8df76ec2 100644 --- a/packages/plugins/error-handler/src/server.ts +++ b/packages/plugins/error-handler/src/server.ts @@ -7,9 +7,6 @@ import enUS from './locale/en_US'; import zhCN from './locale/zh_CN'; export class PluginErrorHandler extends Plugin { - getName(): string { - return this.getPackageName(__dirname); - } errorHandler: ErrorHandler = new ErrorHandler(); i18nNs: string = 'error-handler'; diff --git a/packages/plugins/export/package.json b/packages/plugins/export/package.json index 3d29695ed..3e517aa4f 100644 --- a/packages/plugins/export/package.json +++ b/packages/plugins/export/package.json @@ -1,10 +1,10 @@ { "name": "@nocobase/plugin-export", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/server/index.js", "dependencies": { - "@nocobase/client": "0.7.4-alpha.7", - "@nocobase/server": "0.7.4-alpha.7", + "@nocobase/client": "0.8.0-alpha.1", + "@nocobase/server": "0.8.0-alpha.1", "node-xlsx": "^0.16.1" }, "devDependencies": { diff --git a/packages/plugins/export/src/server/index.ts b/packages/plugins/export/src/server/index.ts index 3b0ed2de1..1e70d13ac 100644 --- a/packages/plugins/export/src/server/index.ts +++ b/packages/plugins/export/src/server/index.ts @@ -2,10 +2,6 @@ import { InstallOptions, Plugin } from '@nocobase/server'; import { exportXlsx } from './actions'; export class ExportPlugin extends Plugin { - getName(): string { - return this.getPackageName(__dirname); - } - beforeLoad() {} async load() { diff --git a/packages/plugins/file-manager/package.json b/packages/plugins/file-manager/package.json index 9638191bb..ddcd12fee 100644 --- a/packages/plugins/file-manager/package.json +++ b/packages/plugins/file-manager/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/plugin-file-manager", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/index.js", "license": "Apache-2.0", "licenses": [ @@ -11,7 +11,7 @@ ], "dependencies": { "@koa/multer": "^3.0.0", - "@nocobase/server": "0.7.4-alpha.7", + "@nocobase/server": "0.8.0-alpha.1", "aws-sdk": "^2.2.32", "koa-static": "^5.0.0", "mime-match": "^1.0.2", @@ -21,7 +21,7 @@ "multer-s3": "^2.10.0" }, "devDependencies": { - "@nocobase/test": "0.7.4-alpha.7", + "@nocobase/test": "0.8.0-alpha.1", "@types/koa-multer": "^1.0.1", "@types/multer": "^1.4.5" }, diff --git a/packages/plugins/file-manager/src/server.ts b/packages/plugins/file-manager/src/server.ts index 55685befa..270f83b02 100644 --- a/packages/plugins/file-manager/src/server.ts +++ b/packages/plugins/file-manager/src/server.ts @@ -39,7 +39,4 @@ export default class PluginFileManager extends Plugin { this.app.acl.allow('attachments', 'upload', 'loggedIn'); } - getName(): string { - return this.getPackageName(__dirname); - } } diff --git a/packages/plugins/multi-app-manager/package.json b/packages/plugins/multi-app-manager/package.json index a0c58f3d3..829199e58 100644 --- a/packages/plugins/multi-app-manager/package.json +++ b/packages/plugins/multi-app-manager/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/plugin-multi-app-manager", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/index.js", "license": "Apache-2.0", "licenses": [ @@ -10,7 +10,7 @@ } ], "dependencies": { - "@nocobase/server": "0.7.4-alpha.7" + "@nocobase/server": "0.8.0-alpha.1" }, "gitHead": "b17d1ecae5dd0b9d4e7170dcf9663bb225598990" } diff --git a/packages/plugins/multi-app-manager/src/__tests__/mock-get-schema.test.ts b/packages/plugins/multi-app-manager/src/__tests__/mock-get-schema.test.ts index 2fc1e89db..1f618ec82 100644 --- a/packages/plugins/multi-app-manager/src/__tests__/mock-get-schema.test.ts +++ b/packages/plugins/multi-app-manager/src/__tests__/mock-get-schema.test.ts @@ -71,7 +71,7 @@ describe('test with start', () => { values: { name, options: { - plugins: ['@nocobase/plugin-ui-schema-storage'], + plugins: ['ui-schema-storage'], }, }, }); diff --git a/packages/plugins/multi-app-manager/src/__tests__/multiple-apps.test.ts b/packages/plugins/multi-app-manager/src/__tests__/multiple-apps.test.ts index 709f36030..da7908a35 100644 --- a/packages/plugins/multi-app-manager/src/__tests__/multiple-apps.test.ts +++ b/packages/plugins/multi-app-manager/src/__tests__/multiple-apps.test.ts @@ -57,7 +57,7 @@ describe('multiple apps create', () => { values: { name, options: { - plugins: [['@nocobase/plugin-ui-schema-storage', { test: 'B' }]], + plugins: [['ui-schema-storage', { test: 'B' }]], }, }, }); @@ -65,7 +65,7 @@ describe('multiple apps create', () => { const miniApp = app.appManager.applications.get(name); expect(miniApp).toBeDefined(); - const plugin = miniApp.pm.get('@nocobase/plugin-ui-schema-storage'); + const plugin = miniApp.pm.get('ui-schema-storage'); expect(plugin).toBeDefined(); expect(plugin.options).toMatchObject({ @@ -79,7 +79,7 @@ describe('multiple apps create', () => { values: { name, options: { - plugins: ['@nocobase/plugin-ui-schema-storage'], + plugins: ['ui-schema-storage'], }, }, }); @@ -106,7 +106,7 @@ describe('multiple apps create', () => { values: { name, options: { - plugins: ['@nocobase/plugin-ui-schema-storage'], + plugins: ['ui-schema-storage'], }, }, }); diff --git a/packages/plugins/multi-app-manager/src/models/application.ts b/packages/plugins/multi-app-manager/src/models/application.ts index 0a89d282c..00383ff37 100644 --- a/packages/plugins/multi-app-manager/src/models/application.ts +++ b/packages/plugins/multi-app-manager/src/models/application.ts @@ -17,10 +17,9 @@ export class ApplicationModel extends Model { } static async handleAppStart(app: Application, options: registerAppOptions) { + await app.load(); if (!options?.skipInstall) { await app.install(); - } else { - await app.load(); } await app.start(); } diff --git a/packages/plugins/multi-app-manager/src/server.ts b/packages/plugins/multi-app-manager/src/server.ts index eeca27b1b..7d6cf9a8e 100644 --- a/packages/plugins/multi-app-manager/src/server.ts +++ b/packages/plugins/multi-app-manager/src/server.ts @@ -3,9 +3,6 @@ import { resolve } from 'path'; import { ApplicationModel } from './models/application'; export class PluginMultiAppManager extends Plugin { - getName(): string { - return this.getPackageName(__dirname); - } async install(options?: InstallOptions) { const repo = this.db.getRepository('collections'); diff --git a/packages/plugins/notifications/package.json b/packages/plugins/notifications/package.json index fbd978e1b..e461fce2c 100644 --- a/packages/plugins/notifications/package.json +++ b/packages/plugins/notifications/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/plugin-notifications", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/index.js", "license": "Apache-2.0", "licenses": [ @@ -13,7 +13,7 @@ "nodemailer": "^6.6.1" }, "devDependencies": { - "@nocobase/test": "0.7.4-alpha.7", + "@nocobase/test": "0.8.0-alpha.1", "@types/nodemailer": "6.4.4", "nodemailer-mock": "^1.5.11" }, diff --git a/packages/plugins/notifications/src/server.ts b/packages/plugins/notifications/src/server.ts index e6e3d9cb5..03a5943c4 100644 --- a/packages/plugins/notifications/src/server.ts +++ b/packages/plugins/notifications/src/server.ts @@ -1,5 +1,5 @@ -import path from 'path'; import { Plugin } from '@nocobase/server'; +import path from 'path'; export default class PluginNotifications extends Plugin { async load() { @@ -7,8 +7,4 @@ export default class PluginNotifications extends Plugin { directory: path.resolve(__dirname, 'collections'), }); } - - getName(): string { - return this.getPackageName(__dirname); - } } diff --git a/packages/plugins/system-settings/package.json b/packages/plugins/system-settings/package.json index 02e2ae5af..58988eed9 100644 --- a/packages/plugins/system-settings/package.json +++ b/packages/plugins/system-settings/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/plugin-system-settings", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/index.js", "license": "Apache-2.0", "licenses": [ @@ -10,7 +10,7 @@ } ], "devDependencies": { - "@nocobase/test": "0.7.4-alpha.7" + "@nocobase/test": "0.8.0-alpha.1" }, "gitHead": "b17d1ecae5dd0b9d4e7170dcf9663bb225598990" } diff --git a/packages/plugins/system-settings/src/server.ts b/packages/plugins/system-settings/src/server.ts index f215ef87c..80f918645 100644 --- a/packages/plugins/system-settings/src/server.ts +++ b/packages/plugins/system-settings/src/server.ts @@ -42,10 +42,6 @@ export class SystemSettingsPlugin extends Plugin { }), ); } - - getName(): string { - return this.getPackageName(__dirname); - } } export default SystemSettingsPlugin; diff --git a/packages/plugins/ui-routes-storage/package.json b/packages/plugins/ui-routes-storage/package.json index b5d69ffa5..2de51b021 100644 --- a/packages/plugins/ui-routes-storage/package.json +++ b/packages/plugins/ui-routes-storage/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/plugin-ui-routes-storage", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/index.js", "license": "Apache-2.0", "licenses": [ @@ -13,7 +13,7 @@ "flat-to-nested": "^1.1.1" }, "devDependencies": { - "@nocobase/test": "0.7.4-alpha.7" + "@nocobase/test": "0.8.0-alpha.1" }, "gitHead": "b17d1ecae5dd0b9d4e7170dcf9663bb225598990" } diff --git a/packages/plugins/ui-routes-storage/src/server.ts b/packages/plugins/ui-routes-storage/src/server.ts index 096d9263f..519504eca 100644 --- a/packages/plugins/ui-routes-storage/src/server.ts +++ b/packages/plugins/ui-routes-storage/src/server.ts @@ -5,9 +5,6 @@ import { resolve } from 'path'; import { getAccessible } from './actions/getAccessible'; export class UiRoutesStoragePlugin extends Plugin { - getName(): string { - return this.getPackageName(__dirname); - } async install() { const repository = this.app.db.getRepository('uiRoutes'); diff --git a/packages/plugins/ui-schema-storage/package.json b/packages/plugins/ui-schema-storage/package.json index f6fab3133..fb4cfec35 100644 --- a/packages/plugins/ui-schema-storage/package.json +++ b/packages/plugins/ui-schema-storage/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/plugin-ui-schema-storage", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/index.js", "license": "Apache-2.0", "licenses": [ @@ -11,7 +11,7 @@ ], "devDependencies": { "@formily/json-schema": "2.0.20", - "@nocobase/test": "0.7.4-alpha.7" + "@nocobase/test": "0.8.0-alpha.1" }, "gitHead": "b17d1ecae5dd0b9d4e7170dcf9663bb225598990" } diff --git a/packages/plugins/ui-schema-storage/src/__tests__/action.test.ts b/packages/plugins/ui-schema-storage/src/__tests__/action.test.ts index 782df88dd..0cdf4a9e2 100644 --- a/packages/plugins/ui-schema-storage/src/__tests__/action.test.ts +++ b/packages/plugins/ui-schema-storage/src/__tests__/action.test.ts @@ -16,7 +16,7 @@ describe('action test', () => { const queryInterface = db.sequelize.getQueryInterface(); await queryInterface.dropAllTables(); - app.plugin(PluginUiSchema); + app.plugin(PluginUiSchema, { name: 'ui-schema-storage' }); await app.load(); await db.sync({ diff --git a/packages/plugins/ui-schema-storage/src/__tests__/server-hook-impl.test.ts b/packages/plugins/ui-schema-storage/src/__tests__/server-hook-impl.test.ts index c25cd9ba1..376eeb3af 100644 --- a/packages/plugins/ui-schema-storage/src/__tests__/server-hook-impl.test.ts +++ b/packages/plugins/ui-schema-storage/src/__tests__/server-hook-impl.test.ts @@ -1,9 +1,9 @@ import { BelongsToManyRepository, Database } from '@nocobase/database'; -import PluginUsers from '@nocobase/plugin-users'; import PluginACL from '@nocobase/plugin-acl'; -import PluginErrorHandler from '@nocobase/plugin-error-handler'; import PluginCollectionManager from '@nocobase/plugin-collection-manager'; +import PluginErrorHandler from '@nocobase/plugin-error-handler'; import UiSchemaStoragePlugin, { UiSchemaRepository } from '@nocobase/plugin-ui-schema-storage'; +import PluginUsers from '@nocobase/plugin-users'; import { mockServer, MockServer } from '@nocobase/test'; describe('server hooks', () => { @@ -24,17 +24,17 @@ describe('server hooks', () => { await app.cleanDb(); db = app.db; - app.plugin(UiSchemaStoragePlugin); - app.plugin(PluginErrorHandler); - app.plugin(PluginCollectionManager); - app.plugin(PluginUsers); - app.plugin(PluginACL); + app.plugin(UiSchemaStoragePlugin, { name: 'ui-schema-storage' }); + app.plugin(PluginErrorHandler, { name: 'error-handler' }); + app.plugin(PluginCollectionManager, { name: 'collection-manager' }); + app.plugin(PluginUsers, { name: 'users' }); + app.plugin(PluginACL, { name: 'acl' }); await app.loadAndInstall(); uiSchemaRepository = db.getRepository('uiSchemas'); - uiSchemaPlugin = app.getPlugin('@nocobase/plugin-ui-schema-storage'); + uiSchemaPlugin = app.getPlugin('ui-schema-storage'); }); it('should clean row struct', async () => { diff --git a/packages/plugins/ui-schema-storage/src/__tests__/server-hook.test.ts b/packages/plugins/ui-schema-storage/src/__tests__/server-hook.test.ts index 3f019aed9..dedbbc85c 100644 --- a/packages/plugins/ui-schema-storage/src/__tests__/server-hook.test.ts +++ b/packages/plugins/ui-schema-storage/src/__tests__/server-hook.test.ts @@ -1,6 +1,6 @@ import { Database } from '@nocobase/database'; -import PluginErrorHandler from '@nocobase/plugin-error-handler'; import PluginCollectionManager from '@nocobase/plugin-collection-manager'; +import PluginErrorHandler from '@nocobase/plugin-error-handler'; import UiSchemaStoragePlugin, { UiSchemaRepository } from '@nocobase/plugin-ui-schema-storage'; import { mockServer, MockServer } from '@nocobase/test'; @@ -62,16 +62,16 @@ describe('server hooks', () => { await app.cleanDb(); db = app.db; - app.plugin(UiSchemaStoragePlugin); - app.plugin(PluginErrorHandler); - app.plugin(PluginCollectionManager); + app.plugin(UiSchemaStoragePlugin, { name: 'ui-schema-storage' }); + app.plugin(PluginErrorHandler, { name: 'error-handler' }); + app.plugin(PluginCollectionManager, { name: 'collection-manager' }); await app.loadAndInstall(); uiSchemaRepository = db.getRepository('uiSchemas'); await uiSchemaRepository.insert(schema); - uiSchemaPlugin = app.getPlugin('@nocobase/plugin-ui-schema-storage'); + uiSchemaPlugin = app.getPlugin('ui-schema-storage'); }); it('should call server hooks onFieldDestroy', async () => { diff --git a/packages/plugins/ui-schema-storage/src/__tests__/ui-schema-model.test.ts b/packages/plugins/ui-schema-storage/src/__tests__/ui-schema-model.test.ts index 0c1b31801..8f6e6fc51 100644 --- a/packages/plugins/ui-schema-storage/src/__tests__/ui-schema-model.test.ts +++ b/packages/plugins/ui-schema-storage/src/__tests__/ui-schema-model.test.ts @@ -22,7 +22,7 @@ describe('ui schema model', () => { const queryInterface = db.sequelize.getQueryInterface(); await queryInterface.dropAllTables(); - app.plugin(PluginUiSchema); + app.plugin(PluginUiSchema, { name: 'ui-schema-storage' }); await app.load(); diff --git a/packages/plugins/ui-schema-storage/src/__tests__/ui-schema-repository-with-cache.test.ts b/packages/plugins/ui-schema-storage/src/__tests__/ui-schema-repository-with-cache.test.ts index 83cbf9d75..cfb3860d6 100644 --- a/packages/plugins/ui-schema-storage/src/__tests__/ui-schema-repository-with-cache.test.ts +++ b/packages/plugins/ui-schema-storage/src/__tests__/ui-schema-repository-with-cache.test.ts @@ -1,5 +1,5 @@ -import { Database } from '@nocobase/database'; import { Cache } from '@nocobase/cache'; +import { Database } from '@nocobase/database'; import { mockServer, MockServer } from '@nocobase/test'; import UiSchemaRepository, { GetJsonSchemaOptions, GetPropertiesOptions } from '../repository'; import PluginUiSchema from '../server'; @@ -25,7 +25,7 @@ describe('ui_schema repository with cache', () => { await db.sequelize.getQueryInterface().dropAllTables(); - app.plugin(PluginUiSchema); + app.plugin(PluginUiSchema, { name: 'ui-schema-storage' }); await app.load(); await db.sync({ diff --git a/packages/plugins/ui-schema-storage/src/__tests__/ui-schema-repository.test.ts b/packages/plugins/ui-schema-storage/src/__tests__/ui-schema-repository.test.ts index 5f2be907e..723d268a6 100644 --- a/packages/plugins/ui-schema-storage/src/__tests__/ui-schema-repository.test.ts +++ b/packages/plugins/ui-schema-storage/src/__tests__/ui-schema-repository.test.ts @@ -25,7 +25,7 @@ describe('ui_schema repository', () => { const queryInterface = db.sequelize.getQueryInterface(); await queryInterface.dropAllTables(); - app.plugin(PluginUiSchema); + app.plugin(PluginUiSchema, { name: 'ui-schema-storage' }); await app.load(); await db.sync({ diff --git a/packages/plugins/ui-schema-storage/src/__tests__/ui-schema.test.ts b/packages/plugins/ui-schema-storage/src/__tests__/ui-schema.test.ts index 22dbb25d0..0846ef451 100644 --- a/packages/plugins/ui-schema-storage/src/__tests__/ui-schema.test.ts +++ b/packages/plugins/ui-schema-storage/src/__tests__/ui-schema.test.ts @@ -22,7 +22,7 @@ describe('ui-schema', () => { await db.sequelize.getQueryInterface().dropAllTables(); - app.plugin(PluginUiSchema); + app.plugin(PluginUiSchema, { name: 'ui-schema-storage' }); await app.loadAndInstall(); uiSchemaRepository = db.getCollection('uiSchemas').repository as UiSchemaRepository; diff --git a/packages/plugins/ui-schema-storage/src/server.ts b/packages/plugins/ui-schema-storage/src/server.ts index 25dff32ff..8848aecce 100644 --- a/packages/plugins/ui-schema-storage/src/server.ts +++ b/packages/plugins/ui-schema-storage/src/server.ts @@ -70,10 +70,6 @@ export class UiSchemaStoragePlugin extends Plugin { directory: path.resolve(__dirname, 'collections'), }); } - - getName(): string { - return this.getPackageName(__dirname); - } } export default UiSchemaStoragePlugin; diff --git a/packages/plugins/users/package.json b/packages/plugins/users/package.json index d69b25f4a..0c482d95d 100644 --- a/packages/plugins/users/package.json +++ b/packages/plugins/users/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/plugin-users", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/index.js", "license": "Apache-2.0", "licenses": [ @@ -10,16 +10,16 @@ } ], "dependencies": { - "@nocobase/actions": "0.7.4-alpha.7", - "@nocobase/database": "0.7.4-alpha.7", - "@nocobase/resourcer": "0.7.4-alpha.7", - "@nocobase/server": "0.7.4-alpha.7", - "@nocobase/utils": "0.7.4-alpha.7", - "jsonwebtoken": "^8.5.1", - "json-templates": "^4.2.0" + "@nocobase/actions": "0.8.0-alpha.1", + "@nocobase/database": "0.8.0-alpha.1", + "@nocobase/resourcer": "0.8.0-alpha.1", + "@nocobase/server": "0.8.0-alpha.1", + "@nocobase/utils": "0.8.0-alpha.1", + "json-templates": "^4.2.0", + "jsonwebtoken": "^8.5.1" }, "devDependencies": { - "@nocobase/test": "0.7.4-alpha.7", + "@nocobase/test": "0.8.0-alpha.1", "@types/jsonwebtoken": "^8.5.8" }, "gitHead": "b17d1ecae5dd0b9d4e7170dcf9663bb225598990" diff --git a/packages/plugins/users/src/__tests__/actions.test.ts b/packages/plugins/users/src/__tests__/actions.test.ts index a9e96ed09..594871621 100644 --- a/packages/plugins/users/src/__tests__/actions.test.ts +++ b/packages/plugins/users/src/__tests__/actions.test.ts @@ -22,7 +22,7 @@ describe('actions', () => { await app.loadAndInstall(); db = app.db; - pluginUser = app.getPlugin('@nocobase/plugin-users'); + pluginUser = app.getPlugin('users'); adminUser = await db.getRepository('users').findOne({ filter: { email: process.env.INIT_ROOT_EMAIL diff --git a/packages/plugins/users/src/__tests__/fields.test.ts b/packages/plugins/users/src/__tests__/fields.test.ts index 346ce50d7..7c864f0cd 100644 --- a/packages/plugins/users/src/__tests__/fields.test.ts +++ b/packages/plugins/users/src/__tests__/fields.test.ts @@ -10,9 +10,8 @@ describe('createdBy/updatedBy', () => { beforeEach(async () => { api = mockServer(); api.plugin(UsersPlugin, userPluginConfig); - api.plugin(PluginACL); - await api.cleanDb(); - await api.loadAndInstall(); + api.plugin(PluginACL, { name: 'acl' }); + await api.loadAndInstall({ clean: true }); db = api.db; }); @@ -63,7 +62,7 @@ describe('createdBy/updatedBy', () => { await db.sync(); const user1 = await db.getCollection('users').model.create(); const user2 = await db.getCollection('users').model.create(); - const p1 = await Post.repository.create({ + const p1 = await Post.repository.create({ context: { state: { currentUser: user1, diff --git a/packages/plugins/users/src/__tests__/utils.ts b/packages/plugins/users/src/__tests__/utils.ts index 3b5df705f..baa1589e3 100644 --- a/packages/plugins/users/src/__tests__/utils.ts +++ b/packages/plugins/users/src/__tests__/utils.ts @@ -1,6 +1,7 @@ import { UserPluginConfig } from '../server'; export const userPluginConfig: UserPluginConfig = { + name: 'users', jwt: { secret: '09f26e402586e2faa8da4c98a35f1b20d6b033c60', }, diff --git a/packages/plugins/users/src/actions/users.ts b/packages/plugins/users/src/actions/users.ts index 0cabfc4b6..22e54ed73 100644 --- a/packages/plugins/users/src/actions/users.ts +++ b/packages/plugins/users/src/actions/users.ts @@ -15,7 +15,7 @@ export async function check(ctx: Context, next: Next) { } export async function signin(ctx: Context, next: Next) { - const { authenticators, jwtService } = ctx.app.getPlugin('@nocobase/plugin-users'); + const { authenticators, jwtService } = ctx.app.getPlugin('users'); const branches = {}; for (const [name, authenticator] of authenticators.getEntities()) { branches[name] = authenticator; diff --git a/packages/plugins/users/src/middlewares/parseToken.ts b/packages/plugins/users/src/middlewares/parseToken.ts index bc59a72e2..a82137e33 100644 --- a/packages/plugins/users/src/middlewares/parseToken.ts +++ b/packages/plugins/users/src/middlewares/parseToken.ts @@ -13,7 +13,7 @@ async function findUserByToken(ctx: Context) { if (!token) { return null; } - const { jwtService } = ctx.app.getPlugin('@nocobase/plugin-users'); + const { jwtService } = ctx.app.getPlugin('users'); try { const { userId } = await jwtService.decode(token); const collection = ctx.db.getCollection('users'); diff --git a/packages/plugins/users/src/server.ts b/packages/plugins/users/src/server.ts index 4b2efc1c2..dc6b00153 100644 --- a/packages/plugins/users/src/server.ts +++ b/packages/plugins/users/src/server.ts @@ -14,6 +14,7 @@ import { enUS, zhCN } from './locale'; import { parseToken } from './middlewares'; export interface UserPluginConfig { + name?: string; jwt: JwtOptions; } @@ -117,7 +118,7 @@ export default class UsersPlugin extends Plugin { initAuthenticators(this); // TODO(module): should move to preset - const verificationPlugin = this.app.getPlugin('@nocobase/plugin-verification') as any; + const verificationPlugin = this.app.getPlugin('verification') as any; if (verificationPlugin && process.env.DEFAULT_SMS_VERIFY_CODE_PROVIDER) { verificationPlugin.interceptors.register('users:signin', { manual: true, @@ -220,8 +221,4 @@ export default class UsersPlugin extends Plugin { await repo.db2cm('users'); } } - - getName(): string { - return this.getPackageName(__dirname); - } } diff --git a/packages/plugins/verification/package.json b/packages/plugins/verification/package.json index 8895e3bab..7dbb3210d 100644 --- a/packages/plugins/verification/package.json +++ b/packages/plugins/verification/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/plugin-verification", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/index.js", "license": "Apache-2.0", "licenses": [ @@ -13,13 +13,13 @@ "@alicloud/dysmsapi20170525": "2.0.17", "@alicloud/openapi-client": "0.4.1", "@alicloud/tea-util": "1.4.4", - "@nocobase/actions": "0.7.4-alpha.7", - "@nocobase/resourcer": "0.7.4-alpha.7", - "@nocobase/server": "0.7.4-alpha.7", - "@nocobase/utils": "0.7.4-alpha.7" + "@nocobase/actions": "0.8.0-alpha.1", + "@nocobase/resourcer": "0.8.0-alpha.1", + "@nocobase/server": "0.8.0-alpha.1", + "@nocobase/utils": "0.8.0-alpha.1" }, "devDependencies": { - "@nocobase/test": "0.7.4-alpha.7" + "@nocobase/test": "0.8.0-alpha.1" }, "gitHead": "7e9556e489007577fc0ed89063b3a9ce2f9aae53" } diff --git a/packages/plugins/verification/src/Plugin.ts b/packages/plugins/verification/src/Plugin.ts index 64f605fc8..6dbec4579 100644 --- a/packages/plugins/verification/src/Plugin.ts +++ b/packages/plugins/verification/src/Plugin.ts @@ -74,10 +74,6 @@ export default class VerificationPlugin extends Plugin { }); }; - getName(): string { - return this.getPackageName(__dirname); - } - async install() { const { DEFAULT_SMS_VERIFY_CODE_PROVIDER, diff --git a/packages/plugins/verification/src/__tests__/Plugin.test.ts b/packages/plugins/verification/src/__tests__/Plugin.test.ts index 5d65555ad..b98285dca 100644 --- a/packages/plugins/verification/src/__tests__/Plugin.test.ts +++ b/packages/plugins/verification/src/__tests__/Plugin.test.ts @@ -1,5 +1,5 @@ -import { MockServer } from '@nocobase/test'; import Database from '@nocobase/database'; +import { MockServer } from '@nocobase/test'; import Plugin, { Provider } from '..'; @@ -21,7 +21,7 @@ describe('verification > Plugin', () => { app = await getApp(); agent = app.agent(); db = app.db; - plugin = app.getPlugin('@nocobase/plugin-verification'); + plugin = app.getPlugin('verification'); VerificationModel = db.getCollection('verifications').model; AuthorModel = db.getCollection('authors').model; AuthorRepo = db.getCollection('authors').repository; diff --git a/packages/plugins/verification/src/__tests__/index.ts b/packages/plugins/verification/src/__tests__/index.ts index add8e51bb..a5f994c12 100644 --- a/packages/plugins/verification/src/__tests__/index.ts +++ b/packages/plugins/verification/src/__tests__/index.ts @@ -1,8 +1,8 @@ -import path from 'path'; import { MockServer, mockServer } from '@nocobase/test'; +import path from 'path'; -import Plugin from '..'; import { ApplicationOptions } from '@nocobase/server'; +import Plugin from '..'; export function sleep(ms: number) { return new Promise(resolve => { @@ -17,7 +17,7 @@ interface MockAppOptions extends ApplicationOptions { export async function getApp({ manual, ...options }: MockAppOptions = {}): Promise { const app = mockServer(options); - app.plugin(Plugin); + app.plugin(Plugin, { name: 'verification' }); await app.load(); diff --git a/packages/plugins/verification/src/actions/verifications.ts b/packages/plugins/verification/src/actions/verifications.ts index 563d43ddd..301c18e1f 100644 --- a/packages/plugins/verification/src/actions/verifications.ts +++ b/packages/plugins/verification/src/actions/verifications.ts @@ -1,17 +1,17 @@ -import { promisify } from 'util'; import { randomInt, randomUUID } from 'crypto'; +import { promisify } from 'util'; -import { Op } from '@nocobase/database'; import actions, { Context, Next } from '@nocobase/actions'; +import { Op } from '@nocobase/database'; +import moment from 'moment'; import Plugin, { namespace } from '..'; import { CODE_STATUS_UNUSED } from '../constants'; -import moment from 'moment'; const asyncRandomInt = promisify(randomInt); export async function create(context: Context, next: Next) { - const plugin = context.app.getPlugin('@nocobase/plugin-verification') as Plugin; + const plugin = context.app.getPlugin('verification') as Plugin; const { values } = context.action.params; const interceptor = plugin.interceptors.get(values?.type); diff --git a/packages/plugins/workflow/package.json b/packages/plugins/workflow/package.json index aeea40d40..2290e88cb 100644 --- a/packages/plugins/workflow/package.json +++ b/packages/plugins/workflow/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/plugin-workflow", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/index.js", "license": "Apache-2.0", "licenses": [ @@ -10,17 +10,17 @@ } ], "dependencies": { - "@nocobase/actions": "0.7.4-alpha.7", - "@nocobase/client": "0.7.4-alpha.7", - "@nocobase/database": "0.7.4-alpha.7", - "@nocobase/server": "0.7.4-alpha.7", - "@nocobase/utils": "0.7.4-alpha.7", + "@nocobase/actions": "0.8.0-alpha.1", + "@nocobase/client": "0.8.0-alpha.1", + "@nocobase/database": "0.8.0-alpha.1", + "@nocobase/server": "0.8.0-alpha.1", + "@nocobase/utils": "0.8.0-alpha.1", "cron-parser": "4.4.0", "json-templates": "^4.2.0", "react-js-cron": "^1.4.0" }, "devDependencies": { - "@nocobase/test": "0.7.4-alpha.7" + "@nocobase/test": "0.8.0-alpha.1" }, "gitHead": "b17d1ecae5dd0b9d4e7170dcf9663bb225598990" } diff --git a/packages/plugins/workflow/src/server/Plugin.ts b/packages/plugins/workflow/src/server/Plugin.ts index 9c5074448..25b424f86 100644 --- a/packages/plugins/workflow/src/server/Plugin.ts +++ b/packages/plugins/workflow/src/server/Plugin.ts @@ -1,18 +1,18 @@ import path from 'path'; -import { Plugin } from '@nocobase/server'; import { Op, Transactionable } from '@nocobase/database'; +import { Plugin } from '@nocobase/server'; import { Registry } from '@nocobase/utils'; import initActions from './actions'; -import initTriggers, { Trigger } from './triggers'; -import initInstructions, { Instruction } from './instructions'; -import Processor from './Processor'; import calculators from './calculators'; -import extensions from './extensions'; -import WorkflowModel from './models/Workflow'; -import ExecutionModel from './models/Execution'; import { EXECUTION_STATUS } from './constants'; +import extensions from './extensions'; +import initInstructions, { Instruction } from './instructions'; +import ExecutionModel from './models/Execution'; +import WorkflowModel from './models/Workflow'; +import Processor from './Processor'; +import initTriggers, { Trigger } from './triggers'; @@ -65,10 +65,6 @@ export default class WorkflowPlugin extends Plugin { } }; - getName(): string { - return this.getPackageName(__dirname); - } - async load() { const { db, options } = this; diff --git a/packages/plugins/workflow/src/server/__tests__/Processor.test.ts b/packages/plugins/workflow/src/server/__tests__/Processor.test.ts index 6d71a6ffb..08b25b669 100644 --- a/packages/plugins/workflow/src/server/__tests__/Processor.test.ts +++ b/packages/plugins/workflow/src/server/__tests__/Processor.test.ts @@ -1,5 +1,5 @@ -import { Application } from '@nocobase/server'; import Database from '@nocobase/database'; +import { Application } from '@nocobase/server'; import { getApp } from '.'; import { BRANCH_INDEX, EXECUTION_STATUS, JOB_STATUS } from '../constants'; @@ -15,7 +15,7 @@ describe('workflow > Processor', () => { beforeEach(async () => { app = await getApp(); - plugin = app.pm.get('@nocobase/plugin-workflow'); + plugin = app.pm.get('workflow'); db = app.db; WorkflowModel = db.getCollection('workflows').model; diff --git a/packages/plugins/workflow/src/server/__tests__/index.ts b/packages/plugins/workflow/src/server/__tests__/index.ts index 05d75808c..eba067cb2 100644 --- a/packages/plugins/workflow/src/server/__tests__/index.ts +++ b/packages/plugins/workflow/src/server/__tests__/index.ts @@ -1,10 +1,10 @@ -import path from 'path'; import { MockServer, mockServer } from '@nocobase/test'; +import path from 'path'; -import Plugin from '..'; -import { JOB_STATUS } from '../constants'; -import calculators from '../calculators'; import { ApplicationOptions } from '@nocobase/server'; +import Plugin from '..'; +import calculators from '../calculators'; +import { JOB_STATUS } from '../constants'; export function sleep(ms: number) { return new Promise(resolve => { @@ -20,6 +20,7 @@ export async function getApp({ manual, ...options }: MockAppOptions = {}): Promi const app = mockServer(options); app.plugin(Plugin, { + name: 'workflow', instructions: { echo: { run(node, { result }, execution) { diff --git a/packages/plugins/workflow/src/server/__tests__/instructions/parallel.test.ts b/packages/plugins/workflow/src/server/__tests__/instructions/parallel.test.ts index 11a10f8c8..58379b42e 100644 --- a/packages/plugins/workflow/src/server/__tests__/instructions/parallel.test.ts +++ b/packages/plugins/workflow/src/server/__tests__/instructions/parallel.test.ts @@ -1,6 +1,6 @@ -import { Application } from '@nocobase/server'; import Database from '@nocobase/database'; -import { getApp, sleep } from '..'; +import { Application } from '@nocobase/server'; +import { getApp } from '..'; import { EXECUTION_STATUS } from '../../constants'; @@ -15,7 +15,7 @@ describe('workflow > instructions > parallel', () => { beforeEach(async () => { app = await getApp(); - plugin = app.pm.get('@nocobase/plugin-workflow'); + plugin = app.pm.get('workflow'); db = app.db; WorkflowModel = db.getCollection('workflows').model; diff --git a/packages/plugins/workflow/src/server/__tests__/instructions/prompt.test.ts b/packages/plugins/workflow/src/server/__tests__/instructions/prompt.test.ts index c93dc0168..ad9354bf1 100644 --- a/packages/plugins/workflow/src/server/__tests__/instructions/prompt.test.ts +++ b/packages/plugins/workflow/src/server/__tests__/instructions/prompt.test.ts @@ -1,6 +1,6 @@ -import { MockServer } from '@nocobase/test'; import Database from '@nocobase/database'; import UserPlugin from '@nocobase/plugin-users'; +import { MockServer } from '@nocobase/test'; import { getApp, sleep } from '..'; import { EXECUTION_STATUS, JOB_STATUS } from '../../constants'; @@ -86,7 +86,7 @@ describe.skip('workflow > instructions > prompt', () => { beforeEach(async () => { app = await getApp({ plugins: [ - '@nocobase/plugin-users' + 'users' ] }); agent = app.agent(); @@ -101,7 +101,7 @@ describe.skip('workflow > instructions > prompt', () => { { id: 2, nickname: 'b' } ]); - const userPlugin = app.getPlugin('@nocobase/plugin-users') as UserPlugin; + const userPlugin = app.getPlugin('users') as UserPlugin; userAgents = users.map((user) => app.agent().auth(userPlugin.jwtService.sign({ userId: user.id, }), { type: 'bearer' })); diff --git a/packages/plugins/workflow/src/server/actions/jobs.ts b/packages/plugins/workflow/src/server/actions/jobs.ts index ea84b6d47..3350609cd 100644 --- a/packages/plugins/workflow/src/server/actions/jobs.ts +++ b/packages/plugins/workflow/src/server/actions/jobs.ts @@ -21,7 +21,7 @@ export async function submit(context: Context, next) { await next(); - const plugin = context.app.pm.get('@nocobase/plugin-workflow'); + const plugin = context.app.pm.get('workflow'); const processor = plugin.createProcessor(instance.execution); // NOTE: resume the process and no `await` for quick returning processor.resume(instance); diff --git a/packages/plugins/workflow/src/server/extensions/assignees/actions.ts b/packages/plugins/workflow/src/server/extensions/assignees/actions.ts index e981a3227..dc58ad45e 100644 --- a/packages/plugins/workflow/src/server/extensions/assignees/actions.ts +++ b/packages/plugins/workflow/src/server/extensions/assignees/actions.ts @@ -51,7 +51,7 @@ export async function submit(context: Context, next) { instance.job.latestUserJob = instance; - const plugin = context.app.pm.get('@nocobase/plugin-workflow'); + const plugin = context.app.pm.get('workflow'); const processor = plugin.createProcessor(instance.execution); // NOTE: resume the process and no `await` for quick returning processor.resume(instance.job); diff --git a/packages/presets/nocobase/package.json b/packages/presets/nocobase/package.json index feb3dd8f9..65bea5c39 100644 --- a/packages/presets/nocobase/package.json +++ b/packages/presets/nocobase/package.json @@ -1,6 +1,6 @@ { "name": "@nocobase/preset-nocobase", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "license": "Apache-2.0", "licenses": [ { @@ -11,21 +11,21 @@ "main": "./lib/index.js", "types": "./lib/index.d.ts", "dependencies": { - "@nocobase/plugin-acl": "0.7.4-alpha.7", - "@nocobase/plugin-audit-logs": "0.7.4-alpha.7", - "@nocobase/plugin-china-region": "0.7.4-alpha.7", - "@nocobase/plugin-client": "0.7.4-alpha.7", - "@nocobase/plugin-collection-manager": "0.7.4-alpha.7", - "@nocobase/plugin-error-handler": "0.7.4-alpha.7", - "@nocobase/plugin-export": "0.7.4-alpha.7", - "@nocobase/plugin-file-manager": "0.7.4-alpha.7", - "@nocobase/plugin-system-settings": "0.7.4-alpha.7", - "@nocobase/plugin-ui-routes-storage": "0.7.4-alpha.7", - "@nocobase/plugin-ui-schema-storage": "0.7.4-alpha.7", - "@nocobase/plugin-users": "0.7.4-alpha.7", - "@nocobase/plugin-verification": "0.7.4-alpha.7", - "@nocobase/plugin-workflow": "0.7.4-alpha.7", - "@nocobase/server": "0.7.4-alpha.7" + "@nocobase/plugin-acl": "0.8.0-alpha.1", + "@nocobase/plugin-audit-logs": "0.8.0-alpha.1", + "@nocobase/plugin-china-region": "0.8.0-alpha.1", + "@nocobase/plugin-client": "0.8.0-alpha.1", + "@nocobase/plugin-collection-manager": "0.8.0-alpha.1", + "@nocobase/plugin-error-handler": "0.8.0-alpha.1", + "@nocobase/plugin-export": "0.8.0-alpha.1", + "@nocobase/plugin-file-manager": "0.8.0-alpha.1", + "@nocobase/plugin-system-settings": "0.8.0-alpha.1", + "@nocobase/plugin-ui-routes-storage": "0.8.0-alpha.1", + "@nocobase/plugin-ui-schema-storage": "0.8.0-alpha.1", + "@nocobase/plugin-users": "0.8.0-alpha.1", + "@nocobase/plugin-verification": "0.8.0-alpha.1", + "@nocobase/plugin-workflow": "0.8.0-alpha.1", + "@nocobase/server": "0.8.0-alpha.1" }, "repository": { "type": "git", diff --git a/packages/presets/nocobase/src/index.ts b/packages/presets/nocobase/src/index.ts index b8ff80bf2..7593926c7 100644 --- a/packages/presets/nocobase/src/index.ts +++ b/packages/presets/nocobase/src/index.ts @@ -1,39 +1,43 @@ import { Plugin } from '@nocobase/server'; -export class PresetNocoBase extends Plugin { - getName(): string { - return this.getPackageName(__dirname); +export class PresetNocoBase extends Plugin { + async addBuiltInPlugins() { + const plugins = [ + 'error-handler', + 'collection-manager', + 'ui-schema-storage', + 'ui-routes-storage', + 'file-manager', + 'system-settings', + 'verification', + 'users', + 'acl', + 'china-region', + 'workflow', + 'client', + 'export', + 'audit-logs', + ]; + await this.app.pm.add(plugins, { + enabled: true, + builtIn: true, + installed: true, + }); + await this.app.reload(); } - initialize() { - this.app.on('beforeInstall', async () => { - const plugins = [ - 'error-handler', - 'collection-manager', - 'ui-schema-storage', - 'ui-routes-storage', - 'file-manager', - 'system-settings', - 'verification', - 'users', - 'acl', - 'china-region', - 'workflow', - 'client', - 'export', - 'audit-logs', - ]; - for (const plugin of plugins) { - const instance = await this.app.pm.add(plugin); - if (instance.model && plugin !== 'hello') { - instance.model.enabled = true; - instance.model.builtIn = true; - instance.model.installed = true; - await instance.model.save(); - } + afterAdd() { + this.app.on('beforeUpgrade', async () => { + const result = await this.app.version.satisfies('<0.8.0-alpha.1'); + if (result) { + await this.addBuiltInPlugins(); } }); + this.app.on('beforeInstall', async () => { + await this.addBuiltInPlugins(); + }); } + beforeLoad() {} } export default PresetNocoBase; diff --git a/packages/samples/command/package.json b/packages/samples/command/package.json index 878de2e64..fb4cf64d3 100644 --- a/packages/samples/command/package.json +++ b/packages/samples/command/package.json @@ -1,12 +1,11 @@ { "name": "@nocobase/plugin-sample-command", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/server/index.js", - "dependencies": {}, "devDependencies": { - "@nocobase/client": "0.7.4-alpha.7", - "@nocobase/server": "0.7.4-alpha.7", - "@nocobase/test": "0.7.4-alpha.7" + "@nocobase/client": "0.8.0-alpha.1", + "@nocobase/server": "0.8.0-alpha.1", + "@nocobase/test": "0.8.0-alpha.1" }, "peerDependencies": { "@nocobase/client": "*", diff --git a/packages/samples/command/src/server/index.ts b/packages/samples/command/src/server/index.ts index e5ba8e482..dca5ec858 100644 --- a/packages/samples/command/src/server/index.ts +++ b/packages/samples/command/src/server/index.ts @@ -1,12 +1,9 @@ -import path from 'path'; import * as fs from 'fs/promises'; +import path from 'path'; import { InstallOptions, Plugin } from '@nocobase/server'; export class CommandPlugin extends Plugin { - getName(): string { - return this.getPackageName(__dirname); - } beforeLoad() { // TODO diff --git a/packages/samples/custom-block/package.json b/packages/samples/custom-block/package.json index 902188ffb..c8e4d9ce6 100644 --- a/packages/samples/custom-block/package.json +++ b/packages/samples/custom-block/package.json @@ -1,11 +1,10 @@ { "name": "@nocobase/plugin-sample-custom-block", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/server/index.js", - "dependencies": {}, "devDependencies": { - "@nocobase/client": "0.7.4-alpha.7", - "@nocobase/server": "0.7.4-alpha.7", - "@nocobase/test": "0.7.4-alpha.7" + "@nocobase/client": "0.8.0-alpha.1", + "@nocobase/server": "0.8.0-alpha.1", + "@nocobase/test": "0.8.0-alpha.1" } } diff --git a/packages/samples/custom-block/src/server/index.ts b/packages/samples/custom-block/src/server/index.ts index af5edec85..a25716eab 100644 --- a/packages/samples/custom-block/src/server/index.ts +++ b/packages/samples/custom-block/src/server/index.ts @@ -1,9 +1,6 @@ import { InstallOptions, Plugin } from '@nocobase/server'; export class CustomBlockPlugin extends Plugin { - getName(): string { - return this.getPackageName(__dirname); - } beforeLoad() { // TODO diff --git a/packages/samples/custom-page/package.json b/packages/samples/custom-page/package.json index 61b5f8266..180bbce19 100644 --- a/packages/samples/custom-page/package.json +++ b/packages/samples/custom-page/package.json @@ -1,11 +1,10 @@ { "name": "@nocobase/plugin-sample-custom-page", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/server/index.js", - "dependencies": {}, "devDependencies": { - "@nocobase/client": "0.7.4-alpha.7", - "@nocobase/server": "0.7.4-alpha.7", - "@nocobase/test": "0.7.4-alpha.7" + "@nocobase/client": "0.8.0-alpha.1", + "@nocobase/server": "0.8.0-alpha.1", + "@nocobase/test": "0.8.0-alpha.1" } } diff --git a/packages/samples/custom-page/src/server/index.ts b/packages/samples/custom-page/src/server/index.ts index 66d77e12b..d44ae4178 100644 --- a/packages/samples/custom-page/src/server/index.ts +++ b/packages/samples/custom-page/src/server/index.ts @@ -1,15 +1,10 @@ import { InstallOptions, Plugin } from '@nocobase/server'; export class CustomPagePlugin extends Plugin { - getName(): string { - return this.getPackageName(__dirname); - } - initialize() {} + afterAdd() {} - beforeLoad() { - // TODO - } + beforeLoad() {} async load() {} diff --git a/packages/samples/custom-signup-page/package.json b/packages/samples/custom-signup-page/package.json index 374fe18b3..a1e6fb5f2 100644 --- a/packages/samples/custom-signup-page/package.json +++ b/packages/samples/custom-signup-page/package.json @@ -1,11 +1,10 @@ { "name": "@nocobase/plugin-sample-custom-signup-page", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/server/index.js", - "dependencies": {}, "devDependencies": { - "@nocobase/client": "0.7.4-alpha.7", - "@nocobase/server": "0.7.4-alpha.7", - "@nocobase/test": "0.7.4-alpha.7" + "@nocobase/client": "0.8.0-alpha.1", + "@nocobase/server": "0.8.0-alpha.1", + "@nocobase/test": "0.8.0-alpha.1" } } diff --git a/packages/samples/custom-signup-page/src/server/index.ts b/packages/samples/custom-signup-page/src/server/index.ts index 97cab2a60..9ef95e139 100644 --- a/packages/samples/custom-signup-page/src/server/index.ts +++ b/packages/samples/custom-signup-page/src/server/index.ts @@ -1,10 +1,6 @@ import { InstallOptions, Plugin } from '@nocobase/server'; export class CustomSchemaScopePlugin extends Plugin { - getName(): string { - return this.getPackageName(__dirname); - } - beforeLoad() { // TODO } diff --git a/packages/samples/hello/package.json b/packages/samples/hello/package.json index 6526b5544..199681b48 100644 --- a/packages/samples/hello/package.json +++ b/packages/samples/hello/package.json @@ -1,12 +1,11 @@ { "name": "@nocobase/plugin-sample-hello", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/server/index.js", - "dependencies": {}, "devDependencies": { - "@nocobase/client": "0.7.4-alpha.7", - "@nocobase/server": "0.7.4-alpha.7", - "@nocobase/test": "0.7.4-alpha.7" + "@nocobase/client": "0.8.0-alpha.1", + "@nocobase/server": "0.8.0-alpha.1", + "@nocobase/test": "0.8.0-alpha.1" }, "peerDependencies": { "@nocobase/client": "*", diff --git a/packages/samples/hello/src/server/index.ts b/packages/samples/hello/src/server/index.ts index 82f3baf02..fb8ba9199 100644 --- a/packages/samples/hello/src/server/index.ts +++ b/packages/samples/hello/src/server/index.ts @@ -1,9 +1,6 @@ import { InstallOptions, Plugin } from '@nocobase/server'; export class HelloPlugin extends Plugin { - getName(): string { - return this.getPackageName(__dirname); - } beforeLoad() { // TODO diff --git a/packages/samples/ratelimit/package.json b/packages/samples/ratelimit/package.json index 95a6f31c6..f7f631a80 100644 --- a/packages/samples/ratelimit/package.json +++ b/packages/samples/ratelimit/package.json @@ -1,13 +1,13 @@ { "name": "@nocobase/plugin-sample-ratelimit", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/server/index.js", "dependencies": { "koa-ratelimit": "^5.0.1" }, "devDependencies": { - "@nocobase/client": "0.7.4-alpha.7", - "@nocobase/server": "0.7.4-alpha.7", - "@nocobase/test": "0.7.4-alpha.7" + "@nocobase/client": "0.8.0-alpha.1", + "@nocobase/server": "0.8.0-alpha.1", + "@nocobase/test": "0.8.0-alpha.1" } } diff --git a/packages/samples/ratelimit/src/server/index.ts b/packages/samples/ratelimit/src/server/index.ts index 5c679968c..c9b21dcf3 100644 --- a/packages/samples/ratelimit/src/server/index.ts +++ b/packages/samples/ratelimit/src/server/index.ts @@ -2,9 +2,6 @@ import { InstallOptions, Plugin } from '@nocobase/server'; import ratelimit from 'koa-ratelimit'; export class CustomPagePlugin extends Plugin { - getName(): string { - return this.getPackageName(__dirname); - } beforeLoad() { const db = new Map(); diff --git a/packages/samples/shop-actions/package.json b/packages/samples/shop-actions/package.json index ce05d5cdc..b0b3ef944 100644 --- a/packages/samples/shop-actions/package.json +++ b/packages/samples/shop-actions/package.json @@ -1,13 +1,11 @@ { "name": "@nocobase/plugin-sample-shop-actions", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/server/index.js", - "dependencies": { - }, "devDependencies": { - "@nocobase/client": "0.7.4-alpha.7", - "@nocobase/server": "0.7.4-alpha.7", - "@nocobase/test": "0.7.4-alpha.7" + "@nocobase/client": "0.8.0-alpha.1", + "@nocobase/server": "0.8.0-alpha.1", + "@nocobase/test": "0.8.0-alpha.1" }, "peerDependencies": { "@nocobase/client": "*", diff --git a/packages/samples/shop-actions/src/server/index.ts b/packages/samples/shop-actions/src/server/index.ts index f3fa4bd61..9cce26324 100644 --- a/packages/samples/shop-actions/src/server/index.ts +++ b/packages/samples/shop-actions/src/server/index.ts @@ -3,9 +3,6 @@ import path from 'path'; import { InstallOptions, Plugin } from '@nocobase/server'; export class ShopPlugin extends Plugin { - getName(): string { - return this.getPackageName(__dirname); - } beforeLoad() { // TODO diff --git a/packages/samples/shop-events/package.json b/packages/samples/shop-events/package.json index e9192d396..48ff7b4ac 100644 --- a/packages/samples/shop-events/package.json +++ b/packages/samples/shop-events/package.json @@ -1,13 +1,11 @@ { "name": "@nocobase/plugin-sample-shop-hooks", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/server/index.js", - "dependencies": { - }, "devDependencies": { - "@nocobase/client": "0.7.4-alpha.7", - "@nocobase/server": "0.7.4-alpha.7", - "@nocobase/test": "0.7.4-alpha.7" + "@nocobase/client": "0.8.0-alpha.1", + "@nocobase/server": "0.8.0-alpha.1", + "@nocobase/test": "0.8.0-alpha.1" }, "peerDependencies": { "@nocobase/client": "*", diff --git a/packages/samples/shop-events/src/server/index.ts b/packages/samples/shop-events/src/server/index.ts index 95e0823e9..c1b54f10f 100644 --- a/packages/samples/shop-events/src/server/index.ts +++ b/packages/samples/shop-events/src/server/index.ts @@ -40,10 +40,6 @@ export class ShopPlugin extends Plugin { console.log('%d orders expired', updated); }; - getName(): string { - return this.getPackageName(__dirname); - } - beforeLoad() { // TODO } diff --git a/packages/samples/shop-i18n/package.json b/packages/samples/shop-i18n/package.json index 112d126fe..bc3ed6eaa 100644 --- a/packages/samples/shop-i18n/package.json +++ b/packages/samples/shop-i18n/package.json @@ -1,14 +1,14 @@ { "name": "@nocobase/plugin-sample-shop-i18n", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/server/index.js", "dependencies": { "nodejs-snowflake": "2.0.1" }, "devDependencies": { - "@nocobase/client": "0.7.4-alpha.7", - "@nocobase/server": "0.7.4-alpha.7", - "@nocobase/test": "0.7.4-alpha.7" + "@nocobase/client": "0.8.0-alpha.1", + "@nocobase/server": "0.8.0-alpha.1", + "@nocobase/test": "0.8.0-alpha.1" }, "peerDependencies": { "@nocobase/client": "*", diff --git a/packages/samples/shop-i18n/src/server/index.ts b/packages/samples/shop-i18n/src/server/index.ts index 672acfcfc..9951916f9 100644 --- a/packages/samples/shop-i18n/src/server/index.ts +++ b/packages/samples/shop-i18n/src/server/index.ts @@ -7,9 +7,6 @@ import zhCN from './locales/zh-CN'; const ns = '@nocobase/plugin-sample-shop-i18n'; export class ShopPlugin extends Plugin { - getName(): string { - return this.getPackageName(__dirname); - } beforeLoad() { // TODO diff --git a/packages/samples/shop-modeling/package.json b/packages/samples/shop-modeling/package.json index bed61782f..7e61482ed 100644 --- a/packages/samples/shop-modeling/package.json +++ b/packages/samples/shop-modeling/package.json @@ -1,14 +1,14 @@ { "name": "@nocobase/plugin-sample-shop-modeling", - "version": "0.7.4-alpha.7", + "version": "0.8.0-alpha.1", "main": "lib/server/index.js", "dependencies": { "nodejs-snowflake": "2.0.1" }, "devDependencies": { - "@nocobase/client": "0.7.4-alpha.7", - "@nocobase/server": "0.7.4-alpha.7", - "@nocobase/test": "0.7.4-alpha.7" + "@nocobase/client": "0.8.0-alpha.1", + "@nocobase/server": "0.8.0-alpha.1", + "@nocobase/test": "0.8.0-alpha.1" }, "peerDependencies": { "@nocobase/client": "*", diff --git a/packages/samples/shop-modeling/src/server/index.ts b/packages/samples/shop-modeling/src/server/index.ts index 47bf2f849..4a1275277 100644 --- a/packages/samples/shop-modeling/src/server/index.ts +++ b/packages/samples/shop-modeling/src/server/index.ts @@ -5,11 +5,8 @@ import { InstallOptions, Plugin } from '@nocobase/server'; import SnowflakeField from './fields/SnowflakeField'; export class ShopPlugin extends Plugin { - getName(): string { - return this.getPackageName(__dirname); - } - initialize() { + afterAdd() { this.db.registerFieldTypes({ snowflake: SnowflakeField });