plugin.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { execSync } from 'child_process';
  2. import fs from 'fs';
  3. import path from 'path';
  4. import mongoose from 'mongoose';
  5. import { GrowiPlugin, GrowiPluginMeta, 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. export class PluginService {
  13. crowi: any;
  14. growiBridgeService: any;
  15. baseDir: any;
  16. getFile:any;
  17. constructor(crowi) {
  18. this.crowi = crowi;
  19. this.growiBridgeService = crowi.growiBridgeService;
  20. this.baseDir = path.join(crowi.tmpDir, 'plugins');
  21. this.getFile = this.growiBridgeService.getFile.bind(this);
  22. }
  23. async install(crowi: Crowi, origin: GrowiPluginOrigin): Promise<void> {
  24. // download
  25. const ghUrl = origin.url;
  26. const downloadDir = path.join(process.cwd(), 'tmp/plugins/');
  27. try {
  28. await this.downloadZipFile(`${ghUrl}/archive/refs/heads/master.zip`, downloadDir);
  29. }
  30. catch (err) {
  31. console.log('downloadZipFile error', err);
  32. }
  33. // TODO: detect plugins
  34. // save plugin metadata
  35. const ghRepositoryName = ghUrl.split('/').slice(-1)[0];
  36. const installedPath = path.join(downloadDir, ghRepositoryName, 'meta.json');
  37. await this.savePluginMetaData(installedPath);
  38. return;
  39. }
  40. async savePluginMetaData(installedPath: string): Promise<void> {
  41. const metaData = this.getPluginMetaData(installedPath);
  42. const GrowiPlugin = mongoose.model('GrowiPlugin');
  43. await GrowiPlugin.insertMany({
  44. isEnabled: true,
  45. installedPath,
  46. meta: {
  47. name: metaData.name,
  48. types: metaData.types,
  49. author: metaData.author,
  50. },
  51. });
  52. }
  53. private getPluginMetaData(installedPath: string): GrowiPluginMeta {
  54. const metaDataJSON = JSON.parse(fs.readFileSync(installedPath, 'utf-8'));
  55. return metaDataJSON;
  56. }
  57. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  58. static async detectPlugins(origin: GrowiPluginOrigin, installedPath: string, parentPackageJson?: any): Promise<GrowiPlugin[]> {
  59. const packageJsonPath = path.resolve(pluginStoringPath, installedPath, 'package.json');
  60. const packageJson = await import(packageJsonPath);
  61. const { growiPlugin } = packageJson;
  62. const {
  63. name: packageName, description: packageDesc, author: packageAuthor,
  64. } = parentPackageJson ?? packageJson;
  65. if (growiPlugin == null) {
  66. throw new Error('This package does not include \'growiPlugin\' section.');
  67. }
  68. // detect sub plugins for monorepo
  69. if (growiPlugin.isMonorepo && growiPlugin.packages != null) {
  70. const plugins = await Promise.all(
  71. growiPlugin.packages.map(async(subPackagePath) => {
  72. const subPackageInstalledPath = path.join(installedPath, subPackagePath);
  73. return this.detectPlugins(origin, subPackageInstalledPath, packageJson);
  74. }),
  75. );
  76. return plugins.flat();
  77. }
  78. if (growiPlugin.types == null) {
  79. throw new Error('\'growiPlugin\' section must have a \'types\' property.');
  80. }
  81. const plugin = {
  82. isEnabled: true,
  83. installedPath,
  84. origin,
  85. meta: {
  86. name: growiPlugin.name ?? packageName,
  87. desc: growiPlugin.desc ?? packageDesc,
  88. author: growiPlugin.author ?? packageAuthor,
  89. types: growiPlugin.types,
  90. },
  91. };
  92. logger.info('Plugin detected => ', plugin);
  93. return [plugin];
  94. }
  95. async listPlugins(): Promise<GrowiPlugin[]> {
  96. return [];
  97. }
  98. async downloadZipFile(ghUrl: string, filePath:string): Promise<void> {
  99. const stdout1 = execSync(`wget ${ghUrl} -O ${filePath}master.zip`);
  100. const stdout2 = execSync(`unzip ${filePath}master.zip -d ${filePath}`);
  101. const stdout3 = execSync(`rm ${filePath}master.zip`);
  102. return;
  103. }
  104. }