From dbea3db808b520abf8c297dab07b59015fc992f7 Mon Sep 17 00:00:00 2001 From: Meisam Seyed Aliroteh Date: Mon, 3 Jun 2024 13:25:05 -0700 Subject: [PATCH 1/2] refactor: define platform as enum --- messages/common.md | 39 ++++++++++-- .../commands/force/lightning/local/setup.ts | 2 +- src/common/Common.ts | 61 ++++++------------- .../force/lightning/local/setup.test.ts | 9 +-- test/unit/common/Common.test.ts | 8 +-- 5 files changed, 59 insertions(+), 60 deletions(-) diff --git a/messages/common.md b/messages/common.md index 6e676ef..c55daab 100644 --- a/messages/common.md +++ b/messages/common.md @@ -1,98 +1,127 @@ # startPreviewAction + Start Preview # launchAppAction + Launch App # launchBrowserAction + Launch Browser # searchForDeviceStatus + searching for device %s # createDeviceStatus + creating device %s # foundDeviceStatus + found device %s # startDeviceStatus + starting device %s # launchBrowserStatus + navigating mobile browser to %s # launchAppStatus + launching app %s # installAppStatus + installing app %s # terminateAppStatus + terminating app %s # genericErrorStatus + error encountered # notWritableSystemShutDownStatus + emulator currently launched without -writable-system. Shutting down. # emulatorLaunchNotWritableStatus + launching %s on port %s # emulatorRelaunchNotWritableStatus + relaunching %s on port %s # emulatorLaunchWritableStatus + launching %s on port %s with -writable-system # emulatorRelaunchWritableStatus + relaunching %s on port %s with -writable-system # waitForBootStatus + waiting for %s to boot # disableAVBVerityStatus + disabling Android Verified Build and Verity # rebootChangesStatus + rebooting for changes to take effect # remountSystemWritableStatus + remounting system partition to be writable # openBrowserWithUrlStatus + Opening browser with url %s # bootTimeOut + Timeout waiting for %s to boot. # powerOffTimeOut + Timeout waiting for %s to power off. -# platformFlagMobileOnlyDescription -Specify platform ('iOS' or 'Android'). +# platformFlagDescription -# platformFlagIncludingDesktopDescription -Specify platform ('Desktop' or 'iOS' or 'Android'). +Specify platform. # apiLevelFlagDescription + Specify Android API level. Defaults to the latest API level installed. # jsonFlagDescription + Format output as json. # logLevelFlagDescription + Logging level for this command invocation (options: TRACE, DEBUG, INFO, WARN, ERROR, FATAL). # error:invalidFlagValue + Invalid value: %s # error:tempfolder:create + Could not create a temp folder at %s: %s # error:version:codename:comparing + Comparing 2 codename versions is not supported. # error:network:hardware:port -Error collecting network hardware ports: %s \ No newline at end of file + +Error collecting network hardware ports: %s diff --git a/src/cli/commands/force/lightning/local/setup.ts b/src/cli/commands/force/lightning/local/setup.ts index 13224b4..12ea29b 100644 --- a/src/cli/commands/force/lightning/local/setup.ts +++ b/src/cli/commands/force/lightning/local/setup.ts @@ -26,7 +26,7 @@ export class Setup extends BaseCommand { ...CommandLineUtils.createFlag(FlagsConfigType.Json, false), ...CommandLineUtils.createFlag(FlagsConfigType.LogLevel, false), ...CommandLineUtils.createFlag(FlagsConfigType.ApiLevel, false), - ...CommandLineUtils.createFlag(FlagsConfigType.Platform, true) + ...CommandLineUtils.createFlag(FlagsConfigType.PlatformType, true) }; protected _commandName = 'force:lightning:local:setup'; diff --git a/src/common/Common.ts b/src/common/Common.ts index 033a210..2f1c736 100644 --- a/src/common/Common.ts +++ b/src/common/Common.ts @@ -57,26 +57,28 @@ export class SetUtils { } } +export enum Platform { + desktop = 'desktop', + ios = 'ios', + android = 'android' +} + export enum FlagsConfigType { - Platform, + PlatformType, ApiLevel, LogLevel, Json } export class CommandLineUtils { - public static IOS_FLAG = 'ios'; - public static ANDROID_FLAG = 'android'; - public static DESKTOP_FLAG = 'desktop'; - /** * Checks to see if a flag is targeting iOS. * * @param input The input flag. * @returns True if flag is targeting iOS. */ - public static platformFlagIsIOS(input: string): boolean { - return (input ?? '').trim().toLowerCase() === CommandLineUtils.IOS_FLAG; + public static platformFlagIsIOS(input: Platform | string): boolean { + return (input ?? '').trim().toLowerCase() === (Platform.ios as string); } /** @@ -86,7 +88,7 @@ export class CommandLineUtils { * @returns True if flag is targeting Android. */ public static platformFlagIsAndroid(input: string): boolean { - return (input ?? '').trim().toLowerCase() === CommandLineUtils.ANDROID_FLAG; + return (input ?? '').trim().toLowerCase() === (Platform.android as string); } /** @@ -96,7 +98,7 @@ export class CommandLineUtils { * @returns True if flag is targeting Desktop. */ public static platformFlagIsDesktop(input: string): boolean { - return (input ?? '').trim().toLowerCase() === CommandLineUtils.DESKTOP_FLAG; + return (input ?? '').trim().toLowerCase() === (Platform.desktop as string); } /** @@ -116,21 +118,6 @@ export class CommandLineUtils { } } - /** - * Checks to see if a platform flag has a valid value. - * - * @param platformFlag The platform flag. - * @param includeDesktop Indicates whether Desktop is allowed as a target platform. Defaults to false. - * @returns True if flag is valid. - */ - public static platformFlagIsValid(platformFlag: string, includeDesktop = false): boolean { - return ( - CommandLineUtils.platformFlagIsIOS(platformFlag) || - CommandLineUtils.platformFlagIsAndroid(platformFlag) || - (includeDesktop && CommandLineUtils.platformFlagIsDesktop(platformFlag)) - ); - } - public static createFlag(type: FlagsConfigType, isRequired: boolean, supportsDesktop = false): any { switch (type) { case FlagsConfigType.ApiLevel: @@ -142,18 +129,16 @@ export class CommandLineUtils { validate: CommandLineUtils.validateApiLevelFlag }) }; - case FlagsConfigType.Platform: + case FlagsConfigType.PlatformType: return { - platform: Flags.string({ + platform: Flags.option({ char: 'p', - description: supportsDesktop - ? messages.getMessage('platformFlagIncludingDesktopDescription') - : messages.getMessage('platformFlagMobileOnlyDescription'), - required: isRequired, - validate: supportsDesktop - ? CommandLineUtils.validatePlatformFlagIncludingDesktop - : CommandLineUtils.validatePlatformFlagMobileOnly - }) + description: messages.getMessage('platformFlagDescription'), + required: true, + options: supportsDesktop + ? ([Platform.desktop, Platform.ios, Platform.android] as const) + : ([Platform.ios, Platform.android] as const) + })({ required: isRequired }) }; case FlagsConfigType.LogLevel: return { @@ -206,14 +191,6 @@ export class CommandLineUtils { private static validateApiLevelFlag(flag: string): boolean { return flag.trim().length > 0; // if it doesn't follow semver then we'll automatically consider it as a code name } - - private static validatePlatformFlagMobileOnly(flag: string): boolean { - return CommandLineUtils.platformFlagIsValid(flag, false); - } - - private static validatePlatformFlagIncludingDesktop(flag: string): boolean { - return CommandLineUtils.platformFlagIsValid(flag, true); - } } export class Version { diff --git a/test/unit/cli/commands/force/lightning/local/setup.test.ts b/test/unit/cli/commands/force/lightning/local/setup.test.ts index 8e31723..84acc77 100644 --- a/test/unit/cli/commands/force/lightning/local/setup.test.ts +++ b/test/unit/cli/commands/force/lightning/local/setup.test.ts @@ -4,7 +4,7 @@ * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ -import { Logger, Messages, SfError } from '@salesforce/core'; +import { Logger, Messages } from '@salesforce/core'; import { TestContext } from '@salesforce/core/testSetup'; import { stubMethod } from '@salesforce/ts-sinon'; import { expect } from 'chai'; @@ -15,8 +15,6 @@ import { RequirementProcessor } from '../../../../../../../src/common/Requiremen Messages.importMessagesDirectoryFromMetaUrl(import.meta.url); describe('Setup Tests', () => { - const messages = Messages.loadMessages('@salesforce/lwc-dev-mobile-core', 'common'); - const $$ = new TestContext(); let executeSetupMock: sinon.SinonStub; @@ -43,10 +41,7 @@ describe('Setup Tests', () => { try { await Setup.run(['-p', 'someplatform']); } catch (error) { - expect(error instanceof SfError).to.be.true; - expect((error as SfError).message).to.contain( - messages.getMessage('error:invalidFlagValue', ['someplatform']) - ); + expect(error).to.be.an('error').with.property('message').that.includes('--platform=someplatform'); } }); diff --git a/test/unit/common/Common.test.ts b/test/unit/common/Common.test.ts index 979642e..6e3c84e 100644 --- a/test/unit/common/Common.test.ts +++ b/test/unit/common/Common.test.ts @@ -128,17 +128,15 @@ describe('Commons utils tests', () => { }); it('Platform flag config property returns expected flag', async () => { - let platformFlagConfig = common.CommandLineUtils.createFlag(common.FlagsConfigType.Platform, true); - expect(platformFlagConfig.platform?.description).to.be.equal( - messages.getMessage('platformFlagMobileOnlyDescription') - ); + let platformFlagConfig = common.CommandLineUtils.createFlag(common.FlagsConfigType.PlatformType, true); + expect(platformFlagConfig.platform?.description).to.be.equal(messages.getMessage('platformFlagDescription')); let requiredKeyValuePair = Object.entries(platformFlagConfig.platform).find( (keyValuePair) => keyValuePair[0] === 'required' ); expect(requiredKeyValuePair?.[1]).to.be.true; - platformFlagConfig = common.CommandLineUtils.createFlag(common.FlagsConfigType.Platform, false); + platformFlagConfig = common.CommandLineUtils.createFlag(common.FlagsConfigType.PlatformType, false); requiredKeyValuePair = Object.entries(platformFlagConfig.platform).find( (keyValuePair) => keyValuePair[0] === 'required' From e4f7c8e9dbad48305fe9a5297fb48bab91bde6a3 Mon Sep 17 00:00:00 2001 From: Meisam Seyed Aliroteh Date: Mon, 3 Jun 2024 14:41:13 -0700 Subject: [PATCH 2/2] refactor: rename FlagsConfigType enum values --- src/cli/commands/force/lightning/local/setup.ts | 8 ++++---- src/common/Common.ts | 16 ++++++++-------- test/unit/common/Common.test.ts | 8 ++++---- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/cli/commands/force/lightning/local/setup.ts b/src/cli/commands/force/lightning/local/setup.ts index 12ea29b..6f3be37 100644 --- a/src/cli/commands/force/lightning/local/setup.ts +++ b/src/cli/commands/force/lightning/local/setup.ts @@ -23,10 +23,10 @@ export class Setup extends BaseCommand { public static readonly examples = messages.getMessages('examples'); public static readonly flags = { - ...CommandLineUtils.createFlag(FlagsConfigType.Json, false), - ...CommandLineUtils.createFlag(FlagsConfigType.LogLevel, false), - ...CommandLineUtils.createFlag(FlagsConfigType.ApiLevel, false), - ...CommandLineUtils.createFlag(FlagsConfigType.PlatformType, true) + ...CommandLineUtils.createFlag(FlagsConfigType.JsonFlag, false), + ...CommandLineUtils.createFlag(FlagsConfigType.LogLevelFlag, false), + ...CommandLineUtils.createFlag(FlagsConfigType.ApiLevelFlag, false), + ...CommandLineUtils.createFlag(FlagsConfigType.PlatformFlag, true) }; protected _commandName = 'force:lightning:local:setup'; diff --git a/src/common/Common.ts b/src/common/Common.ts index 2f1c736..2fb1928 100644 --- a/src/common/Common.ts +++ b/src/common/Common.ts @@ -64,10 +64,10 @@ export enum Platform { } export enum FlagsConfigType { - PlatformType, - ApiLevel, - LogLevel, - Json + PlatformFlag, + ApiLevelFlag, + LogLevelFlag, + JsonFlag } export class CommandLineUtils { @@ -120,7 +120,7 @@ export class CommandLineUtils { public static createFlag(type: FlagsConfigType, isRequired: boolean, supportsDesktop = false): any { switch (type) { - case FlagsConfigType.ApiLevel: + case FlagsConfigType.ApiLevelFlag: return { apilevel: Flags.string({ char: 'l', @@ -129,7 +129,7 @@ export class CommandLineUtils { validate: CommandLineUtils.validateApiLevelFlag }) }; - case FlagsConfigType.PlatformType: + case FlagsConfigType.PlatformFlag: return { platform: Flags.option({ char: 'p', @@ -140,7 +140,7 @@ export class CommandLineUtils { : ([Platform.ios, Platform.android] as const) })({ required: isRequired }) }; - case FlagsConfigType.LogLevel: + case FlagsConfigType.LogLevelFlag: return { loglevel: Flags.string({ description: messages.getMessage('logLevelFlagDescription'), @@ -149,7 +149,7 @@ export class CommandLineUtils { validate: (level: string) => level && (LoggerLevel as any)[level.trim().toUpperCase()] }) }; - case FlagsConfigType.Json: + case FlagsConfigType.JsonFlag: return { json: Flags.boolean({ description: messages.getMessage('jsonFlagDescription'), diff --git a/test/unit/common/Common.test.ts b/test/unit/common/Common.test.ts index 6e3c84e..26c7a7b 100644 --- a/test/unit/common/Common.test.ts +++ b/test/unit/common/Common.test.ts @@ -128,7 +128,7 @@ describe('Commons utils tests', () => { }); it('Platform flag config property returns expected flag', async () => { - let platformFlagConfig = common.CommandLineUtils.createFlag(common.FlagsConfigType.PlatformType, true); + let platformFlagConfig = common.CommandLineUtils.createFlag(common.FlagsConfigType.PlatformFlag, true); expect(platformFlagConfig.platform?.description).to.be.equal(messages.getMessage('platformFlagDescription')); let requiredKeyValuePair = Object.entries(platformFlagConfig.platform).find( (keyValuePair) => keyValuePair[0] === 'required' @@ -136,7 +136,7 @@ describe('Commons utils tests', () => { expect(requiredKeyValuePair?.[1]).to.be.true; - platformFlagConfig = common.CommandLineUtils.createFlag(common.FlagsConfigType.PlatformType, false); + platformFlagConfig = common.CommandLineUtils.createFlag(common.FlagsConfigType.PlatformFlag, false); requiredKeyValuePair = Object.entries(platformFlagConfig.platform).find( (keyValuePair) => keyValuePair[0] === 'required' @@ -146,7 +146,7 @@ describe('Commons utils tests', () => { }); it('API level flag config property returns expected flag', async () => { - let apiLevelFlagConfig = common.CommandLineUtils.createFlag(common.FlagsConfigType.ApiLevel, true); + let apiLevelFlagConfig = common.CommandLineUtils.createFlag(common.FlagsConfigType.ApiLevelFlag, true); expect(apiLevelFlagConfig.apilevel?.description).to.be.equal(messages.getMessage('apiLevelFlagDescription')); let requiredKeyValuePair = Object.entries(apiLevelFlagConfig.apilevel).find( @@ -155,7 +155,7 @@ describe('Commons utils tests', () => { expect(requiredKeyValuePair?.[1]).to.be.true; - apiLevelFlagConfig = common.CommandLineUtils.createFlag(common.FlagsConfigType.ApiLevel, false); + apiLevelFlagConfig = common.CommandLineUtils.createFlag(common.FlagsConfigType.ApiLevelFlag, false); requiredKeyValuePair = Object.entries(apiLevelFlagConfig.apilevel).find( (keyValuePair) => keyValuePair[0] === 'required'