jam411 3 lat temu
rodzic
commit
8483843183

+ 3 - 3
packages/app/src/interfaces/plugin.ts

@@ -1,7 +1,7 @@
 export const GrowiPluginResourceType = {
-  Template: 'Template',
-  Style: 'Style',
-  Script: 'Script',
+  Template: 'template',
+  Style: 'style',
+  Script: 'script',
 } as const;
 export type GrowiPluginResourceType = typeof GrowiPluginResourceType[keyof typeof GrowiPluginResourceType];
 

+ 1 - 4
packages/app/src/pages/admin/[[...path]].page.tsx

@@ -30,7 +30,6 @@ import AdminTwitterSecurityContainer from '~/client/services/AdminTwitterSecurit
 import AdminUsersContainer from '~/client/services/AdminUsersContainer';
 import { SupportedActionType } from '~/interfaces/activity';
 import { CrowiRequest } from '~/interfaces/crowi-request';
-import PluginUtils from '~/server/plugins/plugin-utils';
 import ConfigLoader from '~/server/service/config-loader';
 import {
   useCurrentUser, /* useSearchServiceConfigured, */ useIsAclEnabled, useIsMailerSetup, useIsSearchServiceReachable, useSiteUrl,
@@ -67,8 +66,6 @@ const AuditLogManagement = dynamic(() => import('../../components/Admin/AuditLog
 const PluginsExtensionPageContents = dynamic(() => import('../../components/Admin/PluginsExtension/PluginsExtensionPageContents')
   .then(mod => mod.PluginsExtensionPageContents), { ssr: false });
 
-const pluginUtils = new PluginUtils();
-
 type Props = CommonProps & {
   currentUser: any,
 
@@ -293,7 +290,7 @@ async function injectServerConfigurations(context: GetServerSidePropsContext, pr
   props.nodeVersion = crowi.runtimeVersions.versions.node ? crowi.runtimeVersions.versions.node.version.version : null;
   props.npmVersion = crowi.runtimeVersions.versions.npm ? crowi.runtimeVersions.versions.npm.version.version : null;
   props.yarnVersion = crowi.runtimeVersions.versions.yarn ? crowi.runtimeVersions.versions.yarn.version.version : null;
-  props.installedPlugins = pluginUtils.listPlugins();
+  props.installedPlugins = crowi.pluginService.listPlugins();
   props.envVars = await ConfigLoader.getEnvVarsForDisplay(true);
   props.isAclEnabled = aclService.isAclEnabled();
 

+ 9 - 0
packages/app/src/server/crowi/index.js

@@ -66,6 +66,7 @@ function Crowi() {
   this.growiBridgeService = null;
   this.exportService = null;
   this.importService = null;
+  this.pluginService = null;
   this.searchService = null;
   this.pluginService = null;
   this.socketIoService = null;
@@ -133,6 +134,7 @@ Crowi.prototype.init = async function() {
     this.setupUserGroupService(),
     this.setupExport(),
     this.setupImport(),
+    this.setupPluginService(),
     this.setupPageService(),
     this.setupInAppNotificationService(),
     this.setupActivityService(),
@@ -693,6 +695,13 @@ Crowi.prototype.setupImport = async function() {
   }
 };
 
+Crowi.prototype.setupPluginService = async function() {
+  const { PluginService } = require('../service/plugin');
+  if (this.pluginService == null) {
+    this.pluginService = new PluginService(this);
+  }
+};
+
 Crowi.prototype.setupPageService = async function() {
   if (this.pageService == null) {
     this.pageService = new PageService(this);

+ 1 - 4
packages/app/src/server/routes/apiv3/admin-home.js

@@ -1,9 +1,6 @@
 import ConfigLoader from '../../service/config-loader';
 
 const express = require('express');
-const PluginUtils = require('../../plugins/plugin-utils');
-
-const pluginUtils = new PluginUtils();
 
 const router = express.Router();
 
@@ -71,7 +68,7 @@ module.exports = (crowi) => {
       nodeVersion: crowi.runtimeVersions.versions.node ? crowi.runtimeVersions.versions.node.version.version : '-',
       npmVersion: crowi.runtimeVersions.versions.npm ? crowi.runtimeVersions.versions.npm.version.version : '-',
       yarnVersion: crowi.runtimeVersions.versions.yarn ? crowi.runtimeVersions.versions.yarn.version.version : '-',
-      installedPlugins: pluginUtils.listPlugins(crowi.rootDir),
+      installedPlugins: crowi.pluginService.listPlugins(),
       envVars: await ConfigLoader.getEnvVarsForDisplay(true),
       isV5Compatible: crowi.configManager.getConfig('crowi', 'app:isV5Compatible'),
       isMaintenanceMode: crowi.configManager.getConfig('crowi', 'app:isMaintenanceMode'),

+ 48 - 65
packages/app/src/server/service/plugin.ts

@@ -50,73 +50,56 @@ export class PluginService {
     return;
   }
 
-  static detectPlugins(origin: GrowiPluginOrigin, installedPath: string): GrowiPlugin[] {
-    // const plugins: GrowiPlugin[] = [];
+  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
+  static async detectPlugins(origin: GrowiPluginOrigin, installedPath: string, parentPackageJson?: any): Promise<GrowiPlugin[]> {
+    const packageJsonPath = path.resolve(pluginStoringPath, installedPath, 'package.json');
+    const packageJson = await import(packageJsonPath);
+
+    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(installedPath, 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,
+      origin,
+      meta: {
+        name: growiPlugin.name ?? packageName,
+        desc: growiPlugin.desc ?? packageDesc,
+        author: growiPlugin.author ?? packageAuthor,
+        types: growiPlugin.types,
+      },
+    };
+
+    logger.info('Plugin detected => ', plugin);
+
+    return [plugin];
+  }
 
-    // const package = require(path.resolve(installedPath, 'package.json'));
 
-    // return scopedPackages;
+  async listPlugins(): Promise<GrowiPlugin[]> {
+    return [];
   }
 
-  // /**
-  //  * list plugin module objects
-  //  *  that starts with 'growi-plugin-' or 'crowi-plugin-'
-  //  * borrowing from: https://github.com/hexojs/hexo/blob/d1db459c92a4765620343b95789361cbbc6414c5/lib/hexo/load_plugins.js#L17
-  //  *
-  //  * @returns array of objects
-  //  *   [
-  //  *     { name: 'growi-plugin-...', requiredVersion: '^1.0.0', installedVersion: '1.0.0' },
-  //  *     { name: 'growi-plugin-...', requiredVersion: '^1.0.0', installedVersion: '1.0.0' },
-  //  *     ...
-  //  *   ]
-  //  *
-  //  * @memberOf PluginService
-  //  */
-  // listPlugins() {
-  //   const packagePath = resolveFromRoot('package.json');
-
-  //   // Make sure package.json exists
-  //   if (!fs.existsSync(packagePath)) {
-  //     return [];
-  //   }
-
-  //   // Read package.json and find dependencies
-  //   const content = fs.readFileSync(packagePath);
-  //   const json = JSON.parse(content);
-  //   const deps = json.dependencies || {};
-
-  //   const pluginNames = Object.keys(deps).filter((name) => {
-  //     return /^@growi\/plugin-/.test(name);
-  //   });
-
-  //   return pluginNames.map((name) => {
-  //     return {
-  //       name,
-  //       requiredVersion: deps[name],
-  //       installedVersion: this.getVersion(name),
-  //     };
-  //   });
-  // }
-
-  // /**
-  //  * list plugin module names that starts with 'crowi-plugin-'
-  //  *
-  //  * @returns array of plugin names
-  //  *
-  //  * @memberOf PluginService
-  //  */
-  // listPluginNames() {
-  //   const plugins = this.listPlugins();
-  //   return plugins.map((plugin) => { return plugin.name });
-  // }
-
-  // getVersion(packageName) {
-  //   const packagePath = resolveFromRoot(`../../node_modules/${packageName}/package.json`);
-
-  //   // Read package.json and find version
-  //   const content = fs.readFileSync(packagePath);
-  //   const json = JSON.parse(content);
-  //   return json.version || '';
-  // }
-
 }