import { FileUtils, FileVo } from '../../utils/FileUtils'; import { Paths } from '../../utils/Paths'; import { PluginVo } from './PluginVo'; import { Property } from './Property'; import { SelectItemProperty } from './SelectItemProperty'; const fs = require('fs'); export class PluginDecoder { //缓存插件 private static _cachePlugins: any = {}; //初始化插件系统解析 static initalize(complete: Function, thisObject: any): void { FileUtils.getDirectionListAsync(Paths.PATH_BASE_PLUGINS, (list: FileVo[]) => { var nums: number = 0; var plugins: PluginVo[] = []; for (var i: number = 0, l: number = list.length; i < l; i++) { var fileVo: FileVo = list[i]; let pluginVo: PluginVo = new PluginVo(); pluginVo.nativePath = fileVo.url; pluginVo.name = fileVo.fileName; //检测插件是否有配置文件,如果有,则解析,否则不解析 FileUtils.exists(pluginVo.nativePath + "/" + "config.json", (ret) => { if (ret) { fs.readFile(pluginVo.nativePath + "/" + "config.json", function (err, data) { if (err) throw err; var jsonObj = JSON.parse(data); var base: any = jsonObj.base; pluginVo.author = base.author; pluginVo.dependencies = base.dependencies; pluginVo.description = base.description; pluginVo.homepage = base.homepage; pluginVo.icon = base.icon; pluginVo.main = base.main; pluginVo.name = base.name; pluginVo.title = base.title; pluginVo.toggle = base.toggle; pluginVo.version = base.version; pluginVo.properties = []; var properties: any[] = jsonObj.properties; for (var j: number = 0, jl: number = properties.length; j < jl; j++){ var propertyData: any = properties[j]; var property: Property = new Property(); property.name = propertyData.name; property.label = propertyData.label; property.value = propertyData.value; property.editType = propertyData.editType; property.restrict = propertyData.restrict; property.description = propertyData.description; property.selects = []; if (propertyData.selects) { for (var k: number = 0, kl: number = propertyData.selects.length; k < kl; k++){ var propertySelectItem: any = propertyData.selects[k]; var selectItemProperty: SelectItemProperty = new SelectItemProperty(); selectItemProperty.label = propertySelectItem.label; selectItemProperty.value = propertySelectItem.value; propertyData.selects[k] = selectItemProperty; } } pluginVo.properties[j] = property; } plugins.push(pluginVo); PluginDecoder._cachePlugins[pluginVo.name] = pluginVo; nums++; if (nums == l) { plugins.sort(); complete.apply(thisObject, [plugins]); } }); } else { nums++; if (nums == l) { complete.apply(thisObject, [plugins]); } } }); } }, this); } static getPlugin(name: string): PluginVo{ return this._cachePlugins[name]; } }