Skip to content

Commit

Permalink
fix: type issues and more safety around getting config (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
grabbou authored Nov 26, 2024
1 parent 8167760 commit 5b37de1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
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

0 comments on commit 5b37de1

Please sign in to comment.