Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: safer react native config retrieval, unified with the rest #31

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/tools/apple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const buildAppleAppWithoutStarting = tool({
}
} catch (error) {
return {
error: JSON.stringify(error),
error: error instanceof Error ? error.message : 'Failed to build application',
}
}
},
Expand All @@ -104,17 +104,17 @@ export const buildStartAppleApp = tool({
clean: z.boolean().optional().default(false),
}),
execute: async ({ platform, ...params }) => {
const config = await loadReactNativeConfig()
const run = createAppleRun({ platformName: platform })

try {
const config = await loadReactNativeConfig()
await run([], config, params)
return {
success: true,
}
} catch (error) {
return {
error: JSON.stringify(error),
error: error instanceof Error ? error.message : 'Failed to start application',
}
}
},
Expand Down Expand Up @@ -176,11 +176,17 @@ export const startAppleLogging = tool({
interactive: z.boolean().optional().default(true),
}),
execute: async ({ platform, ...params }) => {
const config = await loadReactNativeConfig()
const log = createLogCommand({ platformName: platform })
await log([], config, params)
return {
success: true,
try {
const config = await loadReactNativeConfig()
const log = createLogCommand({ platformName: platform })
await log([], config, params)
return {
success: true,
}
} catch (error) {
return {
error: error instanceof Error ? error.message : 'Failed to start logging',
}
}
},
})
32 changes: 19 additions & 13 deletions src/tools/react-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,25 @@ export const getReactNativeConfig = tool({
`,
parameters: z.object({}),
execute: async () => {
const {
root,
reactNativePath: path,
reactNativeVersion: version,
project,
platforms,
} = await loadReactNativeConfig()
return {
root,
path,
version,
project,
platforms,
try {
const {
root,
reactNativePath: path,
reactNativeVersion: version,
project,
platforms,
} = await loadReactNativeConfig()
return {
root,
path,
version,
project,
platforms,
}
} catch (error) {
return {
error: error instanceof Error ? error.message : 'Failed to get React Native config',
}
}
},
})
Expand Down
8 changes: 3 additions & 5 deletions src/tools/vendor-rncli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ export function getPhoneName(adbPath: string, deviceId: string) {
// Cache for React Native config
let reactNativeConfigCache: Config | null = null

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

try {
const output = execSync('npx react-native config', {
env: {
Expand All @@ -51,12 +50,11 @@ export async function loadReactNativeConfig(): Promise<Config | null> {
}).toString()

// Store the parsed output in cache
reactNativeConfigCache = JSON.parse(output)
reactNativeConfigCache = JSON.parse(output) as Config
return reactNativeConfigCache
} catch (error) {
console.error('Failed to load React Native config:', error)
throw new Error(`Failed to load React Native config. Error: ${error}`)
}
return null
}

// Optional: Add a method to clear the cache if needed
Expand Down