plugin.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import fs, { readFileSync } from 'fs';
  2. import path from 'path';
  3. import { GrowiThemeMetadata, ViteManifest } from '@growi/core';
  4. // eslint-disable-next-line no-restricted-imports
  5. import axios from 'axios';
  6. import mongoose from 'mongoose';
  7. import streamToPromise from 'stream-to-promise';
  8. import unzipper from 'unzipper';
  9. import {
  10. GrowiPlugin, GrowiPluginOrigin, GrowiPluginResourceType, GrowiThemePluginMeta,
  11. } from '~/interfaces/plugin';
  12. import loggerFactory from '~/utils/logger';
  13. import { resolveFromRoot } from '~/utils/project-dir-utils';
  14. import type { GrowiPluginModel } from '../models/growi-plugin';
  15. const logger = loggerFactory('growi:plugins:plugin-utils');
  16. const pluginStoringPath = resolveFromRoot('tmp/plugins');
  17. // https://regex101.com/r/fK2rV3/1
  18. const githubReposIdPattern = new RegExp(/^\/([^/]+)\/([^/]+)$/);
  19. const PLUGINS_STATIC_DIR = '/static/plugins'; // configured by express.static
  20. export type GrowiPluginResourceEntries = [installedPath: string, href: string][];
  21. function retrievePluginManifest(growiPlugin: GrowiPlugin): ViteManifest {
  22. const manifestPath = resolveFromRoot(path.join('tmp/plugins', growiPlugin.installedPath, 'dist/manifest.json'));
  23. const manifestStr: string = readFileSync(manifestPath, 'utf-8');
  24. return JSON.parse(manifestStr);
  25. }
  26. export interface IPluginService {
  27. install(origin: GrowiPluginOrigin): Promise<void>
  28. retrieveThemeHref(theme: string): Promise<string | undefined>
  29. retrieveAllPluginResourceEntries(): Promise<GrowiPluginResourceEntries>
  30. intallNotExistPluginRepositories(): Promise<void>
  31. }
  32. export class PluginService implements IPluginService {
  33. async intallNotExistPluginRepositories(): Promise<void> {
  34. try {
  35. // check all growi plugin documents
  36. const GrowiPlugin = mongoose.model<GrowiPlugin>('GrowiPlugin');
  37. const growiPlugins = await GrowiPlugin.find({});
  38. for await (const growiPluginData of growiPlugins) {
  39. const pluginPath = path.join(pluginStoringPath, growiPluginData.installedPath);
  40. if (fs.existsSync(pluginPath)) {
  41. // if exists repository, do nothing
  42. continue;
  43. }
  44. else {
  45. // if not exists repository, reinstall latest plugin
  46. // delete old document
  47. await GrowiPlugin.findByIdAndDelete(growiPluginData._id);
  48. // reinstall latest plugin
  49. await this.install(growiPluginData.origin);
  50. continue;
  51. }
  52. }
  53. }
  54. catch (err) {
  55. throw new Error(err);
  56. }
  57. }
  58. async install(origin: GrowiPluginOrigin): Promise<void> {
  59. // download
  60. const ghUrl = new URL(origin.url);
  61. const ghPathname = ghUrl.pathname;
  62. // TODO: Branch names can be specified.
  63. const ghBranch = 'main';
  64. const match = ghPathname.match(githubReposIdPattern);
  65. if (ghUrl.hostname !== 'github.com' || match == null) {
  66. throw new Error('The GitHub Repository URL is invalid.');
  67. }
  68. const ghOrganizationName = match[1];
  69. const ghReposName = match[2];
  70. const requestUrl = `https://github.com/${ghOrganizationName}/${ghReposName}/archive/refs/heads/${ghBranch}.zip`;
  71. // download github repository to local file system
  72. await this.download(requestUrl, ghOrganizationName, ghReposName, ghBranch);
  73. // save plugin metadata
  74. const installedPath = `${ghOrganizationName}/${ghReposName}`;
  75. const plugins = await PluginService.detectPlugins(origin, installedPath);
  76. await this.savePluginMetaData(plugins);
  77. return;
  78. }
  79. private async download(requestUrl: string, ghOrganizationName: string, ghReposName: string, ghBranch: string): Promise<void> {
  80. const zipFilePath = path.join(pluginStoringPath, `${ghBranch}.zip`);
  81. const unzippedPath = path.join(pluginStoringPath, ghOrganizationName);
  82. const downloadFile = async(requestUrl: string, filePath: string) => {
  83. return new Promise<void>((resolve, reject) => {
  84. axios({
  85. method: 'GET',
  86. url: requestUrl,
  87. responseType: 'stream',
  88. })
  89. .then((res) => {
  90. if (res.status === 200) {
  91. const file = fs.createWriteStream(filePath);
  92. res.data.pipe(file)
  93. .on('close', () => file.close())
  94. .on('finish', () => {
  95. return resolve();
  96. });
  97. }
  98. else {
  99. return reject(res.status);
  100. }
  101. }).catch((err) => {
  102. return reject(err);
  103. });
  104. });
  105. };
  106. const unzip = async(zipFilePath: fs.PathLike, unzippedPath: fs.PathLike) => {
  107. const stream = fs.createReadStream(zipFilePath);
  108. const unzipStream = stream.pipe(unzipper.Extract({ path: unzippedPath }));
  109. const deleteZipFile = (path: fs.PathLike) => fs.unlinkSync(path);
  110. try {
  111. await streamToPromise(unzipStream);
  112. deleteZipFile(zipFilePath);
  113. }
  114. catch (err) {
  115. return err;
  116. }
  117. };
  118. const renamePath = async(oldPath: fs.PathLike, newPath: fs.PathLike) => {
  119. try {
  120. // if repository already exists, delete old repository before rename path
  121. if (fs.existsSync(newPath)) await fs.promises.rm(newPath, { recursive: true });
  122. const GrowiPlugin = mongoose.model<GrowiPlugin>('GrowiPlugin');
  123. const isGrowiPlugin = await GrowiPlugin.findOne({ installedPath: `${ghOrganizationName}/${ghReposName}` });
  124. // if document already exists, delete old document before rename path
  125. if (isGrowiPlugin) await GrowiPlugin.findOneAndDelete({ installedPath: `${ghOrganizationName}/${ghReposName}` });
  126. }
  127. catch (err) {
  128. throw new Error(err);
  129. }
  130. // rename repository
  131. fs.renameSync(oldPath, newPath);
  132. };
  133. try {
  134. await downloadFile(requestUrl, zipFilePath);
  135. await unzip(zipFilePath, unzippedPath);
  136. await renamePath(`${unzippedPath}/${ghReposName}-${ghBranch}`, `${unzippedPath}/${ghReposName}`);
  137. }
  138. catch (err) {
  139. logger.error(err);
  140. throw new Error(err);
  141. }
  142. return;
  143. }
  144. private async savePluginMetaData(plugins: GrowiPlugin[]): Promise<void> {
  145. const GrowiPlugin = mongoose.model('GrowiPlugin');
  146. await GrowiPlugin.insertMany(plugins);
  147. }
  148. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  149. private static async detectPlugins(origin: GrowiPluginOrigin, installedPath: string, parentPackageJson?: any): Promise<GrowiPlugin[]> {
  150. const packageJsonPath = path.resolve(pluginStoringPath, installedPath, 'package.json');
  151. const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
  152. const { growiPlugin } = packageJson;
  153. const {
  154. name: packageName, description: packageDesc, author: packageAuthor,
  155. } = parentPackageJson ?? packageJson;
  156. if (growiPlugin == null) {
  157. throw new Error('This package does not include \'growiPlugin\' section.');
  158. }
  159. // detect sub plugins for monorepo
  160. if (growiPlugin.isMonorepo && growiPlugin.packages != null) {
  161. const plugins = await Promise.all(
  162. growiPlugin.packages.map(async(subPackagePath) => {
  163. const subPackageInstalledPath = path.join(installedPath, subPackagePath);
  164. return this.detectPlugins(origin, subPackageInstalledPath, packageJson);
  165. }),
  166. );
  167. return plugins.flat();
  168. }
  169. if (growiPlugin.types == null) {
  170. throw new Error('\'growiPlugin\' section must have a \'types\' property.');
  171. }
  172. const plugin = {
  173. isEnabled: true,
  174. installedPath,
  175. origin,
  176. meta: {
  177. name: growiPlugin.name ?? packageName,
  178. desc: growiPlugin.desc ?? packageDesc,
  179. author: growiPlugin.author ?? packageAuthor,
  180. types: growiPlugin.types,
  181. },
  182. };
  183. // add theme metadata
  184. if (growiPlugin.types.includes(GrowiPluginResourceType.Theme)) {
  185. (plugin as GrowiPlugin<GrowiThemePluginMeta>).meta = {
  186. ...plugin.meta,
  187. themes: growiPlugin.themes,
  188. };
  189. }
  190. logger.info('Plugin detected => ', plugin);
  191. return [plugin];
  192. }
  193. async listPlugins(): Promise<GrowiPlugin[]> {
  194. return [];
  195. }
  196. async retrieveThemeHref(theme: string): Promise<string | undefined> {
  197. const GrowiPlugin = mongoose.model('GrowiPlugin') as GrowiPluginModel;
  198. let matchedPlugin: GrowiPlugin | undefined;
  199. let matchedThemeMetadata: GrowiThemeMetadata | undefined;
  200. try {
  201. // retrieve plugin manifests
  202. const growiPlugins = await GrowiPlugin.findEnabledPluginsIncludingAnyTypes([GrowiPluginResourceType.Theme]) as GrowiPlugin<GrowiThemePluginMeta>[];
  203. growiPlugins
  204. .forEach(async(growiPlugin) => {
  205. const themeMetadatas = growiPlugin.meta.themes;
  206. const themeMetadata = themeMetadatas.find(t => t.name === theme);
  207. // found
  208. if (themeMetadata != null) {
  209. matchedPlugin = growiPlugin;
  210. matchedThemeMetadata = themeMetadata;
  211. }
  212. });
  213. }
  214. catch (e) {
  215. logger.error(`Could not find the theme '${theme}' from GrowiPlugin documents.`, e);
  216. }
  217. try {
  218. if (matchedPlugin != null && matchedThemeMetadata != null) {
  219. const manifest = await retrievePluginManifest(matchedPlugin);
  220. return `${PLUGINS_STATIC_DIR}/${matchedPlugin.installedPath}/dist/${manifest[matchedThemeMetadata.manifestKey].file}`;
  221. }
  222. }
  223. catch (e) {
  224. logger.error(`Could not read manifest file for the theme '${theme}'`, e);
  225. }
  226. }
  227. async retrieveAllPluginResourceEntries(): Promise<GrowiPluginResourceEntries> {
  228. const GrowiPlugin = mongoose.model('GrowiPlugin') as GrowiPluginModel;
  229. const entries: GrowiPluginResourceEntries = [];
  230. try {
  231. const growiPlugins = await GrowiPlugin.findEnabledPlugins();
  232. growiPlugins.forEach(async(growiPlugin) => {
  233. try {
  234. const { types } = growiPlugin.meta;
  235. const manifest = await retrievePluginManifest(growiPlugin);
  236. // add script
  237. if (types.includes(GrowiPluginResourceType.Script) || types.includes(GrowiPluginResourceType.Template)) {
  238. const href = `${PLUGINS_STATIC_DIR}/${growiPlugin.installedPath}/dist/${manifest['client-entry.tsx'].file}`;
  239. entries.push([growiPlugin.installedPath, href]);
  240. }
  241. // add link
  242. if (types.includes(GrowiPluginResourceType.Script) || types.includes(GrowiPluginResourceType.Style)) {
  243. const href = `${PLUGINS_STATIC_DIR}/${growiPlugin.installedPath}/dist/${manifest['client-entry.tsx'].css}`;
  244. entries.push([growiPlugin.installedPath, href]);
  245. }
  246. }
  247. catch (e) {
  248. logger.warn(e);
  249. }
  250. });
  251. }
  252. catch (e) {
  253. logger.error('Could not retrieve GrowiPlugin documents.', e);
  254. }
  255. return entries;
  256. }
  257. }