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 fs from "fs"
import path from "path" import path from "path"
import { promisify } from "util" import { promisify } from "util"
const readdir = promisify(fs.readdir)
const copyFile = promisify(fs.copyFile) const copyFile = promisify(fs.copyFile)
import services from "../src/data/services.json" with { type: "json" } 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 backgroundsDir = path.join(import.meta.dirname, "..", "src", "assets", "backgrounds")
const outputDir = path.join(import.meta.dirname, "..", "public", "api", "backgrounds", "v1") 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() { async function buildStaticBackgrounds() {
try { try {
console.log(`Building backgrounds with offset ${weekOffset}`) console.log(`Building backgrounds with offset ${weekOffset}`)
services.forEach(async (service, i) => { services.forEach(async (service, i) => {
;[".avif", ".jpg"].forEach(async (ext) => { ;[".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}`) const destPath = path.join(outputDir, `${service.id}${ext}`)
await copyFile(srcPath, destPath) await copyFile(srcPath, destPath)
console.log(`Copied ${getFilename(srcPath)} to ${getFilename(destPath)}`) console.log(`Copied ${getFilename(srcPath)} to ${getFilename(destPath)}`)

View File

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

View File

Before

Width:  |  Height:  |  Size: 90 KiB

After

Width:  |  Height:  |  Size: 90 KiB

View File

Before

Width:  |  Height:  |  Size: 289 KiB

After

Width:  |  Height:  |  Size: 289 KiB

View File

Before

Width:  |  Height:  |  Size: 84 KiB

After

Width:  |  Height:  |  Size: 84 KiB

View File

Before

Width:  |  Height:  |  Size: 255 KiB

After

Width:  |  Height:  |  Size: 255 KiB

View File

Before

Width:  |  Height:  |  Size: 53 KiB

After

Width:  |  Height:  |  Size: 53 KiB

View File

Before

Width:  |  Height:  |  Size: 194 KiB

After

Width:  |  Height:  |  Size: 194 KiB

View File

Before

Width:  |  Height:  |  Size: 109 KiB

After

Width:  |  Height:  |  Size: 109 KiB

View File

Before

Width:  |  Height:  |  Size: 295 KiB

After

Width:  |  Height:  |  Size: 295 KiB

View File

Before

Width:  |  Height:  |  Size: 98 KiB

After

Width:  |  Height:  |  Size: 98 KiB

View File

Before

Width:  |  Height:  |  Size: 263 KiB

After

Width:  |  Height:  |  Size: 263 KiB

View File

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View File

Before

Width:  |  Height:  |  Size: 145 KiB

After

Width:  |  Height:  |  Size: 145 KiB

View File

Before

Width:  |  Height:  |  Size: 229 KiB

After

Width:  |  Height:  |  Size: 229 KiB

View File

Before

Width:  |  Height:  |  Size: 475 KiB

After

Width:  |  Height:  |  Size: 475 KiB

View File

Before

Width:  |  Height:  |  Size: 122 KiB

After

Width:  |  Height:  |  Size: 122 KiB

View File

Before

Width:  |  Height:  |  Size: 297 KiB

After

Width:  |  Height:  |  Size: 297 KiB

View File

Before

Width:  |  Height:  |  Size: 95 KiB

After

Width:  |  Height:  |  Size: 95 KiB

View File

Before

Width:  |  Height:  |  Size: 251 KiB

After

Width:  |  Height:  |  Size: 251 KiB

View File

Before

Width:  |  Height:  |  Size: 58 KiB

After

Width:  |  Height:  |  Size: 58 KiB

View File

Before

Width:  |  Height:  |  Size: 193 KiB

After

Width:  |  Height:  |  Size: 193 KiB

View File

Before

Width:  |  Height:  |  Size: 79 KiB

After

Width:  |  Height:  |  Size: 79 KiB

View File

Before

Width:  |  Height:  |  Size: 256 KiB

After

Width:  |  Height:  |  Size: 256 KiB

View File

Before

Width:  |  Height:  |  Size: 57 KiB

After

Width:  |  Height:  |  Size: 57 KiB

View File

Before

Width:  |  Height:  |  Size: 172 KiB

After

Width:  |  Height:  |  Size: 172 KiB

View File

Before

Width:  |  Height:  |  Size: 105 KiB

After

Width:  |  Height:  |  Size: 105 KiB

View File

Before

Width:  |  Height:  |  Size: 281 KiB

After

Width:  |  Height:  |  Size: 281 KiB

View File

Before

Width:  |  Height:  |  Size: 131 KiB

After

Width:  |  Height:  |  Size: 131 KiB

View File

Before

Width:  |  Height:  |  Size: 332 KiB

After

Width:  |  Height:  |  Size: 332 KiB

View File

Before

Width:  |  Height:  |  Size: 46 KiB

After

Width:  |  Height:  |  Size: 46 KiB

View File

Before

Width:  |  Height:  |  Size: 172 KiB

After

Width:  |  Height:  |  Size: 172 KiB

View File

Before

Width:  |  Height:  |  Size: 83 KiB

After

Width:  |  Height:  |  Size: 83 KiB

View File

Before

Width:  |  Height:  |  Size: 248 KiB

After

Width:  |  Height:  |  Size: 248 KiB

View File

Before

Width:  |  Height:  |  Size: 45 KiB

After

Width:  |  Height:  |  Size: 45 KiB

View File

Before

Width:  |  Height:  |  Size: 192 KiB

After

Width:  |  Height:  |  Size: 192 KiB

View File

Before

Width:  |  Height:  |  Size: 77 KiB

After

Width:  |  Height:  |  Size: 77 KiB

View File

Before

Width:  |  Height:  |  Size: 219 KiB

After

Width:  |  Height:  |  Size: 219 KiB

View File

Before

Width:  |  Height:  |  Size: 78 KiB

After

Width:  |  Height:  |  Size: 78 KiB

View File

Before

Width:  |  Height:  |  Size: 278 KiB

After

Width:  |  Height:  |  Size: 278 KiB

View File

Before

Width:  |  Height:  |  Size: 32 KiB

After

Width:  |  Height:  |  Size: 32 KiB

View File

Before

Width:  |  Height:  |  Size: 152 KiB

After

Width:  |  Height:  |  Size: 152 KiB

View File

Before

Width:  |  Height:  |  Size: 243 KiB

After

Width:  |  Height:  |  Size: 243 KiB

View File

Before

Width:  |  Height:  |  Size: 444 KiB

After

Width:  |  Height:  |  Size: 444 KiB

View File

Before

Width:  |  Height:  |  Size: 212 KiB

After

Width:  |  Height:  |  Size: 212 KiB

View File

Before

Width:  |  Height:  |  Size: 423 KiB

After

Width:  |  Height:  |  Size: 423 KiB