Files
integration/website/bin/build-services-backgrounds.mjs
Emmanuel Pelletier e083128b05 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.
2024-05-24 11:02:04 +02:00

46 lines
1.7 KiB
JavaScript

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" }
const args = process.argv.slice(2)
const weekOffset = args[0] && !isNaN(args[0]) ? args[0] * 1 : Math.floor(new Date().getDate() / 7)
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) => {
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)}`)
if (i === 0) {
await copyFile(srcPath, path.join(outputDir, `default${ext}`))
console.log(`Copied ${getFilename(srcPath)} to default${ext}`)
}
})
})
console.log("Backgrounds have been successfully built.")
} catch (error) {
console.error("Error building static backgrounds:", error)
}
}
function getFilename(path) {
return path.split("/").pop()
}
buildStaticBackgrounds()