* Read the port from a port file using the filesystem API. * Returns null if the file doesn't exist or contains invalid data.
(filePath: string)
| 140 | * Returns null if the file doesn't exist or contains invalid data. |
| 141 | */ |
| 142 | async function readPortFromFile(filePath: string): Promise<number | null> { |
| 143 | try { |
| 144 | // Dynamic import to get fsOperation |
| 145 | const { default: fsOperation } = await import("fileSystem"); |
| 146 | const fs = fsOperation(filePath); |
| 147 | |
| 148 | // Check if file exists first |
| 149 | const exists = await fs.exists(); |
| 150 | if (!exists) { |
| 151 | return null; |
| 152 | } |
| 153 | |
| 154 | // Read the file content as text |
| 155 | const content = (await fs.readFile("utf-8")) as string; |
| 156 | const port = Number.parseInt(content.trim(), 10); |
| 157 | |
| 158 | if (!Number.isFinite(port) || port <= 0 || port > 65535) { |
| 159 | return null; |
| 160 | } |
| 161 | |
| 162 | return port; |
| 163 | } catch { |
| 164 | // File doesn't exist or couldn't be read |
| 165 | return null; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Get the port for a running LSP server from the axs port file. |
no test coverage detected