(json: string)
| 12 | * 如果格式不合法则会抛出异常 |
| 13 | */ |
| 14 | export function parsePluginManifest(json: string): PluginManifest { |
| 15 | if (!isValidJson(json)) { |
| 16 | throw new Error(t('不是一个合法的JSON字符串')); |
| 17 | } |
| 18 | |
| 19 | const obj = JSON.parse(json); |
| 20 | const { valid, errors } = new Validator().validate(obj, { |
| 21 | type: 'object', |
| 22 | properties: { |
| 23 | label: { type: 'string' }, |
| 24 | name: { type: 'string' }, |
| 25 | url: { type: 'string' }, |
| 26 | icon: { type: 'string' }, |
| 27 | version: { type: 'string' }, |
| 28 | author: { type: 'string' }, |
| 29 | description: { type: 'string' }, |
| 30 | requireRestart: { type: 'boolean' }, |
| 31 | }, |
| 32 | required: ['label', 'name', 'url', 'version', 'author', 'description'], |
| 33 | additionalProperties: true, |
| 34 | }); |
| 35 | |
| 36 | if (!valid) { |
| 37 | console.error( |
| 38 | '[PluginManifest validation]:', |
| 39 | errors.map((e) => e.message).join(', ') |
| 40 | ); |
| 41 | |
| 42 | throw new Error(t('不是一个合法的插件配置')); |
| 43 | } |
| 44 | |
| 45 | // 后端url策略。根据前端的url在获取时自动变更为当前链接的后端地址 |
| 46 | obj.url = parseUrlStr(obj.url); |
| 47 | |
| 48 | return obj; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get manifest field with i18n support, |
no test coverage detected