1
+ 'use strict' ;
2
+
3
+ var _typeof = typeof Symbol === "function" && typeof Symbol . iterator === "symbol" ? function ( obj ) { return typeof obj ; } : function ( obj ) { return obj && typeof Symbol === "function" && obj . constructor === Symbol && obj !== Symbol . prototype ? "symbol" : typeof obj ; } ;
4
+
5
+ var _createClass = function ( ) { function defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( "value" in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } } return function ( Constructor , protoProps , staticProps ) { if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) defineProperties ( Constructor , staticProps ) ; return Constructor ; } ; } ( ) ;
6
+
7
+ function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
8
+
9
+ var chalk = require ( 'chalk' ) ;
10
+ var extend = require ( 'extend' ) ;
11
+ var cliCursor = require ( 'cli-cursor' ) ;
12
+ var cliSpinners = require ( 'cli-spinners' ) ;
13
+ var logSymbols = require ( 'log-symbols' ) ;
14
+
15
+ var Ora = function ( ) {
16
+ function Ora ( options ) {
17
+ _classCallCheck ( this , Ora ) ;
18
+
19
+ if ( typeof options === 'string' ) {
20
+ options = {
21
+ text : options
22
+ } ;
23
+ }
24
+
25
+ this . options = extend ( true , {
26
+ text : '' ,
27
+ color : 'cyan' ,
28
+ stream : process . stderr
29
+ } , options ) ;
30
+
31
+ var sp = this . options . spinner ;
32
+ this . spinner = ( typeof sp === 'undefined' ? 'undefined' : _typeof ( sp ) ) === 'object' ? sp : process . platform === 'win32' ? cliSpinners . line : cliSpinners [ sp ] || cliSpinners . dots ; // eslint-disable-line no-nested-ternary
33
+
34
+ if ( this . spinner . frames === undefined ) {
35
+ throw new Error ( 'Spinner must define `frames`' ) ;
36
+ }
37
+
38
+ this . text = this . options . text ;
39
+ this . color = this . options . color ;
40
+ this . interval = this . options . interval || this . spinner . interval || 100 ;
41
+ this . stream = this . options . stream ;
42
+ this . id = null ;
43
+ this . frameIndex = 0 ;
44
+ this . enabled = this . options . enabled || this . stream && this . stream . isTTY && ! process . env . CI ;
45
+ }
46
+
47
+ _createClass ( Ora , [ {
48
+ key : 'frame' ,
49
+ value : function frame ( ) {
50
+ var frames = this . spinner . frames ;
51
+ var frame = frames [ this . frameIndex ] ;
52
+
53
+ if ( this . color ) {
54
+ frame = chalk [ this . color ] ( frame ) ;
55
+ }
56
+
57
+ this . frameIndex = ++ this . frameIndex % frames . length ;
58
+
59
+ return frame + ' ' + this . text ;
60
+ }
61
+ } , {
62
+ key : 'clear' ,
63
+ value : function clear ( ) {
64
+ if ( ! this . enabled ) {
65
+ return this ;
66
+ }
67
+
68
+ this . stream . clearLine ( ) ;
69
+ this . stream . cursorTo ( 0 ) ;
70
+
71
+ return this ;
72
+ }
73
+ } , {
74
+ key : 'render' ,
75
+ value : function render ( ) {
76
+ this . clear ( ) ;
77
+ this . stream . write ( this . frame ( ) ) ;
78
+
79
+ return this ;
80
+ }
81
+ } , {
82
+ key : 'start' ,
83
+ value : function start ( ) {
84
+ if ( ! this . enabled || this . id ) {
85
+ return this ;
86
+ }
87
+
88
+ cliCursor . hide ( ) ;
89
+ this . render ( ) ;
90
+ this . id = setInterval ( this . render . bind ( this ) , this . interval ) ;
91
+
92
+ return this ;
93
+ }
94
+ } , {
95
+ key : 'stop' ,
96
+ value : function stop ( ) {
97
+ if ( ! this . enabled ) {
98
+ return this ;
99
+ }
100
+
101
+ clearInterval ( this . id ) ;
102
+ this . id = null ;
103
+ this . frameIndex = 0 ;
104
+ this . clear ( ) ;
105
+ cliCursor . show ( ) ;
106
+
107
+ return this ;
108
+ }
109
+ } , {
110
+ key : 'succeed' ,
111
+ value : function succeed ( ) {
112
+ return this . stopAndPersist ( logSymbols . success ) ;
113
+ }
114
+ } , {
115
+ key : 'fail' ,
116
+ value : function fail ( ) {
117
+ return this . stopAndPersist ( logSymbols . error ) ;
118
+ }
119
+ } , {
120
+ key : 'stopAndPersist' ,
121
+ value : function stopAndPersist ( symbol ) {
122
+ this . stop ( ) ;
123
+ this . stream . write ( ( symbol || ' ' ) + ' ' + this . text + '\n' ) ;
124
+
125
+ return this ;
126
+ }
127
+ } ] ) ;
128
+
129
+ return Ora ;
130
+ } ( ) ;
131
+
132
+ module . exports = function ( opts ) {
133
+ return new Ora ( opts ) ;
134
+ } ;
0 commit comments