| 1 | import fs from 'node:fs' |
| 2 | import path from 'node:path' |
| 3 | import { |
| 4 | enqueueHtmlThumbnails, |
| 5 | type HtmlThumbnailRequest, |
| 6 | type HtmlThumbnailTask |
| 7 | } from '../io/thumbnails/html-thumbnail-service' |
| 8 | |
| 9 | export async function warmStyleThumbnails( |
| 10 | installedRootPath: string, |
| 11 | styles: Array<{ id: string; style: string; source: string; packageDir?: string | null }>, |
| 12 | delayMs = 500 |
| 13 | ): Promise<HtmlThumbnailTask[]> { |
| 14 | const requests: HtmlThumbnailRequest[] = styles.flatMap((style) => { |
| 15 | const packagePath = style.packageDir |
| 16 | ? path.join(installedRootPath, style.packageDir) |
| 17 | : style.source === 'builtin' |
| 18 | ? path.join(installedRootPath, 'system', style.style) |
| 19 | : path.join(installedRootPath, 'user', style.id) |
| 20 | const sourcePath = path.join(packagePath, 'preview.html') |
| 21 | return fs.existsSync(sourcePath) |
| 22 | ? [{ resourceType: 'style', resourceId: style.id, sourcePath }] |
| 23 | : [] |
| 24 | }) |
| 25 | return enqueueHtmlThumbnails(requests, { delayMs }) |
| 26 | } |
| 27 |