import { posix } from "../deps/path.ts"; import { emptyDir, ensureDir } from "../deps/fs.ts"; import { concurrent } from "./utils/concurrent.ts"; import { sha1 } from "./utils/digest.ts"; import { log } from "./utils/log.ts"; import binaryLoader from "./loaders/binary.ts"; import type { Page, StaticFile } from "./file.ts"; export interface Options { dest: string; caseSensitiveUrls: boolean; } /** Generic interface for Writer */ export interface Writer { savePages(pages: Page[]): Promise; copyFiles(files: StaticFile[]): Promise; clear(): Promise; removeFiles(files: string[]): Promise; } /** * Class to write the generated pages and static files * in the dest folder. */ export class FSWriter implements Writer { dest: string; caseSensitiveUrls: boolean; #outputs = new Map(); #saveCount = 0; constructor(options: Options) { this.dest = options.dest; this.caseSensitiveUrls = options.caseSensitiveUrls; } /** * Save the pages in the dest folder * Returns an array of pages that have been saved */ async savePages(pages: Page[]): Promise { const savedPages: Page[] = []; ++this.#saveCount; await concurrent( pages, async (page) => { if (await this.savePage(page)) { savedPages.push(page); } }, ); return savedPages; } /** * Save a page in the dest folder * Returns a boolean indicating if the page has saved */ async savePage(page: Page): Promise { const { sourcePath, outputPath, content } = page; // Ignore empty pages if (!content) { log.warn( `[Lume] Skipped page ${page.data.url} (file content is empty)`, ); return false; } const filename = posix.join(this.dest, outputPath); const id = this.caseSensitiveUrls ? filename : filename.toLowerCase(); const hash = await sha1(content); const previous = this.#outputs.get(id); this.#outputs.set(id, [this.#saveCount, sourcePath, hash]); if (previous) { const [previousCount, previousSourcePath, previousHash] = previous; if (previousCount === this.#saveCount) { log.error( `The pages ${sourcePath} and ${previousSourcePath} have the same output path "${outputPath}". Use distinct 'url' values to resolve the conflict.`, ); } // The page content didn't change if (previousHash === hash) { return false; } } log.info(`🔥 ${page.data.url} <- ${sourcePath}`); await ensureDir(posix.dirname(filename)); content instanceof Uint8Array ? await Deno.writeFile(filename, content) : await Deno.writeTextFile(filename, content); return true; } /** * Copy the static files in the dest folder */ async copyFiles(files: StaticFile[]): Promise { const copyFiles: StaticFile[] = []; await concurrent( files, async (file) => { if (await this.copyFile(file)) { copyFiles.push(file); } }, ); return copyFiles; } /** * Copy a static file in the dest folder * Returns a boolean indicating if the file has saved */ async copyFile(file: StaticFile): Promise { const { entry } = file.src; if (entry.flags.has("saved")) { return false; } entry.flags.add("saved"); const pathTo = posix.join(this.dest, file.outputPath); try { await ensureDir(posix.dirname(pathTo)); if (entry.flags.has("remote")) { await Deno.writeFile( pathTo, (await entry.getContent(binaryLoader)).content as Uint8Array, ); } else { // Copy file https://github.com/denoland/deno/issues/19425 Deno.writeFileSync(pathTo, Deno.readFileSync(entry.src)); } log.info( `🔥 ${file.outputPath} <- ${ entry.flags.has("remote") ? entry.src : entry.path }`, ); return true; } catch (error: unknown) { log.error( // deno-lint-ignore no-explicit-any `Failed to copy file: ${file.outputPath}: ${(error as any).message}`, ); } return false; } /** Empty the dest folder */ async clear() { await emptyDir(this.dest); this.#outputs.clear(); } async removeFiles(files: string[]) { await concurrent( files, async (file) => { try { const outputPath = posix.join(this.dest, file); this.#outputs.delete(outputPath.toLowerCase()); await Deno.remove(outputPath); // Remove empty directories removeEmptyDirectory(outputPath, this.dest); } catch { // Ignored } }, ); } } function removeEmptyDirectory(path: string, base: string) { const dir = posix.dirname(path); try { if (dir !== base) { Deno.removeSync(dir); // Check if the parent directory is also empty removeEmptyDirectory(dir, base); } } catch { // Ignored } }