From 30002f7f1ddeccb9e9b301497cd5ccbacac4213a Mon Sep 17 00:00:00 2001 From: Mike Grabowski Date: Tue, 26 Nov 2024 06:11:38 +0100 Subject: [PATCH] fix: type issues and more safety around getting config --- src/tools/apple.ts | 22 ++++++++++++++-------- src/tools/react-native.ts | 32 +++++++++++++++++++------------- src/tools/vendor-rncli.ts | 8 +++----- 3 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/tools/apple.ts b/src/tools/apple.ts index 6d3dbb2..db4857e 100644 --- a/src/tools/apple.ts +++ b/src/tools/apple.ts @@ -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', } } }, @@ -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', } } }, @@ -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', + } } }, }) diff --git a/src/tools/react-native.ts b/src/tools/react-native.ts index 350c60e..30e9a9a 100644 --- a/src/tools/react-native.ts +++ b/src/tools/react-native.ts @@ -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', + } } }, }) diff --git a/src/tools/vendor-rncli.ts b/src/tools/vendor-rncli.ts index bc1c4a1..af1634a 100644 --- a/src/tools/vendor-rncli.ts +++ b/src/tools/vendor-rncli.ts @@ -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 { +export async function loadReactNativeConfig(): Promise { // Return cached config if available if (reactNativeConfigCache !== null) { return reactNativeConfigCache } - try { const output = execSync('npx react-native config', { env: { @@ -51,12 +50,11 @@ export async function loadReactNativeConfig(): Promise { }).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