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 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>(
taskName: string,
@ -14,14 +24,17 @@ export async function task<T>(
const spinner = ora({ discardStdin: false })
spinner.start(taskName + '...')
const updateHandle: UpdateHandle = (done, total) => {
if (total === undefined) {
spinner.text = `${taskName} [ ${done} ]`
updateHandle = (done, total, subtask) => {
const taskFullName = subtask ? `${taskName} - ${subtask}` : taskName
if (done === undefined) {
spinner.text = taskFullName + '...'
} else if (total === undefined) {
spinner.text = `${taskFullName} [ ${done} ]`
} else {
// match length to display them in same width
const _total = `${total}`
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
throw e
} finally {
updateHandle = null
const timeEnd = performance.now()
const duration = humanizeDuration(timeEnd - timeStart, {
maxDecimalPoints: 2

Loading…
Cancel
Save