Skip to content

Commit

Permalink
Merge pull request forcedotcom#89 from maliroteh-sf/main
Browse files Browse the repository at this point in the history
refactor: define platform as enum
  • Loading branch information
maliroteh-sf authored Jun 3, 2024
2 parents a01d18c + e4f7c8e commit 24e0846
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 71 deletions.
39 changes: 34 additions & 5 deletions messages/common.md
Original file line number Diff line number Diff line change
@@ -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

Error collecting network hardware ports: %s
8 changes: 4 additions & 4 deletions src/cli/commands/force/lightning/local/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.Platform, 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';
Expand Down
73 changes: 25 additions & 48 deletions src/common/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,28 @@ export class SetUtils {
}
}

export enum Platform {
desktop = 'desktop',
ios = 'ios',
android = 'android'
}

export enum FlagsConfigType {
Platform,
ApiLevel,
LogLevel,
Json
PlatformFlag,
ApiLevelFlag,
LogLevelFlag,
JsonFlag
}

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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -116,24 +118,9 @@ 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:
case FlagsConfigType.ApiLevelFlag:
return {
apilevel: Flags.string({
char: 'l',
Expand All @@ -142,20 +129,18 @@ export class CommandLineUtils {
validate: CommandLineUtils.validateApiLevelFlag
})
};
case FlagsConfigType.Platform:
case FlagsConfigType.PlatformFlag:
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:
case FlagsConfigType.LogLevelFlag:
return {
loglevel: Flags.string({
description: messages.getMessage('logLevelFlagDescription'),
Expand All @@ -164,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'),
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 2 additions & 7 deletions test/unit/cli/commands/force/lightning/local/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<any[], any>;

Expand All @@ -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');
}
});

Expand Down
12 changes: 5 additions & 7 deletions test/unit/common/Common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.PlatformFlag, 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.PlatformFlag, false);

requiredKeyValuePair = Object.entries(platformFlagConfig.platform).find(
(keyValuePair) => keyValuePair[0] === 'required'
Expand All @@ -148,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(
Expand All @@ -157,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'
Expand Down

0 comments on commit 24e0846

Please sign in to comment.