diff --git a/src/__tests__/utils.spec.js b/src/__tests__/utils.spec.js index 768a981d..d01996b3 100644 --- a/src/__tests__/utils.spec.js +++ b/src/__tests__/utils.spec.js @@ -96,7 +96,7 @@ describe('replaceAppDetails ', () => { 'android/app/src/debug/java/com/rndiffapp/ReactNativeFlipper.java', 'SuperApp', 'au.org.mycorp', - 'android/app/src/debug/java/au/org/mycorp/superapp/ReactNativeFlipper.java', + 'android/app/src/debug/java/au/org/mycorp/ReactNativeFlipper.java', ], // Update the app details in file contents. [ @@ -109,7 +109,7 @@ describe('replaceAppDetails ', () => { 'applicationId "com.rndiffapp"', 'ACoolApp', 'net.foobar', - 'applicationId "net.foobar.acoolapp"', + 'applicationId "net.foobar"', ], // Don't accidentally pick up other instances of "com" such as in domain // names, or android or facebook packages. diff --git a/src/constants.js b/src/constants.js index d798c505..fe74a2d2 100644 --- a/src/constants.js +++ b/src/constants.js @@ -1,5 +1,5 @@ export const DEFAULT_APP_NAME = 'RnDiffApp' -export const DEFAULT_APP_PACKAGE = 'com' +export const DEFAULT_APP_PACKAGE = 'com.rndiffapp' export const PACKAGE_NAMES = { RN: 'react-native', diff --git a/src/utils.js b/src/utils.js index 10857ea9..ac6ab452 100644 --- a/src/utils.js +++ b/src/utils.js @@ -54,38 +54,23 @@ export const removeAppPathPrefix = (path, appName) => path.replace(new RegExp(`${appName || DEFAULT_APP_NAME}/`), '') /** - * Replaces DEFAULT_APP_NAME and DEFAULT_APP_PACKAGE in str with custom + * Replaces DEFAULT_APP_PACKAGE and DEFAULT_APP_NAME in str with custom * values if provided. * str could be a path, or content from a text file. */ export const replaceAppDetails = (str, appName, appPackage) => { - let newStr = str - if (appName) { - newStr = newStr - .replaceAll(DEFAULT_APP_NAME, appName) - .replaceAll(DEFAULT_APP_NAME.toLowerCase(), appName.toLowerCase()) - } - - if (appPackage) { - // TODO should we use node path.sep instead of hardcoding /? - // com.foo.bar -> com/foo/bar - const appPackageAsPath = appPackage.replaceAll('.', '/') - - // Only replace if DEFAULT_APP_PACKAGE a.k.a. "com" is followed by lower - // case appName. Otherwise we unwittingly pick up "com.android..." or - // "https://github.com/facebook..." - newStr = newStr - .replaceAll( - `${DEFAULT_APP_PACKAGE}/${appName.toLowerCase()}`, - `${appPackageAsPath}/${appName.toLowerCase()}` - ) - .replaceAll( - `${DEFAULT_APP_PACKAGE}.${appName.toLowerCase()}`, - `${appPackage}.${appName.toLowerCase()}` - ) - } - - return newStr + const appNameOrFallback = appName || DEFAULT_APP_NAME + const appPackageOrFallback = + appPackage || `com.${appNameOrFallback.toLowerCase()}` + + return str + .replaceAll(DEFAULT_APP_PACKAGE, appPackageOrFallback) + .replaceAll( + DEFAULT_APP_PACKAGE.replaceAll('.', '/'), + appPackageOrFallback.replaceAll('.', '/') + ) + .replaceAll(DEFAULT_APP_NAME, appNameOrFallback) + .replaceAll(DEFAULT_APP_NAME.toLowerCase(), appNameOrFallback.toLowerCase()) } export const getVersionsContentInDiff = ({