plugin.ts 5.2 KB

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