-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update sample app build info (#383)
- Loading branch information
Showing
4 changed files
with
144 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
const Env = { | ||
siteId: 'YourSiteId', | ||
cdpApiKey: 'YourCdpApiKey', | ||
buildTimestamp: Math.floor(Date.now() / 1000), | ||
cdpApiKey: 'CDP_API_KEY', | ||
siteId: 'SITE_ID', | ||
workspaceName: 'WORKSPACE_NAME', | ||
branchName: 'BRANCH_NAME', | ||
commitHash: 'COMMIT_HASH', | ||
commitsAheadCount: 'COMMITS_AHEAD_COUNT', | ||
}; | ||
|
||
export default Env; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { getVersion } from 'react-native-device-info'; | ||
import Env from '../../env'; | ||
|
||
const BuildMetadata = { | ||
sdkVersion: getSdkVersion(), | ||
appVersion: resolveValidOrElse(getVersion()), | ||
buildDate: formatBuildDateWithRelativeTime(Env.buildTimestamp), | ||
gitMetadata: `${resolveValidOrElse( | ||
Env.branchName, | ||
() => 'development build' | ||
)}-${resolveValidOrElse(Env.commitHash, () => 'untracked')}`, | ||
defaultWorkspace: resolveValidOrElse(Env.workspaceName), | ||
language: 'JavaScript', | ||
uiFramework: 'React Native', | ||
sdkIntegration: 'npm', | ||
|
||
toString() { | ||
return ` | ||
SDK Version: ${this.sdkVersion} \tApp Version: ${this.appVersion} | ||
Build Date: ${this.buildDate} | ||
Branch: ${this.gitMetadata} | ||
Default Workspace: ${this.defaultWorkspace} | ||
Language: ${this.language} \tUI Framework: ${this.uiFramework} | ||
SDK Integration: ${this.sdkIntegration} | ||
`; | ||
}, | ||
}; | ||
|
||
function resolveValidOrElse(value, fallback = () => 'unknown') { | ||
return value && value.trim() ? value : fallback(); | ||
} | ||
|
||
function formatBuildDateWithRelativeTime(timestamp) { | ||
if (!timestamp) return 'unavailable'; | ||
const parsedTimestamp = parseInt(timestamp, 10); | ||
if (isNaN(parsedTimestamp)) return 'invalid timestamp'; | ||
|
||
const buildDate = new Date(parsedTimestamp * 1000); | ||
const now = new Date(); | ||
const daysAgo = Math.floor((now - buildDate) / (1000 * 60 * 60 * 24)); | ||
|
||
return `${buildDate.toLocaleString()} ${ | ||
daysAgo === 0 ? '(Today)' : `(${daysAgo} days ago)` | ||
}`; | ||
} | ||
|
||
function getSdkVersion() { | ||
const sdkPackageName = 'customerio-reactnative'; | ||
try { | ||
const sdkPackage = getSdkMetadataFromPackageLock(sdkPackageName); | ||
if (!sdkPackage) { | ||
console.warn(`${sdkPackageName} not found in package-lock.json`); | ||
return undefined; | ||
} | ||
|
||
const version = resolveValidOrElse(sdkPackage.version); | ||
const isPathDependency = | ||
sdkPackage.resolved && sdkPackage.resolved.startsWith('file:'); | ||
if (isPathDependency) { | ||
return `${version}-${resolveValidOrElse( | ||
Env.commitsAheadCount, | ||
() => 'as-source' | ||
)}`; | ||
} | ||
|
||
return version; | ||
} catch (error) { | ||
console.warn( | ||
`Failed to read ${sdkPackageName} sdk version: ${error.message}` | ||
); | ||
return undefined; | ||
} | ||
} | ||
|
||
function getSdkMetadataFromPackageLock(packageName) { | ||
const packageLockPath = '../../package-lock.json'; | ||
try { | ||
const packageLock = require(packageLockPath); | ||
const packages = packageLock.packages || {}; | ||
const resolvedPackageName = `node_modules/${packageName}`; | ||
const sdkPackage = packages[resolvedPackageName]; | ||
if (sdkPackage) { | ||
return sdkPackage; | ||
} | ||
} catch (error) { | ||
console.warn(`Failed to read ${packageLockPath}: ${error.message}`); | ||
} | ||
return undefined; | ||
} | ||
|
||
export { BuildMetadata }; |