| 
									
										
										
										
											2018-12-06 18:03:56 +01:00
										 |  |  | const fs = require('fs'); | 
					
						
							|  |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2018-12-06 23:20:18 +01:00
										 |  |  | const _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											2018-12-06 18:03:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = async cb => { | 
					
						
							|  |  |  |   // Check if the plugin users-permissions is installed because the documentation needs it
 | 
					
						
							|  |  |  |   if (Object.keys(strapi.plugins).indexOf('users-permissions') === -1) { | 
					
						
							| 
									
										
										
										
											2018-12-06 23:20:18 +01:00
										 |  |  |     throw new Error( | 
					
						
							|  |  |  |       'In order to make the documentation plugin works the users-permissions one is required', | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2018-12-06 18:03:56 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const pluginStore = strapi.store({ | 
					
						
							|  |  |  |     environment: '', | 
					
						
							|  |  |  |     type: 'plugin', | 
					
						
							|  |  |  |     name: 'documentation', | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const restrictedAccess = await pluginStore.get({ key: 'config' }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!restrictedAccess) { | 
					
						
							|  |  |  |     pluginStore.set({ key: 'config', value: { restrictedAccess: false } }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let shouldUpdateFullDoc = false; | 
					
						
							|  |  |  |   const services = strapi.plugins['documentation'].services.documentation; | 
					
						
							|  |  |  |   // Generate plugins' documentation
 | 
					
						
							|  |  |  |   const pluginsWithDocumentationNeeded = services.getPluginsWithDocumentationNeeded(); | 
					
						
							|  |  |  |   pluginsWithDocumentationNeeded.forEach(plugin => { | 
					
						
							|  |  |  |     const isDocExisting = services.checkIfPluginDocumentationFolderExists(plugin); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!isDocExisting) { | 
					
						
							|  |  |  |       services.createDocumentationDirectory(services.getPluginDocumentationPath(plugin)); | 
					
						
							|  |  |  |       // create the overrides directory
 | 
					
						
							|  |  |  |       services.createDocumentationDirectory(services.getPluginOverrideDocumentationPath(plugin)); | 
					
						
							|  |  |  |       services.createPluginDocumentationFile(plugin); | 
					
						
							| 
									
										
										
										
											2018-12-06 23:20:18 +01:00
										 |  |  |       shouldUpdateFullDoc = true; | 
					
						
							| 
									
										
										
										
											2018-12-06 18:03:56 +01:00
										 |  |  |     } else { | 
					
						
							|  |  |  |       const needToUpdatePluginDoc = services.checkIfPluginDocNeedsUpdate(plugin); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (needToUpdatePluginDoc) { | 
					
						
							|  |  |  |         services.createPluginDocumentationFile(plugin); | 
					
						
							|  |  |  |         shouldUpdateFullDoc = true; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2018-12-06 23:20:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-06 18:03:56 +01:00
										 |  |  |   // Retrieve all the apis from the apis directory
 | 
					
						
							|  |  |  |   const apis = services.getApis(); | 
					
						
							|  |  |  |   // Generate APIS' documentation
 | 
					
						
							|  |  |  |   apis.forEach(api => { | 
					
						
							|  |  |  |     const isDocExisting = services.checkIfDocumentationFolderExists(api); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!isDocExisting) { | 
					
						
							|  |  |  |       // If the documentation directory doesn't exist create it
 | 
					
						
							|  |  |  |       services.createDocumentationDirectory(services.getDocumentationPath(api)); | 
					
						
							|  |  |  |       // Create the overrides directory
 | 
					
						
							|  |  |  |       services.createDocumentationDirectory(services.getDocumentationOverridesPath(api)); | 
					
						
							|  |  |  |       // Create the documentation files per version
 | 
					
						
							|  |  |  |       services.createDocumentationFile(api); // Then create the {api}.json documentation file
 | 
					
						
							| 
									
										
										
										
											2018-12-06 23:20:18 +01:00
										 |  |  |       shouldUpdateFullDoc = true; | 
					
						
							| 
									
										
										
										
											2018-12-06 18:03:56 +01:00
										 |  |  |     } else { | 
					
						
							|  |  |  |       const needToUpdateAPIDoc = services.checkIfAPIDocNeedsUpdate(api); | 
					
						
							| 
									
										
										
										
											2018-12-06 23:20:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-06 18:03:56 +01:00
										 |  |  |       if (needToUpdateAPIDoc) { | 
					
						
							|  |  |  |         services.createDocumentationFile(api); | 
					
						
							|  |  |  |         shouldUpdateFullDoc = true; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2018-12-06 23:20:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-06 18:03:56 +01:00
										 |  |  |   const fullDoc = services.generateFullDoc(); | 
					
						
							| 
									
										
										
										
											2018-12-06 23:20:18 +01:00
										 |  |  |   // Verify that the correct documentation folder exists in the documentation plugin
 | 
					
						
							| 
									
										
										
										
											2018-12-06 18:03:56 +01:00
										 |  |  |   const isMergedDocumentationExists = services.checkIfMergedDocumentationFolderExists(); | 
					
						
							|  |  |  |   const documentationPath = services.getMergedDocumentationPath(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-06 23:20:18 +01:00
										 |  |  |   if (isMergedDocumentationExists) { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Retrieve all tags from the documentation and join them | 
					
						
							|  |  |  |      * @param {Object} documentation | 
					
						
							|  |  |  |      * @returns {String} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     const getDocTagsToString = documentation => { | 
					
						
							|  |  |  |       const defaultGeneratedTags = [ | 
					
						
							|  |  |  |         'email', | 
					
						
							|  |  |  |         'upload - file', | 
					
						
							|  |  |  |         'users-permissions - role', | 
					
						
							|  |  |  |         'authentication', | 
					
						
							|  |  |  |         'users-permissions - user', | 
					
						
							|  |  |  |       ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return _.get(documentation, 'tags', []) | 
					
						
							|  |  |  |         .map(tag => { | 
					
						
							|  |  |  |           return tag.name.toLowerCase(); | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         .filter(tag => defaultGeneratedTags.indexOf(tag) === -1) | 
					
						
							|  |  |  |         .sort((a, b) => a - b) | 
					
						
							|  |  |  |         .join('.'); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     const oldDoc = require(path.resolve(documentationPath, 'full_documentation.json')); | 
					
						
							|  |  |  |     const oldDocTags = getDocTagsToString(oldDoc); | 
					
						
							|  |  |  |     const currentDocTags = getDocTagsToString(fullDoc); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // If the tags are different (an api has been deleted) we need to rebuild the documentation
 | 
					
						
							|  |  |  |     if (oldDocTags !== currentDocTags) { | 
					
						
							|  |  |  |       shouldUpdateFullDoc = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-06 18:03:56 +01:00
										 |  |  |   if (!isMergedDocumentationExists || shouldUpdateFullDoc) { | 
					
						
							|  |  |  |     // Create the folder
 | 
					
						
							|  |  |  |     services.createDocumentationDirectory(documentationPath); | 
					
						
							|  |  |  |     // Write the file
 | 
					
						
							| 
									
										
										
										
											2018-12-06 23:20:18 +01:00
										 |  |  |     fs.writeFileSync( | 
					
						
							|  |  |  |       path.resolve(documentationPath, 'full_documentation.json'), | 
					
						
							|  |  |  |       JSON.stringify(fullDoc, null, 2), | 
					
						
							|  |  |  |       'utf8', | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2018-12-06 18:03:56 +01:00
										 |  |  |   } | 
					
						
							|  |  |  |   cb(); | 
					
						
							|  |  |  | }; |