Skip to content

Commit

Permalink
feat: implement simple cache for config
Browse files Browse the repository at this point in the history
  • Loading branch information
okwasniewski committed Nov 24, 2024
1 parent cdbb484 commit 474c823
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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
19 changes: 19 additions & 0 deletions src/tools/apple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ export const iosTools = {
}),
execute: async ({ platform, ...params }) => {
const config = await loadReactNativeConfig()
if (!config) {
return {
success: false,
error: 'Project configuration not found',
}
}
const run = createAppleRun({ platformName: platform })

try {
Expand All @@ -135,6 +141,12 @@ export const iosTools = {
execute: async ({ newArchitecture, platform, clean }) => {
try {
const config = await loadReactNativeConfig()
if (!config) {
return {
success: false,
error: 'Project configuration not found',
}
}
const directory = config.project?.[platform]?.sourceDir ?? 'ios'

if (!directory) {
Expand Down Expand Up @@ -186,6 +198,13 @@ export const iosTools = {
execute: async ({ platform, ...params }) => {
const config = await loadReactNativeConfig()
const log = createLogCommand({ platformName: platform })

if (!config) {
return {
success: false,
error: 'Project configuration not found',
}
}
await log([], config, params)
return {
success: true,
Expand Down
11 changes: 10 additions & 1 deletion src/tools/react-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,22 @@ export const reactNativeTools = {
`,
parameters: z.object({}),
execute: async () => {
const config = await loadReactNativeConfig()
if (!config) {
return {
success: false,
error: 'Project configuration not found',
}
}

const {
root,
reactNativePath: path,
reactNativeVersion: version,
project,
platforms,
} = await loadReactNativeConfig()
} = config

return {
root,
path,
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 { build } from '@react-native-community/cli-platform-android/build/commands/buildAndroid'
Expand Down

0 comments on commit 474c823

Please sign in to comment.