@@ -2,7 +2,7 @@ import * as path from "path";
22import { EOL } from "os" ;
33import marked = require( "marked" ) ;
44
5- export class HtmlHelpService implements IHtmlHelpService {
5+ export class HelpService implements IHelpService {
66 private static MARKDOWN_FILE_EXTENSION = ".md" ;
77 private static HTML_FILE_EXTENSION = ".html" ;
88 private static MAN_PAGE_NAME_REGEX = / @ M A N _ P A G E _ N A M E @ / g;
@@ -38,23 +38,64 @@ export class HtmlHelpService implements IHtmlHelpService {
3838 private $fs : IFileSystem ,
3939 private $staticConfig : Config . IStaticConfig ,
4040 private $microTemplateService : IMicroTemplateService ,
41- private $opener : IOpener ,
42- private $commandsServiceProvider : ICommandsServiceProvider ) {
41+ private $opener : IOpener ) {
4342 this . pathToHtmlPages = this . $staticConfig . HTML_PAGES_DIR ;
4443 this . pathToManPages = this . $staticConfig . MAN_PAGES_DIR ;
4544 }
4645
46+ public async openHelpForCommandInBrowser ( commandName : string ) : Promise < void > {
47+ const htmlPage = await this . convertCommandNameToFileName ( commandName ) + HelpService . HTML_FILE_EXTENSION ;
48+ this . $logger . trace ( "Opening help for command '%s'. FileName is '%s'." , commandName , htmlPage ) ;
49+
50+ this . $fs . ensureDirectoryExists ( this . pathToHtmlPages ) ;
51+ if ( ! this . tryOpeningSelectedPage ( htmlPage ) ) {
52+ // HTML pages may have been skipped on post-install, lets generate them.
53+ this . $logger . trace ( "Required HTML file '%s' is missing. Let's try generating HTML files and see if we'll find it." , htmlPage ) ;
54+ await this . generateHtmlPages ( ) ;
55+ if ( ! this . tryOpeningSelectedPage ( htmlPage ) ) {
56+ this . $errors . failWithoutHelp ( "Unable to find help for '%s'" , commandName ) ;
57+ }
58+ }
59+ }
60+
4761 public async generateHtmlPages ( ) : Promise < void > {
4862 const mdFiles = this . $fs . enumerateFilesInDirectorySync ( this . pathToManPages ) ;
4963 const basicHtmlPage = this . $fs . readText ( this . pathToBasicPage ) ;
5064 await Promise . all ( _ . map ( mdFiles , markdownFile => this . createHtmlPage ( basicHtmlPage , markdownFile ) ) ) ;
5165 this . $logger . trace ( "Finished generating HTML files." ) ;
5266 }
5367
68+ public async showCommandLineHelp ( commandName : string ) : Promise < void > {
69+ const help = await this . getCommandLineHelpForCommand ( commandName ) ;
70+ if ( this . $staticConfig . FULL_CLIENT_NAME ) {
71+ this . $logger . info ( this . $staticConfig . FULL_CLIENT_NAME . green . bold + EOL ) ;
72+ }
73+
74+ this . $logger . printMarkdown ( help ) ;
75+ }
76+
77+ /**
78+ * Gets the help content for a specific command that should be shown on the terminal.
79+ * @param {string } commandName Name of the command for which to read the help.
80+ * @returns {Promise<string> } Help content of the command parsed with all terminal rules applied (stripped content that should be shown only for html help).
81+ */
82+ private async getCommandLineHelpForCommand ( commandName : string ) : Promise < string > {
83+ const helpText = await this . readMdFileForCommand ( commandName ) ;
84+ const commandLineHelp = ( await this . $microTemplateService . parseContent ( helpText , { isHtml : false } ) )
85+ . replace ( / & n b s p ; / g, " " )
86+ . replace ( HelpService . MARKDOWN_LINK_REGEX , "$1" )
87+ . replace ( HelpService . SPAN_REGEX , ( matchingSubstring : string , textBeforeSpan : string , textInsideSpan : string , index : number , fullString : string ) : string => {
88+ return textBeforeSpan + textInsideSpan . replace ( this . newLineRegex , "" ) ;
89+ } )
90+ . replace ( HelpService . NEW_LINE_REGEX , EOL ) ;
91+
92+ return commandLineHelp ;
93+ }
94+
5495 // This method should return Promise in order to generate all html pages simultaneously.
5596 private async createHtmlPage ( basicHtmlPage : string , pathToMdFile : string ) : Promise < void > {
5697 const mdFileName = path . basename ( pathToMdFile ) ;
57- const htmlFileName = mdFileName . replace ( HtmlHelpService . MARKDOWN_FILE_EXTENSION , HtmlHelpService . HTML_FILE_EXTENSION ) ;
98+ const htmlFileName = mdFileName . replace ( HelpService . MARKDOWN_FILE_EXTENSION , HelpService . HTML_FILE_EXTENSION ) ;
5899 this . $logger . trace ( "Generating '%s' help topic." , htmlFileName ) ;
59100
60101 const helpText = this . $fs . readText ( pathToMdFile ) ;
@@ -67,31 +108,16 @@ export class HtmlHelpService implements IHtmlHelpService {
67108 this . $logger . trace ( "HTML file path for '%s' man page is: '%s'." , mdFileName , filePath ) ;
68109
69110 const outputHtml = basicHtmlPage
70- . replace ( HtmlHelpService . MAN_PAGE_NAME_REGEX , mdFileName . replace ( HtmlHelpService . MARKDOWN_FILE_EXTENSION , "" ) )
71- . replace ( HtmlHelpService . HTML_COMMAND_HELP_REGEX , htmlText )
72- . replace ( HtmlHelpService . RELATIVE_PATH_TO_STYLES_CSS_REGEX , path . relative ( path . dirname ( filePath ) , this . pathToStylesCss ) )
73- . replace ( HtmlHelpService . RELATIVE_PATH_TO_IMAGES_REGEX , path . relative ( path . dirname ( filePath ) , this . pathToImages ) )
74- . replace ( HtmlHelpService . RELATIVE_PATH_TO_INDEX_REGEX , path . relative ( path . dirname ( filePath ) , this . pathToIndexHtml ) ) ;
111+ . replace ( HelpService . MAN_PAGE_NAME_REGEX , mdFileName . replace ( HelpService . MARKDOWN_FILE_EXTENSION , "" ) )
112+ . replace ( HelpService . HTML_COMMAND_HELP_REGEX , htmlText )
113+ . replace ( HelpService . RELATIVE_PATH_TO_STYLES_CSS_REGEX , path . relative ( path . dirname ( filePath ) , this . pathToStylesCss ) )
114+ . replace ( HelpService . RELATIVE_PATH_TO_IMAGES_REGEX , path . relative ( path . dirname ( filePath ) , this . pathToImages ) )
115+ . replace ( HelpService . RELATIVE_PATH_TO_INDEX_REGEX , path . relative ( path . dirname ( filePath ) , this . pathToIndexHtml ) ) ;
75116
76117 this . $fs . writeFile ( filePath , outputHtml ) ;
77118 this . $logger . trace ( "Finished writing file '%s'." , filePath ) ;
78119 }
79120
80- public async openHelpForCommandInBrowser ( commandName : string ) : Promise < void > {
81- const htmlPage = await this . convertCommandNameToFileName ( commandName ) + HtmlHelpService . HTML_FILE_EXTENSION ;
82- this . $logger . trace ( "Opening help for command '%s'. FileName is '%s'." , commandName , htmlPage ) ;
83-
84- this . $fs . ensureDirectoryExists ( this . pathToHtmlPages ) ;
85- if ( ! this . tryOpeningSelectedPage ( htmlPage ) ) {
86- // HTML pages may have been skipped on post-install, lets generate them.
87- this . $logger . trace ( "Required HTML file '%s' is missing. Let's try generating HTML files and see if we'll find it." , htmlPage ) ;
88- await this . generateHtmlPages ( ) ;
89- if ( ! this . tryOpeningSelectedPage ( htmlPage ) ) {
90- this . $errors . failWithoutHelp ( "Unable to find help for '%s'" , commandName ) ;
91- }
92- }
93- }
94-
95121 private async convertCommandNameToFileName ( commandName : string ) : Promise < string > {
96122 const defaultCommandMatch = commandName . match ( / ( \w + ?) \| \* / ) ;
97123 if ( defaultCommandMatch ) {
@@ -101,11 +127,8 @@ export class HtmlHelpService implements IHtmlHelpService {
101127
102128 const availableCommands = this . $injector . getRegisteredCommandsNames ( true ) . sort ( ) ;
103129 this . $logger . trace ( "List of registered commands: %s" , availableCommands . join ( ", " ) ) ;
104- if ( commandName && _ . startsWith ( commandName , this . $commandsServiceProvider . dynamicCommandsPrefix ) && ! _ . includes ( availableCommands , commandName ) ) {
105- const dynamicCommands = await this . $commandsServiceProvider . getDynamicCommands ( ) ;
106- if ( ! _ . includes ( dynamicCommands , commandName ) ) {
107- this . $errors . failWithoutHelp ( "Unknown command '%s'. Try '$ %s help' for a full list of supported commands." , commandName , this . $staticConfig . CLIENT_NAME . toLowerCase ( ) ) ;
108- }
130+ if ( ! _ . includes ( availableCommands , commandName ) ) {
131+ this . $errors . failWithoutHelp ( "Unknown command '%s'. Try '$ %s help' for a full list of supported commands." , commandName , this . $staticConfig . CLIENT_NAME . toLowerCase ( ) ) ;
109132 }
110133
111134 return commandName . replace ( / \| / g, "-" ) || "index" ;
@@ -127,7 +150,7 @@ export class HtmlHelpService implements IHtmlHelpService {
127150 }
128151
129152 private async readMdFileForCommand ( commandName : string ) : Promise < string > {
130- const mdFileName = await this . convertCommandNameToFileName ( commandName ) + HtmlHelpService . MARKDOWN_FILE_EXTENSION ;
153+ const mdFileName = await this . convertCommandNameToFileName ( commandName ) + HelpService . MARKDOWN_FILE_EXTENSION ;
131154 this . $logger . trace ( "Reading help for command '%s'. FileName is '%s'." , commandName , mdFileName ) ;
132155
133156 const markdownFile = _ . find ( this . $fs . enumerateFilesInDirectorySync ( this . pathToManPages ) , file => path . basename ( file ) === mdFileName ) ;
@@ -137,19 +160,6 @@ export class HtmlHelpService implements IHtmlHelpService {
137160
138161 this . $errors . failWithoutHelp ( "Unknown command '%s'. Try '$ %s help' for a full list of supported commands." , mdFileName . replace ( ".md" , "" ) , this . $staticConfig . CLIENT_NAME . toLowerCase ( ) ) ;
139162 }
140-
141- public async getCommandLineHelpForCommand ( commandName : string ) : Promise < string > {
142- const helpText = await this . readMdFileForCommand ( commandName ) ;
143- const commandLineHelp = ( await this . $microTemplateService . parseContent ( helpText , { isHtml : false } ) )
144- . replace ( / & n b s p ; / g, " " )
145- . replace ( HtmlHelpService . MARKDOWN_LINK_REGEX , "$1" )
146- . replace ( HtmlHelpService . SPAN_REGEX , ( matchingSubstring : string , textBeforeSpan : string , textInsideSpan : string , index : number , fullString : string ) : string => {
147- return textBeforeSpan + textInsideSpan . replace ( this . newLineRegex , "" ) ;
148- } )
149- . replace ( HtmlHelpService . NEW_LINE_REGEX , EOL ) ;
150-
151- return commandLineHelp ;
152- }
153163}
154164
155- $injector . register ( "htmlHelpService " , HtmlHelpService ) ;
165+ $injector . register ( "helpService " , HelpService ) ;
0 commit comments