| 
									
										
										
										
											2018-09-14 10:05:57 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * This file reader will pull the contents from a cli command json file | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The doc will initially have the form: | 
					
						
							|  |  |  |  * ```
 | 
					
						
							|  |  |  |  * { | 
					
						
							|  |  |  |  *   startingLine: 1, | 
					
						
							|  |  |  |  *   ... | 
					
						
							|  |  |  |  * } | 
					
						
							|  |  |  |  * ```
 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-06-06 20:08:59 +01:00
										 |  |  | module.exports = function cliCommandFileReader() { | 
					
						
							| 
									
										
										
										
											2018-09-14 10:05:57 +01:00
										 |  |  |   const json5 = require('json5'); | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     name: 'cliCommandFileReader', | 
					
						
							|  |  |  |     defaultPattern: /\.json$/, | 
					
						
							|  |  |  |     getDocs(fileInfo) { | 
					
						
							| 
									
										
										
										
											2018-10-17 15:45:47 +01:00
										 |  |  |       fileInfo.realProjectRelativePath = 'packages/angular/cli/commands/' + fileInfo.relativePath; | 
					
						
							| 
									
										
										
										
											2018-09-14 10:05:57 +01:00
										 |  |  |       try { | 
					
						
							|  |  |  |         const doc = json5.parse(fileInfo.content); | 
					
						
							|  |  |  |         const name = fileInfo.baseName; | 
					
						
							|  |  |  |         const path = `cli/${name}`; | 
					
						
							|  |  |  |         // We return a single element array because content files only contain one document
 | 
					
						
							|  |  |  |         const result = Object.assign(doc, { | 
					
						
							|  |  |  |           content: doc.description, | 
					
						
							|  |  |  |           docType: 'cli-command', | 
					
						
							|  |  |  |           id: `cli-${doc.name}`, | 
					
						
							|  |  |  |           commandAliases: doc.aliases || [], | 
					
						
							| 
									
										
										
										
											2019-06-06 09:45:07 +01:00
										 |  |  |           aliases: computeAliases(doc), path, | 
					
						
							| 
									
										
										
										
											2018-09-14 10:05:57 +01:00
										 |  |  |           outputPath: `${path}.json`, | 
					
						
							|  |  |  |           breadCrumbs: [ | 
					
						
							| 
									
										
										
										
											2019-06-06 09:45:07 +01:00
										 |  |  |             {text: 'CLI', path: 'cli'}, | 
					
						
							|  |  |  |             {text: name, path}, | 
					
						
							| 
									
										
										
										
											2018-09-14 10:05:57 +01:00
										 |  |  |           ] | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2019-06-06 09:45:07 +01:00
										 |  |  |         if (doc.longDescription) { | 
					
						
							|  |  |  |           doc.longDescriptionDoc = createLongDescriptionDoc(fileInfo); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-09-14 10:05:57 +01:00
										 |  |  |         return [result]; | 
					
						
							|  |  |  |       } catch (e) { | 
					
						
							| 
									
										
										
										
											2019-06-06 20:08:59 +01:00
										 |  |  |         throw new Error( | 
					
						
							|  |  |  |             `Failed to read cli command file: "${fileInfo.relativePath}" - ${e.message}`); | 
					
						
							| 
									
										
										
										
											2018-09-14 10:05:57 +01:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2019-06-06 09:45:07 +01:00
										 |  |  |   function computeAliases(doc) { | 
					
						
							|  |  |  |     return [doc.name].concat(doc.aliases || []).map(alias => `cli-${alias}`); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Synthesize a doc for the CLI command long description, which is used to generate links | 
					
						
							|  |  |  |    * for viewing and editing the long description in GitHub. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * The long description is stored in a markdown file that is referenced from the original | 
					
						
							|  |  |  |    * schema file for the command, via the `$longDescription` field. The field is a relative path | 
					
						
							|  |  |  |    * to the markdown file from the schema file. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * This function tries to retrieve that original schema based on the file path of the help JSON | 
					
						
							|  |  |  |    * file, which was passed to the `cliCommandFileReader.getDocs()` method. | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   function createLongDescriptionDoc(fileInfo) { | 
					
						
							| 
									
										
										
										
											2019-06-06 20:08:59 +01:00
										 |  |  |     const path = require('canonical-path'); | 
					
						
							|  |  |  |     const fs = require('fs'); | 
					
						
							|  |  |  |     const json5 = require('json5'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const schemaJsonPath = path.resolve(fileInfo.basePath, '../commands', fileInfo.relativePath); | 
					
						
							| 
									
										
										
										
											2018-09-14 10:05:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 20:08:59 +01:00
										 |  |  |     try { | 
					
						
							| 
									
										
										
										
											2019-06-06 09:45:07 +01:00
										 |  |  |       const schemaJson = fs.readFileSync(schemaJsonPath); | 
					
						
							|  |  |  |       const schema = json5.parse(schemaJson); | 
					
						
							|  |  |  |       if (schema.$longDescription) { | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |           docType: 'content', | 
					
						
							|  |  |  |           startingLine: 0, | 
					
						
							|  |  |  |           fileInfo: { | 
					
						
							|  |  |  |             realProjectRelativePath: | 
					
						
							|  |  |  |                 path.join(path.dirname(fileInfo.realProjectRelativePath), schema.$longDescription) | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } catch (e) { | 
					
						
							| 
									
										
										
										
											2019-06-06 20:08:59 +01:00
										 |  |  |       throw new Error( | 
					
						
							|  |  |  |           `Unable to read CLI "$longDescription" info from the schema: "${schemaJsonPath}" - ${e.message}`); | 
					
						
							| 
									
										
										
										
											2019-06-06 09:45:07 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; |