| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- import fs, { readFileSync } from 'fs';
- import path from 'path';
- import { GrowiPluginType, GrowiThemeMetadata, ViteManifest } from '@growi/core';
- // eslint-disable-next-line no-restricted-imports
- import axios from 'axios';
- import mongoose from 'mongoose';
- import streamToPromise from 'stream-to-promise';
- import unzipper from 'unzipper';
- import loggerFactory from '~/utils/logger';
- import { resolveFromRoot } from '~/utils/project-dir-utils';
- import type {
- IGrowiPlugin, IGrowiPluginOrigin, IGrowiThemePluginMeta, IGrowiPluginMeta,
- } from '../../interfaces';
- import { GrowiPlugin } from '../models';
- import { GitHubUrl } from '../models/vo/github-url';
- const logger = loggerFactory('growi:plugins:plugin-utils');
- const pluginStoringPath = resolveFromRoot('tmp/plugins');
- const PLUGINS_STATIC_DIR = '/static/plugins'; // configured by express.static
- export type GrowiPluginResourceEntries = [installedPath: string, href: string][];
- function retrievePluginManifest(growiPlugin: IGrowiPlugin): ViteManifest {
- const manifestPath = resolveFromRoot(path.join('tmp/plugins', growiPlugin.installedPath, 'dist/manifest.json'));
- const manifestStr: string = readFileSync(manifestPath, 'utf-8');
- return JSON.parse(manifestStr);
- }
- type FindThemePluginResult = {
- growiPlugin: IGrowiPlugin,
- themeMetadata: GrowiThemeMetadata,
- themeHref: string,
- }
- export interface IGrowiPluginService {
- install(origin: IGrowiPluginOrigin): Promise<string>
- findThemePlugin(theme: string): Promise<FindThemePluginResult | null>
- retrieveAllPluginResourceEntries(): Promise<GrowiPluginResourceEntries>
- downloadNotExistPluginRepositories(): Promise<void>
- }
- export class GrowiPluginService implements IGrowiPluginService {
- /*
- * Downloading a non-existent repository to the file system
- */
- async downloadNotExistPluginRepositories(): Promise<void> {
- try {
- // find all growi plugin documents
- const growiPlugins = await GrowiPlugin.find({});
- // if not exists repository in file system, download latest plugin repository
- for await (const growiPlugin of growiPlugins) {
- const pluginPath = path.join(pluginStoringPath, growiPlugin.installedPath);
- const organizationName = path.join(pluginStoringPath, growiPlugin.organizationName);
- if (fs.existsSync(pluginPath)) {
- continue;
- }
- else {
- if (!fs.existsSync(organizationName)) {
- fs.mkdirSync(organizationName);
- }
- // TODO: imprv Document version and repository version possibly different.
- const ghUrl = new GitHubUrl(growiPlugin.origin.url, growiPlugin.origin.branchName);
- const { reposName, branchName, archiveUrl } = ghUrl;
- const zipFilePath = path.join(pluginStoringPath, `${branchName}.zip`);
- const unzippedPath = pluginStoringPath;
- const unzippedReposPath = path.join(pluginStoringPath, `${reposName}-${branchName}`);
- try {
- // download github repository to local file system
- await this.download(archiveUrl, zipFilePath);
- await this.unzip(zipFilePath, unzippedPath);
- fs.renameSync(unzippedReposPath, pluginPath);
- }
- catch (err) {
- // clean up, documents are not operated
- if (fs.existsSync(unzippedReposPath)) await fs.promises.rm(unzippedReposPath, { recursive: true });
- if (fs.existsSync(pluginPath)) await fs.promises.rm(pluginPath, { recursive: true });
- logger.error(err);
- }
- continue;
- }
- }
- }
- catch (err) {
- logger.error(err);
- }
- }
- /*
- * Install a plugin from URL and save it in the DB and file system.
- */
- async install(origin: IGrowiPluginOrigin): Promise<string> {
- const ghUrl = new GitHubUrl(origin.url, origin.ghBranch);
- const {
- organizationName, reposName, branchName, archiveUrl,
- } = ghUrl;
- const installedPath = `${organizationName}/${reposName}`;
- const zipFilePath = path.join(pluginStoringPath, `${branchName}.zip`);
- const unzippedPath = pluginStoringPath;
- const unzippedReposPath = path.join(pluginStoringPath, `${reposName}-${branchName}`);
- const temporaryReposPath = path.join(pluginStoringPath, reposName);
- const reposStoringPath = path.join(pluginStoringPath, `${installedPath}`);
- const organizationPath = path.join(pluginStoringPath, organizationName);
- let plugins: IGrowiPlugin<IGrowiPluginMeta>[];
- try {
- // download github repository to file system's temporary path
- await this.download(archiveUrl, zipFilePath);
- await this.unzip(zipFilePath, unzippedPath);
- fs.renameSync(unzippedReposPath, temporaryReposPath);
- // detect plugins
- plugins = await GrowiPluginService.detectPlugins(origin, organizationName, reposName);
- if (!fs.existsSync(organizationPath)) fs.mkdirSync(organizationPath);
- // remove the old repository from the storing path
- if (fs.existsSync(reposStoringPath)) await fs.promises.rm(reposStoringPath, { recursive: true });
- // move new repository from temporary path to storing path.
- fs.renameSync(temporaryReposPath, reposStoringPath);
- }
- catch (err) {
- // clean up
- if (fs.existsSync(zipFilePath)) await fs.promises.rm(zipFilePath);
- if (fs.existsSync(unzippedReposPath)) await fs.promises.rm(unzippedReposPath, { recursive: true });
- if (fs.existsSync(temporaryReposPath)) await fs.promises.rm(temporaryReposPath, { recursive: true });
- logger.error(err);
- throw err;
- }
- try {
- // delete plugin documents if these exist
- await this.deleteOldPluginDocument(installedPath);
- // save new plugins metadata
- await this.savePluginMetaData(plugins);
- return plugins[0].meta.name;
- }
- catch (err) {
- // clean up
- if (fs.existsSync(reposStoringPath)) await fs.promises.rm(reposStoringPath, { recursive: true });
- await this.deleteOldPluginDocument(installedPath);
- logger.error(err);
- throw err;
- }
- }
- private async deleteOldPluginDocument(path: string): Promise<void> {
- await GrowiPlugin.deleteMany({ installedPath: path });
- }
- // !! DO NOT USE WHERE NOT SSRF GUARDED !! -- 2022.12.26 ryoji-s
- private async download(requestUrl: string, filePath: string): Promise<void> {
- return new Promise<void>((resolve, rejects) => {
- axios({
- method: 'GET',
- url: requestUrl,
- responseType: 'stream',
- })
- .then((res) => {
- if (res.status === 200) {
- const file = fs.createWriteStream(filePath);
- res.data.pipe(file)
- .on('close', () => file.close())
- .on('finish', () => {
- return resolve();
- });
- }
- else {
- rejects(res.status);
- }
- }).catch((err) => {
- logger.error(err);
- // eslint-disable-next-line prefer-promise-reject-errors
- rejects('Filed to download file.');
- });
- });
- }
- private async unzip(zipFilePath: fs.PathLike, unzippedPath: fs.PathLike): Promise<void> {
- try {
- const stream = fs.createReadStream(zipFilePath);
- const unzipStream = stream.pipe(unzipper.Extract({ path: unzippedPath }));
- await streamToPromise(unzipStream);
- await fs.promises.rm(zipFilePath);
- }
- catch (err) {
- logger.error(err);
- throw new Error('Filed to unzip.');
- }
- }
- private async savePluginMetaData(plugins: IGrowiPlugin[]): Promise<void> {
- await GrowiPlugin.insertMany(plugins);
- }
- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, max-len
- private static async detectPlugins(origin: IGrowiPluginOrigin, ghOrganizationName: string, ghReposName: string, parentPackageJson?: any): Promise<IGrowiPlugin[]> {
- const packageJsonPath = path.resolve(pluginStoringPath, ghReposName, 'package.json');
- const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
- const { growiPlugin } = packageJson;
- const {
- name: packageName, description: packageDesc, author: packageAuthor,
- } = parentPackageJson ?? packageJson;
- if (growiPlugin == null) {
- throw new Error('This package does not include \'growiPlugin\' section.');
- }
- // detect sub plugins for monorepo
- if (growiPlugin.isMonorepo && growiPlugin.packages != null) {
- const plugins = await Promise.all(
- growiPlugin.packages.map(async(subPackagePath) => {
- const subPackageInstalledPath = path.join(ghReposName, subPackagePath);
- return this.detectPlugins(origin, subPackageInstalledPath, packageJson);
- }),
- );
- return plugins.flat();
- }
- if (growiPlugin.types == null) {
- throw new Error('\'growiPlugin\' section must have a \'types\' property.');
- }
- const plugin = {
- isEnabled: true,
- installedPath: `${ghOrganizationName}/${ghReposName}`,
- organizationName: ghOrganizationName,
- origin,
- meta: {
- name: growiPlugin.name ?? packageName,
- desc: growiPlugin.desc ?? packageDesc,
- author: growiPlugin.author ?? packageAuthor,
- types: growiPlugin.types,
- },
- };
- // add theme metadata
- if (growiPlugin.types.includes(GrowiPluginType.Theme)) {
- (plugin as IGrowiPlugin<IGrowiThemePluginMeta>).meta = {
- ...plugin.meta,
- themes: growiPlugin.themes,
- };
- }
- logger.info('Plugin detected => ', plugin);
- return [plugin];
- }
- async listPlugins(): Promise<IGrowiPlugin[]> {
- return [];
- }
- /**
- * Delete plugin
- */
- async deletePlugin(pluginId: mongoose.Types.ObjectId): Promise<string> {
- const deleteFolder = (path: fs.PathLike): Promise<void> => {
- return fs.promises.rm(path, { recursive: true });
- };
- const growiPlugins = await GrowiPlugin.findById(pluginId);
- if (growiPlugins == null) {
- throw new Error('No plugin found for this ID.');
- }
- try {
- const growiPluginsPath = path.join(pluginStoringPath, growiPlugins.installedPath);
- await deleteFolder(growiPluginsPath);
- }
- catch (err) {
- logger.error(err);
- throw new Error('Filed to delete plugin repository.');
- }
- try {
- await GrowiPlugin.deleteOne({ _id: pluginId });
- }
- catch (err) {
- logger.error(err);
- throw new Error('Filed to delete plugin from GrowiPlugin documents.');
- }
- return growiPlugins.meta.name;
- }
- async findThemePlugin(theme: string): Promise<FindThemePluginResult | null> {
- let matchedPlugin: IGrowiPlugin | undefined;
- let matchedThemeMetadata: GrowiThemeMetadata | undefined;
- try {
- // retrieve plugin manifests
- const growiPlugins = await GrowiPlugin.findEnabledPluginsIncludingAnyTypes([GrowiPluginType.Theme]) as IGrowiPlugin<IGrowiThemePluginMeta>[];
- growiPlugins
- .forEach(async(growiPlugin) => {
- const themeMetadatas = growiPlugin.meta.themes;
- const themeMetadata = themeMetadatas.find(t => t.name === theme);
- // found
- if (themeMetadata != null) {
- matchedPlugin = growiPlugin;
- matchedThemeMetadata = themeMetadata;
- }
- });
- }
- catch (e) {
- logger.error(`Could not find the theme '${theme}' from GrowiPlugin documents.`, e);
- }
- if (matchedPlugin == null || matchedThemeMetadata == null) {
- return null;
- }
- let themeHref;
- try {
- const manifest = retrievePluginManifest(matchedPlugin);
- themeHref = `${PLUGINS_STATIC_DIR}/${matchedPlugin.installedPath}/dist/${manifest[matchedThemeMetadata.manifestKey].file}`;
- }
- catch (e) {
- logger.error(`Could not read manifest file for the theme '${theme}'`, e);
- }
- return { growiPlugin: matchedPlugin, themeMetadata: matchedThemeMetadata, themeHref };
- }
- async retrieveAllPluginResourceEntries(): Promise<GrowiPluginResourceEntries> {
- const entries: GrowiPluginResourceEntries = [];
- try {
- const growiPlugins = await GrowiPlugin.findEnabledPlugins();
- growiPlugins.forEach(async(growiPlugin) => {
- try {
- const { types } = growiPlugin.meta;
- const manifest = await retrievePluginManifest(growiPlugin);
- // add script
- if (types.includes(GrowiPluginType.Script)) {
- const href = `${PLUGINS_STATIC_DIR}/${growiPlugin.installedPath}/dist/${manifest['client-entry.tsx'].file}`;
- entries.push([growiPlugin.installedPath, href]);
- }
- // add link
- if (types.includes(GrowiPluginType.Script) || types.includes(GrowiPluginType.Style)) {
- const href = `${PLUGINS_STATIC_DIR}/${growiPlugin.installedPath}/dist/${manifest['client-entry.tsx'].css}`;
- entries.push([growiPlugin.installedPath, href]);
- }
- }
- catch (e) {
- logger.warn(e);
- }
- });
- }
- catch (e) {
- logger.error('Could not retrieve GrowiPlugin documents.', e);
- }
- return entries;
- }
- }
- export const growiPluginService = new GrowiPluginService();
|