@@ -29,7 +29,14 @@ import { BulkQueryRequestCache } from '../../bulkDataRequestCache.js';
2929Messages . importMessagesDirectoryFromMetaUrl ( import . meta. url ) ;
3030const messages = Messages . loadMessages ( '@salesforce/plugin-data' , 'soql.query' ) ;
3131
32- export class DataSoqlQueryCommand extends SfCommand < unknown > {
32+ export type DataQueryResult = {
33+ records : jsforceRecord [ ] ;
34+ totalSize : number ;
35+ done : boolean ;
36+ outputFile ?: string ;
37+ } ;
38+
39+ export class DataSoqlQueryCommand extends SfCommand < DataQueryResult > {
3340 public static readonly summary = messages . getMessage ( 'summary' ) ;
3441 public static readonly description = messages . getMessage ( 'description' ) ;
3542 public static readonly examples = messages . getMessages ( 'examples' ) ;
@@ -81,6 +88,22 @@ export class DataSoqlQueryCommand extends SfCommand<unknown> {
8188 } ) ,
8289 'result-format' : resultFormatFlag ( ) ,
8390 perflog : perflogFlag ,
91+ 'output-file' : Flags . file ( {
92+ summary : messages . getMessage ( 'flags.output-file.summary' ) ,
93+ relationships : [
94+ {
95+ type : 'some' ,
96+ flags : [
97+ {
98+ name : 'result-format' ,
99+ // eslint-disable-next-line @typescript-eslint/require-await
100+ when : async ( flags ) : Promise < boolean > =>
101+ flags [ 'result-format' ] === 'csv' || flags [ 'result-format' ] === 'json' ,
102+ } ,
103+ ] ,
104+ } ,
105+ ] ,
106+ } ) ,
84107 } ;
85108
86109 private logger ! : Logger ;
@@ -100,7 +123,7 @@ export class DataSoqlQueryCommand extends SfCommand<unknown> {
100123 * the command, which are necessary for reporter selection.
101124 *
102125 */
103- public async run ( ) : Promise < unknown > {
126+ public async run ( ) : Promise < DataQueryResult > {
104127 this . logger = await Logger . child ( 'data:soql:query' ) ;
105128 const flags = ( await this . parse ( DataSoqlQueryCommand ) ) . flags ;
106129
@@ -134,10 +157,17 @@ You can safely remove \`--async\` (it never had any effect on the command withou
134157 this . configAggregator . getInfo ( 'org-max-query-limit' ) . value as number ,
135158 flags [ 'all-rows' ]
136159 ) ;
137- if ( ! this . jsonEnabled ( ) ) {
138- displayResults ( { ...queryResult } , flags [ 'result-format' ] ) ;
160+
161+ if ( flags [ 'output-file' ] ?? ! this . jsonEnabled ( ) ) {
162+ displayResults ( { ...queryResult } , flags [ 'result-format' ] , flags [ 'output-file' ] ) ;
163+ }
164+
165+ if ( flags [ 'output-file' ] ) {
166+ this . log ( `${ queryResult . result . totalSize } records written to ${ flags [ 'output-file' ] } ` ) ;
167+ return { ...queryResult . result , outputFile : flags [ 'output-file' ] } ;
168+ } else {
169+ return queryResult . result ;
139170 }
140- return queryResult . result ;
141171 } finally {
142172 if ( flags [ 'result-format' ] !== 'json' ) this . spinner . stop ( ) ;
143173 }
0 commit comments