Commit 11c0acb2 authored by chenos's avatar chenos
Browse files

feat: improve code

parent 2c690a39
Showing with 39 additions and 32 deletions
+39 -32
......@@ -319,13 +319,18 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
return (<any>this.cli)._findCommand(name);
}
async load() {
await this.pm.load();
async load(options?: any) {
if (options?.reload) {
this.init();
}
await this.pm.load(options);
}
async reload() {
this.init();
await this.pm.load();
async reload(options?: any) {
await this.load({
...options,
reload: true,
});
}
getPlugin<P extends Plugin>(name: string) {
......@@ -337,9 +342,9 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> 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);
}
......@@ -419,11 +424,11 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> 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();
......
......@@ -23,6 +23,18 @@ class PluginManagerRepository extends Repository {
async upgrade(name: string | string[], options) {}
}
const collectionOptions = {
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' },
],
};
export class PluginManager {
app: Application;
collection: Collection;
......@@ -31,17 +43,7 @@ export class PluginManager {
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' },
],
});
this.collection = this.app.db.collection(collectionOptions);
const app = this.app;
const pm = this;
this.repository = this.collection.repository as PluginManagerRepository;
......@@ -138,11 +140,10 @@ export class PluginManager {
}
await next();
});
this.app.on('beforeInstall', async () => {
await this.collection.sync();
});
this.app.on('beforeLoadAll', async (options) => {
await this.collection.sync();
this.app.on('beforeLoadAll', 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;
......@@ -250,8 +251,8 @@ export class PluginManager {
return instance;
}
async load() {
await this.app.emitAsync('beforeLoadAll');
async load(options: any = {}) {
await this.app.emitAsync('beforeLoadAll', this.app, options);
for (const [name, plugin] of this.plugins) {
if (!plugin.enabled) {
......@@ -264,12 +265,12 @@ export class PluginManager {
if (!plugin.enabled) {
continue;
}
await this.app.emitAsync('beforeLoadPlugin', plugin);
await this.app.emitAsync('beforeLoadPlugin', plugin, options);
await plugin.load();
await this.app.emitAsync('afterLoadPlugin', plugin);
await this.app.emitAsync('afterLoadPlugin', plugin, options);
}
await this.app.emitAsync('afterLoadAll');
await this.app.emitAsync('afterLoadAll', this.app, options);
}
async install(options: InstallOptions = {}) {
......
......@@ -6,7 +6,7 @@ export class PresetNocoBase<O = any> extends Plugin {
}
initialize() {
this.app.on('beforeInstall', async () => {
this.app.on('beforeInstall', async (app, options) => {
const plugins = [
'error-handler',
'collection-manager',
......@@ -25,13 +25,14 @@ export class PresetNocoBase<O = any> extends Plugin {
];
for (const plugin of plugins) {
const instance = await this.app.pm.add(plugin);
if (instance.model && plugin !== 'hello') {
if (instance.model) {
instance.model.enabled = true;
instance.model.builtIn = true;
instance.model.installed = true;
await instance.model.save();
}
}
await this.app.reload();
});
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment