feat: optimize plugin scan & load
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
		@@ -10,9 +10,14 @@ export class BasicLoader implements plugin.PluginLoader {
 | 
			
		||||
    constructor() {
 | 
			
		||||
        this.pluginRequireMap = new Map()
 | 
			
		||||
    }
 | 
			
		||||
    require(target: any, result: any) {
 | 
			
		||||
        this.pluginRequireMap.set(target.toString(), result)
 | 
			
		||||
        return result
 | 
			
		||||
    require(loadMetadata: plugin.PluginLoadMetadata) {
 | 
			
		||||
        let metadata = loadMetadata.instance.description
 | 
			
		||||
        if (metadata && metadata.type == this.type) {
 | 
			
		||||
            loadMetadata.metadata = metadata
 | 
			
		||||
            loadMetadata.loaded = true
 | 
			
		||||
            this.pluginRequireMap.set(metadata.source.toString(), loadMetadata.instance)
 | 
			
		||||
        }
 | 
			
		||||
        return loadMetadata
 | 
			
		||||
    }
 | 
			
		||||
    build(metadata: plugin.PluginMetadata) {
 | 
			
		||||
        return this.pluginRequireMap.get(metadata.source.toString())
 | 
			
		||||
 
 | 
			
		||||
@@ -18,8 +18,13 @@ export class IocLoader implements plugin.PluginLoader {
 | 
			
		||||
        this.pluginMetadataMap = getPluginSources()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    require(target: any, result: any) {
 | 
			
		||||
        return this.pluginMetadataMap.get(target.toString())
 | 
			
		||||
    require(loadMetadata: plugin.PluginLoadMetadata) {
 | 
			
		||||
        let metadata = this.pluginMetadataMap.get(loadMetadata.file.toString())
 | 
			
		||||
        if (metadata && metadata.type == this.type) {
 | 
			
		||||
            loadMetadata.metadata = metadata
 | 
			
		||||
            loadMetadata.loaded = true
 | 
			
		||||
        }
 | 
			
		||||
        return loadMetadata
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    build(metadata: plugin.PluginMetadata) {
 | 
			
		||||
 
 | 
			
		||||
@@ -64,8 +64,13 @@ export class PluginManagerImpl implements plugin.PluginManager {
 | 
			
		||||
        this.initialize()
 | 
			
		||||
        for (const [, scanner] of this.sacnnerMap) {
 | 
			
		||||
            try {
 | 
			
		||||
                scanner.scan(folder).forEach(file => {
 | 
			
		||||
                    this.loadPlugin(file, scanner)
 | 
			
		||||
                scanner.scan(folder).forEach(loadMetadata => {
 | 
			
		||||
                    try {
 | 
			
		||||
                        this.loadPlugin(scanner.load(loadMetadata))
 | 
			
		||||
                    } catch (error) {
 | 
			
		||||
                        console.error(`plugin scanner ${scanner.type} load ${loadMetadata.name} occurred error ${error}`)
 | 
			
		||||
                        console.ex(error)
 | 
			
		||||
                    }
 | 
			
		||||
                })
 | 
			
		||||
            } catch (error) {
 | 
			
		||||
                console.error(`plugin scanner ${scanner.type} occurred error ${error}`)
 | 
			
		||||
@@ -89,28 +94,28 @@ export class PluginManagerImpl implements plugin.PluginManager {
 | 
			
		||||
            ext()
 | 
			
		||||
            this.runCatch(plugin, stage)
 | 
			
		||||
            this.runCatch(plugin, `${this.serverType}${stage}`)
 | 
			
		||||
            plugin.description.loader[stage](plugin)
 | 
			
		||||
            plugin.description.loadMetadata.loader[stage](plugin)
 | 
			
		||||
        } catch (ex) {
 | 
			
		||||
            console.i18n("ms.plugin.manager.stage.exec.error", { plugin: plugin.description.name, executor: stage, error: ex })
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private loadPlugin(file: string, scanner: plugin.PluginScanner) {
 | 
			
		||||
    private loadPlugin(loadMetadata: plugin.PluginLoadMetadata) {
 | 
			
		||||
        try {
 | 
			
		||||
            let requireInstance = scanner.load(file)
 | 
			
		||||
            for (const [, loader] of this.loaderMap) {
 | 
			
		||||
                let metadata = loader.require(file, requireInstance)
 | 
			
		||||
                if (metadata && metadata.source && metadata.name) {
 | 
			
		||||
                    metadata.loader = loader
 | 
			
		||||
                if (loader.require(loadMetadata).loaded) {
 | 
			
		||||
                    loadMetadata.loader = loader
 | 
			
		||||
                    let metadata = loadMetadata.metadata
 | 
			
		||||
                    this.metadataMap.set(metadata.name, metadata)
 | 
			
		||||
                    metadata.loadMetadata = loadMetadata
 | 
			
		||||
                    return metadata
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        } catch (error) {
 | 
			
		||||
            console.i18n("ms.plugin.manager.initialize.error", { name: file, ex: error })
 | 
			
		||||
            console.i18n("ms.plugin.manager.initialize.error", { name: loadMetadata.file, ex: error })
 | 
			
		||||
            console.ex(error)
 | 
			
		||||
        }
 | 
			
		||||
        console.console(`§efile §b${file} §ccan't load metadata. §eskip load!`)
 | 
			
		||||
        console.console(`§6scanner: §b${loadMetadata.scanner.type} §ccan\'t load §6file §b${loadMetadata.file}. §eskip!`)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -120,8 +125,8 @@ export class PluginManagerImpl implements plugin.PluginManager {
 | 
			
		||||
    loadFromFile(file: string, scanner = this.sacnnerMap.get('file')): plugin.Plugin {
 | 
			
		||||
        if (!file) { throw new Error('plugin file can\'t be null!') }
 | 
			
		||||
        if (!scanner) { throw new Error('plugin scanner can\'t be null!') }
 | 
			
		||||
        let metadata = this.loadPlugin(file, scanner)
 | 
			
		||||
        let plugin = metadata.loader.build(metadata)
 | 
			
		||||
        let metadata = this.loadPlugin(scanner.read(file))
 | 
			
		||||
        let plugin = metadata.loadMetadata.loader.build(metadata)
 | 
			
		||||
        this.load(plugin)
 | 
			
		||||
        this.enable(plugin)
 | 
			
		||||
        return plugin
 | 
			
		||||
@@ -157,7 +162,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
 | 
			
		||||
    reload(...args: any[]): void {
 | 
			
		||||
        this.checkAndGet(args[0]).forEach((pl: plugin.Plugin) => {
 | 
			
		||||
            this.disable(pl)
 | 
			
		||||
            this.loadFromFile(pl.description.source, pl.description.scanner)
 | 
			
		||||
            this.loadFromFile(pl.description.source, pl.description.loadMetadata.scanner)
 | 
			
		||||
        })
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -206,7 +211,9 @@ export class PluginManagerImpl implements plugin.PluginManager {
 | 
			
		||||
                console.error(`§4无法加载插件 §c${metadata.name} §4请检查 §c${metadata.type} §4加载器是否正常启用!`)
 | 
			
		||||
                continue
 | 
			
		||||
            }
 | 
			
		||||
            (pluginInstance = this.loaderMap.get(metadata.type).build(metadata)) && this.instanceMap.set(metadata.name, pluginInstance)
 | 
			
		||||
            pluginInstance = this.loaderMap.get(metadata.type).build(metadata)
 | 
			
		||||
            if (!pluginInstance) { console.error(`§4加载器 §c${metadata.type} §4加载插件 §c${metadata.name} §4失败!`); continue }
 | 
			
		||||
            this.instanceMap.set(metadata.name, pluginInstance)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -6,15 +6,20 @@ import { provideSingletonNamed } from "@ccms/container"
 | 
			
		||||
export class JSFileScanner implements plugin.PluginScanner {
 | 
			
		||||
    type: string = 'file'
 | 
			
		||||
 | 
			
		||||
    scan(target: any): string[] {
 | 
			
		||||
        return this.scanFolder(fs.concat(root, target))
 | 
			
		||||
    scan(target: any): plugin.PluginLoadMetadata[] {
 | 
			
		||||
        return this.scanFolder(fs.concat(root, target)).map((file) => this.read(file))
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    load(file: string) {
 | 
			
		||||
        if (typeof file === "string") { return }
 | 
			
		||||
        this.updatePlugin(file)
 | 
			
		||||
    read(file: any): plugin.PluginLoadMetadata {
 | 
			
		||||
        return { file, type: this.type, scanner: this, loaded: false }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    load(metadata: plugin.PluginLoadMetadata): plugin.PluginLoadMetadata {
 | 
			
		||||
        if (metadata.type !== this.type) { return }
 | 
			
		||||
        this.updatePlugin(metadata.file)
 | 
			
		||||
        //@ts-ignore
 | 
			
		||||
        return require(file.toString(), { cache: false })
 | 
			
		||||
        metadata.instance = require(metadata.file.toString(), { cache: false })
 | 
			
		||||
        return metadata
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private scanFolder(folder: any): string[] {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user