@@ -2,7 +2,7 @@ import * as path from "path";
2
2
import { EOL } from "os" ;
3
3
import marked = require( "marked" ) ;
4
4
5
- export class HtmlHelpService implements IHtmlHelpService {
5
+ export class HelpService implements IHelpService {
6
6
private static MARKDOWN_FILE_EXTENSION = ".md" ;
7
7
private static HTML_FILE_EXTENSION = ".html" ;
8
8
private static MAN_PAGE_NAME_REGEX = / @ M A N _ P A G E _ N A M E @ / g;
@@ -38,23 +38,59 @@ export class HtmlHelpService implements IHtmlHelpService {
38
38
private $fs : IFileSystem ,
39
39
private $staticConfig : Config . IStaticConfig ,
40
40
private $microTemplateService : IMicroTemplateService ,
41
- private $opener : IOpener ,
42
- private $commandsServiceProvider : ICommandsServiceProvider ) {
41
+ private $opener : IOpener ) {
43
42
this . pathToHtmlPages = this . $staticConfig . HTML_PAGES_DIR ;
44
43
this . pathToManPages = this . $staticConfig . MAN_PAGES_DIR ;
45
44
}
46
45
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
+
47
61
public async generateHtmlPages ( ) : Promise < void > {
48
62
const mdFiles = this . $fs . enumerateFilesInDirectorySync ( this . pathToManPages ) ;
49
63
const basicHtmlPage = this . $fs . readText ( this . pathToBasicPage ) ;
50
64
await Promise . all ( _ . map ( mdFiles , markdownFile => this . createHtmlPage ( basicHtmlPage , markdownFile ) ) ) ;
51
65
this . $logger . trace ( "Finished generating HTML files." ) ;
52
66
}
53
67
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
+ public async getCommandLineHelpForCommand ( commandName : string ) : Promise < string > {
78
+ const helpText = await this . readMdFileForCommand ( commandName ) ;
79
+ const commandLineHelp = ( await this . $microTemplateService . parseContent ( helpText , { isHtml : false } ) )
80
+ . replace ( / & n b s p ; / g, " " )
81
+ . replace ( HelpService . MARKDOWN_LINK_REGEX , "$1" )
82
+ . replace ( HelpService . SPAN_REGEX , ( matchingSubstring : string , textBeforeSpan : string , textInsideSpan : string , index : number , fullString : string ) : string => {
83
+ return textBeforeSpan + textInsideSpan . replace ( this . newLineRegex , "" ) ;
84
+ } )
85
+ . replace ( HelpService . NEW_LINE_REGEX , EOL ) ;
86
+
87
+ return commandLineHelp ;
88
+ }
89
+
54
90
// This method should return Promise in order to generate all html pages simultaneously.
55
91
private async createHtmlPage ( basicHtmlPage : string , pathToMdFile : string ) : Promise < void > {
56
92
const mdFileName = path . basename ( pathToMdFile ) ;
57
- const htmlFileName = mdFileName . replace ( HtmlHelpService . MARKDOWN_FILE_EXTENSION , HtmlHelpService . HTML_FILE_EXTENSION ) ;
93
+ const htmlFileName = mdFileName . replace ( HelpService . MARKDOWN_FILE_EXTENSION , HelpService . HTML_FILE_EXTENSION ) ;
58
94
this . $logger . trace ( "Generating '%s' help topic." , htmlFileName ) ;
59
95
60
96
const helpText = this . $fs . readText ( pathToMdFile ) ;
@@ -67,31 +103,16 @@ export class HtmlHelpService implements IHtmlHelpService {
67
103
this . $logger . trace ( "HTML file path for '%s' man page is: '%s'." , mdFileName , filePath ) ;
68
104
69
105
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 ) ) ;
106
+ . replace ( HelpService . MAN_PAGE_NAME_REGEX , mdFileName . replace ( HelpService . MARKDOWN_FILE_EXTENSION , "" ) )
107
+ . replace ( HelpService . HTML_COMMAND_HELP_REGEX , htmlText )
108
+ . replace ( HelpService . RELATIVE_PATH_TO_STYLES_CSS_REGEX , path . relative ( path . dirname ( filePath ) , this . pathToStylesCss ) )
109
+ . replace ( HelpService . RELATIVE_PATH_TO_IMAGES_REGEX , path . relative ( path . dirname ( filePath ) , this . pathToImages ) )
110
+ . replace ( HelpService . RELATIVE_PATH_TO_INDEX_REGEX , path . relative ( path . dirname ( filePath ) , this . pathToIndexHtml ) ) ;
75
111
76
112
this . $fs . writeFile ( filePath , outputHtml ) ;
77
113
this . $logger . trace ( "Finished writing file '%s'." , filePath ) ;
78
114
}
79
115
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
-
95
116
private async convertCommandNameToFileName ( commandName : string ) : Promise < string > {
96
117
const defaultCommandMatch = commandName . match ( / ( \w + ?) \| \* / ) ;
97
118
if ( defaultCommandMatch ) {
@@ -101,11 +122,8 @@ export class HtmlHelpService implements IHtmlHelpService {
101
122
102
123
const availableCommands = this . $injector . getRegisteredCommandsNames ( true ) . sort ( ) ;
103
124
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
- }
125
+ if ( ! _ . includes ( availableCommands , commandName ) ) {
126
+ this . $errors . failWithoutHelp ( "Unknown command '%s'. Try '$ %s help' for a full list of supported commands." , commandName , this . $staticConfig . CLIENT_NAME . toLowerCase ( ) ) ;
109
127
}
110
128
111
129
return commandName . replace ( / \| / g, "-" ) || "index" ;
@@ -127,7 +145,7 @@ export class HtmlHelpService implements IHtmlHelpService {
127
145
}
128
146
129
147
private async readMdFileForCommand ( commandName : string ) : Promise < string > {
130
- const mdFileName = await this . convertCommandNameToFileName ( commandName ) + HtmlHelpService . MARKDOWN_FILE_EXTENSION ;
148
+ const mdFileName = await this . convertCommandNameToFileName ( commandName ) + HelpService . MARKDOWN_FILE_EXTENSION ;
131
149
this . $logger . trace ( "Reading help for command '%s'. FileName is '%s'." , commandName , mdFileName ) ;
132
150
133
151
const markdownFile = _ . find ( this . $fs . enumerateFilesInDirectorySync ( this . pathToManPages ) , file => path . basename ( file ) === mdFileName ) ;
@@ -137,19 +155,6 @@ export class HtmlHelpService implements IHtmlHelpService {
137
155
138
156
this . $errors . failWithoutHelp ( "Unknown command '%s'. Try '$ %s help' for a full list of supported commands." , mdFileName . replace ( ".md" , "" ) , this . $staticConfig . CLIENT_NAME . toLowerCase ( ) ) ;
139
157
}
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
- }
153
158
}
154
159
155
- $injector . register ( "htmlHelpService " , HtmlHelpService ) ;
160
+ $injector . register ( "helpService " , HelpService ) ;
0 commit comments