Skip to main content
Module

x/lume/core/writer.ts

πŸ”₯ Static site generator for Deno πŸ¦•
Very Popular
Go to Latest
File
import { dirname, join } from "../deps/path.ts";import { emptyDir, ensureDir } from "../deps/fs.ts";import { copy } from "../deps/fs_copy.ts";import { concurrent, normalizePath, sha1 } from "./utils.ts";
import type { Page } from "./filesystem.ts";import type Logger from "./logger.ts";
export interface Options { src: string; dest: string; logger: Logger;}
/** * Class to write the generated pages and static files * in the dest folder. */export default class Writer { src: string; dest: string; logger: Logger; #hashes = new Map();
constructor(options: Options) { this.src = options.src; this.dest = options.dest; this.logger = options.logger; }
/** * Save the pages in the dest folder * Returns an array of pages that have been saved */ async savePages(pages: Page[]): Promise<Page[]> { const savedPages: Page[] = [];
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<boolean> { // Ignore empty files if (!page.content) { return false; }
const dest = page.dest.path + page.dest.ext; const hash = await sha1(page.content); const previousHash = this.#hashes.get(dest);
// The page content didn't change if (previousHash === hash) { return false; }
this.#hashes.set(dest, hash);
const src = page.src.path ? page.src.path + (page.src.ext || "") : "(generated)"; this.logger.log(`πŸ”₯ ${dest} <dim>${src}</dim>`);
const filename = join(this.dest, dest); await ensureDir(dirname(filename));
page.content instanceof Uint8Array ? await Deno.writeFile(filename, page.content) : await Deno.writeTextFile(filename, page.content);
return true; }
/** Copy a static file in the dest folder */ async copyFile(from: string, to: string) { const pathFrom = join(this.src, from); const pathTo = join(this.dest, to);
try { await ensureDir(dirname(pathTo)); this.logger.log(`πŸ”₯ ${normalizePath(to)} <dim>${from}</dim>`); return copy(pathFrom, pathTo, { overwrite: true }); } catch { //Ignored } }
/** Empty the dest folder */ async clear() { await emptyDir(this.dest); this.#hashes.clear(); }}