@@ -2,7 +2,6 @@ import {GetBulkOperationById, BulkOperation, GetBulkOperationByIdSchema} from '.
22import { adminRequest } from '@shopify/cli-kit/node/api/admin'
33import { ensureAuthenticatedAdmin } from '@shopify/cli-kit/node/session'
44import { sleep } from '@shopify/cli-kit/node/system'
5- import { outputInfo } from '@shopify/cli-kit/node/output'
65
76interface WatchBulkOperationOptions {
87 id : string
@@ -11,28 +10,48 @@ interface WatchBulkOperationOptions {
1110
1211const TERMINAL_STATUSES = [ 'COMPLETED' , 'FAILED' , 'CANCELED' , 'EXPIRED' ]
1312const POLL_INTERVAL_SECONDS = 5
13+ const SPINNER_FRAMES = [ '⠋' , '⠙' , '⠹' , '⠸' , '⠼' , '⠴' , '⠦' , '⠧' , '⠇' , '⠏' ]
1414
1515export async function watchBulkOperation ( options : WatchBulkOperationOptions ) : Promise < BulkOperation | null > {
1616 const { id, storeFqdn} = options
1717 const adminSession = await ensureAuthenticatedAdmin ( storeFqdn )
1818
19- while ( true ) {
20- // eslint-disable-next-line no-await-in-loop
21- const response = await adminRequest < GetBulkOperationByIdSchema > ( GetBulkOperationById , adminSession , { id } )
19+ let frameIndex = 0
20+ let currentOperation : BulkOperation | null = null
21+ let isDone = false
2222
23- if ( ! response . node ) {
24- outputInfo ( `bulk operation ${ id } not found` )
25- return null
26- }
23+ const spinnerInterval = setInterval ( ( ) => {
24+ if ( isDone ) return
25+ if ( ! currentOperation ) return
2726
28- const operation = response . node
29- outputInfo ( ` ${ operation . status } - ${ operation . objectCount } objects` )
27+ const spinner = SPINNER_FRAMES [ frameIndex % SPINNER_FRAMES . length ]
28+ frameIndex ++
3029
31- if ( TERMINAL_STATUSES . includes ( operation . status ) ) {
32- return operation
33- }
30+ process . stdout . write ( `\r\x1b[K${ spinner } ${ currentOperation . status } - ${ currentOperation . objectCount } objects` )
31+ } , 80 )
32+
33+ try {
34+ while ( true ) {
35+ // eslint-disable-next-line no-await-in-loop
36+ const response = await adminRequest < GetBulkOperationByIdSchema > ( GetBulkOperationById , adminSession , { id} )
37+
38+ if ( ! response . node ) {
39+ process . stdout . write ( '\r\x1b[K' )
40+ return null
41+ }
3442
35- // eslint-disable-next-line no-await-in-loop
36- await sleep ( POLL_INTERVAL_SECONDS )
43+ currentOperation = response . node
44+
45+ if ( TERMINAL_STATUSES . includes ( currentOperation . status ) ) {
46+ process . stdout . write ( '\n' )
47+ return currentOperation
48+ }
49+
50+ // eslint-disable-next-line no-await-in-loop
51+ await sleep ( POLL_INTERVAL_SECONDS )
52+ }
53+ } finally {
54+ isDone = true
55+ clearInterval ( spinnerInterval )
3756 }
3857}
0 commit comments