Просмотр исходного кода

Merge pull request #13 from weseek/feat/list-plugins

feat: Detect plugins 2
Yuki Takei 3 лет назад
Родитель
Сommit
458f52a5aa
2 измененных файлов с 49 добавлено и 11 удалено
  1. 3 3
      packages/app/src/interfaces/plugin.ts
  2. 46 8
      packages/app/src/server/service/plugin.ts

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

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

+ 46 - 8
packages/app/src/server/service/plugin.ts

@@ -1,11 +1,10 @@
-import fs from 'fs';
 import path from 'path';
 import path from 'path';
 
 
 import { GrowiPlugin, GrowiPluginOrigin } from '~/interfaces/plugin';
 import { GrowiPlugin, GrowiPluginOrigin } from '~/interfaces/plugin';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 import { resolveFromRoot } from '~/utils/project-dir-utils';
 import { resolveFromRoot } from '~/utils/project-dir-utils';
 
 
-const logger = loggerFactory('growi:plugins:plugin-utils');
+const logger = loggerFactory('growi:service:PluginService');
 
 
 
 
 const pluginStoringPath = resolveFromRoot('tmp/plugins');
 const pluginStoringPath = resolveFromRoot('tmp/plugins');
@@ -19,12 +18,51 @@ export class PluginService {
     return;
     return;
   }
   }
 
 
-  static detectPlugins(origin: GrowiPluginOrigin, installedPath: string): GrowiPlugin[] {
-    // const plugins: GrowiPlugin[] = [];
-
-    // const package = require(path.resolve(installedPath, 'package.json'));
-
-    // return scopedPackages;
+  // 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];
   }
   }
 
 
   // /**
   // /**