Skip to content

Commit

Permalink
Merge pull request #369 from zoontek/fix-package-name
Browse files Browse the repository at this point in the history
  • Loading branch information
pvinis authored Sep 11, 2023
2 parents a2b831b + 5e272fa commit 940a99c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/__tests__/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
[
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
41 changes: 13 additions & 28 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ({
Expand Down

0 comments on commit 940a99c

Please sign in to comment.