|
14 | 14 | import * as fs from 'node:fs'; |
15 | 15 | import * as path from 'node:path'; |
16 | 16 | import type {AuthMethod} from '../auth/types.js'; |
| 17 | +import {getLogger} from '../logging/logger.js'; |
17 | 18 |
|
18 | 19 | /** |
19 | 20 | * Configuration structure matching dw.json file format. |
@@ -114,35 +115,58 @@ export function findDwJson(startDir: string = process.cwd()): string | undefined |
114 | 115 | * 3. Root-level config |
115 | 116 | */ |
116 | 117 | function selectConfig(json: DwJsonMultiConfig, instanceName?: string): DwJsonConfig { |
| 118 | + const logger = getLogger(); |
| 119 | + |
117 | 120 | // Single config or no configs array |
118 | 121 | if (!Array.isArray(json.configs) || json.configs.length === 0) { |
| 122 | + logger.trace( |
| 123 | + {selection: 'root', instanceName: json.name}, |
| 124 | + `[DwJsonSource] Selected config "${json.name ?? 'root'}" (single config)`, |
| 125 | + ); |
119 | 126 | return json; |
120 | 127 | } |
121 | 128 |
|
122 | 129 | // Find by instance name |
123 | 130 | if (instanceName) { |
124 | 131 | // Check root first |
125 | 132 | if (json.name === instanceName) { |
| 133 | + logger.trace( |
| 134 | + {selection: 'named', instanceName}, |
| 135 | + `[DwJsonSource] Selected config "${instanceName}" by name (root)`, |
| 136 | + ); |
126 | 137 | return json; |
127 | 138 | } |
128 | 139 | // Then check configs array |
129 | 140 | const found = json.configs.find((c) => c.name === instanceName); |
130 | 141 | if (found) { |
| 142 | + logger.trace({selection: 'named', instanceName}, `[DwJsonSource] Selected config "${instanceName}" by name`); |
131 | 143 | return found; |
132 | 144 | } |
133 | 145 | // Instance not found, fall through to other selection methods |
| 146 | + logger.trace( |
| 147 | + {requestedInstance: instanceName}, |
| 148 | + `[DwJsonSource] Named instance "${instanceName}" not found, falling back`, |
| 149 | + ); |
134 | 150 | } |
135 | 151 |
|
136 | 152 | // Find active config |
137 | 153 | if (json.active === false) { |
138 | 154 | // Root is inactive, look for active in configs |
139 | 155 | const activeConfig = json.configs.find((c) => c.active === true); |
140 | 156 | if (activeConfig) { |
| 157 | + logger.trace( |
| 158 | + {selection: 'active', instanceName: activeConfig.name}, |
| 159 | + `[DwJsonSource] Selected config "${activeConfig.name}" by active flag`, |
| 160 | + ); |
141 | 161 | return activeConfig; |
142 | 162 | } |
143 | 163 | } |
144 | 164 |
|
145 | 165 | // Default to root config |
| 166 | + logger.trace( |
| 167 | + {selection: 'root', instanceName: json.name}, |
| 168 | + `[DwJsonSource] Selected config "${json.name ?? 'root'}" (default to root)`, |
| 169 | + ); |
146 | 170 | return json; |
147 | 171 | } |
148 | 172 |
|
@@ -171,19 +195,26 @@ function selectConfig(json: DwJsonMultiConfig, instanceName?: string): DwJsonCon |
171 | 195 | * const config = loadDwJson({ path: './config/dw.json' }); |
172 | 196 | */ |
173 | 197 | export function loadDwJson(options: LoadDwJsonOptions = {}): DwJsonConfig | undefined { |
| 198 | + const logger = getLogger(); |
| 199 | + |
174 | 200 | // If explicit path provided, use it. Otherwise default to ./dw.json (no upward search) |
175 | 201 | const dwJsonPath = options.path ?? path.join(options.startDir || process.cwd(), 'dw.json'); |
176 | 202 |
|
| 203 | + logger.trace({path: dwJsonPath}, '[DwJsonSource] Checking for config file'); |
| 204 | + |
177 | 205 | if (!fs.existsSync(dwJsonPath)) { |
| 206 | + logger.trace({path: dwJsonPath}, '[DwJsonSource] No config file found'); |
178 | 207 | return undefined; |
179 | 208 | } |
180 | 209 |
|
181 | 210 | try { |
182 | 211 | const content = fs.readFileSync(dwJsonPath, 'utf8'); |
183 | 212 | const json = JSON.parse(content) as DwJsonMultiConfig; |
184 | 213 | return selectConfig(json, options.instance); |
185 | | - } catch { |
| 214 | + } catch (error) { |
186 | 215 | // Invalid JSON or read error |
| 216 | + const message = error instanceof Error ? error.message : String(error); |
| 217 | + logger.trace({path: dwJsonPath, error: message}, '[DwJsonSource] Failed to parse config file'); |
187 | 218 | return undefined; |
188 | 219 | } |
189 | 220 | } |
0 commit comments