#!/usr/bin/env node
/**
* Copy Astro build assets to WordPress plugin structure
* and generate PHP-readable manifest
*/
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
class WordPressAssetCopier {
constructor() {
this.projectRoot = path.resolve(__dirname, '..');
this.astroDistDir = path.join(this.projectRoot, 'admin/assets/dist');
this.wpAdminDir = path.join(this.projectRoot, 'admin');
this.manifest = {};
this.stats = {
filesCopied: 0,
errors: 0
};
}
async run() {
console.log('🚀 Starting WordPress asset integration...');
try {
// Ensure directories exist
await this.ensureDirectories();
// Parse Astro build output
await this.parseAstroOutput();
// Generate WordPress manifest
await this.generateManifest();
// Copy additional assets
await this.copyStaticAssets();
// Generate asset loader
await this.generateAssetLoader();
console.log(`✅ Successfully processed ${this.stats.filesCopied} files`);
if (this.stats.errors > 0) {
console.warn(`⚠️ ${this.stats.errors} errors encountered`);
}
} catch (error) {
console.error('❌ Asset integration failed:', error.message);
process.exit(1);
}
}
async ensureDirectories() {
const dirs = [
path.join(this.wpAdminDir, 'js'),
path.join(this.wpAdminDir, 'css'),
path.join(this.wpAdminDir, 'images')
];
for (const dir of dirs) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
}
}
async parseAstroOutput() {
if (!fs.existsSync(this.astroDistDir)) {
throw new Error(`Astro dist directory not found: ${this.astroDistDir}`);
}
// Read all files from dist directory
const files = await this.getAllFiles(this.astroDistDir);
for (const file of files) {
const relativePath = path.relative(this.astroDistDir, file);
const ext = path.extname(file).toLowerCase();
// Categorize and process files
if (ext === '.html') {
await this.processHtmlFile(file, relativePath);
} else if (ext === '.js') {
await this.processJsFile(file, relativePath);
} else if (ext === '.css') {
await this.processCssFile(file, relativePath);
} else if (['.png', '.jpg', '.jpeg', '.svg', '.gif'].includes(ext)) {
await this.processImageFile(file, relativePath);
}
}
}
async processHtmlFile(file, relativePath) {
const content = fs.readFileSync(file, 'utf-8');
const pageName = path.basename(relativePath, '.html');
// Extract CSS and JS references
const cssMatches = content.match(/]*href="([^"]*\.css)"[^>]*>/g) || [];
const jsMatches = content.match(/