46 lines
1.2 KiB
JavaScript
46 lines
1.2 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
// Simple script to save products data to the main website's products.json
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
// Path to the main website's products.json
|
|
const mainProductsPath = path.join(__dirname, "..", "data", "products.json");
|
|
|
|
console.log("💾 Saving products to:", mainProductsPath);
|
|
|
|
// Read data from stdin (sent by the admin dashboard)
|
|
let inputData = "";
|
|
process.stdin.on("data", (chunk) => {
|
|
inputData += chunk;
|
|
});
|
|
|
|
process.stdin.on("end", () => {
|
|
try {
|
|
const data = JSON.parse(inputData);
|
|
|
|
// Validate data structure
|
|
if (!data.products || !Array.isArray(data.products)) {
|
|
throw new Error("Invalid data structure: products array is required");
|
|
}
|
|
|
|
// Write to the main website's products.json
|
|
fs.writeFileSync(mainProductsPath, JSON.stringify(data, null, 2));
|
|
|
|
console.log(
|
|
"✅ Successfully saved",
|
|
data.products.length,
|
|
"products to main website"
|
|
);
|
|
process.exit(0);
|
|
} catch (error) {
|
|
console.error("❌ Error saving products:", error.message);
|
|
process.exit(1);
|
|
}
|
|
});
|
|
|
|
// Handle errors
|
|
process.stdin.on("error", (error) => {
|
|
console.error("❌ Error reading input:", error.message);
|
|
process.exit(1);
|
|
});
|