(path = credentialPath())
| 118 | } |
| 119 | |
| 120 | export async function readStore(path = credentialPath()): Promise<ReadResult> { |
| 121 | let raw: string; |
| 122 | try { |
| 123 | raw = await fs.readFile(path, "utf8"); |
| 124 | } catch (err) { |
| 125 | if ((err as NodeJS.ErrnoException).code === "ENOENT") { |
| 126 | return { credentials: {}, source: "absent" }; |
| 127 | } |
| 128 | throw ErrInvalidStore(`unable to read ${path}: ${(err as Error).message}`); |
| 129 | } |
| 130 | |
| 131 | const trimmed = raw.trim(); |
| 132 | if (trimmed.length === 0) return { credentials: {}, source: "absent" }; |
| 133 | |
| 134 | if (trimmed.startsWith("{")) { |
| 135 | return { credentials: parseJsonStore(trimmed), source: "file_json" }; |
| 136 | } |
| 137 | |
| 138 | if (looksLikeApiKey(trimmed)) { |
| 139 | return { credentials: { api_key: trimmed }, source: "file_legacy" }; |
| 140 | } |
| 141 | |
| 142 | throw ErrInvalidStore("file is not JSON and does not look like a plain API key"); |
| 143 | } |
| 144 | |
| 145 | export async function writeStore(credentials: Credentials, path = credentialPath()): Promise<void> { |
| 146 | await ensureDir(dirname(path)); |
no test coverage detected