pluginValidation.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // See: packages/grafana-ui/src/types/plugin.ts
  2. interface PluginJSONSchema {
  3. id: string;
  4. info: PluginMetaInfo;
  5. }
  6. interface PluginMetaInfo {
  7. version: string;
  8. }
  9. export const validatePluginJson = (pluginJson: any) => {
  10. if (!pluginJson.id) {
  11. throw new Error('Plugin id is missing in plugin.json');
  12. }
  13. if (!pluginJson.info) {
  14. throw new Error('Plugin info node is missing in plugin.json');
  15. }
  16. if (!pluginJson.info.version) {
  17. throw new Error('Plugin info.version is missing in plugin.json');
  18. }
  19. const types = ['panel', 'datasource', 'app'];
  20. const type = pluginJson.type;
  21. if (!types.includes(type)) {
  22. throw new Error('Invalid plugin type in plugin.json: ' + type);
  23. }
  24. if (!pluginJson.id.endsWith('-' + type)) {
  25. throw new Error('[plugin.json] id should end with: -' + type);
  26. }
  27. };
  28. export const getPluginJson = (path: string): PluginJSONSchema => {
  29. let pluginJson;
  30. try {
  31. pluginJson = require(path);
  32. } catch (e) {
  33. throw new Error('Unable to find: ' + path);
  34. }
  35. validatePluginJson(pluginJson);
  36. return pluginJson as PluginJSONSchema;
  37. };