UNPKG

742 BJavaScriptView Raw
1const ora = require('ora');
2const { format } = require('./util');
3
4const noop = Promise.resolve();
5
6class Spinner {
7 constructor({ global = {}, container = {} } = {}) {
8 this.isSpinnerDisabled = !global.isCI || global.isVerbose || global.isDryRun || global.isDebug;
9 this.canForce = !global.isCI && !global.isVerbose && !global.isDryRun && !global.isDebug;
10 this.ora = container.ora || ora;
11 }
12 show({ enabled = true, task, label, forced = false, context }) {
13 if (!enabled) return noop;
14 const awaitTask = task();
15 if (!this.isSpinnerDisabled || (forced && this.canForce)) {
16 const text = format(label, context);
17 this.ora.promise(awaitTask, text);
18 }
19 return awaitTask;
20 }
21}
22
23module.exports = Spinner;