@@ -3,41 +3,49 @@ import { join } from "node:path";
33import { globby } from "globby" ;
44import type { SiteFile } from "./schema.js" ;
55
6- /**
7- * Reads all files from a site output directory and encodes them as base64.
8- *
9- * @param outputDir - The directory containing built site files (e.g., "dist")
10- * @returns Array of SiteFile objects with relative paths and base64 content
11- *
12- * @example
13- * const files = await readSiteFiles("./dist");
14- * // files = [{ path: "index.html", content: "PGh0bWw+..." }, ...]
15- */
16- export async function readSiteFiles ( outputDir : string ) : Promise < SiteFile [ ] > {
17- // Glob all files (not directories) in the output directory
18- const filePaths = await globby ( "**/*" , {
6+ async function readSiteFile (
7+ outputDir : string ,
8+ relativePath : string
9+ ) : Promise < SiteFile > {
10+ const absolutePath = join ( outputDir , relativePath ) ;
11+ const buffer = await readFile ( absolutePath ) ;
12+ const content = buffer . toString ( "base64" ) ;
13+
14+ await new Promise ( ( resolve ) => setTimeout ( resolve , 20 ) ) ;
15+
16+ return {
17+ path : relativePath ,
18+ content,
19+ } ;
20+ }
21+
22+ export async function getSiteFilePaths ( outputDir : string ) : Promise < string [ ] > {
23+ return await globby ( "**/*" , {
1924 cwd : outputDir ,
2025 onlyFiles : true ,
2126 absolute : false ,
2227 } ) ;
28+ }
2329
24- if ( filePaths . length === 0 ) {
25- return [ ] ;
30+ /**
31+ * Reads site files one by one, yielding each file as it's read.
32+ * Useful for showing progress during file reading.
33+ *
34+ * @param outputDir - The directory containing built site files
35+ * @param filePaths - Array of relative file paths to read
36+ * @yields Each file as it's read
37+ *
38+ * @example
39+ * const paths = await getSiteFilePaths("./dist");
40+ * for await (const file of readSiteFilesStream("./dist", paths)) {
41+ * console.log(`Read: ${file.path}`);
42+ * }
43+ */
44+ export async function * readSiteFilesStream (
45+ outputDir : string ,
46+ filePaths : string [ ]
47+ ) : AsyncGenerator < SiteFile > {
48+ for ( const relativePath of filePaths ) {
49+ yield await readSiteFile ( outputDir , relativePath ) ;
2650 }
27-
28- // Read each file and encode as base64
29- const files = await Promise . all (
30- filePaths . map ( async ( relativePath ) : Promise < SiteFile > => {
31- const absolutePath = join ( outputDir , relativePath ) ;
32- const buffer = await readFile ( absolutePath ) ;
33- const content = buffer . toString ( "base64" ) ;
34-
35- return {
36- path : relativePath ,
37- content,
38- } ;
39- } )
40- ) ;
41-
42- return files ;
4351}
0 commit comments