Unverified Commit 3205aedf authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

fix: handle terminal color in chrome console (#71581)

parent 2b860fdf
...@@ -649,12 +649,20 @@ const staticWorkerExposedMethods = [ ...@@ -649,12 +649,20 @@ const staticWorkerExposedMethods = [
type StaticWorker = typeof import('./worker') & Worker type StaticWorker = typeof import('./worker') & Worker
export function createStaticWorker( export function createStaticWorker(
config: NextConfigComplete, config: NextConfigComplete,
onActivity?: () => void progress?: {
run: () => void
clear: () => void
}
): StaticWorker { ): StaticWorker {
return new Worker(staticWorkerPath, { return new Worker(staticWorkerPath, {
logger: Log, logger: Log,
numWorkers: getNumberOfWorkers(config), numWorkers: getNumberOfWorkers(config),
onActivity, onActivity: () => {
progress?.run()
},
onActivityAbort: () => {
progress?.clear()
},
forkOptions: { forkOptions: {
env: process.env, env: process.env,
}, },
...@@ -1516,7 +1524,7 @@ export default async function build( ...@@ -1516,7 +1524,7 @@ export default async function build(
remainingRampup-- remainingRampup--
sema.release() sema.release()
} }
progress() progress.run()
} }
})() })()
) )
......
...@@ -47,7 +47,7 @@ export const createProgress = (total: number, label: string) => { ...@@ -47,7 +47,7 @@ export const createProgress = (total: number, label: string) => {
}, },
}) })
return () => { const run = () => {
curProgress++ curProgress++
// Make sure we only log once // Make sure we only log once
...@@ -80,4 +80,22 @@ export const createProgress = (total: number, label: string) => { ...@@ -80,4 +80,22 @@ export const createProgress = (total: number, label: string) => {
} }
} }
} }
const clear = () => {
if (
progressSpinner &&
// Ensure only reset and clear once to avoid set operation overflow in ora
progressSpinner.isSpinning
) {
progressSpinner.prefixText = '\r'
progressSpinner.text = '\r'
progressSpinner.clear()
progressSpinner.stop()
}
}
return {
run,
clear,
}
} }
...@@ -33,9 +33,20 @@ export default function createSpinner( ...@@ -33,9 +33,20 @@ export default function createSpinner(
const origStopAndPersist = spinner.stopAndPersist.bind(spinner) const origStopAndPersist = spinner.stopAndPersist.bind(spinner)
const logHandle = (method: any, args: any[]) => { const logHandle = (method: any, args: any[]) => {
origStop() // Enter a new line before logging new message, to avoid
// the new message shows up right after the spinner in the same line.
const isInProgress = spinner?.isSpinning
if (spinner && isInProgress) {
// Reset the current running spinner to empty line by `\r`
spinner.prefixText = '\r'
spinner.text = '\r'
spinner.clear()
origStop()
}
method(...args) method(...args)
spinner!.start() if (spinner && isInProgress) {
spinner.start()
}
} }
console.log = (...args: any) => logHandle(origLog, args) console.log = (...args: any) => logHandle(origLog, args)
......
...@@ -539,7 +539,7 @@ async function exportPage( ...@@ -539,7 +539,7 @@ async function exportPage(
} }
} catch (err) { } catch (err) {
console.error( console.error(
`\nError occurred prerendering page "${input.path}". Read more: https://nextjs.org/docs/messages/prerender-error\n` `Error occurred prerendering page "${input.path}". Read more: https://nextjs.org/docs/messages/prerender-error`
) )
// bailoutToCSRError errors should not leak to the user as they are not actionable; they're // bailoutToCSRError errors should not leak to the user as they are not actionable; they're
......
...@@ -4,6 +4,8 @@ import { ...@@ -4,6 +4,8 @@ import {
getParsedNodeOptionsWithoutInspect, getParsedNodeOptionsWithoutInspect,
formatNodeOptions, formatNodeOptions,
} from '../server/lib/utils' } from '../server/lib/utils'
import { Transform } from 'stream'
type FarmOptions = ConstructorParameters<typeof JestWorker>[1] type FarmOptions = ConstructorParameters<typeof JestWorker>[1]
const RESTARTED = Symbol('restarted') const RESTARTED = Symbol('restarted')
...@@ -24,6 +26,7 @@ export class Worker { ...@@ -24,6 +26,7 @@ export class Worker {
options: FarmOptions & { options: FarmOptions & {
timeout?: number timeout?: number
onActivity?: () => void onActivity?: () => void
onActivityAbort?: () => void
onRestart?: (method: string, args: any[], attempts: number) => void onRestart?: (method: string, args: any[], attempts: number) => void
logger?: Pick<typeof console, 'error' | 'info' | 'warn'> logger?: Pick<typeof console, 'error' | 'info' | 'warn'>
exposedMethods: ReadonlyArray<string> exposedMethods: ReadonlyArray<string>
...@@ -106,6 +109,26 @@ export class Worker { ...@@ -106,6 +109,26 @@ export class Worker {
} }
} }
let aborted = false
const onActivityAbort = () => {
if (!aborted) {
options.onActivityAbort?.()
aborted = true
}
}
// Listen to the worker's stdout and stderr, if there's any thing logged, abort the activity first
const abortActivityStreamOnLog = new Transform({
transform(_chunk, _encoding, callback) {
onActivityAbort()
callback()
},
})
// Stop the activity if there's any output from the worker
this._worker.getStdout().pipe(abortActivityStreamOnLog)
this._worker.getStderr().pipe(abortActivityStreamOnLog)
// Pipe the worker's stdout and stderr to the parent process
this._worker.getStdout().pipe(process.stdout) this._worker.getStdout().pipe(process.stdout)
this._worker.getStderr().pipe(process.stderr) this._worker.getStderr().pipe(process.stderr)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment