migration wip
This commit is contained in:
@@ -31,6 +31,7 @@ if (!BASE_URL || !CONSUMER_KEY || !CONSUMER_SECRET) {
|
||||
const TIMESTAMP = new Date().toISOString().replace(/[:.]/g, '-');
|
||||
const OUTPUT_DIR = path.join(__dirname, '..', 'data', 'raw', TIMESTAMP);
|
||||
const MEDIA_DIR = path.join(__dirname, '..', 'public', 'media');
|
||||
const PUBLIC_DIR = path.join(__dirname, '..', 'public');
|
||||
|
||||
// Create output directories
|
||||
if (!fs.existsSync(OUTPUT_DIR)) {
|
||||
@@ -192,6 +193,36 @@ async function downloadMedia(url, filename) {
|
||||
});
|
||||
}
|
||||
|
||||
async function downloadFavicon(url, filename) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const filePath = path.join(PUBLIC_DIR, filename);
|
||||
|
||||
// Check if file already exists
|
||||
if (fs.existsSync(filePath)) {
|
||||
console.log(`✅ Favicon already exists: ${filename}`);
|
||||
resolve(filePath);
|
||||
return;
|
||||
}
|
||||
|
||||
const file = fs.createWriteStream(filePath);
|
||||
|
||||
https.get(url, (res) => {
|
||||
if (res.statusCode === 200) {
|
||||
res.pipe(file);
|
||||
file.on('finish', () => {
|
||||
console.log(`✅ Downloaded favicon: ${filename}`);
|
||||
resolve(filePath);
|
||||
});
|
||||
} else {
|
||||
reject(new Error(`Failed to download favicon: ${res.statusCode}`));
|
||||
}
|
||||
}).on('error', (err) => {
|
||||
fs.unlink(filePath, () => {});
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Data Processing Functions
|
||||
function extractFeaturedImage(item) {
|
||||
if (item.featured_media) {
|
||||
@@ -545,6 +576,109 @@ async function exportSiteInfo() {
|
||||
return siteInfo;
|
||||
}
|
||||
|
||||
async function exportLogoAndFavicon() {
|
||||
console.log('\n📊 EXPORTING LOGO AND FAVICON');
|
||||
|
||||
const assets = {
|
||||
logo: null,
|
||||
favicon: null,
|
||||
appleTouchIcon: null
|
||||
};
|
||||
|
||||
try {
|
||||
// Get site settings which may include logo and icon IDs
|
||||
const settings = await makeRequest(`${BASE_URL}/wp-json/wp/v2/settings`, buildWordPressAuth());
|
||||
|
||||
// Try to get custom_logo
|
||||
if (settings.custom_logo) {
|
||||
console.log(`📥 Found custom_logo ID: ${settings.custom_logo}`);
|
||||
const logoMedia = await fetchMedia(settings.custom_logo);
|
||||
if (logoMedia && logoMedia.source_url) {
|
||||
const logoFilename = 'logo.webp';
|
||||
await downloadMedia(logoMedia.source_url, logoFilename);
|
||||
assets.logo = `/media/${logoFilename}`;
|
||||
console.log(`✅ Logo downloaded: ${logoFilename}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Try to get site_icon
|
||||
if (settings.site_icon) {
|
||||
console.log(`📥 Found site_icon ID: ${settings.site_icon}`);
|
||||
const iconMedia = await fetchMedia(settings.site_icon);
|
||||
if (iconMedia && iconMedia.source_url) {
|
||||
// Download as favicon.ico
|
||||
const faviconFilename = 'favicon.ico';
|
||||
await downloadFavicon(iconMedia.source_url, faviconFilename);
|
||||
assets.favicon = `/favicon.ico`;
|
||||
console.log(`✅ Favicon downloaded: ${faviconFilename}`);
|
||||
|
||||
// Also create apple-touch-icon.png (same file, different name)
|
||||
const appleTouchFilename = 'apple-touch-icon.png';
|
||||
await downloadFavicon(iconMedia.source_url, appleTouchFilename);
|
||||
assets.appleTouchIcon = `/apple-touch-icon.png`;
|
||||
console.log(`✅ Apple touch icon downloaded: ${appleTouchFilename}`);
|
||||
}
|
||||
}
|
||||
|
||||
// If no logo found in settings, try to find it in media
|
||||
if (!assets.logo) {
|
||||
console.log('⚠️ No logo found in settings, searching media...');
|
||||
// Try to find logo by filename pattern
|
||||
const allMedia = await fetchWithPagination('media', { per_page: 100 });
|
||||
const logoCandidates = allMedia.filter(m =>
|
||||
m.title?.rendered?.toLowerCase().includes('logo') ||
|
||||
m.slug?.toLowerCase().includes('logo') ||
|
||||
m.source_url?.toLowerCase().includes('logo')
|
||||
);
|
||||
|
||||
if (logoCandidates.length > 0) {
|
||||
const logoMedia = logoCandidates[0];
|
||||
const logoFilename = 'logo.webp';
|
||||
await downloadMedia(logoMedia.source_url, logoFilename);
|
||||
assets.logo = `/media/${logoFilename}`;
|
||||
console.log(`✅ Logo found and downloaded: ${logoFilename}`);
|
||||
}
|
||||
}
|
||||
|
||||
// If no favicon found, try to download from common locations
|
||||
if (!assets.favicon) {
|
||||
console.log('⚠️ No favicon found in settings, trying common locations...');
|
||||
const faviconUrls = [
|
||||
`${BASE_URL}/favicon.ico`,
|
||||
`${BASE_URL}/wp-content/uploads/favicon.ico`
|
||||
];
|
||||
|
||||
for (const url of faviconUrls) {
|
||||
try {
|
||||
await downloadFavicon(url, 'favicon.ico');
|
||||
assets.favicon = '/favicon.ico';
|
||||
console.log(`✅ Favicon downloaded from: ${url}`);
|
||||
|
||||
// Also create apple-touch-icon
|
||||
await downloadFavicon(url, 'apple-touch-icon.png');
|
||||
assets.appleTouchIcon = '/apple-touch-icon.png';
|
||||
break;
|
||||
} catch (e) {
|
||||
// Continue to next URL
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Save asset manifest
|
||||
fs.writeFileSync(
|
||||
path.join(OUTPUT_DIR, 'assets.json'),
|
||||
JSON.stringify(assets, null, 2)
|
||||
);
|
||||
|
||||
console.log('✅ Logo and favicon export complete');
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Error exporting logo/favicon:', error.message);
|
||||
}
|
||||
|
||||
return assets;
|
||||
}
|
||||
|
||||
async function generateTranslationMapping() {
|
||||
console.log('\n📊 GENERATING TRANSLATION MAPPING');
|
||||
|
||||
@@ -666,6 +800,7 @@ async function main() {
|
||||
await exportProductCategories();
|
||||
await exportMenus();
|
||||
await exportMedia();
|
||||
await exportLogoAndFavicon();
|
||||
|
||||
// Step 2: Generate mappings and redirects
|
||||
await generateTranslationMapping();
|
||||
@@ -675,6 +810,7 @@ async function main() {
|
||||
console.log('=====================================');
|
||||
console.log(`📁 Data directory: data/raw/${TIMESTAMP}`);
|
||||
console.log(`🖼️ Media directory: public/media/`);
|
||||
console.log(`🎨 Logo/Favicon: public/`);
|
||||
console.log('');
|
||||
console.log('Next steps:');
|
||||
console.log('1. Review exported data for completeness');
|
||||
@@ -701,6 +837,7 @@ module.exports = {
|
||||
exportMenus,
|
||||
exportMedia,
|
||||
exportSiteInfo,
|
||||
exportLogoAndFavicon,
|
||||
generateTranslationMapping,
|
||||
generateRedirects
|
||||
};
|
||||
Reference in New Issue
Block a user