aboutsummaryrefslogtreecommitdiff
path: root/node_modules/nexe/lib/logger.js
blob: 31f33a23e4d49252a9bad59154acaa8aaa42152a (plain)
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = require("chalk");
const ora_1 = require("ora");
const frameLength = 120;
class Logger {
    constructor(level) {
        this.verbose = level === 'verbose';
        this.silent = level === 'silent';
        if (!this.silent) {
            this.ora = ora_1.default({
                text: 'Starting...',
                color: 'blue',
                spinner: 'dots',
            });
            this.ora.stop();
        }
        const noop = () => { };
        this.modify = this.silent ? noop : this._modify.bind(this);
        this.write = this.silent ? noop : this._write.bind(this);
    }
    flush() {
        !this.silent && this.ora.succeed();
        return new Promise((resolve) => setTimeout(resolve, frameLength));
    }
    _write(update, color = 'green') {
        this.ora.succeed().text = chalk_1.default[color](update);
        this.ora.start();
    }
    _modify(update, color = this.ora.color) {
        this.ora.text = update;
        this.ora.color = color;
    }
    step(text, method = 'succeed') {
        if (this.silent) {
            return { modify() { }, log() { }, pause() { }, resume() { } };
        }
        if (!this.ora.id) {
            this.ora.start().text = text;
            if (method !== 'succeed') {
                this.ora[method]();
            }
        }
        else {
            this.ora[method]().text = text;
            this.ora.start();
        }
        return {
            modify: this.modify,
            log: this.verbose ? this.write : this.modify,
            pause: () => this.ora.stopAndPersist(),
            resume: () => this.ora.start(),
        };
    }
}
exports.Logger = Logger;