2020-10-09 19:35:46 +02:00
|
|
|
import { PluginManifest, PluginPermission } from './types';
|
|
|
|
|
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];
|
|
|
|
};
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
const permissions: PluginPermission[] = [];
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
const manifest = {
|
|
|
|
manifest_version: getNumber('manifest_version', true),
|
|
|
|
name: getString('name', true),
|
|
|
|
version: getString('version', true),
|
|
|
|
description: getString('description', false),
|
2020-10-13 13:57:03 +02:00
|
|
|
homepage_url: getString('homepage_url', false),
|
2020-10-09 19:35:46 +02:00
|
|
|
permissions: permissions,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (o.permissions) {
|
|
|
|
for (const p of o.permissions) {
|
|
|
|
manifest.permissions.push(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return manifest;
|
|
|
|
}
|