plugin.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import path from 'path';
  2. import wget from 'node-wget-js';
  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. function downloadZipFile(ghUrl: string, filename:string): void {
  11. wget({ url: ghUrl, dest: filename });
  12. return;
  13. }
  14. export class PluginService {
  15. static async install(crowi: Crowi, origin: GrowiPluginOrigin): Promise<void> {
  16. // const { importServic } = crowi;
  17. // download
  18. const ghUrl = origin.url;
  19. // const ghBranch = origin.ghBranch;
  20. // const ghTag = origin.ghTag;
  21. const downloadDir = path.join(process.cwd(), 'tmp/plugins/');
  22. downloadZipFile(`${ghUrl}/archive/refs/heads/master.zip`, downloadDir);
  23. // const test = '/workspace/growi/packages/app/tmp/plugins/master.zip';
  24. // const file = unzip();
  25. // // unzip
  26. // const files = await unzip(`${downloadDir}master.zip`);
  27. // console.log('fle', files);
  28. // const file = await importService.unzip(`${downloadDir}master.zip`);
  29. // console.log(file);
  30. // try {
  31. // // unzip
  32. // const file = await importService.unzip(zipFile);
  33. // console.log('fle', file)
  34. // }
  35. // catch (err) {
  36. // // TODO:
  37. // }
  38. // TODO: detect plugins
  39. // TODO: save documents
  40. return;
  41. }
  42. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  43. static async detectPlugins(origin: GrowiPluginOrigin, installedPath: string, parentPackageJson?: any): Promise<GrowiPlugin[]> {
  44. const packageJsonPath = path.resolve(pluginStoringPath, installedPath, 'package.json');
  45. const packageJson = await import(packageJsonPath);
  46. const { growiPlugin } = packageJson;
  47. const {
  48. name: packageName, description: packageDesc, author: packageAuthor,
  49. } = parentPackageJson ?? packageJson;
  50. if (growiPlugin == null) {
  51. throw new Error('This package does not include \'growiPlugin\' section.');
  52. }
  53. // detect sub plugins for monorepo
  54. if (growiPlugin.isMonorepo && growiPlugin.packages != null) {
  55. const plugins = await Promise.all(
  56. growiPlugin.packages.map(async(subPackagePath) => {
  57. const subPackageInstalledPath = path.join(installedPath, subPackagePath);
  58. return this.detectPlugins(origin, subPackageInstalledPath, packageJson);
  59. }),
  60. );
  61. return plugins.flat();
  62. }
  63. if (growiPlugin.types == null) {
  64. throw new Error('\'growiPlugin\' section must have a \'types\' property.');
  65. }
  66. const plugin = {
  67. isEnabled: true,
  68. installedPath,
  69. origin,
  70. meta: {
  71. name: growiPlugin.name ?? packageName,
  72. desc: growiPlugin.desc ?? packageDesc,
  73. author: growiPlugin.author ?? packageAuthor,
  74. types: growiPlugin.types,
  75. },
  76. };
  77. logger.info('Plugin detected => ', plugin);
  78. return [plugin];
  79. }
  80. async listPlugins(): Promise<GrowiPlugin[]> {
  81. return [];
  82. }
  83. }