sindresorhus / ora

Elegant terminal spinner
MIT License
9.08k stars 269 forks source link

ora blinks after using writeFileSync. #190

Closed SparkElf closed 2 years ago

SparkElf commented 2 years ago

os:ubuntu 20.04 env:nodejs 14.18.0 code:

export class Logger {
    logger: Ora
    constructor() {
        this.logger = ora()
    }
    start(s): Logger {
        this.logger = ora().start(chalk.bold(s))
        return this
    }
    fail(s): Logger {
        if (this.logger.isSpinning)
            this.logger.fail(chalk.red.bold(s))
        else
            this.logger = ora().fail(chalk.red.bold(s))
        if (s instanceof Error)
            console.log(s)
        return this
    }
    succeed(s): Logger {
        if (this.logger.isSpinning)
            this.logger.succeed(chalk.bold(s))
        else
            this.logger = ora().succeed(chalk.bold(s))
        return this
    }

}
export function Log(): Logger {
    return new Logger()
}
export const log = Log()

 log.start('connecting')
  let { data, headers } = await axios.get<Stream>(url, {
    timeout: 30 * SECOND,
    responseType: "stream"
  })
  //BUG:ora blinks
  writeFileSync('axios.log', toJsonBeautiful(headers))
  log.succeed('connect succeed')
sindresorhus commented 2 years ago

See https://github.com/sindresorhus/ora#why-does-the-spinner-freeze

This is just how JavaScript works. That's why async methods should be preferred in Node.js.