All files spin.js

0% Statements 0/16
0% Branches 0/11
0% Functions 0/1
0% Lines 0/16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45                                                                                         
// @flow
 
import ora from 'ora'
 
let spinner
let isSpinning = false
 
// promisify ora spinner
// there is already a promise method on ora spinner, unfortunately it does
// not return the wrapped promise so that's a bit useless.
export default async function spin<T> (
  text: string,
  prom: Promise<T>) : Promise<T> {
  if (spinner) {
    spinner.text = text
  } else {
    spinner = ora({
      text,
      enabled: global.ernLogLevel !== 'debug' &&
               global.ernLogLevel !== 'trace' &&
               !process.env.__ERN_TEST__
    })
  }
 
  if (!isSpinning) {
    spinner.start()
    isSpinning = true
  }
 
  try {
    let result = await prom
    if (isSpinning) {
      spinner.succeed()
    }
    return result
  } catch (e) {
    if (isSpinning) {
      spinner.fail(e.message)
    }
    throw e
  } finally {
    isSpinning = false
  }
}