11import dotenv from "dotenv" ;
2- import path from ' path' ;
3- import fs from 'fs' ;
4- import { fileURLToPath } from ' url' ;
2+ import path from " path" ;
3+ import fs from "fs" ;
4+ import { fileURLToPath } from " url" ;
55
66// Create __dirname equivalent for ES modules
77const __filename = fileURLToPath ( import . meta. url ) ;
@@ -12,25 +12,25 @@ export function parseCommandLineArgs() {
1212 // Check if any args start with '--' (the way tsx passes them)
1313 const args = process . argv . slice ( 2 ) ;
1414 const parsedManually : Record < string , string > = { } ;
15-
15+
1616 for ( let i = 0 ; i < args . length ; i ++ ) {
1717 const arg = args [ i ] ;
18- if ( arg . startsWith ( '--' ) ) {
19- const [ key , value ] = arg . substring ( 2 ) . split ( '=' ) ;
18+ if ( arg . startsWith ( "--" ) ) {
19+ const [ key , value ] = arg . substring ( 2 ) . split ( "=" ) ;
2020 if ( value ) {
2121 // Handle --key=value format
2222 parsedManually [ key ] = value ;
23- } else if ( i + 1 < args . length && ! args [ i + 1 ] . startsWith ( '--' ) ) {
23+ } else if ( i + 1 < args . length && ! args [ i + 1 ] . startsWith ( "--" ) ) {
2424 // Handle --key value format
2525 parsedManually [ key ] = args [ i + 1 ] ;
2626 i ++ ; // Skip the next argument as it's the value
2727 } else {
2828 // Handle --key format (boolean flag)
29- parsedManually [ key ] = ' true' ;
29+ parsedManually [ key ] = " true" ;
3030 }
3131 }
3232 }
33-
33+
3434 // Just use the manually parsed args - removed parseArgs dependency for Node.js <18.3.0 compatibility
3535 return parsedManually ;
3636}
@@ -41,19 +41,19 @@ export function parseCommandLineArgs() {
4141 */
4242export function loadEnvFiles ( ) : string | null {
4343 // Determine if we're in development or production mode
44- const isDevelopment = process . env . NODE_ENV === ' development' || process . argv [ 1 ] ?. includes ( ' tsx' ) ;
44+ const isDevelopment = process . env . NODE_ENV === " development" || process . argv [ 1 ] ?. includes ( " tsx" ) ;
4545
4646 // Select environment file names based on environment
47- const envFileNames = isDevelopment
48- ? [ ' .env.local' , ' .env' ] // In development, try .env.local first, then .env
49- : [ ' .env' ] ; // In production, only look for .env
47+ const envFileNames = isDevelopment
48+ ? [ " .env.local" , " .env" ] // In development, try .env.local first, then .env
49+ : [ " .env" ] ; // In production, only look for .env
5050
5151 // Build paths to check for environment files
5252 const envPaths = [ ] ;
5353 for ( const fileName of envFileNames ) {
5454 envPaths . push (
5555 fileName , // Current working directory
56- path . join ( __dirname , '..' , '..' , fileName ) , // Two levels up (src/config -> src -> root)
56+ path . join ( __dirname , ".." , ".." , fileName ) , // Two levels up (src/config -> src -> root)
5757 path . join ( process . cwd ( ) , fileName ) // Explicit current working directory
5858 ) ;
5959 }
@@ -67,7 +67,7 @@ export function loadEnvFiles(): string | null {
6767 return path . basename ( envPath ) ;
6868 }
6969 }
70-
70+
7171 return null ;
7272}
7373
@@ -77,7 +77,7 @@ export function loadEnvFiles(): string | null {
7777 */
7878export function isDemoMode ( ) : boolean {
7979 const args = parseCommandLineArgs ( ) ;
80- return args . demo === ' true' ;
80+ return args . demo === " true" ;
8181}
8282
8383/**
@@ -87,85 +87,85 @@ export function isDemoMode(): boolean {
8787export function resolveDSN ( ) : { dsn : string ; source : string ; isDemo ?: boolean } | null {
8888 // Get command line arguments
8989 const args = parseCommandLineArgs ( ) ;
90-
90+
9191 // Check for demo mode first (highest priority)
9292 if ( isDemoMode ( ) ) {
9393 // Will use in-memory SQLite with demo data
94- return {
95- dsn : ' sqlite::memory:' ,
96- source : ' demo mode' ,
97- isDemo : true
94+ return {
95+ dsn : " sqlite::memory:" ,
96+ source : " demo mode" ,
97+ isDemo : true ,
9898 } ;
9999 }
100-
101- // 1. Check command line arguments
100+
101+ // 1. Check command line arguments
102102 if ( args . dsn ) {
103- return { dsn : args . dsn , source : ' command line argument' } ;
103+ return { dsn : args . dsn , source : " command line argument" } ;
104104 }
105-
105+
106106 // 2. Check environment variables before loading .env
107107 if ( process . env . DSN ) {
108- return { dsn : process . env . DSN , source : ' environment variable' } ;
108+ return { dsn : process . env . DSN , source : " environment variable" } ;
109109 }
110-
110+
111111 // 3. Try loading from .env files
112112 const loadedEnvFile = loadEnvFiles ( ) ;
113113 if ( loadedEnvFile && process . env . DSN ) {
114114 return { dsn : process . env . DSN , source : `${ loadedEnvFile } file` } ;
115115 }
116-
116+
117117 return null ;
118118}
119119
120120/**
121121 * Resolve transport type from command line args or environment variables
122122 * Returns 'stdio' or 'sse', with 'stdio' as the default
123123 */
124- export function resolveTransport ( ) : { type : ' stdio' | ' sse' ; source : string } {
124+ export function resolveTransport ( ) : { type : " stdio" | " sse" ; source : string } {
125125 // Get command line arguments
126126 const args = parseCommandLineArgs ( ) ;
127-
127+
128128 // 1. Check command line arguments first (highest priority)
129129 if ( args . transport ) {
130- const type = args . transport === ' sse' ? ' sse' : ' stdio' ;
131- return { type, source : ' command line argument' } ;
130+ const type = args . transport === " sse" ? " sse" : " stdio" ;
131+ return { type, source : " command line argument" } ;
132132 }
133-
133+
134134 // 2. Check environment variables
135135 if ( process . env . TRANSPORT ) {
136- const type = process . env . TRANSPORT === ' sse' ? ' sse' : ' stdio' ;
137- return { type, source : ' environment variable' } ;
136+ const type = process . env . TRANSPORT === " sse" ? " sse" : " stdio" ;
137+ return { type, source : " environment variable" } ;
138138 }
139-
139+
140140 // 3. Default to stdio
141- return { type : ' stdio' , source : ' default' } ;
141+ return { type : " stdio" , source : " default" } ;
142142}
143143
144144/**
145145 * Resolve port from command line args or environment variables
146146 * Returns port number with 8080 as the default
147- *
147+ *
148148 * Note: The port option is only applicable when using --transport=sse
149149 * as it controls the HTTP server port for SSE connections.
150150 */
151151export function resolvePort ( ) : { port : number ; source : string } {
152152 // Get command line arguments
153153 const args = parseCommandLineArgs ( ) ;
154-
154+
155155 // 1. Check command line arguments first (highest priority)
156156 if ( args . port ) {
157157 const port = parseInt ( args . port , 10 ) ;
158- return { port, source : ' command line argument' } ;
158+ return { port, source : " command line argument" } ;
159159 }
160-
160+
161161 // 2. Check environment variables
162162 if ( process . env . PORT ) {
163163 const port = parseInt ( process . env . PORT , 10 ) ;
164- return { port, source : ' environment variable' } ;
164+ return { port, source : " environment variable" } ;
165165 }
166-
166+
167167 // 3. Default to 8080
168- return { port : 8080 , source : ' default' } ;
168+ return { port : 8080 , source : " default" } ;
169169}
170170
171171/**
@@ -178,16 +178,16 @@ export function redactDSN(dsn: string): string {
178178 try {
179179 // Create a URL object to parse the DSN
180180 const url = new URL ( dsn ) ;
181-
181+
182182 // Replace the password with asterisks
183183 if ( url . password ) {
184- url . password = ' *******' ;
184+ url . password = " *******" ;
185185 }
186-
186+
187187 // Return the sanitized DSN
188188 return url . toString ( ) ;
189189 } catch ( error ) {
190190 // If parsing fails, do basic redaction with regex
191- return dsn . replace ( / \/ \/ ( [ ^ : ] + ) : ( [ ^ @ ] + ) @ / , ' //$1:***@' ) ;
191+ return dsn . replace ( / \/ \/ ( [ ^ : ] + ) : ( [ ^ @ ] + ) @ / , " //$1:***@" ) ;
192192 }
193193}
0 commit comments