Skip to content

Commit

Permalink
feat: implement simple cache for config (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
okwasniewski authored Nov 24, 2024
1 parent 20e4772 commit 48d4388
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"devDependencies": {
"@release-it-plugins/workspaces": "^4.2.0",
"@release-it/conventional-changelog": "^9.0.3",
"@react-native-community/cli-types": "^15.1.2",
"@typescript-eslint/eslint-plugin": "^7.3.1",
"@typescript-eslint/parser": "^7.3.1",
"bun-types": "^1.1.33",
Expand Down
27 changes: 22 additions & 5 deletions src/tools/vendor-rncli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export {
default as tryLaunchEmulator,
} from '@react-native-community/cli-platform-android/build/commands/runAndroid/tryLaunchEmulator'

import { Config } from '@react-native-community/cli-types'

export function getEmulatorName(adbPath: string, deviceId: string) {
const buffer = execSync(`${adbPath} -s ${deviceId} emu avd name`)
return buffer
Expand All @@ -29,7 +31,15 @@ export function getPhoneName(adbPath: string, deviceId: string) {
.trim()
}

export async function loadReactNativeConfig() {
// Cache for React Native config
let reactNativeConfigCache: Config | null = null

export async function loadReactNativeConfig(): Promise<Config | null> {
// Return cached config if available
if (reactNativeConfigCache !== null) {
return reactNativeConfigCache
}

try {
const output = execSync('npx react-native config', {
env: {
Expand All @@ -39,12 +49,19 @@ export async function loadReactNativeConfig() {
stdio: ['pipe', 'pipe', 'ignore'],
encoding: 'utf8',
}).toString()
return JSON.parse(output)

// Store the parsed output in cache
reactNativeConfigCache = JSON.parse(output)
return reactNativeConfigCache
} catch (error) {
return {
error: `There was an error loading project configuration: ${JSON.stringify(error)}`,
}
console.error('Failed to load React Native config:', error)
}
return null
}

// Optional: Add a method to clear the cache if needed
export function clearReactNativeConfigCache() {
reactNativeConfigCache = null
}

export {
Expand Down

0 comments on commit 48d4388

Please sign in to comment.