plugin.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { execSync } from 'child_process';
  2. import path from 'path';
  3. import { GrowiPlugin, GrowiPluginOrigin } from '~/interfaces/plugin';
  4. import loggerFactory from '~/utils/logger';
  5. import { resolveFromRoot } from '~/utils/project-dir-utils';
  6. // eslint-disable-next-line import/no-cycle
  7. import Crowi from '../crowi';
  8. const logger = loggerFactory('growi:plugins:plugin-utils');
  9. const pluginStoringPath = resolveFromRoot('tmp/plugins');
  10. export class PluginService {
  11. crowi: any;
  12. growiBridgeService: any;
  13. baseDir: any;
  14. getFile:any;
  15. constructor(crowi) {
  16. this.crowi = crowi;
  17. this.growiBridgeService = crowi.growiBridgeService;
  18. this.baseDir = path.join(crowi.tmpDir, 'plugins');
  19. this.getFile = this.growiBridgeService.getFile.bind(this);
  20. }
  21. async install(crowi: Crowi, origin: GrowiPluginOrigin): Promise<void> {
  22. // download
  23. const ghUrl = origin.url;
  24. const downloadDir = path.join(process.cwd(), 'tmp/plugins/');
  25. try {
  26. await this.downloadZipFile(`${ghUrl}/archive/refs/heads/master.zip`, downloadDir);
  27. }
  28. catch (err) {
  29. console.log('downloadZipFile error', err);
  30. }
  31. // TODO: detect plugins
  32. // TODO: save documents
  33. return;
  34. }
  35. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  36. static async detectPlugins(origin: GrowiPluginOrigin, installedPath: string, parentPackageJson?: any): Promise<GrowiPlugin[]> {
  37. const packageJsonPath = path.resolve(pluginStoringPath, installedPath, 'package.json');
  38. const packageJson = await import(packageJsonPath);
  39. const { growiPlugin } = packageJson;
  40. const {
  41. name: packageName, description: packageDesc, author: packageAuthor,
  42. } = parentPackageJson ?? packageJson;
  43. if (growiPlugin == null) {
  44. throw new Error('This package does not include \'growiPlugin\' section.');
  45. }
  46. // detect sub plugins for monorepo
  47. if (growiPlugin.isMonorepo && growiPlugin.packages != null) {
  48. const plugins = await Promise.all(
  49. growiPlugin.packages.map(async(subPackagePath) => {
  50. const subPackageInstalledPath = path.join(installedPath, subPackagePath);
  51. return this.detectPlugins(origin, subPackageInstalledPath, packageJson);
  52. }),
  53. );
  54. return plugins.flat();
  55. }
  56. if (growiPlugin.types == null) {
  57. throw new Error('\'growiPlugin\' section must have a \'types\' property.');
  58. }
  59. const plugin = {
  60. isEnabled: true,
  61. installedPath,
  62. origin,
  63. meta: {
  64. name: growiPlugin.name ?? packageName,
  65. desc: growiPlugin.desc ?? packageDesc,
  66. author: growiPlugin.author ?? packageAuthor,
  67. types: growiPlugin.types,
  68. },
  69. };
  70. logger.info('Plugin detected => ', plugin);
  71. return [plugin];
  72. }
  73. async listPlugins(): Promise<GrowiPlugin[]> {
  74. return [];
  75. }
  76. async downloadZipFile(ghUrl: string, filePath:string): Promise<void> {
  77. const stdout1 = execSync(`wget ${ghUrl} -O ${filePath}master.zip`);
  78. const stdout2 = execSync(`unzip ${filePath}master.zip -d ${filePath}`);
  79. const stdout3 = execSync(`rm ${filePath}master.zip`);
  80. return;
  81. }
  82. }