(options: StartWatcherOptions = {})
| 187 | * Start watching the xcuserstate file for changes |
| 188 | */ |
| 189 | export async function startXcodeStateWatcher(options: StartWatcherOptions = {}): Promise<boolean> { |
| 190 | if (state.watcher) { |
| 191 | log('debug', '[xcode-watcher] Watcher already running'); |
| 192 | return true; |
| 193 | } |
| 194 | |
| 195 | const executor = options.executor ?? getDefaultCommandExecutor(); |
| 196 | const cwd = options.cwd ?? process.cwd(); |
| 197 | |
| 198 | const xcuserstatePath = await findXcodeStateFile({ |
| 199 | executor, |
| 200 | cwd, |
| 201 | searchRoot: options.searchRoot, |
| 202 | workspacePath: options.workspacePath, |
| 203 | projectPath: options.projectPath, |
| 204 | }); |
| 205 | |
| 206 | if (!xcuserstatePath) { |
| 207 | log('debug', '[xcode-watcher] No xcuserstate file found, watcher not started'); |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | // Initialize cached state |
| 212 | const initialState = extractState(xcuserstatePath); |
| 213 | state.cachedScheme = initialState.scheme; |
| 214 | state.cachedSimulatorId = initialState.simulatorId; |
| 215 | state.watchedPath = xcuserstatePath; |
| 216 | state.executor = executor; |
| 217 | state.cwd = cwd; |
| 218 | state.projectPath = options.projectPath ?? null; |
| 219 | state.workspacePath = options.workspacePath ?? null; |
| 220 | |
| 221 | log( |
| 222 | 'info', |
| 223 | `[xcode-watcher] Starting watcher for ${xcuserstatePath} (scheme="${initialState.scheme}", sim="${initialState.simulatorId}")`, |
| 224 | ); |
| 225 | |
| 226 | state.watcher = watch(xcuserstatePath, { |
| 227 | persistent: true, |
| 228 | ignoreInitial: true, |
| 229 | awaitWriteFinish: { |
| 230 | stabilityThreshold: 100, |
| 231 | pollInterval: 50, |
| 232 | }, |
| 233 | }); |
| 234 | |
| 235 | state.watcher.on('change', () => { |
| 236 | log('debug', '[xcode-watcher] File change detected'); |
| 237 | handleFileChange(); |
| 238 | }); |
| 239 | |
| 240 | state.watcher.on('error', (error: unknown) => { |
| 241 | const message = error instanceof Error ? error.message : String(error); |
| 242 | log('warn', `[xcode-watcher] Watcher error: ${message}`); |
| 243 | }); |
| 244 | |
| 245 | return true; |
| 246 | } |
no test coverage detected