plugin.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import path from 'path';
  2. import { GrowiPlugin, GrowiPluginOrigin } from '~/interfaces/plugin';
  3. import loggerFactory from '~/utils/logger';
  4. import { resolveFromRoot } from '~/utils/project-dir-utils';
  5. const logger = loggerFactory('growi:service:PluginService');
  6. const pluginStoringPath = resolveFromRoot('tmp/plugins');
  7. export class PluginService {
  8. static async install(origin: GrowiPluginOrigin): Promise<void> {
  9. // TODO: download
  10. // TODO: detect plugins
  11. // TODO: save documents
  12. return;
  13. }
  14. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  15. static async detectPlugins(origin: GrowiPluginOrigin, installedPath: string, parentPackageJson?: any): Promise<GrowiPlugin[]> {
  16. const packageJsonPath = path.resolve(pluginStoringPath, installedPath, 'package.json');
  17. const packageJson = await import(packageJsonPath);
  18. const { growiPlugin } = packageJson;
  19. const {
  20. name: packageName, description: packageDesc, author: packageAuthor,
  21. } = parentPackageJson ?? packageJson;
  22. if (growiPlugin == null) {
  23. throw new Error('This package does not include \'growiPlugin\' section.');
  24. }
  25. // detect sub plugins for monorepo
  26. if (growiPlugin.isMonorepo && growiPlugin.packages != null) {
  27. const plugins = await Promise.all(
  28. growiPlugin.packages.map(async(subPackagePath) => {
  29. const subPackageInstalledPath = path.join(installedPath, subPackagePath);
  30. return this.detectPlugins(origin, subPackageInstalledPath, packageJson);
  31. }),
  32. );
  33. return plugins.flat();
  34. }
  35. if (growiPlugin.types == null) {
  36. throw new Error('\'growiPlugin\' section must have a \'types\' property.');
  37. }
  38. const plugin = {
  39. isEnabled: true,
  40. installedPath,
  41. origin,
  42. meta: {
  43. name: growiPlugin.name ?? packageName,
  44. desc: growiPlugin.desc ?? packageDesc,
  45. author: growiPlugin.author ?? packageAuthor,
  46. types: growiPlugin.types,
  47. },
  48. };
  49. logger.info('Plugin detected => ', plugin);
  50. return [plugin];
  51. }
  52. async listPlugins(): Promise<GrowiPlugin[]> {
  53. return [];
  54. }
  55. }