growi-plugin.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { GrowiThemeMetadata, GrowiThemeSchemeType } from '@growi/core';
  2. import {
  3. Schema, Model, Document,
  4. } from 'mongoose';
  5. import {
  6. GrowiPlugin, GrowiPluginMeta, GrowiPluginOrigin, GrowiPluginResourceType, GrowiThemePluginMeta,
  7. } from '~/interfaces/plugin';
  8. import { getOrCreateModel } from '../util/mongoose-utils';
  9. export interface GrowiPluginDocument extends GrowiPlugin, Document {
  10. }
  11. export interface GrowiPluginModel extends Model<GrowiPluginDocument> {
  12. findEnabledPlugins(): Promise<GrowiPlugin[]>
  13. findEnabledPluginsIncludingAnyTypes(includingTypes: GrowiPluginResourceType[]): Promise<GrowiPlugin[]>
  14. }
  15. const growiThemeMetadataSchema = new Schema<GrowiThemeMetadata>({
  16. name: { type: String, required: true },
  17. manifestKey: { type: String, required: true },
  18. schemeType: {
  19. type: String,
  20. enum: GrowiThemeSchemeType,
  21. require: true,
  22. },
  23. bg: { type: String, required: true },
  24. topbar: { type: String, required: true },
  25. sidebar: { type: String, required: true },
  26. accent: { type: String, required: true },
  27. });
  28. const growiPluginMetaSchema = new Schema<GrowiPluginMeta|GrowiThemePluginMeta>({
  29. name: { type: String, required: true },
  30. types: {
  31. type: [String],
  32. enum: GrowiPluginResourceType,
  33. require: true,
  34. },
  35. desc: { type: String },
  36. author: { type: String },
  37. themes: [growiThemeMetadataSchema],
  38. });
  39. const growiPluginOriginSchema = new Schema<GrowiPluginOrigin>({
  40. url: { type: String },
  41. ghBranch: { type: String },
  42. ghTag: { type: String },
  43. });
  44. const growiPluginSchema = new Schema<GrowiPluginDocument, GrowiPluginModel>({
  45. isEnabled: { type: Boolean },
  46. installedPath: { type: String },
  47. origin: growiPluginOriginSchema,
  48. meta: growiPluginMetaSchema,
  49. });
  50. growiPluginSchema.statics.findEnabledPlugins = async function(): Promise<GrowiPlugin[]> {
  51. return this.find({ isEnabled: true });
  52. };
  53. growiPluginSchema.statics.findEnabledPluginsIncludingAnyTypes = async function(types: GrowiPluginResourceType[]): Promise<GrowiPlugin[]> {
  54. return this.find({
  55. isEnabled: true,
  56. 'meta.types': { $in: types },
  57. });
  58. };
  59. export default getOrCreateModel<GrowiPluginDocument, GrowiPluginModel>('GrowiPlugin', growiPluginSchema);