|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT |
| 6 | + */ |
| 7 | + |
| 8 | +import { Flags } from '@salesforce/sf-plugins-core'; |
| 9 | +import { Messages } from '@salesforce/core'; |
| 10 | +import { |
| 11 | + AndroidDeviceManager, |
| 12 | + AndroidEnvironmentRequirements, |
| 13 | + AppleDeviceManager, |
| 14 | + BaseCommand, |
| 15 | + CommandLineUtils, |
| 16 | + CommandRequirements, |
| 17 | + CommonUtils, |
| 18 | + FlagsConfigType, |
| 19 | + IOSEnvironmentRequirements, |
| 20 | + RequirementProcessor |
| 21 | +} from '@salesforce/lwc-dev-mobile-core'; |
| 22 | +import { z } from 'zod'; |
| 23 | +import { DeviceOperationResultSchema, DeviceOperationResultType, DeviceSchema } from '../schema/device.js'; |
| 24 | + |
| 25 | +Messages.importMessagesDirectoryFromMetaUrl(import.meta.url); |
| 26 | +const messages = Messages.loadMessages('@salesforce/lwc-dev-mobile', 'app-install'); |
| 27 | + |
| 28 | +export class Install extends BaseCommand { |
| 29 | + public static readonly summary = messages.getMessage('summary'); |
| 30 | + public static readonly examples = messages.getMessages('examples'); |
| 31 | + |
| 32 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 33 | + public static readonly flags = { |
| 34 | + ...CommandLineUtils.createFlag(FlagsConfigType.JsonFlag, false), |
| 35 | + ...CommandLineUtils.createFlag(FlagsConfigType.OutputFormatFlag, false), |
| 36 | + ...CommandLineUtils.createFlag(FlagsConfigType.LogLevelFlag, false), |
| 37 | + ...CommandLineUtils.createFlag(FlagsConfigType.PlatformFlag, true), |
| 38 | + target: Flags.string({ |
| 39 | + char: 't', |
| 40 | + description: messages.getMessage('flags.target.description'), |
| 41 | + required: true, |
| 42 | + validate: (val: string) => val && val.trim().length > 0 |
| 43 | + }), |
| 44 | + appbundlepath: Flags.string({ |
| 45 | + char: 'a', |
| 46 | + description: messages.getMessage('flags.appBundlePath.description'), |
| 47 | + required: true, |
| 48 | + validate: (val: string) => val && val.trim().length > 0 |
| 49 | + }) |
| 50 | + }; |
| 51 | + |
| 52 | + protected _commandName = 'force:lightning:local:app:install'; |
| 53 | + |
| 54 | + private get platform(): string { |
| 55 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 56 | + return this.flagValues.platform as string; |
| 57 | + } |
| 58 | + |
| 59 | + private get target(): string { |
| 60 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 61 | + return this.flagValues.target as string; |
| 62 | + } |
| 63 | + private get appBundlePath(): string { |
| 64 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 65 | + return this.flagValues.appbundlepath as string; |
| 66 | + } |
| 67 | + |
| 68 | + protected static getOutputSchema(): z.ZodTypeAny { |
| 69 | + return DeviceOperationResultSchema; |
| 70 | + } |
| 71 | + |
| 72 | + public async run(): Promise<DeviceOperationResultType | void> { |
| 73 | + this.logger.info(`app install command invoked for ${this.platform}`); |
| 74 | + |
| 75 | + const successMessage = messages.getMessage('app.install.successStatus', [this.appBundlePath, this.target]); |
| 76 | + |
| 77 | + try { |
| 78 | + // if not in JSON mode, execute the requirements and start the CLI action |
| 79 | + if (!this.jsonEnabled()) { |
| 80 | + await RequirementProcessor.execute(this.commandRequirements); |
| 81 | + this.logger.info('Setup requirements met, continuing with app install'); |
| 82 | + CommonUtils.startCliAction( |
| 83 | + messages.getMessage('app.install.action'), |
| 84 | + messages.getMessage('app.install.status', [this.appBundlePath, this.target]) |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + // execute the app install command |
| 89 | + await this.executeAppInstall(); |
| 90 | + |
| 91 | + if (this.jsonEnabled()) { |
| 92 | + // In JSON mode, get the device and return it in the format of the DeviceSchema |
| 93 | + const deviceManager = CommandLineUtils.platformFlagIsAndroid(this.platform) |
| 94 | + ? new AndroidDeviceManager() |
| 95 | + : new AppleDeviceManager(); |
| 96 | + const device = await deviceManager.getDevice(this.target); |
| 97 | + |
| 98 | + if (!device) { |
| 99 | + throw new Error(messages.getMessage('error.target.doesNotExist', [this.target])); |
| 100 | + } |
| 101 | + |
| 102 | + const deviceInfo = DeviceSchema.parse(device); |
| 103 | + return { |
| 104 | + device: deviceInfo, |
| 105 | + success: true, |
| 106 | + message: successMessage |
| 107 | + }; |
| 108 | + } else { |
| 109 | + // if cli mode, stop the CLI action |
| 110 | + CommonUtils.stopCliAction(successMessage); |
| 111 | + } |
| 112 | + } catch (error) { |
| 113 | + if (!this.jsonEnabled()) { |
| 114 | + // if cli mode, stop the CLI action |
| 115 | + CommonUtils.stopCliAction(messages.getMessage('app.install.failureStatus')); |
| 116 | + } |
| 117 | + this.logger.warn(`App Install failed for ${this.platform} - ${(error as Error).message}`); |
| 118 | + throw error; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + protected populateCommandRequirements(): void { |
| 123 | + const requirements: CommandRequirements = {}; |
| 124 | + |
| 125 | + // check environment is setup correctly for the platform |
| 126 | + requirements.setup = CommandLineUtils.platformFlagIsAndroid(this.platform) |
| 127 | + ? new AndroidEnvironmentRequirements(this.logger) |
| 128 | + : new IOSEnvironmentRequirements(this.logger); |
| 129 | + |
| 130 | + this.commandRequirements = requirements; |
| 131 | + } |
| 132 | + |
| 133 | + private async executeAppInstall(): Promise<void> { |
| 134 | + const deviceManager = CommandLineUtils.platformFlagIsAndroid(this.platform) |
| 135 | + ? new AndroidDeviceManager() |
| 136 | + : new AppleDeviceManager(); |
| 137 | + const device = await deviceManager.getDevice(this.target); |
| 138 | + |
| 139 | + if (!device) { |
| 140 | + return Promise.reject(messages.getMessage('error.target.doesNotExist', [this.target])); |
| 141 | + } |
| 142 | + |
| 143 | + await device.boot(true); |
| 144 | + await device.installApp(this.appBundlePath); |
| 145 | + } |
| 146 | +} |
0 commit comments