Skip to content

Commit 961f033

Browse files
committed
feat($core): render progress bar for static html pages
1 parent 83d68f7 commit 961f033

File tree

1 file changed

+50
-2
lines changed
  • packages/@vuepress/core/lib/node/build

1 file changed

+50
-2
lines changed

β€Žpackages/@vuepress/core/lib/node/build/index.js

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,25 @@ module.exports = class Build extends EventEmitter {
9191

9292
// render pages
9393
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`)
94105

95106
// Use p-limit to throttle number of files done at the same time
96107
const limit = pLimit(RENDER_LIMIT)
97108
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)))
99110
)
100111

112+
// Wipe progress bar
101113
readline.clearLine(process.stdout, 0)
102114
readline.cursorTo(process.stdout, 0)
103115

@@ -132,11 +144,12 @@ module.exports = class Build extends EventEmitter {
132144
* Render page
133145
*
134146
* @param {Page} page
147+
* @param {Number} total total number of static pages we're rendering
135148
* @returns {Promise<string>}
136149
* @api private
137150
*/
138151

139-
async renderPage (page) {
152+
async renderPage (page, total) {
140153
const pagePath = decodeURIComponent(page.path)
141154

142155
const context = {
@@ -148,6 +161,7 @@ module.exports = class Build extends EventEmitter {
148161
version
149162
}
150163

164+
this.active++
151165
const filename = pagePath.replace(/\/$/, '/index.html').replace(/^\//, '')
152166
const filePath = path.resolve(this.outDir, filename)
153167
try {
@@ -157,11 +171,45 @@ module.exports = class Build extends EventEmitter {
157171
} catch (e) {
158172
console.error(logger.error(chalk.red(`Error rendering ${pagePath}:`), false))
159173
throw e
174+
} finally {
175+
this.counter++
176+
this.active--
160177
}
178+
renderProgress(this.counter, total, this.active, pagePath)
161179
return filePath
162180
}
163181
}
164182

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+
165213
/**
166214
* Pipes rendered static HTML to a file
167215
*

0 commit comments

Comments
Β (0)