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

reorganize consts and interfaces

Yuki Takei 2 лет назад
Родитель
Сommit
99e3b40d64

+ 2 - 10
apps/app/src/features/growi-plugin/interfaces/growi-plugin.ts

@@ -1,12 +1,4 @@
-import { GrowiThemeMetadata, HasObjectId } from '@growi/core';
-
-export const GrowiPluginResourceType = {
-  Template: 'template',
-  Style: 'style',
-  Theme: 'theme',
-  Script: 'script',
-} as const;
-export type GrowiPluginResourceType = typeof GrowiPluginResourceType[keyof typeof GrowiPluginResourceType];
+import { GrowiPluginType, GrowiThemeMetadata, HasObjectId } from '@growi/core';
 
 export type IGrowiPluginOrigin = {
   url: string,
@@ -24,7 +16,7 @@ export type IGrowiPlugin<M extends IGrowiPluginMeta = IGrowiPluginMeta> = {
 
 export type IGrowiPluginMeta = {
   name: string,
-  types: GrowiPluginResourceType[],
+  types: GrowiPluginType[],
   desc?: string,
   author?: string,
 }

+ 4 - 5
apps/app/src/features/growi-plugin/server/models/growi-plugin.ts

@@ -1,11 +1,10 @@
-import { GrowiThemeMetadata, GrowiThemeSchemeType } from '@growi/core';
+import { GrowiPluginType, GrowiThemeMetadata, GrowiThemeSchemeType } from '@growi/core';
 import {
   Schema, type Model, type Document, type Types,
 } from 'mongoose';
 
 import { getOrCreateModel } from '~/server/util/mongoose-utils';
 
-import { GrowiPluginResourceType } from '../../interfaces';
 import type {
   IGrowiPlugin, IGrowiPluginMeta, IGrowiPluginOrigin, IGrowiThemePluginMeta,
 } from '../../interfaces';
@@ -14,7 +13,7 @@ export interface IGrowiPluginDocument extends IGrowiPlugin, Document {
 }
 export interface IGrowiPluginModel extends Model<IGrowiPluginDocument> {
   findEnabledPlugins(): Promise<IGrowiPlugin[]>
-  findEnabledPluginsIncludingAnyTypes(includingTypes: GrowiPluginResourceType[]): Promise<IGrowiPlugin[]>
+  findEnabledPluginsIncludingAnyTypes(includingTypes: GrowiPluginType[]): Promise<IGrowiPlugin[]>
   activatePlugin(id: Types.ObjectId): Promise<string>
   deactivatePlugin(id: Types.ObjectId): Promise<string>
 }
@@ -37,7 +36,7 @@ const growiPluginMetaSchema = new Schema<IGrowiPluginMeta|IGrowiThemePluginMeta>
   name: { type: String, required: true },
   types: {
     type: [String],
-    enum: GrowiPluginResourceType,
+    enum: GrowiPluginType,
     require: true,
   },
   desc: { type: String },
@@ -63,7 +62,7 @@ growiPluginSchema.statics.findEnabledPlugins = async function(): Promise<IGrowiP
   return this.find({ isEnabled: true });
 };
 
-growiPluginSchema.statics.findEnabledPluginsIncludingAnyTypes = async function(types: GrowiPluginResourceType[]): Promise<IGrowiPlugin[]> {
+growiPluginSchema.statics.findEnabledPluginsIncludingAnyTypes = async function(types: GrowiPluginType[]): Promise<IGrowiPlugin[]> {
   return this.find({
     isEnabled: true,
     'meta.types': { $in: types },

+ 5 - 6
apps/app/src/features/growi-plugin/server/services/growi-plugin.ts

@@ -1,7 +1,7 @@
 import fs, { readFileSync } from 'fs';
 import path from 'path';
 
-import { GrowiThemeMetadata, ViteManifest } from '@growi/core';
+import { GrowiPluginType, GrowiThemeMetadata, ViteManifest } from '@growi/core';
 // eslint-disable-next-line no-restricted-imports
 import axios from 'axios';
 import mongoose from 'mongoose';
@@ -11,7 +11,6 @@ import unzipper from 'unzipper';
 import loggerFactory from '~/utils/logger';
 import { resolveFromRoot } from '~/utils/project-dir-utils';
 
-import { GrowiPluginResourceType } from '../../interfaces';
 import type {
   IGrowiPlugin, IGrowiPluginOrigin, IGrowiThemePluginMeta, IGrowiPluginMeta,
 } from '../../interfaces';
@@ -255,7 +254,7 @@ export class GrowiPluginService implements IGrowiPluginService {
     };
 
     // add theme metadata
-    if (growiPlugin.types.includes(GrowiPluginResourceType.Theme)) {
+    if (growiPlugin.types.includes(GrowiPluginType.Theme)) {
       (plugin as IGrowiPlugin<IGrowiThemePluginMeta>).meta = {
         ...plugin.meta,
         themes: growiPlugin.themes,
@@ -311,7 +310,7 @@ export class GrowiPluginService implements IGrowiPluginService {
 
     try {
       // retrieve plugin manifests
-      const growiPlugins = await GrowiPlugin.findEnabledPluginsIncludingAnyTypes([GrowiPluginResourceType.Theme]) as IGrowiPlugin<IGrowiThemePluginMeta>[];
+      const growiPlugins = await GrowiPlugin.findEnabledPluginsIncludingAnyTypes([GrowiPluginType.Theme]) as IGrowiPlugin<IGrowiThemePluginMeta>[];
 
       growiPlugins
         .forEach(async(growiPlugin) => {
@@ -358,12 +357,12 @@ export class GrowiPluginService implements IGrowiPluginService {
           const manifest = await retrievePluginManifest(growiPlugin);
 
           // add script
-          if (types.includes(GrowiPluginResourceType.Script) || types.includes(GrowiPluginResourceType.Template)) {
+          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(GrowiPluginResourceType.Script) || types.includes(GrowiPluginResourceType.Style)) {
+          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]);
           }

+ 3 - 4
apps/app/src/server/routes/apiv3/customize-setting.js

@@ -1,12 +1,11 @@
 /* eslint-disable no-unused-vars */
 
-import { ErrorV3 } from '@growi/core';
+import { GrowiPluginType, ErrorV3 } from '@growi/core';
 import express from 'express';
 import { body } from 'express-validator';
 import multer from 'multer';
 
-import { GrowiPluginResourceType } from '~/features/growi-plugin/interfaces';
-import { GrowiPlugin } from '~/features/growi-plugin/server/models';
+import { GrowiPlugin } from '~/features/growi-plugin/models';
 import { SupportedAction } from '~/interfaces/activity';
 import { AttachmentType } from '~/server/interfaces/attachment';
 import loggerFactory from '~/utils/logger';
@@ -274,7 +273,7 @@ module.exports = (crowi) => {
       const currentTheme = await crowi.configManager.getConfig('crowi', 'customize:theme');
 
       // retrieve plugin manifests
-      const themePlugins = await GrowiPlugin.findEnabledPluginsIncludingAnyTypes([GrowiPluginResourceType.Theme]);
+      const themePlugins = await GrowiPlugin.findEnabledPluginsIncludingAnyTypes([GrowiPluginType.Theme]);
 
       const pluginThemesMetadatas = themePlugins
         .map(themePlugin => themePlugin.meta.themes)

+ 4 - 3
packages/pluginkit/src/consts/types.ts → packages/core/src/consts/growi-plugin.ts

@@ -1,6 +1,7 @@
 export const GrowiPluginType = {
-  SCRIPT: 'script',
-  TEMPLATE: 'template',
-  THEME: 'theme',
+  Template: 'template',
+  Style: 'style',
+  Theme: 'theme',
+  Script: 'script',
 } as const;
 export type GrowiPluginType = typeof GrowiPluginType[keyof typeof GrowiPluginType];

+ 1 - 0
packages/core/src/consts/index.ts

@@ -0,0 +1 @@
+export * from './growi-plugin';

+ 2 - 14
packages/core/src/index.ts

@@ -1,17 +1,5 @@
-export * from './interfaces/attachment';
-export * from './interfaces/color-scheme';
-export * from './interfaces/common';
-export * from './interfaces/growi-facade';
-export * from './interfaces/growi-theme-metadata';
-export * from './interfaces/has-object-id';
-export * from './interfaces/lang';
-export * from './interfaces/page';
-export * from './interfaces/revision';
-export * from './interfaces/subscription';
-export * from './interfaces/tag';
-export * from './interfaces/template';
-export * from './interfaces/user';
-export * from './interfaces/vite';
+export * from './consts';
+export * from './interfaces';
 export * from './models/devided-page-path';
 export * from './models/vo/error-apiv3';
 export * from './plugin';

+ 13 - 0
packages/core/src/interfaces/index.ts

@@ -0,0 +1,13 @@
+export * from './color-scheme';
+export * from './common';
+export * from './growi-facade';
+export * from './growi-theme-metadata';
+export * from './has-object-id';
+export * from './lang';
+export * from './page';
+export * from './revision';
+export * from './subscription';
+export * from './tag';
+export * from './template';
+export * from './user';
+export * from './vite';

+ 5 - 1
packages/core/src/interfaces/template.ts

@@ -1,5 +1,9 @@
-export type ITemplate = {
+export type ITemplateIdentification = {
   id: string,
+  locale: string,
+}
+
+export type ITemplate = ITemplateIdentification & {
   name: string,
   markdown: string,
 }

+ 0 - 1
packages/pluginkit/src/consts/index.ts

@@ -1 +0,0 @@
-export * from './types';

+ 1 - 1
packages/pluginkit/src/model/growi-plugin-validation-data.ts

@@ -1,4 +1,4 @@
-import { GrowiPluginType } from '../consts/types';
+import { GrowiPluginType } from '@growi/core/dist/consts';
 
 export type GrowiPluginValidationData = {
   projectDirRoot: string,

+ 5 - 4
packages/pluginkit/src/server/utils/v4/package-json/validate.spec.ts

@@ -1,6 +1,7 @@
+import { GrowiPluginType } from '@growi/core/dist/consts';
+
 import examplePkg from '^/test/fixtures/example-package/template1/package.json';
 
-import { GrowiPluginType } from '~/consts';
 
 import { validatePackageJson } from './validate';
 
@@ -32,7 +33,7 @@ describe('validatePackageJson()', () => {
     mocks.importPackageJsonMock.mockResolvedValue(examplePkg);
 
     // when
-    const data = await validatePackageJson('package.json', GrowiPluginType.TEMPLATE);
+    const data = await validatePackageJson('package.json', GrowiPluginType.Template);
 
     // then
     expect(data).not.toBeNull();
@@ -101,12 +102,12 @@ describe('validatePackageJson()', () => {
       mocks.importPackageJsonMock.mockResolvedValue({
         growiPlugin: {
           schemaVersion: 4,
-          types: [GrowiPluginType.TEMPLATE],
+          types: [GrowiPluginType.Template],
         },
       });
 
       // when
-      const caller = async() => { await validatePackageJson('package.json', GrowiPluginType.SCRIPT) };
+      const caller = async() => { await validatePackageJson('package.json', GrowiPluginType.Script) };
 
       // then
       await expect(caller).rejects.toThrow("The growiPlugin directive does not have expected plugin type in 'types' directive.");

+ 2 - 1
packages/pluginkit/src/server/utils/v4/package-json/validate.ts

@@ -1,4 +1,5 @@
-import { GrowiPluginType } from '~/consts';
+import { GrowiPluginType } from '@growi/core/dist/consts';
+
 import { type GrowiPluginValidationData, GrowiPluginValidationError } from '~/model';
 
 import { importPackageJson } from './import';

+ 3 - 2
packages/pluginkit/src/server/utils/v4/template.ts

@@ -3,7 +3,8 @@ import fs from 'fs';
 import path from 'path';
 import { promisify } from 'util';
 
-import { GrowiPluginType } from '~/consts';
+import { GrowiPluginType } from '@growi/core/dist/consts';
+
 import type { GrowiPluginValidationData, GrowiTemplatePluginValidationData } from '~/model';
 import { GrowiPluginValidationError } from '~/model';
 
@@ -18,7 +19,7 @@ const statAsync = promisify(fs.stat);
  * @param projectDirRoot
  */
 export const validateTemplatePluginPackageJson = async(projectDirRoot: string): Promise<GrowiTemplatePluginValidationData> => {
-  const data = await validatePackageJson(projectDirRoot, GrowiPluginType.TEMPLATE);
+  const data = await validatePackageJson(projectDirRoot, GrowiPluginType.Template);
 
   const pkg = await importPackageJson(projectDirRoot);