website backgrounds: make it easy to force a bg for a service

rework a bit the backgrounds transformation script so that we can later
easily force a specific background for a specific service. This comes
from a request from france-transfert but they changed their mind in the
end. Felt like the logic is good to keep for later though.
This commit is contained in:
Emmanuel Pelletier
2024-05-24 11:02:04 +02:00
parent 7ec0a278b4
commit e083128b05
46 changed files with 10 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
import fs from "fs"
import path from "path"
import { promisify } from "util"
const readdir = promisify(fs.readdir)
const copyFile = promisify(fs.copyFile)
import services from "../src/data/services.json" with { type: "json" }
@@ -11,12 +12,17 @@ const weekOffset = args[0] && !isNaN(args[0]) ? args[0] * 1 : Math.floor(new Dat
const backgroundsDir = path.join(import.meta.dirname, "..", "src", "assets", "backgrounds")
const outputDir = path.join(import.meta.dirname, "..", "public", "api", "backgrounds", "v1")
let backgrounds = await readdir(backgroundsDir)
backgrounds = backgrounds.filter((bg) => bg.endsWith(".jpg")).map((bg) => bg.replace(".jpg", ""))
async function buildStaticBackgrounds() {
try {
console.log(`Building backgrounds with offset ${weekOffset}`)
services.forEach(async (service, i) => {
;[".avif", ".jpg"].forEach(async (ext) => {
const srcPath = path.join(backgroundsDir, `${weekOffset + i}${ext}`)
let src = backgrounds[weekOffset + i]
// if we want, we can check the service id and force a specific src here
const srcPath = path.join(backgroundsDir, `${src}${ext}`)
const destPath = path.join(outputDir, `${service.id}${ext}`)
await copyFile(srcPath, destPath)
console.log(`Copied ${getFilename(srcPath)} to ${getFilename(destPath)}`)

View File

@@ -23,8 +23,9 @@ async function resizeSourceBackgrounds() {
const backgrounds = await readdir(sourcesDir)
backgrounds.forEach((backgroundFile, i) => {
const srcPath = path.join(sourcesDir, backgroundFile)
const jpegPath = path.join(outputDir, `${i}.jpg`)
const avifPath = path.join(outputDir, `${i}.avif`)
const backgroundName = path.parse(backgroundFile).name
const jpegPath = path.join(outputDir, `${backgroundName}.jpg`)
const avifPath = path.join(outputDir, `${backgroundName}.avif`)
const image = sharp(srcPath)
.resize(1920, 1200, { fit: "cover" })