Do not copy hashed files that already exist in dest

This commit is contained in:
Sofía Aritz 2025-04-26 15:01:54 +02:00
parent a60159ad68
commit 36afcaa753
Signed by: sofia
GPG key ID: 5A1485B4CCCDDB4A
2 changed files with 14 additions and 4 deletions

View file

@ -2,7 +2,7 @@ import { createRequire } from "module"
const require = createRequire(import.meta.url)
import { basename, dirname, extname, join, relative } from "path";
import { copyFile, mkdir, readdir, readFile, stat, unlink } from "fs/promises";
import { access, constants, copyFile, mkdir, readdir, readFile, stat, unlink } from "fs/promises";
import { createHash } from "crypto";
import { DateTime } from "luxon";
@ -64,15 +64,23 @@ async function prepareManifest(assetsPath, log = console.log) {
: join(relDir, newBase)
assetsManifest[relative(assetsPath, absPath)] = newRelPath
log(`[sofi-assets] Manifest: '${absPath}' to '${newRelPath}'`)
}
}
async function fileExists(path) {
return await access(path, constants.F_OK).then(() => true, () => false);
}
async function processAssets(assetsPath, outputPath, log = console.log) {
for (const [origRel, newRel] of Object.entries(assetsManifest)) {
const src = join(assetsPath, origRel);
const dest = join(outputPath, newRel);
if (basename(dest, extname(dest)).endsWith("__h") && await fileExists(dest)) {
log(`[sofi-assets] Skipped copying '${src}' because it already exists as '${dest}'`)
continue
}
await mkdir(dirname(dest), { recursive: true })
await copyFile(src, dest);
@ -116,9 +124,11 @@ function dateDesc(a, b) {
export default async function (eleventyConfig) {
const log = eleventyConfig.logger.log.bind(eleventyConfig.logger)
eleventyConfig.addPassthroughCopy({ "assets/robots.txt": "robots.txt" })
eleventyConfig.addWatchTarget("./assets/")
eleventyConfig.on("eleventy.before", async () => {
prepareManifest("assets", log)
await prepareManifest("assets", log)
})
eleventyConfig.on("eleventy.after", async () => {

View file

@ -14,4 +14,4 @@ a/xLBCS5BgMBCAfCfgQYFgoAJhYhBKEVU9yTYwhnuD7hKloUhbTMzdtKBQJneV6n
AhsMBQkDwmcAAAoJEFoUhbTMzdtK72kA/0JKzhX51gN4YPrsCL3Qc3+kDlR0xG4z
kpeXX7gk1Y7ZAQDagUM1PCkb7vT6stQsPuuhdyvXsYcvWtV15uxPbITUAg==
=b5x3
-----END PGP PUBLIC KEY BLOCK----
-----END PGP PUBLIC KEY BLOCK-----