feat(build): add progress handler for task callback, show progress for render

closes #3384
pull/3385/head
Yuxuan Zhang 2 years ago
parent 0b37eff97e
commit e568de361f
No known key found for this signature in database
GPG Key ID: 6910B04F3351EF7D

@ -55,7 +55,7 @@ export async function build(
const entryPath = path.join(siteConfig.tempDir, 'app.js')
const { render } = await import(pathToFileURL(entryPath).toString())
await task('rendering pages', async () => {
await task('rendering pages', async (updateProgress) => {
const appChunk =
clientResult &&
(clientResult.output.find(
@ -108,9 +108,11 @@ export async function build(
])
}
}
debugger
const pages = ['404.md', ...siteConfig.pages]
let count_done = 0
await pMap(
['404.md', ...siteConfig.pages],
pages,
async (page) => {
await renderPage(
render,
@ -124,6 +126,7 @@ export async function build(
metadataScript,
additionalHeadTags
)
updateProgress(++count_done, pages.length)
},
{ concurrency: siteConfig.buildConcurrency }
)

@ -4,7 +4,12 @@ import humanizeDuration from 'humanize-duration'
export const okMark = '\x1b[32m✓\x1b[0m'
export const failMark = '\x1b[31m✖\x1b[0m'
export async function task(taskName: string, task: () => Promise<void>) {
type UpdateHandle = (done: number, total?: number) => any
export async function task<T>(
taskName: string,
task: (update: UpdateHandle) => Promise<T>
): Promise<T> {
const spinner = ora({ discardStdin: false })
spinner.start(taskName + '...')
@ -12,7 +17,17 @@ export async function task(taskName: string, task: () => Promise<void>) {
const timeStart = performance.now()
try {
await task()
const updateHandle: UpdateHandle = (done, total) => {
if (total === undefined) {
spinner.text = `${taskName} [ ${done} ]`
} else {
// match length to display them in same width
const _total = `${total}`
const _done = `${done}`.padStart(_total.length, ' ')
spinner.text = `${taskName} [ ${_done} / ${_total} ]`
}
}
return await task(updateHandle)
} catch (e) {
symbol = failMark
throw e

Loading…
Cancel
Save