plugin.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { execSync } from 'child_process';
  2. import path from 'path';
  3. import mongoose from 'mongoose';
  4. import { ActivatePluginService, GrowiPluginManifestEntries } from '~/client/services/activate-plugin';
  5. import { GrowiPlugin, GrowiPluginOrigin } from '~/interfaces/plugin';
  6. import loggerFactory from '~/utils/logger';
  7. import { resolveFromRoot } from '~/utils/project-dir-utils';
  8. // eslint-disable-next-line import/no-cycle
  9. import Crowi from '../crowi';
  10. const logger = loggerFactory('growi:plugins:plugin-utils');
  11. const pluginStoringPath = resolveFromRoot('tmp/plugins');
  12. // https://regex101.com/r/fK2rV3/1
  13. const githubReposIdPattern = new RegExp(/^\/([^/]+)\/([^/]+)$/);
  14. export class PluginService {
  15. crowi: any;
  16. growiBridgeService: any;
  17. baseDir: any;
  18. getFile:any;
  19. constructor(crowi) {
  20. this.crowi = crowi;
  21. this.growiBridgeService = crowi.growiBridgeService;
  22. this.baseDir = path.join(crowi.tmpDir, 'plugins');
  23. this.getFile = this.growiBridgeService.getFile.bind(this);
  24. }
  25. async install(crowi: Crowi, origin: GrowiPluginOrigin): Promise<void> {
  26. // download
  27. const ghUrl = new URL(origin.url);
  28. const ghPathname = ghUrl.pathname;
  29. const match = ghPathname.match(githubReposIdPattern);
  30. if (ghUrl.hostname !== 'github.com' || match == null) {
  31. throw new Error('The GitHub Repository URL is invalid.');
  32. }
  33. const ghOrganizationName = match[1];
  34. const ghReposName = match[2];
  35. try {
  36. await this.downloadZipFile(`${ghUrl.href}/archive/refs/heads/main.zip`, ghOrganizationName, ghReposName);
  37. }
  38. catch (err) {
  39. console.log('downloadZipFile error', err);
  40. }
  41. // save plugin metadata
  42. const installedPath = `${ghOrganizationName}/${ghReposName}`;
  43. const plugins = await PluginService.detectPlugins(origin, installedPath);
  44. await this.savePluginMetaData(plugins);
  45. return;
  46. }
  47. async downloadZipFile(url: string, ghOrganizationName: string, ghReposName: string): Promise<void> {
  48. const downloadTargetPath = pluginStoringPath;
  49. const zipFilePath = path.join(downloadTargetPath, 'main.zip');
  50. const unzipTargetPath = path.join(pluginStoringPath, ghOrganizationName);
  51. const stdout1 = execSync(`wget ${url} -O ${zipFilePath}`);
  52. const stdout2 = execSync(`mkdir -p ${ghOrganizationName}`);
  53. const stdout3 = execSync(`rm -rf ${ghOrganizationName}/${ghReposName}`);
  54. const stdout4 = execSync(`unzip ${zipFilePath} -d ${unzipTargetPath}`);
  55. const stdout5 = execSync(`mv ${unzipTargetPath}/${ghReposName}-main ${unzipTargetPath}/${ghReposName}`);
  56. const stdout6 = execSync(`rm ${zipFilePath}`);
  57. return;
  58. }
  59. async savePluginMetaData(plugins: GrowiPlugin[]): Promise<void> {
  60. const GrowiPlugin = mongoose.model('GrowiPlugin');
  61. await GrowiPlugin.insertMany(plugins);
  62. }
  63. async getPlugins(): Promise<any> {
  64. // const initialProps: DocumentInitialProps = await Document.getInitialProps(ctx);
  65. const GrowiPlugin = mongoose.model<GrowiPlugin>('GrowiPlugin');
  66. const growiPlugins = await GrowiPlugin.find({ isEnabled: true });
  67. const pluginManifestEntries: GrowiPluginManifestEntries = await ActivatePluginService.retrievePluginManifests(growiPlugins);
  68. return JSON.parse(JSON.stringify(pluginManifestEntries));
  69. }
  70. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  71. static async detectPlugins(origin: GrowiPluginOrigin, installedPath: string, parentPackageJson?: any): Promise<GrowiPlugin[]> {
  72. const packageJsonPath = path.resolve(pluginStoringPath, installedPath, 'package.json');
  73. const packageJson = await import(packageJsonPath);
  74. const { growiPlugin } = packageJson;
  75. const {
  76. name: packageName, description: packageDesc, author: packageAuthor,
  77. } = parentPackageJson ?? packageJson;
  78. if (growiPlugin == null) {
  79. throw new Error('This package does not include \'growiPlugin\' section.');
  80. }
  81. // detect sub plugins for monorepo
  82. if (growiPlugin.isMonorepo && growiPlugin.packages != null) {
  83. const plugins = await Promise.all(
  84. growiPlugin.packages.map(async(subPackagePath) => {
  85. const subPackageInstalledPath = path.join(installedPath, subPackagePath);
  86. return this.detectPlugins(origin, subPackageInstalledPath, packageJson);
  87. }),
  88. );
  89. return plugins.flat();
  90. }
  91. if (growiPlugin.types == null) {
  92. throw new Error('\'growiPlugin\' section must have a \'types\' property.');
  93. }
  94. const plugin = {
  95. isEnabled: true,
  96. installedPath,
  97. origin,
  98. meta: {
  99. name: growiPlugin.name ?? packageName,
  100. desc: growiPlugin.desc ?? packageDesc,
  101. author: growiPlugin.author ?? packageAuthor,
  102. types: growiPlugin.types,
  103. },
  104. };
  105. logger.info('Plugin detected => ', plugin);
  106. return [plugin];
  107. }
  108. async listPlugins(): Promise<GrowiPlugin[]> {
  109. return [];
  110. }
  111. }