import { access, cp, readdir } from 'fs/promises'; import { constants } from 'fs'; import { join } from 'path'; import { debuglog } from 'util'; import { kFileNotFoundError } from '../constants.js'; const internals = { debuglog: debuglog('blog'), kAssetsDirectoryName: 'assets' }; /** * Generates the static assets required for the blog * * @name StaticGenerator * @param {string} source the source directory * @param {string} target the target directory * @param {Array.} posts the list of posts */ export default async function StaticGenerator(source, target, _) { try { await access(source, constants.R_OK); const entries = await readdir(source, { withFileTypes: true }); for (const entry of entries) { const sourceAsset = join(source, entry.name); const targetAsset = join(target, entry.name); internals.debuglog(`Copying ${sourceAsset} to ${targetAsset}`); if (entry.isDirectory()) { await cp(sourceAsset, targetAsset, { recursive: true }); } else { await cp(sourceAsset, targetAsset); } } } catch (error) { if (error.code === kFileNotFoundError) { internals.debuglog(`No static directory found in ${source}`); return; } throw error; } }