(options: SyncOptions)
| 237 | } |
| 238 | |
| 239 | export async function syncModels(options: SyncOptions): Promise<SyncResult> { |
| 240 | const { |
| 241 | modelsYamlPath, |
| 242 | write = true, |
| 243 | mode = "append", |
| 244 | maxPerProvider, |
| 245 | providers, |
| 246 | } = options; |
| 247 | const modelsDev = options.modelsDev ?? (await fetchModelsDev()); |
| 248 | |
| 249 | // `replace` mode pretends the file is empty so everything is treated as new. |
| 250 | const existingText = |
| 251 | mode === "replace" ? "" : readFileSync(modelsYamlPath, "utf-8"); |
| 252 | const existing = parseExistingModels(existingText); |
| 253 | const summary = mergeModels(existing, modelsDev, { |
| 254 | maxPerProvider, |
| 255 | providers, |
| 256 | }); |
| 257 | |
| 258 | const addedCount = countEntries(summary.newEntries); |
| 259 | const isFresh = mode === "replace" || existingText.trim() === ""; |
| 260 | const yaml = isFresh |
| 261 | ? renderFresh(summary.newEntries) |
| 262 | : addedCount > 0 |
| 263 | ? insertIntoDocument(existingText, summary.newEntries) |
| 264 | : existingText; |
| 265 | |
| 266 | if (write && (addedCount > 0 || mode === "replace")) { |
| 267 | writeFileSync(modelsYamlPath, yaml); |
| 268 | } |
| 269 | |
| 270 | return { |
| 271 | added: addedCount, |
| 272 | preserved: summary.preservedCount, |
| 273 | yaml, |
| 274 | }; |
| 275 | } |
| 276 | |
| 277 | async function main(): Promise<void> { |
| 278 | try { |
no test coverage detected
searching dependent graphs…