plugin.ts 12 KB

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