expose updateHandle for subtasks

pull/3385/head
Yuxuan Zhang 2 years ago
parent ace515b3f9
commit 9195369746
No known key found for this signature in database
GPG Key ID: 6910B04F3351EF7D

@ -5,7 +5,17 @@ import c from 'picocolors'
export const okMark = c.green('✓') export const okMark = c.green('✓')
export const failMark = c.red('✖') export const failMark = c.red('✖')
export type UpdateHandle = (done: number, total?: number) => any export type UpdateHandle = (
done?: number,
total?: number,
subtask?: string
) => any
let updateHandle: UpdateHandle | null = null
export const updateCurrentTask: UpdateHandle = (...args) => {
updateHandle?.(...args)
}
export async function task<T>( export async function task<T>(
taskName: string, taskName: string,
@ -14,14 +24,17 @@ export async function task<T>(
const spinner = ora({ discardStdin: false }) const spinner = ora({ discardStdin: false })
spinner.start(taskName + '...') spinner.start(taskName + '...')
const updateHandle: UpdateHandle = (done, total) => { updateHandle = (done, total, subtask) => {
if (total === undefined) { const taskFullName = subtask ? `${taskName} - ${subtask}` : taskName
spinner.text = `${taskName} [ ${done} ]` if (done === undefined) {
spinner.text = taskFullName + '...'
} else if (total === undefined) {
spinner.text = `${taskFullName} [ ${done} ]`
} else { } else {
// match length to display them in same width // match length to display them in same width
const _total = `${total}` const _total = `${total}`
const _done = `${done}`.padStart(_total.length, ' ') const _done = `${done}`.padStart(_total.length, ' ')
spinner.text = `${taskName} [ ${_done} / ${_total} ]` spinner.text = `${taskFullName} [ ${_done} / ${_total} ]`
} }
} }
@ -34,6 +47,7 @@ export async function task<T>(
success = false success = false
throw e throw e
} finally { } finally {
updateHandle = null
const timeEnd = performance.now() const timeEnd = performance.now()
const duration = humanizeDuration(timeEnd - timeStart, { const duration = humanizeDuration(timeEnd - timeStart, {
maxDecimalPoints: 2 maxDecimalPoints: 2

Loading…
Cancel
Save