2020-10-09 19:35:46 +02:00
|
|
|
import { PluginManifest, PluginPermission } from './types';
|
2021-01-06 22:27:04 +02:00
|
|
|
import validatePluginId from './validatePluginId';
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
export default function manifestFromObject(o: any): PluginManifest {
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
const getString = (name: string, required: boolean = true, defaultValue: string = ''): string => {
|
2020-10-09 19:35:46 +02:00
|
|
|
if (required && !o[name]) throw new Error(`Missing required field: ${name}`);
|
|
|
|
if (!o[name]) return defaultValue;
|
|
|
|
if (typeof o[name] !== 'string') throw new Error(`Field must be a string: ${name}`);
|
|
|
|
return o[name];
|
|
|
|
};
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
const getNumber = (name: string, required: boolean = true): number => {
|
2020-10-09 19:35:46 +02:00
|
|
|
if (required && !o[name]) throw new Error(`Missing required field: ${name}`);
|
|
|
|
if (!o[name]) return 0;
|
|
|
|
if (typeof o[name] !== 'number') throw new Error(`Field must be a number: ${name}`);
|
|
|
|
return o[name];
|
|
|
|
};
|
|
|
|
|
2021-01-12 17:34:59 +02:00
|
|
|
const getStrings = (name: string, required: boolean = true, defaultValue: string[] = []): string[] => {
|
|
|
|
if (required && !o[name]) throw new Error(`Missing required field: ${name}`);
|
|
|
|
if (!o[name]) return defaultValue;
|
|
|
|
if (!Array.isArray(o[name])) throw new Error(`Field must be an array: ${name}`);
|
|
|
|
return o[name];
|
|
|
|
};
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
const permissions: PluginPermission[] = [];
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2020-11-15 16:18:46 +02:00
|
|
|
const manifest: PluginManifest = {
|
2020-10-09 19:35:46 +02:00
|
|
|
manifest_version: getNumber('manifest_version', true),
|
2020-11-17 20:26:24 +02:00
|
|
|
id: getString('id', true),
|
2020-10-09 19:35:46 +02:00
|
|
|
name: getString('name', true),
|
|
|
|
version: getString('version', true),
|
2020-11-15 16:18:46 +02:00
|
|
|
app_min_version: getString('app_min_version', true),
|
|
|
|
|
|
|
|
author: getString('author', false),
|
2020-10-09 19:35:46 +02:00
|
|
|
description: getString('description', false),
|
2020-10-13 13:57:03 +02:00
|
|
|
homepage_url: getString('homepage_url', false),
|
2021-01-06 22:27:04 +02:00
|
|
|
repository_url: getString('repository_url', false),
|
2021-01-12 17:34:59 +02:00
|
|
|
keywords: getStrings('keywords', false),
|
2020-10-09 19:35:46 +02:00
|
|
|
permissions: permissions,
|
|
|
|
};
|
|
|
|
|
2021-01-06 22:27:04 +02:00
|
|
|
validatePluginId(manifest.id);
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
if (o.permissions) {
|
|
|
|
for (const p of o.permissions) {
|
|
|
|
manifest.permissions.push(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return manifest;
|
|
|
|
}
|