@@ -91,13 +91,25 @@ module.exports = class Build extends EventEmitter {
91
91
92
92
// render pages
93
93
logger . wait ( 'Rendering static HTML...' )
94
+ console . log ( )
95
+
96
+ // Keep track of progress
97
+ const total = this . context . pages . length
98
+ this . counter = 0
99
+ this . active = 0
100
+ const RENDER_LIMIT = 50
101
+
102
+ // start with empty progress bar
103
+ console . log ( )
104
+ renderProgress ( this . counter , total , RENDER_LIMIT - 1 , `Rendering ${ total } pages` )
94
105
95
106
// Use p-limit to throttle number of files done at the same time
96
107
const limit = pLimit ( RENDER_LIMIT )
97
108
const pagePaths = await Promise . all (
98
- this . context . pages . map ( page => limit ( ( ) => this . renderPage ( page ) ) )
109
+ this . context . pages . map ( page => limit ( ( ) => this . renderPage ( page , total ) ) )
99
110
)
100
111
112
+ // Wipe progress bar
101
113
readline . clearLine ( process . stdout , 0 )
102
114
readline . cursorTo ( process . stdout , 0 )
103
115
@@ -132,11 +144,12 @@ module.exports = class Build extends EventEmitter {
132
144
* Render page
133
145
*
134
146
* @param {Page } page
147
+ * @param {Number } total total number of static pages we're rendering
135
148
* @returns {Promise<string> }
136
149
* @api private
137
150
*/
138
151
139
- async renderPage ( page ) {
152
+ async renderPage ( page , total ) {
140
153
const pagePath = decodeURIComponent ( page . path )
141
154
142
155
const context = {
@@ -148,6 +161,7 @@ module.exports = class Build extends EventEmitter {
148
161
version
149
162
}
150
163
164
+ this . active ++
151
165
const filename = pagePath . replace ( / \/ $ / , '/index.html' ) . replace ( / ^ \/ / , '' )
152
166
const filePath = path . resolve ( this . outDir , filename )
153
167
try {
@@ -157,11 +171,45 @@ module.exports = class Build extends EventEmitter {
157
171
} catch ( e ) {
158
172
console . error ( logger . error ( chalk . red ( `Error rendering ${ pagePath } :` ) , false ) )
159
173
throw e
174
+ } finally {
175
+ this . counter ++
176
+ this . active --
160
177
}
178
+ renderProgress ( this . counter , total , this . active , pagePath )
161
179
return filePath
162
180
}
163
181
}
164
182
183
+ const BAR_LENGTH = 50
184
+ const BAR_BG = chalk . white ( 'β' )
185
+ const BAR_FG = chalk . blueBright ( 'β' )
186
+ /**
187
+ * Renders a progres sbar of static html pages, like this:
188
+ *
189
+ * ββββββββββββββββββββββββββββββββββββββββββββββββββ (44%) 721/1618 files, 50 active
190
+ * ...cations/Managing_Non-Titanium_Client_Applications_in_Dashboard.html
191
+ *
192
+ * @param {Number } count current count of files done
193
+ * @param {Number } total total number of files to process
194
+ * @param {Number } active number of files being actively processed
195
+ * @param {string } filename last file finished
196
+ */
197
+ function renderProgress ( count , total , active , filename ) {
198
+ const progress = count / total // as a [0.0, 1.0] float
199
+ const blocks = Math . floor ( progress * BAR_LENGTH )
200
+
201
+ readline . moveCursor ( process . stdout , 0 , - 1 ) // move up to
202
+ readline . cursorTo ( process . stdout , 0 ) // start at beginning of line
203
+ readline . clearScreenDown ( process . stdout ) // clear everything below
204
+
205
+ const bar = BAR_FG . repeat ( blocks ) + BAR_BG . repeat ( BAR_LENGTH - blocks )
206
+ const percent = Math . floor ( progress * 100 ) // as a 0-100 integer
207
+ const totals = chalk . gray ( `${ count } /${ total } files, ${ active + 1 } active` )
208
+ console . log ( `${ bar } (${ percent } %) ${ totals } ` ) // print the bar, progress
209
+ const shortFilename = filename . length > 70 ? `...${ filename . slice ( - 67 ) } ` : filename
210
+ process . stdout . write ( `${ chalk . gray ( shortFilename ) } ` ) // print the filename
211
+ }
212
+
165
213
/**
166
214
* Pipes rendered static HTML to a file
167
215
*
0 commit comments