Read + JSON.parse the lock file; returns undefined on any error so callers can fall through.
(path: string)
| 127 | |
| 128 | /** Read + JSON.parse the lock file; returns undefined on any error so callers can fall through. */ |
| 129 | function readLockContents(path: string): LockContents | undefined { |
| 130 | try { |
| 131 | const raw = readFileSync(path, 'utf8'); |
| 132 | const parsed = JSON.parse(raw) as unknown; |
| 133 | if ( |
| 134 | typeof parsed === 'object' && |
| 135 | parsed !== null && |
| 136 | typeof (parsed as LockContents).pid === 'number' && |
| 137 | typeof (parsed as LockContents).started_at === 'string' && |
| 138 | typeof (parsed as LockContents).port === 'number' |
| 139 | ) { |
| 140 | return parsed as LockContents; |
| 141 | } |
| 142 | return undefined; |
| 143 | } catch { |
| 144 | return undefined; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Read the lock file and return its contents only when it describes a *live* |
no outgoing calls
no test coverage detected