11import { runBulkOperationQuery } from './run-query.js'
2+ import { watchBulkOperation } from './watch-bulk-operation.js'
23import { AppLinkedInterface } from '../../models/app/app.js'
34import { renderSuccess , renderInfo , renderWarning } from '@shopify/cli-kit/node/ui'
45import { outputContent , outputToken } from '@shopify/cli-kit/node/output'
@@ -7,10 +8,11 @@ interface ExecuteBulkOperationInput {
78 app : AppLinkedInterface
89 storeFqdn : string
910 query : string
11+ watch ?: boolean
1012}
1113
1214export async function executeBulkOperation ( input : ExecuteBulkOperationInput ) : Promise < void > {
13- const { app, storeFqdn, query} = input
15+ const { app, storeFqdn, query, watch = false } = input
1416
1517 renderInfo ( {
1618 headline : 'Starting bulk operation.' ,
@@ -57,9 +59,65 @@ export async function executeBulkOperation(input: ExecuteBulkOperationInput): Pr
5759
5860 renderInfo ( { customSections : infoSections } )
5961
60- renderSuccess ( {
61- headline : 'Bulk operation started successfully!' ,
62- body : 'Congrats!' ,
63- } )
62+ if ( watch ) {
63+ const finalOperation = await watchBulkOperation ( {
64+ id : result . id ,
65+ storeFqdn,
66+ } )
67+
68+ if ( ! finalOperation ) {
69+ renderWarning ( {
70+ headline : 'Operation not found.' ,
71+ body : 'The bulk operation could not be found' ,
72+ } )
73+ return
74+ }
75+
76+ if ( ! ( 'status' in finalOperation ) || ! ( 'objectCount' in finalOperation ) ) {
77+ renderWarning ( {
78+ headline : 'Operation incomplete.' ,
79+ body : 'The bulk operation did not return complete data' ,
80+ } )
81+ return
82+ }
83+
84+ const count = finalOperation . objectCount
85+ switch ( finalOperation . status ) {
86+ case 'COMPLETED' :
87+ renderSuccess ( {
88+ headline : 'Query complete!' ,
89+ body : `${ count } objects processed` ,
90+ } )
91+ break
92+ case 'FAILED' :
93+ renderWarning ( {
94+ headline : 'Query failed.' ,
95+ body : `${ count } objects processed before failure` ,
96+ } )
97+ break
98+ case 'CANCELED' :
99+ renderWarning ( {
100+ headline : 'Query canceled.' ,
101+ body : `${ count } objects processed before cancellation` ,
102+ } )
103+ break
104+ case 'EXPIRED' :
105+ renderWarning ( {
106+ headline : 'Query expired.' ,
107+ body : `${ count } objects processed before expiration` ,
108+ } )
109+ break
110+ default :
111+ renderWarning ( {
112+ headline : `Query ${ finalOperation . status . toLowerCase ( ) } .` ,
113+ body : `${ count } objects processed` ,
114+ } )
115+ }
116+ } else {
117+ renderSuccess ( {
118+ headline : 'Bulk operation started successfully!' ,
119+ body : 'Congrats!' ,
120+ } )
121+ }
64122 }
65123}
0 commit comments