@@ -11,7 +11,7 @@ const messages = require('./messages');
11
11
const parser = require ( './parser' ) ;
12
12
const render = require ( './render' ) ;
13
13
const index = require ( './index' ) ;
14
- const exit = process . exit ;
14
+
15
15
16
16
class Tldr {
17
17
constructor ( config ) {
@@ -23,34 +23,33 @@ class Tldr {
23
23
24
24
list ( singleColumn ) {
25
25
let os = platform . getPreferredPlatformFolder ( this . config ) ;
26
- index . commandsFor ( os )
26
+ return index . commandsFor ( os )
27
27
. then ( ( commands ) => {
28
- this . printPages ( commands , singleColumn ) ;
28
+ return this . printPages ( commands , singleColumn ) ;
29
29
} ) ;
30
30
}
31
31
32
32
listAll ( singleColumn ) {
33
- index . commands ( )
33
+ return index . commands ( )
34
34
. then ( ( commands ) => {
35
- this . printPages ( commands , singleColumn ) ;
35
+ return this . printPages ( commands , singleColumn ) ;
36
36
} ) ;
37
37
}
38
38
39
39
get ( commands , options ) {
40
- this . printBestPage ( commands . join ( '-' ) , options ) ;
40
+ return this . printBestPage ( commands . join ( '-' ) , options ) ;
41
41
}
42
42
43
43
random ( options ) {
44
44
let os = platform . getPreferredPlatformFolder ( this . config ) ;
45
- index . commandsFor ( os )
45
+ return index . commandsFor ( os )
46
46
. then ( ( pages ) => {
47
47
if ( pages . length === 0 ) {
48
- console . error ( messages . emptyCache ( ) ) ;
49
- exit ( 1 ) ;
48
+ throw new Error ( messages . emptyCache ( ) ) ;
50
49
}
51
50
let page = sample ( pages ) ;
52
51
console . log ( 'PAGE' , page ) ;
53
- this . printBestPage ( page , options ) ;
52
+ return this . printBestPage ( page , options ) ;
54
53
} )
55
54
. catch ( ( err ) => {
56
55
console . error ( err ) ;
@@ -59,138 +58,107 @@ class Tldr {
59
58
60
59
randomExample ( ) {
61
60
let os = platform . getPreferredPlatformFolder ( this . config ) ;
62
- index . commandsFor ( os )
61
+ return index . commandsFor ( os )
63
62
. then ( ( pages ) => {
64
63
if ( pages . length === 0 ) {
65
- console . error ( messages . emptyCache ( ) ) ;
66
- exit ( 1 ) ;
64
+ throw new Error ( messages . emptyCache ( ) ) ;
67
65
}
68
66
let page = sample ( pages ) ;
69
67
console . log ( 'PAGE' , page ) ;
70
- this . printBestPage ( page , { randomExample : true } ) ;
68
+ return this . printBestPage ( page , { randomExample : true } ) ;
71
69
} )
72
70
. catch ( ( err ) => {
73
71
console . error ( err ) ;
74
72
} ) ;
75
73
}
76
74
77
75
render ( file ) {
78
- fs . readFile ( file , 'utf8' )
76
+ return fs . readFile ( file , 'utf8' )
79
77
. then ( ( content ) => {
80
78
// Getting the shortindex first to populate the shortindex var
81
79
return index . getShortIndex ( ) . then ( ( ) => {
82
80
this . renderContent ( content ) ;
83
81
} ) ;
84
- } )
85
- . catch ( ( err ) => {
86
- console . error ( err ) ;
87
- exit ( 1 ) ;
88
82
} ) ;
89
83
}
90
84
91
85
clearCache ( ) {
92
- this . cache . clear ( ) . then ( ( ) => {
86
+ return this . cache . clear ( ) . then ( ( ) => {
93
87
console . log ( 'Done' ) ;
94
88
} ) ;
95
89
}
96
90
97
91
updateCache ( ) {
98
- const spinner = ora ( ) ;
99
- spinner . start ( 'Updating...' ) ;
100
- return this . cache . update ( )
101
- . then ( ( ) => {
102
- spinner . succeed ( ) ;
103
- } )
104
- . catch ( ( err ) => {
105
- console . error ( err ) ;
106
- exit ( 1 ) ;
107
- } ) ;
92
+ return spinningPromise ( 'Updating...' , ( ) => {
93
+ return this . cache . update ( ) ;
94
+ } ) ;
108
95
}
109
96
110
97
updateIndex ( ) {
111
- const spinner = ora ( ) ;
112
- spinner . start ( 'Creating index...' ) ;
113
- search . createIndex ( )
114
- . then ( ( ) => {
115
- spinner . succeed ( ) ;
116
- } )
117
- . catch ( ( err ) => {
118
- console . error ( err ) ;
119
- } ) ;
98
+ return spinningPromise ( 'Creating index...' , ( ) => {
99
+ return search . createIndex ( ) ;
100
+ } ) ;
120
101
}
121
102
122
103
search ( keywords ) {
123
- search . getResults ( keywords . join ( ' ' ) )
104
+ return search . getResults ( keywords . join ( ' ' ) )
124
105
. then ( ( results ) => {
125
106
// TODO: make search into a class also.
126
107
search . printResults ( results , this . config ) ;
127
- } )
128
- . catch ( ( err ) => {
129
- console . error ( err ) ;
130
108
} ) ;
131
109
}
132
110
133
111
printPages ( pages , singleColumn ) {
134
112
if ( pages . length === 0 ) {
135
- console . error ( messages . emptyCache ( ) ) ;
136
- exit ( 1 ) ;
113
+ throw new Error ( messages . emptyCache ( ) ) ;
137
114
}
138
- this . checkStale ( ) ;
139
- let endOfLine = require ( 'os' ) . EOL ;
140
- let delimiter = singleColumn ? endOfLine : ', ' ;
141
- console . log ( '\n' + pages . join ( delimiter ) ) ;
115
+ return this . checkStale ( )
116
+ . then ( ( ) => {
117
+ let endOfLine = require ( 'os' ) . EOL ;
118
+ let delimiter = singleColumn ? endOfLine : ', ' ;
119
+ console . log ( '\n' + pages . join ( delimiter ) ) ;
120
+ } ) ;
142
121
}
143
122
144
123
printBestPage ( command , options = { } ) {
145
- const spinner = ora ( ) ;
146
-
147
124
// Trying to get the page from cache first
148
- this . cache . getPage ( command )
125
+ return this . cache . getPage ( command )
149
126
. then ( ( content ) => {
150
127
// If found in first try, render it
151
- if ( content ) {
152
- this . checkStale ( ) ;
153
- this . renderContent ( content , options ) ;
154
- return exit ( ) ;
128
+ if ( ! content ) {
129
+ // If not found, try to update
130
+ return spinningPromise ( 'Page not found. Updating cache...' , ( ) => {
131
+ return this . cache . update ( ) ;
132
+ } )
133
+ . then ( ( ) => {
134
+ return spinningPromise ( 'Creating index...' , ( ) => {
135
+ return search . createIndex ( ) ;
136
+ } ) ;
137
+ } )
138
+ . then ( ( ) => {
139
+ // And then, try to check in cache again
140
+ return this . cache . getPage ( command ) ;
141
+ } ) ;
155
142
}
156
- // If not found, try to update
157
- spinner . start ( 'Page not found. Updating cache...' ) ;
158
- return this . cache . update ( ) ;
159
- } )
160
- . then ( ( ) => {
161
- spinner . succeed ( ) ;
162
- spinner . start ( 'Creating index...' ) ;
163
- return search . createIndex ( ) ;
164
- } )
165
- . then ( ( ) => {
166
- spinner . succeed ( ) ;
167
- // And then, try to check in cache again
168
- return this . cache . getPage ( command ) ;
143
+ return content ;
169
144
} )
170
145
. then ( ( content ) => {
171
146
if ( ! content ) {
172
- console . error ( messages . notFound ( ) ) ;
173
- return exit ( 1 ) ;
147
+ throw new Error ( messages . notFound ( ) ) ;
174
148
}
175
- this . checkStale ( ) ;
176
- this . renderContent ( content , options ) ;
177
- } )
178
- . catch ( ( err ) => {
179
- console . error ( err ) ;
180
- exit ( 1 ) ;
149
+ return this . checkStale ( )
150
+ . then ( ( ) => {
151
+ this . renderContent ( content , options ) ;
152
+ } ) ;
181
153
} ) ;
182
154
}
183
155
184
156
checkStale ( ) {
185
- this . cache . lastUpdated ( )
157
+ return this . cache . lastUpdated ( )
186
158
. then ( ( stats ) => {
187
159
if ( stats . mtime < Date . now ( ) - ms ( '30d' ) ) {
188
160
console . warn ( 'Cache is out of date. You should run "tldr --update"' ) ;
189
161
}
190
- } )
191
- . catch ( ( err ) => {
192
- console . error ( err ) ;
193
- exit ( 1 ) ;
194
162
} ) ;
195
163
}
196
164
@@ -209,4 +177,18 @@ class Tldr {
209
177
}
210
178
}
211
179
180
+ function spinningPromise ( text , factory ) {
181
+ const spinner = ora ( ) ;
182
+ spinner . start ( text ) ;
183
+ return factory ( )
184
+ . then ( ( val ) => {
185
+ spinner . succeed ( ) ;
186
+ return val ;
187
+ } )
188
+ . catch ( ( err ) => {
189
+ spinner . fail ( ) ;
190
+ throw err ;
191
+ } ) ;
192
+ }
193
+
212
194
module . exports = Tldr ;
0 commit comments