Skip to content

Commit

Permalink
fix spinner text update
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Feb 21, 2024
1 parent bdabb0a commit 742e956
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
27 changes: 12 additions & 15 deletions packages/next/src/build/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,19 @@ export const createProgress = (total: number, label: string) => {
}

const isFinished = curProgress === total
// Use \r to reset current line with spinner.
// If it's 100% progressed, then we don't need to break a new line to avoid logging from routes while building.
const newText = `\r ${
isFinished ? Log.prefixes.event : Log.prefixes.info
} ${label} (${curProgress}/${total}) ${
isFinished ? '' : process.stdout.isTTY ? '\n' : '\r'
}`
if (progressSpinner) {
progressSpinner.text = newText
if (progressSpinner && !isFinished) {
progressSpinner.setText(`${label} (${curProgress}/${total})`)
} else {
console.log(newText)
}

if (isFinished && progressSpinner) {
progressSpinner.stop()
console.log(newText)
if (progressSpinner) {
progressSpinner.stop()
}
console.log(
` ${
isFinished ? Log.prefixes.event : Log.prefixes.info
} ${label} (${curProgress}/${total}) ${
isFinished ? '' : process.stdout.isTTY ? '\n' : '\r'
}`
)
}
}
}
26 changes: 15 additions & 11 deletions packages/next/src/build/spinner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ export default function createSpinner(
options: ora.Options = {},
logFn: (...data: any[]) => void = console.log
) {
let spinner: undefined | ora.Ora
let spinner: undefined | (ora.Ora & { setText: (text: string) => void })

const prefixText = ` ${Log.prefixes.info} ${text} `
// Add \r at beginning to reset the current line of loading status text
const suffixText = `\r ${Log.prefixes.event} ${text} `
let prefixText = ` ${Log.prefixes.info} ${text} `

if (process.stdout.isTTY) {
spinner = ora({
Expand All @@ -24,7 +22,7 @@ export default function createSpinner(
spinner: dotsSpinner,
stream: process.stdout,
...options,
}).start()
}).start() as ora.Ora & { setText: (text: string) => void }

// Add capturing of console.log/warn/error to allow pausing
// the spinner before logging and then restarting spinner after
Expand All @@ -49,18 +47,24 @@ export default function createSpinner(
console.warn = origWarn
console.error = origError
}
spinner.setText = (newText: string) => {
text = newText
prefixText = ` ${Log.prefixes.info} ${newText} `
spinner!.prefixText = prefixText
return spinner!
}
spinner.stop = (): ora.Ora => {
origStop()
resetLog()
return spinner!
}
spinner.stopAndPersist = (): ora.Ora => {
if (suffixText) {
if (spinner) {
spinner.text = suffixText
} else {
logFn(suffixText)
}
// Add \r at beginning to reset the current line of loading status text
const suffixText = `\r ${Log.prefixes.event} ${text} `
if (spinner) {
spinner.text = suffixText
} else {
logFn(suffixText)
}
origStopAndPersist()
resetLog()
Expand Down

0 comments on commit 742e956

Please sign in to comment.