jam411 3 лет назад
Родитель
Сommit
0f010daab6
2 измененных файлов с 21 добавлено и 23 удалено
  1. 1 1
      packages/app/src/pages/_document.page.tsx
  2. 20 22
      packages/app/src/server/service/plugin.ts

+ 1 - 1
packages/app/src/pages/_document.page.tsx

@@ -90,7 +90,7 @@ class GrowiDocument extends Document<GrowiDocumentInitialProps> {
     const { configManager, customizeService, pluginService } = crowi;
     const { configManager, customizeService, pluginService } = crowi;
 
 
     // document exists but there is no repository, reinstall the plugins
     // document exists but there is no repository, reinstall the plugins
-    await (pluginService as IPluginService).reintallNotExistPluginRepositories();
+    await (pluginService as IPluginService).intallNotExistPluginRepositories();
 
 
     const theme = configManager.getConfig('crowi', 'customize:theme');
     const theme = configManager.getConfig('crowi', 'customize:theme');
     const customScript: string | null = customizeService.getCustomScript();
     const customScript: string | null = customizeService.getCustomScript();

+ 20 - 22
packages/app/src/server/service/plugin.ts

@@ -38,12 +38,12 @@ export interface IPluginService {
   install(origin: GrowiPluginOrigin): Promise<void>
   install(origin: GrowiPluginOrigin): Promise<void>
   retrieveThemeHref(theme: string): Promise<string | undefined>
   retrieveThemeHref(theme: string): Promise<string | undefined>
   retrieveAllPluginResourceEntries(): Promise<GrowiPluginResourceEntries>
   retrieveAllPluginResourceEntries(): Promise<GrowiPluginResourceEntries>
-  reintallNotExistPluginRepositories(): Promise<void>
+  intallNotExistPluginRepositories(): Promise<void>
 }
 }
 
 
 export class PluginService implements IPluginService {
 export class PluginService implements IPluginService {
 
 
-  async reintallNotExistPluginRepositories(): Promise<void> {
+  async intallNotExistPluginRepositories(): Promise<void> {
     try {
     try {
       // check all growi plugin documents
       // check all growi plugin documents
       const GrowiPlugin = mongoose.model<GrowiPlugin>('GrowiPlugin');
       const GrowiPlugin = mongoose.model<GrowiPlugin>('GrowiPlugin');
@@ -57,7 +57,7 @@ export class PluginService implements IPluginService {
         else {
         else {
           // if not exists repository, reinstall latest plugin
           // if not exists repository, reinstall latest plugin
           // delete old document
           // delete old document
-          await GrowiPlugin.deleteOne({ _id: growiPluginData._id });
+          await GrowiPlugin.findByIdAndDelete(growiPluginData._id);
           // reinstall latest plugin
           // reinstall latest plugin
           await this.install(growiPluginData.origin);
           await this.install(growiPluginData.origin);
           continue;
           continue;
@@ -101,25 +101,6 @@ export class PluginService implements IPluginService {
     const zipFilePath = path.join(pluginStoringPath, `${ghBranch}.zip`);
     const zipFilePath = path.join(pluginStoringPath, `${ghBranch}.zip`);
     const unzippedPath = path.join(pluginStoringPath, ghOrganizationName);
     const unzippedPath = path.join(pluginStoringPath, ghOrganizationName);
 
 
-    const renamePath = async(oldPath: fs.PathLike, newPath: fs.PathLike) => {
-      if (fs.existsSync(newPath)) {
-        // if a repository already exists, delete old repository before rename path
-        const GrowiPlugin = mongoose.model<GrowiPlugin>('GrowiPlugin');
-
-        try {
-          // delete old repository
-          await fs.promises.rm(newPath, { recursive: true });
-          // delete old document
-          await GrowiPlugin.deleteOne({ url: `https://github.com/${ghOrganizationName}/${ghReposName}` });
-        }
-        catch (err) {
-          throw new Error(err);
-        }
-      }
-      // rename new repository
-      fs.renameSync(oldPath, newPath);
-    };
-
     const downloadFile = async(requestUrl: string, filePath: string) => {
     const downloadFile = async(requestUrl: string, filePath: string) => {
       return new Promise<void>((resolve, reject) => {
       return new Promise<void>((resolve, reject) => {
         axios({
         axios({
@@ -159,6 +140,23 @@ export class PluginService implements IPluginService {
       }
       }
     };
     };
 
 
+    const renamePath = async(oldPath: fs.PathLike, newPath: fs.PathLike) => {
+      try {
+        // if repository already exists, delete old repository before rename path
+        if (fs.existsSync(newPath)) await fs.promises.rm(newPath, { recursive: true });
+
+        const GrowiPlugin = mongoose.model<GrowiPlugin>('GrowiPlugin');
+        const isGrowiPlugin = await GrowiPlugin.findOne({ installedPath: `${ghOrganizationName}/${ghReposName}` });
+        // if document already exists, delete old document before rename path
+        if (isGrowiPlugin) await GrowiPlugin.findOneAndDelete({ installedPath: `${ghOrganizationName}/${ghReposName}` });
+      }
+      catch (err) {
+        throw new Error(err);
+      }
+      // rename repository
+      fs.renameSync(oldPath, newPath);
+    };
+
     try {
     try {
       await downloadFile(requestUrl, zipFilePath);
       await downloadFile(requestUrl, zipFilePath);
       await unzip(zipFilePath, unzippedPath);
       await unzip(zipFilePath, unzippedPath);