Browse Source

model 作成

takuya 3 years ago
parent
commit
a9036b3d15

+ 41 - 0
packages/app/src/server/models/growi-plugin.ts

@@ -0,0 +1,41 @@
+import {
+  Schema, Model, Document,
+} from 'mongoose';
+
+
+import {
+  GrowiPlugin, GrowiPluginMeta, GrowiPluginOrigin, GrowiPluginResourceType,
+} from '~/interfaces/plugin';
+
+import { getOrCreateModel } from '../util/mongoose-utils';
+
+export interface GrowiPluginDocument extends GrowiPlugin, Document {
+}
+export type GrowiPluginModel = Model<GrowiPluginDocument>
+
+const growiPluginMetaSchema = new Schema<GrowiPluginMeta>({
+  name: { type: String, required: true },
+  types: {
+    type: [String],
+    enum: GrowiPluginResourceType,
+    require: true,
+  },
+  desc: { type: String },
+  author: { type: String },
+});
+
+const growiPluginOriginSchema = new Schema<GrowiPluginOrigin>({
+  url: { type: String },
+  ghBranch: { type: String },
+  ghTag: { type: String },
+});
+
+const growiPluginSchema = new Schema<GrowiPluginDocument, GrowiPluginModel>({
+  isEnabled: { type: Boolean },
+  installedPath: { type: String },
+  origin: growiPluginOriginSchema,
+  meta: growiPluginMetaSchema,
+});
+
+
+export default getOrCreateModel<GrowiPluginDocument, GrowiPluginModel>('GrowiPlugin', growiPluginSchema);

+ 33 - 9
packages/app/src/server/service/plugin.ts

@@ -2,11 +2,12 @@
 import fs from 'fs';
 import path from 'path';
 
+import mongoose from 'mongoose';
 import wget from 'node-wget-js';
 import streamToPromise from 'stream-to-promise';
 import unzipper from 'unzipper';
 
-import { GrowiPlugin, GrowiPluginOrigin } from '~/interfaces/plugin';
+import { GrowiPlugin, GrowiPluginMeta, GrowiPluginOrigin } from '~/interfaces/plugin';
 import loggerFactory from '~/utils/logger';
 import { resolveFromRoot } from '~/utils/project-dir-utils';
 
@@ -39,20 +40,43 @@ export class PluginService {
 
   async install(crowi: Crowi, origin: GrowiPluginOrigin): Promise<void> {
     // download
-    const ghUrl = origin.url;
-    const downloadDir = path.join(process.cwd(), 'tmp/plugins/');
-    try {
-      await this.downloadZipFile(`${ghUrl}/archive/refs/heads/master.zip`, downloadDir);
-    }
-    catch (err) {
-      // TODO: error handling
-    }
+    // const ghUrl = origin.url;
+    // const downloadDir = path.join(process.cwd(), 'tmp/plugins/');
+    // try {
+    //   await this.downloadZipFile(`${ghUrl}/archive/refs/heads/master.zip`, downloadDir);
+    // }
+    // catch (err) {
+    //   // TODO: error handling
+    // }
 
     // TODO: detect plugins
     // TODO: save documents
+    const installedSamplePath = '/workspace/growi/packages/app/tmp/plugins/hogerepository/meta.json';
+    await this.savePluginMetaData(installedSamplePath);
+
     return;
   }
 
+  async savePluginMetaData(installedPath: string): Promise<void> {
+    const metaData = this.getPluginMetaData(installedPath);
+    const GrowiPlugin = mongoose.model('GrowiPlugin');
+
+    // await GrowiPlugin.insertMany({
+    //   isEnabled: true,
+    //   installedPath,
+    //   meta: {
+    //     name: metaData.name,
+    //     types: metaData.types,
+    //     author: metaData.author,
+    //   },
+    // });
+  }
+
+  private getPluginMetaData(installedPath: string): GrowiPluginMeta {
+    const metaDataJSON = JSON.parse(fs.readFileSync(installedPath, 'utf-8'));
+    return metaDataJSON;
+  }
+
   // 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');