fix: auto install a plugin on enable (#852)

This commit is contained in:
chenos 2022-09-26 23:47:07 +08:00 committed by GitHub
parent f4a10cb7ad
commit 7e929b8f68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 3 deletions

View File

@ -15,6 +15,7 @@ const env = {
DB_TIMEZONE: '+00:00', DB_TIMEZONE: '+00:00',
DEFAULT_STORAGE_TYPE: 'local', DEFAULT_STORAGE_TYPE: 'local',
LOCAL_STORAGE_DEST: 'storage/uploads', LOCAL_STORAGE_DEST: 'storage/uploads',
MFSU_AD: 'none',
}; };
if (!process.env.APP_ENV_PATH && process.argv[2] && process.argv[2] === 'test') { if (!process.env.APP_ENV_PATH && process.argv[2] && process.argv[2] === 'test') {

View File

@ -37,6 +37,7 @@ export class PluginManager {
{ type: 'string', name: 'name', unique: true }, { type: 'string', name: 'name', unique: true },
{ type: 'string', name: 'version' }, { type: 'string', name: 'version' },
{ type: 'boolean', name: 'enabled' }, { type: 'boolean', name: 'enabled' },
{ type: 'boolean', name: 'installed' },
{ type: 'boolean', name: 'builtIn' }, { type: 'boolean', name: 'builtIn' },
{ type: 'json', name: 'options' }, { type: 'json', name: 'options' },
], ],
@ -71,6 +72,12 @@ export class PluginManager {
ctx.throw(400, 'plugin invalid'); ctx.throw(400, 'plugin invalid');
} }
await app.reload(); 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(); await app.start();
ctx.body = 'ok'; ctx.body = 'ok';
await next(); await next();
@ -128,6 +135,7 @@ export class PluginManager {
await this.collection.sync(); await this.collection.sync();
}); });
this.app.on('beforeLoadAll', async (options) => { this.app.on('beforeLoadAll', async (options) => {
await this.collection.sync();
const exists = await this.app.db.collectionExistsInDb('applicationPlugins'); const exists = await this.app.db.collectionExistsInDb('applicationPlugins');
if (!exists) { if (!exists) {
return; return;
@ -184,7 +192,7 @@ export class PluginManager {
for (const plugin of pluginClass) { for (const plugin of pluginClass) {
await this.add(plugin); await this.add(plugin);
} }
} };
return addMultiple(); return addMultiple();
} }
if (typeof pluginClass === 'string') { if (typeof pluginClass === 'string') {

View File

@ -28,6 +28,7 @@ export class PresetNocoBase<O = any> extends Plugin {
if (instance.model && plugin !== 'hello') { if (instance.model && plugin !== 'hello') {
instance.model.enabled = true; instance.model.enabled = true;
instance.model.builtIn = true; instance.model.builtIn = true;
instance.model.installed = true;
await instance.model.save(); await instance.model.save();
} }
} }

View File

@ -19,7 +19,7 @@ yarn pm enable sample-hello
yarn dev yarn dev
# for production # for production
yarn build samples/hello yarn build
yarn start yarn start
``` ```

View File

@ -19,7 +19,7 @@ yarn pm enable sample-shop-modeling
yarn dev yarn dev
# for production # for production
yarn build samples/shop-modeling yarn build
yarn start yarn start
``` ```