@@ -4,25 +4,55 @@ import { cors } from "hono/cors";
4
4
import { logger } from "hono/logger" ;
5
5
import { serveStatic } from "@hono/node-server/serve-static" ;
6
6
import dotenv from "dotenv" ;
7
+ import { existsSync } from "fs" ;
8
+ import { join , dirname , resolve } from "path" ;
9
+ import { fileURLToPath } from "url" ;
7
10
8
11
// Import routes
9
12
import mcpRoutes from "./routes/mcp/index.js" ;
10
13
import { MCPJamClientManager } from "./services/mcpjam-client-manager.js" ;
11
14
import path from "path" ;
12
15
16
+ const __filename = fileURLToPath ( import . meta. url ) ;
17
+ const __dirname = dirname ( __filename ) ;
18
+
13
19
export function createHonoApp ( ) {
14
20
// Load environment variables early so route handlers can read CONVEX_HTTP_URL
15
- try {
16
- const envFile =
17
- process . env . NODE_ENV === "production"
18
- ? ".env.production"
19
- : ".env.development" ;
20
- dotenv . config ( { path : envFile } ) ;
21
- if ( ! process . env . CONVEX_HTTP_URL ) {
22
- dotenv . config ( ) ;
21
+ const envFile =
22
+ process . env . NODE_ENV === "production"
23
+ ? ".env.production"
24
+ : ".env.development" ;
25
+
26
+ // Determine where to look for .env file:
27
+ // 1. Electron packaged: use process.resourcesPath directly
28
+ // 2. npm package: package root (two levels up from dist/server)
29
+ // 3. Local dev: current working directory
30
+ let envPath = envFile ;
31
+
32
+ if ( process . env . IS_PACKAGED === "true" && process . resourcesPath ) {
33
+ // Electron packaged app - use process.resourcesPath directly
34
+ envPath = join ( process . resourcesPath , envFile ) ;
35
+ } else if ( process . env . ELECTRON_APP === "true" ) {
36
+ // Electron dev mode - already handled by src/main.ts setting env vars
37
+ envPath = join ( process . env . ELECTRON_RESOURCES_PATH || "." , envFile ) ;
38
+ } else {
39
+ // npm package or local dev
40
+ const packageRoot = resolve ( __dirname , ".." , ".." ) ;
41
+ const packageEnvPath = join ( packageRoot , envFile ) ;
42
+ if ( existsSync ( packageEnvPath ) ) {
43
+ envPath = packageEnvPath ;
23
44
}
24
- } catch ( error ) {
25
- console . warn ( "[startup] Failed loading env files" , error ) ;
45
+ }
46
+
47
+ dotenv . config ( { path : envPath } ) ;
48
+
49
+ // Validate required env vars
50
+ if ( ! process . env . CONVEX_HTTP_URL ) {
51
+ throw new Error (
52
+ `CONVEX_HTTP_URL is required but not set. Tried loading from: ${ envPath } \n` +
53
+ `IS_PACKAGED=${ process . env . IS_PACKAGED } , resourcesPath=${ process . resourcesPath } \n` +
54
+ `File exists: ${ existsSync ( envPath ) } ` ,
55
+ ) ;
26
56
}
27
57
28
58
// Ensure PATH includes user shell paths so child processes (e.g., npx) can be found
0 commit comments