strapi
This commit is contained in:
64
scripts/migrate-to-strapi.ts
Normal file
64
scripts/migrate-to-strapi.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import matter from 'gray-matter';
|
||||
import axios from 'axios';
|
||||
|
||||
const STRAPI_URL = process.env.STRAPI_URL || 'http://localhost:1337';
|
||||
const STRAPI_TOKEN = process.env.STRAPI_ADMIN_TOKEN; // You'll need to generate this
|
||||
|
||||
async function migrateProducts() {
|
||||
const productsDir = path.join(process.cwd(), 'data/products');
|
||||
const locales = ['de', 'en'];
|
||||
|
||||
for (const locale of locales) {
|
||||
const localeDir = path.join(productsDir, locale);
|
||||
if (!fs.existsSync(localeDir)) continue;
|
||||
|
||||
const files = fs.readdirSync(localeDir).filter(f => f.endsWith('.mdx'));
|
||||
|
||||
for (const file of files) {
|
||||
const filePath = path.join(localeDir, file);
|
||||
const fileContent = fs.readFileSync(filePath, 'utf8');
|
||||
const { data, content } = matter(fileContent);
|
||||
|
||||
console.log(`Migrating ${data.title} (${locale})...`);
|
||||
|
||||
try {
|
||||
// 1. Check if product exists (by SKU)
|
||||
const existing = await axios.get(`${STRAPI_URL}/api/products?filters[sku][$eq]=${data.sku}&locale=${locale}`, {
|
||||
headers: { Authorization: `Bearer ${STRAPI_TOKEN}` }
|
||||
});
|
||||
|
||||
const productData = {
|
||||
title: data.title,
|
||||
sku: data.sku,
|
||||
description: data.description,
|
||||
application: data.application,
|
||||
content: content,
|
||||
technicalData: data.technicalData || {}, // This might need adjustment based on how it's stored in MDX
|
||||
locale: locale,
|
||||
};
|
||||
|
||||
if (existing.data.data.length > 0) {
|
||||
// Update
|
||||
const id = existing.data.data[0].id;
|
||||
await axios.put(`${STRAPI_URL}/api/products/${id}`, { data: productData }, {
|
||||
headers: { Authorization: `Bearer ${STRAPI_TOKEN}` }
|
||||
});
|
||||
console.log(`Updated ${data.title}`);
|
||||
} else {
|
||||
// Create
|
||||
await axios.post(`${STRAPI_URL}/api/products`, { data: productData }, {
|
||||
headers: { Authorization: `Bearer ${STRAPI_TOKEN}` }
|
||||
});
|
||||
console.log(`Created ${data.title}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error migrating ${data.title}:`, error.response?.data || error.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Note: This script requires a running Strapi instance and an admin token.
|
||||
// migrateProducts();
|
||||
33
scripts/strapi-sync.sh
Executable file
33
scripts/strapi-sync.sh
Executable file
@@ -0,0 +1,33 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Script to sync Strapi data between environments
|
||||
# Usage: ./scripts/strapi-sync.sh [export|import] [filename]
|
||||
|
||||
COMMAND=$1
|
||||
FILENAME=$2
|
||||
|
||||
if [ -z "$COMMAND" ]; then
|
||||
echo "Usage: $0 [export|import] [filename]"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ "$COMMAND" == "export" ]; then
|
||||
if [ -z "$FILENAME" ]; then
|
||||
FILENAME="strapi-export-$(date +%Y%m%d%H%M%S).tar.gz"
|
||||
fi
|
||||
echo "Exporting Strapi data to $FILENAME..."
|
||||
docker-compose exec cms npm run strapi export -- --no-encrypt -f "$FILENAME"
|
||||
docker cp $(docker-compose ps -q cms):/opt/app/$FILENAME .
|
||||
echo "Export complete: $FILENAME"
|
||||
fi
|
||||
|
||||
if [ "$COMMAND" == "import" ]; then
|
||||
if [ -z "$FILENAME" ]; then
|
||||
echo "Please specify a filename to import"
|
||||
exit 1
|
||||
fi
|
||||
echo "Importing Strapi data from $FILENAME..."
|
||||
docker cp $FILENAME $(docker-compose ps -q cms):/opt/app/$FILENAME
|
||||
docker-compose exec cms npm run strapi import -- -f "$FILENAME" --force
|
||||
echo "Import complete"
|
||||
fi
|
||||
Reference in New Issue
Block a user