plugin.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. static detectPlugins(origin: GrowiPluginOrigin, installedPath: string): GrowiPlugin[] {
  43. // const plugins: GrowiPlugin[] = [];
  44. // const package = require(path.resolve(installedPath, 'package.json'));
  45. // return scopedPackages;
  46. }
  47. // /**
  48. // * list plugin module objects
  49. // * that starts with 'growi-plugin-' or 'crowi-plugin-'
  50. // * borrowing from: https://github.com/hexojs/hexo/blob/d1db459c92a4765620343b95789361cbbc6414c5/lib/hexo/load_plugins.js#L17
  51. // *
  52. // * @returns array of objects
  53. // * [
  54. // * { name: 'growi-plugin-...', requiredVersion: '^1.0.0', installedVersion: '1.0.0' },
  55. // * { name: 'growi-plugin-...', requiredVersion: '^1.0.0', installedVersion: '1.0.0' },
  56. // * ...
  57. // * ]
  58. // *
  59. // * @memberOf PluginService
  60. // */
  61. // listPlugins() {
  62. // const packagePath = resolveFromRoot('package.json');
  63. // // Make sure package.json exists
  64. // if (!fs.existsSync(packagePath)) {
  65. // return [];
  66. // }
  67. // // Read package.json and find dependencies
  68. // const content = fs.readFileSync(packagePath);
  69. // const json = JSON.parse(content);
  70. // const deps = json.dependencies || {};
  71. // const pluginNames = Object.keys(deps).filter((name) => {
  72. // return /^@growi\/plugin-/.test(name);
  73. // });
  74. // return pluginNames.map((name) => {
  75. // return {
  76. // name,
  77. // requiredVersion: deps[name],
  78. // installedVersion: this.getVersion(name),
  79. // };
  80. // });
  81. // }
  82. // /**
  83. // * list plugin module names that starts with 'crowi-plugin-'
  84. // *
  85. // * @returns array of plugin names
  86. // *
  87. // * @memberOf PluginService
  88. // */
  89. // listPluginNames() {
  90. // const plugins = this.listPlugins();
  91. // return plugins.map((plugin) => { return plugin.name });
  92. // }
  93. // getVersion(packageName) {
  94. // const packagePath = resolveFromRoot(`../../node_modules/${packageName}/package.json`);
  95. // // Read package.json and find version
  96. // const content = fs.readFileSync(packagePath);
  97. // const json = JSON.parse(content);
  98. // return json.version || '';
  99. // }
  100. }