@@ -31,6 +31,22 @@ function commaSeparatedCodesList(value, dummyPrevious) {
31
31
} ) ;
32
32
}
33
33
34
+ /**
35
+ * Load all files in the rootFolder and all subfolders that end with .md
36
+ */
37
+ function loadAllMarkdownFiles ( rootFolder = '.' ) {
38
+ const files = [ ] ;
39
+ fs . readdirSync ( rootFolder ) . forEach ( file => {
40
+ const fullPath = path . join ( rootFolder , file ) ;
41
+ if ( fs . lstatSync ( fullPath ) . isDirectory ( ) ) {
42
+ files . push ( ...loadAllMarkdownFiles ( fullPath ) ) ;
43
+ } else if ( fullPath . endsWith ( '.md' ) ) {
44
+ files . push ( fullPath ) ;
45
+ }
46
+ } ) ;
47
+ return files ;
48
+ }
49
+
34
50
function commaSeparatedReportersList ( value ) {
35
51
return value . split ( ',' ) . map ( ( reporter ) => require ( path . resolve ( 'reporters' , reporter ) ) ) ;
36
52
}
@@ -44,12 +60,12 @@ function getInputs() {
44
60
. option ( '-c, --config [config]' , 'apply a config file (JSON), holding e.g. url specific header configuration' )
45
61
. option ( '-q, --quiet' , 'displays errors only' )
46
62
. option ( '-v, --verbose' , 'displays detailed error information' )
47
- . option ( '-i --ignore <paths>' , 'ignore input paths including an ignore path' , commaSeparatedPathsList )
63
+ . option ( '-i, --ignore <paths>' , 'ignore input paths including an ignore path' , commaSeparatedPathsList )
48
64
. option ( '-a, --alive <code>' , 'comma separated list of HTTP codes to be considered as alive' , commaSeparatedCodesList )
49
65
. option ( '-r, --retry' , 'retry after the duration indicated in \'retry-after\' header when HTTP code is 429' )
50
66
. option ( '--reporters <names>' , 'specify reporters to use' , commaSeparatedReportersList )
51
67
. option ( '--projectBaseUrl <url>' , 'the URL to use for {{BASEURL}} replacement' )
52
- . arguments ( '[filenamesOrUrls ...]' )
68
+ . arguments ( '[filenamesOrDirectorynamesOrUrls ...]' )
53
69
. action ( function ( filenamesOrUrls ) {
54
70
let filenameForOutput ;
55
71
let stream ;
@@ -75,6 +91,7 @@ function getInputs() {
75
91
for ( const filenameOrUrl of filenamesOrUrls ) {
76
92
filenameForOutput = filenameOrUrl ;
77
93
let baseUrl = '' ;
94
+ // remote file
78
95
if ( / h t t p s ? : / . test ( filenameOrUrl ) ) {
79
96
stream = needle . get (
80
97
filenameOrUrl , { agent : new ProxyAgent ( ) , use_proxy_from_env_var : false }
@@ -86,37 +103,44 @@ function getInputs() {
86
103
parsed . search = '' ;
87
104
parsed . hash = '' ;
88
105
if ( parsed . pathname . lastIndexOf ( '/' ) !== - 1 ) {
89
- parsed . pathname = parsed . pathname . substr ( 0 , parsed . pathname . lastIndexOf ( '/' ) + 1 ) ;
106
+ parsed . pathname = parsed . pathname . substring ( 0 , parsed . pathname . lastIndexOf ( '/' ) + 1 ) ;
90
107
}
91
108
baseUrl = parsed . toString ( ) ;
92
- } catch ( err ) { /* ignore error */
93
- }
109
+ inputs . push ( new Input ( filenameForOutput , stream , { baseUrl : baseUrl } ) ) ;
110
+ } catch ( err ) {
111
+ /* ignore error */
112
+ }
94
113
} else {
95
- const stats = fs . statSync ( filenameOrUrl ) ;
96
- if ( stats . isDirectory ( ) ) {
97
- console . error ( chalk . red ( '\nERROR: ' + filenameOrUrl + ' is a directory! Please provide a valid filename as an argument.' ) ) ;
98
- process . exit ( 1 ) ;
114
+ // local file or directory
115
+ let files = [ ] ;
116
+
117
+ if ( fs . statSync ( filenameOrUrl ) . isDirectory ( ) ) {
118
+ files = loadAllMarkdownFiles ( filenameOrUrl )
119
+ } else {
120
+ files = [ filenameOrUrl ]
99
121
}
100
122
101
- const resolved = path . resolve ( filenameOrUrl ) ;
123
+ for ( let file of files ) {
124
+ filenameForOutput = file ;
125
+ const resolved = path . resolve ( filenameForOutput ) ;
102
126
103
- // skip paths given if it includes a path to ignore.
104
- // todo: allow ignore paths to be glob or regex instead of just includes?
105
- if ( ignore && ignore . some ( ( ignorePath ) => resolved . includes ( ignorePath ) ) ) {
106
- continue ;
107
- }
127
+ // skip paths given if it includes a path to ignore.
128
+ // todo: allow ignore paths to be glob or regex instead of just includes?
129
+ if ( ignore && ignore . some ( ( ignorePath ) => resolved . includes ( ignorePath ) ) ) {
130
+ continue ;
131
+ }
108
132
109
- if ( process . platform === 'win32' ) {
110
- baseUrl = 'file://' + path . dirname ( resolved ) . replace ( / \\ / g, '/' ) ;
111
- }
112
- else {
113
- baseUrl = 'file://' + path . dirname ( resolved ) ;
114
- }
133
+ if ( process . platform === 'win32' ) {
134
+ baseUrl = 'file://' + path . dirname ( resolved ) . replace ( / \\ / g, '/' ) ;
135
+ }
136
+ else {
137
+ baseUrl = 'file://' + path . dirname ( resolved ) ;
138
+ }
115
139
116
- stream = fs . createReadStream ( filenameOrUrl ) ;
140
+ stream = fs . createReadStream ( filenameForOutput ) ;
141
+ inputs . push ( new Input ( filenameForOutput , stream , { baseUrl : baseUrl } ) ) ;
142
+ }
117
143
}
118
-
119
- inputs . push ( new Input ( filenameForOutput , stream , { baseUrl : baseUrl } ) ) ;
120
144
}
121
145
}
122
146
) . parse ( process . argv ) ;
0 commit comments