# all files / src/models/ ui.js

100% Statements 72/72
92.86% Branches 39/42
100% Functions 19/19
100% Lines 43/43
2 statements, 5 branches Ignored

    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
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    961×
    1×
    1×
    1×

    1×
    1×
    1×

    7×
    7×
    7×
    7×

    7×
    7×

    16×
    3×
    13×
    13×

    15×

    1×
    1×

    7×
    7×

    2×
    2×

    1×
    1×

    3×
    3×

    32×
    32×

    2×
    2×
    1×

    1×

    3×
    3×
    3×
    3×
    1×

    1×
    1×
    1×

     import { EOL } from 'os';
    import chalk from 'chalk';
    import elegantSpinner from 'elegant-spinner';
    import logUpdate from 'log-update';

    const frame = elegantSpinner();
    const DEFAULT_WRITE_LEVEL = 'INFO';
    const WRITE_LEVELS = {
      'DEBUG': 1,
      'INFO': 2,
      'WARNING': 3,
      'ERROR': 4
    };

    class UI {
      constructor(options = {}) {
        this.inputStream  = options.inputStream || process.stdin;
        this.outputStream = options.outputStream || process.stdout;
        this.errorStream  = options.errorStream || process.stderr;

        this.writeLevel  = options.writeLevel || DEFAULT_WRITE_LEVEL;
        this.streaming = false;
      }

      write(data, writeLevel) {
        if (writeLevel === 'ERROR') {
          this.errorStream.write(data);
        } else Eif (this.writeLevelVisible(writeLevel)) {
          this.outputStream.write(data);
        }
      }

      writeLine(text, writeLevel) {
        this.write(text + EOL, writeLevel);
      }

      writeInfo(text) {
        const content = chalk.blue('  info: ') + chalk.white(text);
        this.writeLine(content, 'INFO');
      }

      writeDebug(text) {
        const content = chalk.gray('  debug: ') + chalk.white(text);
        this.writeLine(content, 'DEBUG');
      }

      writeError(text) {
        const content = chalk.red('  error: ') + chalk.white(text);
        this.writeLine(content, 'ERROR');
      }

      writeWarning(text) {
        const content = chalk.yellow('  warning: ') + chalk.white(text);
        this.writeLine(content, 'WARNING');
      }

      writeCreate(text) {
        const content = chalk.green('  create: ') + chalk.white(text);
        this.writeLine(content, 'INFO');
      }

      writeLevelVisible(writeLevel = DEFAULT_WRITE_LEVEL) {
        return WRITE_LEVELS[writeLevel] >= WRITE_LEVELS[this.writeLevel];
      }

      setWriteLevel(newLevel) {
        const allowedLevels = Object.keys(WRITE_LEVELS);
        if (allowedLevels.indexOf(newLevel) === -1) {
          throw new Error(
            `Unknown write level. Valid values are: ${allowedLevels.join(', ')}`
          );
        }

        this.writeLevel = newLevel;
      }

      startProgress(string, customStream) {
        const stream = customStream || logUpdate.create(this.outputStream);
        Eif (this.writeLevelVisible(this.writeLevel)) {
          this.streaming = true;
          this.progressInterval = setInterval(() => {
            stream(`  ${chalk.green('loading:')} ${string} ${chalk.cyan.bold.dim(frame())}`);
          }, 100);
        }
      }

      stopProgress() {
        Eif (this.progressInterval) {
          this.streaming = false;
          clearInterval(this.progressInterval);
        }
      }
    }

    export default UI;