plugin.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import fs from 'fs';
  2. import path from 'path';
  3. import mongoose from 'mongoose';
  4. import request from 'superagent';
  5. import unzipper from 'unzipper';
  6. import type { GrowiPlugin, GrowiPluginOrigin } from '~/interfaces/plugin';
  7. import loggerFactory from '~/utils/logger';
  8. import { resolveFromRoot } from '~/utils/project-dir-utils';
  9. const logger = loggerFactory('growi:plugins:plugin-utils');
  10. const pluginStoringPath = resolveFromRoot('tmp/plugins');
  11. // https://regex101.com/r/fK2rV3/1
  12. const githubReposIdPattern = new RegExp(/^\/([^/]+)\/([^/]+)$/);
  13. export class PluginService {
  14. async install(origin: GrowiPluginOrigin): Promise<void> {
  15. // download
  16. const ghUrl = new URL(origin.url);
  17. const ghPathname = ghUrl.pathname;
  18. const match = ghPathname.match(githubReposIdPattern);
  19. if (ghUrl.hostname !== 'github.com' || match == null) {
  20. throw new Error('The GitHub Repository URL is invalid.');
  21. }
  22. const ghOrganizationName = match[1];
  23. const ghReposName = match[2];
  24. // download github repository to local file system
  25. await this.download(`${ghUrl.href}/archive/refs/heads/main.zip`, ghOrganizationName, ghReposName);
  26. // save plugin metadata
  27. const installedPath = `${ghOrganizationName}/${ghReposName}`;
  28. const plugins = await PluginService.detectPlugins(origin, installedPath);
  29. await this.savePluginMetaData(plugins);
  30. return;
  31. }
  32. async download(url: string, ghOrganizationName: string, ghReposName: string): Promise<void> {
  33. const zipFilePath = path.join(pluginStoringPath, 'main.zip');
  34. const unzipFolderPath = path.join(pluginStoringPath, ghOrganizationName);
  35. const unzippedFolderPath = `${unzipFolderPath}/${ghReposName}-main`;
  36. const newFolderPath = `${unzipFolderPath}/${ghReposName}`;
  37. const deleteFile = (path: fs.PathLike) => {
  38. fs.unlink(path, (err) => {
  39. if (err) throw err;
  40. });
  41. };
  42. const downloadRepository = () => {
  43. const writeStream = fs.createWriteStream(zipFilePath);
  44. return new Promise<void>((resolve, reject) => {
  45. request
  46. .get(url)
  47. .pipe(writeStream)
  48. .on('close', () => writeStream.close())
  49. .on('finish', () => resolve())
  50. .on('error', (error: any) => reject(error));
  51. });
  52. };
  53. const unzip = () => {
  54. const stream = fs.createReadStream(zipFilePath);
  55. return new Promise<void>((resolve, reject) => {
  56. stream.pipe(unzipper.Extract({ path: unzipFolderPath }))
  57. .on('finish', () => {
  58. deleteFile(zipFilePath);
  59. resolve();
  60. })
  61. .on('error', (error: any) => reject(error));
  62. });
  63. };
  64. const rename = async() => {
  65. fs.renameSync(unzippedFolderPath, newFolderPath);
  66. };
  67. await downloadRepository();
  68. await unzip();
  69. await rename();
  70. return;
  71. }
  72. async savePluginMetaData(plugins: GrowiPlugin[]): Promise<void> {
  73. const GrowiPlugin = mongoose.model('GrowiPlugin');
  74. await GrowiPlugin.insertMany(plugins);
  75. }
  76. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  77. static async detectPlugins(origin: GrowiPluginOrigin, installedPath: string, parentPackageJson?: any): Promise<GrowiPlugin[]> {
  78. const packageJsonPath = path.resolve(pluginStoringPath, installedPath, 'package.json');
  79. const packageJson = await import(packageJsonPath);
  80. const { growiPlugin } = packageJson;
  81. const {
  82. name: packageName, description: packageDesc, author: packageAuthor,
  83. } = parentPackageJson ?? packageJson;
  84. if (growiPlugin == null) {
  85. throw new Error('This package does not include \'growiPlugin\' section.');
  86. }
  87. // detect sub plugins for monorepo
  88. if (growiPlugin.isMonorepo && growiPlugin.packages != null) {
  89. const plugins = await Promise.all(
  90. growiPlugin.packages.map(async(subPackagePath) => {
  91. const subPackageInstalledPath = path.join(installedPath, subPackagePath);
  92. return this.detectPlugins(origin, subPackageInstalledPath, packageJson);
  93. }),
  94. );
  95. return plugins.flat();
  96. }
  97. if (growiPlugin.types == null) {
  98. throw new Error('\'growiPlugin\' section must have a \'types\' property.');
  99. }
  100. const plugin = {
  101. isEnabled: true,
  102. installedPath,
  103. origin,
  104. meta: {
  105. name: growiPlugin.name ?? packageName,
  106. desc: growiPlugin.desc ?? packageDesc,
  107. author: growiPlugin.author ?? packageAuthor,
  108. types: growiPlugin.types,
  109. },
  110. };
  111. logger.info('Plugin detected => ', plugin);
  112. return [plugin];
  113. }
  114. async listPlugins(): Promise<GrowiPlugin[]> {
  115. return [];
  116. }
  117. }