Browse Source

add plugin service

jam411 3 years ago
parent
commit
99dffd7388

+ 3 - 0
packages/app/src/pages/_document.page.tsx

@@ -89,6 +89,9 @@ class GrowiDocument extends Document<GrowiDocumentInitialProps> {
     const { crowi } = ctx.req as CrowiRequest<any>;
     const { crowi } = ctx.req as CrowiRequest<any>;
     const { configManager, customizeService, pluginService } = crowi;
     const { configManager, customizeService, pluginService } = crowi;
 
 
+    // document exists but there is no repository, reinstall the plugins
+    await (pluginService as IPluginService).reintallNotExistPluginRepositories();
+
     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();
     const customCss: string | null = customizeService.getCustomCss();
     const customCss: string | null = customizeService.getCustomCss();

+ 31 - 3
packages/app/src/server/service/plugin.ts

@@ -38,10 +38,37 @@ 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>
 }
 }
 
 
 export class PluginService implements IPluginService {
 export class PluginService implements IPluginService {
 
 
+  async reintallNotExistPluginRepositories(): Promise<void> {
+    try {
+      // check all growi plugin documents
+      const GrowiPlugin = mongoose.model<GrowiPlugin>('GrowiPlugin');
+      const growiPlugins = await GrowiPlugin.find({});
+      for await (const growiPluginData of JSON.parse(JSON.stringify(growiPlugins))) {
+        const pluginPath = path.join(pluginStoringPath, growiPluginData.installedPath);
+        if (fs.existsSync(pluginPath)) {
+          // if exists repository, do nothing
+          continue;
+        }
+        else {
+          // if not exists repository, reinstall latest plugin
+          // delete old document
+          await GrowiPlugin.deleteOne({ _id: growiPluginData._id });
+          // reinstall latest plugin
+          await this.install(growiPluginData.origin);
+          continue;
+        }
+      }
+    }
+    catch (err) {
+      throw new Error(err);
+    }
+  }
+
   async install(origin: GrowiPluginOrigin): Promise<void> {
   async install(origin: GrowiPluginOrigin): Promise<void> {
     // download
     // download
     const ghUrl = new URL(origin.url);
     const ghUrl = new URL(origin.url);
@@ -76,14 +103,15 @@ export class PluginService implements IPluginService {
 
 
     const renamePath = async(oldPath: fs.PathLike, newPath: fs.PathLike) => {
     const renamePath = async(oldPath: fs.PathLike, newPath: fs.PathLike) => {
       if (fs.existsSync(newPath)) {
       if (fs.existsSync(newPath)) {
-        // delete old directory
+        // if a repository already exists, delete old repository before rename path
+        // delete old repository
         await fs.promises.rm(newPath, { recursive: true });
         await fs.promises.rm(newPath, { recursive: true });
 
 
         // delete old document
         // delete old document
         const GrowiPlugin = mongoose.model<GrowiPlugin>('GrowiPlugin');
         const GrowiPlugin = mongoose.model<GrowiPlugin>('GrowiPlugin');
         await GrowiPlugin.deleteOne({ url: `https://github.com/${ghOrganizationName}/${ghReposName}` });
         await GrowiPlugin.deleteOne({ url: `https://github.com/${ghOrganizationName}/${ghReposName}` });
 
 
-        // rename new directory
+        // rename new repository
         fs.renameSync(oldPath, newPath);
         fs.renameSync(oldPath, newPath);
       }
       }
       else {
       else {
@@ -119,7 +147,7 @@ export class PluginService implements IPluginService {
     const unzip = async(zipFilePath: fs.PathLike, unzippedPath: fs.PathLike) => {
     const unzip = async(zipFilePath: fs.PathLike, unzippedPath: fs.PathLike) => {
       const stream = fs.createReadStream(zipFilePath);
       const stream = fs.createReadStream(zipFilePath);
       const unzipStream = stream.pipe(unzipper.Extract({ path: unzippedPath }));
       const unzipStream = stream.pipe(unzipper.Extract({ path: unzippedPath }));
-      const deleteZipFile = (path: fs.PathLike) => fs.unlink(path, (err) => { return err });
+      const deleteZipFile = (path: fs.PathLike) => fs.unlinkSync(path);
 
 
       try {
       try {
         await streamToPromise(unzipStream);
         await streamToPromise(unzipStream);