| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- import fs from "node:fs";
- const root = new URL("../", import.meta.url);
- const read = (n) =>
- JSON.parse(fs.readFileSync(new URL("research/" + n + ".json", root)));
- const base = "https://cms.aducktive.com/panel";
- async function asset(url, name) {
- const path = "/images/" + name;
- const r = await fetch(url, { signal: AbortSignal.timeout(25000) });
- if (!r.ok) throw Error(url + ": " + r.status);
- fs.writeFileSync(
- new URL("public" + path, root),
- Buffer.from(await r.arrayBuffer()),
- );
- return path;
- }
- const raw = read("store-products");
- const products = [];
- for (const p of raw) {
- const f = p.product_flat;
- const images = await Promise.all(
- p.product_images.map((im, i) => asset(im.url, `snack-${p.id}-${i}.webp`)),
- );
- const name = f.name
- .replace(/^(FP |VP |\(NEW\) )/, "")
- .replace(/\s\d+g$/, "")
- .toLowerCase()
- .replace(/\b\w/g, (c) => c.toUpperCase());
- const weight = f.name.match(/\d+g$/)?.[0] || "";
- const category = /TEMPEH/.test(f.name)
- ? "Tempeh chips"
- : /MUSHROOM/.test(f.name)
- ? "Mushroom crisps"
- : /CORNFLAKES|Cornflakes/.test(f.name)
- ? "Cornflakes"
- : "Bundles & gifts";
- products.push({
- id: p.id,
- name,
- originalName: f.name,
- slug: f.urlKey,
- price: Number(f.specialPrice || f.price),
- regularPrice: Number(f.price),
- weight,
- pack: f.name.startsWith("FP")
- ? "Family pack"
- : f.name.startsWith("VP")
- ? "Value pack"
- : f.name.includes("Fun Pack")
- ? "Fun pack"
- : "Bundle",
- category,
- stock: p.inventories?.qty || 0,
- images,
- isNew: f.name.includes("(NEW)"),
- spicy: /spicy/i.test(f.name),
- description: "",
- categories: p.categories.map((c) => c.slug),
- });
- }
- const story = read("story").data.attributes;
- const stories = await Promise.all(
- story.our_milestone.map(async (m, i) => ({
- year: m.year,
- text: m.desc,
- image: await asset(
- base +
- (m.image.data[0].attributes.formats?.medium?.url ||
- m.image.data[0].attributes.url),
- `story-${i}.webp`,
- ),
- })),
- );
- const news = await Promise.all(
- read("news")
- .data.slice(0, 12)
- .map(async (p, i) => {
- let a = p.attributes;
- let img = a.image.data[0]?.attributes;
- return {
- id: p.id,
- title: a.title,
- content: a.content,
- date: a.date,
- category: /retreat|Iftar/i.test(a.title)
- ? "Our people"
- : /MIHAS|AHEX|Bazaar/i.test(a.title)
- ? "Events"
- : "Brand news",
- image: img
- ? await asset(
- base + (img.formats?.medium?.url || img.url),
- `journal-${p.id}.webp`,
- )
- : "",
- };
- }),
- );
- await asset(
- "https://aducktive.com/favicon_io/favicon-32x32.png",
- "favicon.png",
- );
- fs.writeFileSync(
- new URL("src/content.json", root),
- JSON.stringify(
- {
- products,
- story: {
- description: story.desc,
- mission: story.our_mission,
- vision: story.our_vision,
- milestones: stories,
- },
- reviews: read("testimonials").data.attributes.comment.map((r) => ({
- name: r.name,
- message: r.message,
- })),
- social: read("social").data.attributes.social.map((s) => s.link),
- articles: news,
- },
- null,
- 2,
- ),
- );
- console.log(
- "Imported",
- products.length,
- "products and",
- news.length,
- "articles.",
- );
|