@@ -22,6 +22,8 @@ import {
2222 IOSEnvironmentRequirements ,
2323 RequirementProcessor
2424} from '@salesforce/lwc-dev-mobile-core' ;
25+ import { z } from 'zod' ;
26+ import { DeviceOperationResultSchema , DeviceOperationResultType , DeviceSchema } from '../schema/device.js' ;
2527
2628Messages . importMessagesDirectoryFromMetaUrl ( import . meta. url ) ;
2729const messages = Messages . loadMessages ( '@salesforce/lwc-dev-mobile' , 'device-start' ) ;
@@ -33,6 +35,7 @@ export class Start extends BaseCommand {
3335 // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
3436 public static readonly flags = {
3537 ...CommandLineUtils . createFlag ( FlagsConfigType . JsonFlag , false ) ,
38+ ...CommandLineUtils . createFlag ( FlagsConfigType . OutputFormatFlag , false ) ,
3639 ...CommandLineUtils . createFlag ( FlagsConfigType . LogLevelFlag , false ) ,
3740 ...CommandLineUtils . createFlag ( FlagsConfigType . PlatformFlag , true ) ,
3841 target : Flags . string ( {
@@ -64,10 +67,29 @@ export class Start extends BaseCommand {
6467 return this . flagValues . writablesystem as boolean ;
6568 }
6669
67- public async run ( ) : Promise < void > {
70+ protected static getOutputSchema ( ) : z . ZodTypeAny {
71+ return DeviceOperationResultSchema ;
72+ }
73+
74+ public async run ( ) : Promise < DeviceOperationResultType | void > {
6875 this . logger . info ( `Device Start command invoked for ${ this . platform } ` ) ;
6976
70- return RequirementProcessor . execute ( this . commandRequirements ) . then ( ( ) => {
77+ if ( this . jsonEnabled ( ) ) {
78+ // if in JSON mode, just execute the start command and return the device
79+ const device = await this . executeDeviceStart ( ) ;
80+ const deviceInfo = DeviceSchema . parse ( device ) ;
81+ const message = CommandLineUtils . platformFlagIsAndroid ( this . platform )
82+ ? this . getAndroidSuccessMessage ( device as AndroidDevice )
83+ : this . getIOSSuccessMessage ( ) ;
84+ return {
85+ device : deviceInfo ,
86+ success : true ,
87+ message
88+ } ;
89+ }
90+
91+ // do the RequirementProcessor.execute if not in JSON mode
92+ await RequirementProcessor . execute ( this . commandRequirements ) . then ( ( ) => {
7193 // execute the start command
7294 this . logger . info ( 'Setup requirements met, continuing with Device Start' ) ;
7395 return this . executeDeviceStart ( ) ;
@@ -84,7 +106,7 @@ export class Start extends BaseCommand {
84106 this . commandRequirements = requirements ;
85107 }
86108
87- private async executeDeviceStart ( ) : Promise < void > {
109+ private async executeDeviceStart ( ) : Promise < AndroidDevice | AppleDevice > {
88110 const isAndroid = CommandLineUtils . platformFlagIsAndroid ( this . platform ) ;
89111
90112 const device = isAndroid
@@ -95,24 +117,38 @@ export class Start extends BaseCommand {
95117 return Promise . reject ( messages . getMessage ( 'error.target.doesNotExist' , [ this . target ] ) ) ;
96118 }
97119
98- CommonUtils . startCliAction (
99- messages . getMessage ( 'device.start.action' ) ,
100- messages . getMessage ( 'device.start.status' , [ this . target ] )
101- ) ;
120+ if ( ! this . jsonEnabled ( ) ) {
121+ CommonUtils . startCliAction (
122+ messages . getMessage ( 'device.start.action' ) ,
123+ messages . getMessage ( 'device.start.status' , [ this . target ] )
124+ ) ;
125+ }
102126
103127 if ( isAndroid ) {
104128 const avd = device as AndroidDevice ;
105129 await avd . boot ( true , this . writablesystem ? BootMode . systemWritableMandatory : BootMode . normal ) ;
106- CommonUtils . stopCliAction (
107- messages . getMessage ( 'device.start.successStatus.android' , [
108- this . target ,
109- avd . emulatorPort ( ) ,
110- this . writablesystem
111- ] )
112- ) ;
130+ if ( ! this . jsonEnabled ( ) ) {
131+ CommonUtils . stopCliAction ( this . getAndroidSuccessMessage ( device as AndroidDevice ) ) ;
132+ }
113133 } else {
114134 await ( device as AppleDevice ) . boot ( true ) ;
115- CommonUtils . stopCliAction ( messages . getMessage ( 'device.start.successStatus.ios' , [ this . target ] ) ) ;
135+ if ( ! this . jsonEnabled ( ) ) {
136+ CommonUtils . stopCliAction ( this . getIOSSuccessMessage ( ) ) ;
137+ }
116138 }
139+
140+ return device ;
141+ }
142+
143+ private getAndroidSuccessMessage ( deviceInfo : AndroidDevice ) : string {
144+ return messages . getMessage ( 'device.start.successStatus.android' , [
145+ this . target ,
146+ deviceInfo . emulatorPort ( ) ,
147+ this . writablesystem
148+ ] ) ;
149+ }
150+
151+ private getIOSSuccessMessage ( ) : string {
152+ return messages . getMessage ( 'device.start.successStatus.ios' , [ this . target ] ) ;
117153 }
118154}
0 commit comments