@@ -39,21 +39,6 @@ interface CliOptions {
3939 allVersions ?: boolean
4040}
4141
42- interface QueryResults {
43- views ?: string
44- viewsDocset ?: string
45- users ?: string
46- usersDocset ?: string
47- viewDuration ?: string
48- viewDurationDocset ?: string
49- bounces ?: string
50- bouncesDocset ?: string
51- score ?: string
52- scoreDocset ?: string
53- exits ?: string
54- exitsDocset ?: string
55- }
56-
5742interface JsonOutput {
5843 daysRange : string
5944 startDate : string
@@ -222,130 +207,8 @@ async function main(): Promise<void> {
222207 console . log ( `\n\nSkipping comparison, since '${ cleanPath } ' is already a docset.\n` )
223208 }
224209
225- // Create query promises for all requested metrics
226- const queryPromises : Promise < void > [ ] = [ ]
227- const results : QueryResults = { }
228-
229- // Setup all the promises for parallel execution
230- if ( options . views ) {
231- const queryType = 'views'
232- queryPromises . push (
233- getViews ( queryPaths , client , dates , version , options . verbose , queryType ) . then ( ( data ) => {
234- results . views = data
235- } ) ,
236- )
237- if ( options . showDocset ) {
238- const queryType = 'docset views'
239- queryPromises . push (
240- getViews ( docsetPath , client , dates , version , options . verbose , queryType ) . then ( ( data ) => {
241- results . viewsDocset = data
242- } ) ,
243- )
244- }
245- }
246-
247- if ( options . users ) {
248- const queryType = 'users'
249- queryPromises . push (
250- getUsers ( queryPaths , client , dates , version , options . verbose , queryType ) . then ( ( data ) => {
251- results . users = data
252- } ) ,
253- )
254- if ( options . showDocset ) {
255- const queryType = 'docset users'
256- queryPromises . push (
257- getUsers ( docsetPath , client , dates , version , options . verbose , queryType ) . then ( ( data ) => {
258- results . usersDocset = data
259- } ) ,
260- )
261- }
262- }
263-
264- if ( options . viewDuration ) {
265- const queryType = 'view duration'
266- queryPromises . push (
267- getViewDuration ( queryPaths , client , dates , version , options . verbose , queryType ) . then (
268- ( data ) => {
269- results . viewDuration = data
270- } ,
271- ) ,
272- )
273- if ( options . showDocset ) {
274- const queryType = 'docset view duration'
275- queryPromises . push (
276- getViewDuration ( docsetPath , client , dates , version , options . verbose , queryType ) . then (
277- ( data ) => {
278- results . viewDurationDocset = data
279- } ,
280- ) ,
281- )
282- }
283- }
284-
285- if ( options . bounces ) {
286- const queryType = 'bounces'
287- queryPromises . push (
288- getBounces ( queryPaths , client , dates , version , options . verbose , queryType ) . then ( ( data ) => {
289- results . bounces = data
290- } ) ,
291- )
292- if ( options . showDocset ) {
293- const queryType = 'docset bounces'
294- queryPromises . push (
295- getBounces ( docsetPath , client , dates , version , options . verbose , queryType ) . then (
296- ( data ) => {
297- results . bouncesDocset = data
298- } ,
299- ) ,
300- )
301- }
302- }
303-
304- if ( options . score ) {
305- const queryType = 'score'
306- queryPromises . push (
307- getScore ( queryPaths , client , dates , version , options . verbose , queryType ) . then ( ( data ) => {
308- results . score = data
309- } ) ,
310- )
311- if ( options . showDocset ) {
312- const queryType = 'docset score'
313- queryPromises . push (
314- getScore ( docsetPath , client , dates , version , options . verbose , queryType ) . then ( ( data ) => {
315- results . scoreDocset = data
316- } ) ,
317- )
318- }
319- }
320-
321- if ( options . exits ) {
322- const queryType = 'exits'
323- queryPromises . push (
324- getExitsToSupport ( queryPaths , client , dates , version , options . verbose , queryType ) . then (
325- ( data ) => {
326- results . exits = data
327- } ,
328- ) ,
329- )
330- if ( options . showDocset ) {
331- const queryType = 'docset exits'
332- queryPromises . push (
333- getExitsToSupport ( docsetPath , client , dates , version , options . verbose , queryType ) . then (
334- ( data ) => {
335- results . exitsDocset = data
336- } ,
337- ) ,
338- )
339- }
340- }
341-
342- // Execute all queries in parallel
343- await Promise . all ( queryPromises )
344-
345- spinner . succeed ( 'Data retrieved successfully!\n' )
346-
347- // Extract all results from the results object
348- const {
210+ // Execute all queries in parallel and destructure results
211+ const [
349212 views ,
350213 viewsDocset ,
351214 users ,
@@ -358,7 +221,53 @@ async function main(): Promise<void> {
358221 scoreDocset ,
359222 exits ,
360223 exitsDocset ,
361- } = results
224+ ] = await Promise . all ( [
225+ options . views
226+ ? getViews ( queryPaths , client , dates , version , options . verbose , 'views' )
227+ : undefined ,
228+ options . views && options . showDocset
229+ ? getViews ( docsetPath , client , dates , version , options . verbose , 'docset views' )
230+ : undefined ,
231+ options . users
232+ ? getUsers ( queryPaths , client , dates , version , options . verbose , 'users' )
233+ : undefined ,
234+ options . users && options . showDocset
235+ ? getUsers ( docsetPath , client , dates , version , options . verbose , 'docset users' )
236+ : undefined ,
237+ options . viewDuration
238+ ? getViewDuration ( queryPaths , client , dates , version , options . verbose , 'view duration' )
239+ : undefined ,
240+ options . viewDuration && options . showDocset
241+ ? getViewDuration (
242+ docsetPath ,
243+ client ,
244+ dates ,
245+ version ,
246+ options . verbose ,
247+ 'docset view duration' ,
248+ )
249+ : undefined ,
250+ options . bounces
251+ ? getBounces ( queryPaths , client , dates , version , options . verbose , 'bounces' )
252+ : undefined ,
253+ options . bounces && options . showDocset
254+ ? getBounces ( docsetPath , client , dates , version , options . verbose , 'docset bounces' )
255+ : undefined ,
256+ options . score
257+ ? getScore ( queryPaths , client , dates , version , options . verbose , 'score' )
258+ : undefined ,
259+ options . score && options . showDocset
260+ ? getScore ( docsetPath , client , dates , version , options . verbose , 'docset score' )
261+ : undefined ,
262+ options . exits
263+ ? getExitsToSupport ( queryPaths , client , dates , version , options . verbose , 'exits' )
264+ : undefined ,
265+ options . exits && options . showDocset
266+ ? getExitsToSupport ( docsetPath , client , dates , version , options . verbose , 'docset exits' )
267+ : undefined ,
268+ ] )
269+
270+ spinner . succeed ( 'Data retrieved successfully!\n' )
362271
363272 // Output JSON and exit
364273 if ( options . json ) {
@@ -491,11 +400,13 @@ async function main(): Promise<void> {
491400 }
492401}
493402
494- main ( ) . catch ( ( error ) => {
403+ try {
404+ await main ( )
405+ } catch ( error ) {
495406 console . error ( red ( 'Unexpected error:' ) )
496407 console . error ( error )
497408 process . exit ( 1 )
498- } )
409+ }
499410
500411/* -------- UTILITY FUNCTIONS -------- */
501412
0 commit comments