const fs = require('fs-extra');
const path = require('path');
async function main() {
try {
console.log('Starting electron build process...');
// Clean directories
const distElectronDir = path.resolve(__dirname, '../dist-electron');
const buildDir = path.resolve(__dirname, '../dist-electron-build');
await fs.emptyDir(distElectronDir);
await fs.emptyDir(buildDir);
console.log('Cleaned directories');
// First build the web app if it doesn't exist
const webDist = path.resolve(__dirname, '../dist');
if (!await fs.pathExists(webDist)) {
console.log('Web dist not found, building web app first...');
throw new Error('Please run \'npm run build\' first to build the web app');
}
// Copy web files to www directory
const wwwDir = path.join(distElectronDir, 'www');
await fs.copy(webDist, wwwDir);
// Fix paths in index.html
const indexPath = path.join(wwwDir, 'index.html');
let indexContent = await fs.readFile(indexPath, 'utf8');
indexContent = indexContent
// Fix absolute paths to be relative
.replace(/src="\//g, 'src="\./')
.replace(/href="\//g, 'href="\./')
// Fix relative asset paths
.replace(/src="\.\.\/assets\//g, 'src="./www/assets/')
.replace(/href="\.\.\/assets\//g, 'href="./www/assets/')
// Fix modulepreload paths specifically
.replace(/]*rel="modulepreload"[^>]*href="(?!\.?\/www\/)(\/\.\/)?assets\//g, ']*href="(?!\.?\/www\/)(\/)?assets\//g, ']*href="(?!\.?\/www\/)(\/\.\/)?assets\//g, ']*href="(?!\.?\/www\/)(\/)?assets\//g, ' {
const items = await fs.readdir(dir);
for (const item of items) {
const fullPath = path.join(dir, item);
const stat = await fs.stat(fullPath);
console.log(`${prefix}${item}${stat.isDirectory() ? '/' : ''}`);
if (stat.isDirectory()) {
await printDir(fullPath, `${prefix} `);
}
}
};
await printDir(distElectronDir);
console.log('\nBuild completed successfully!');
} catch (error) {
console.error('Build failed:', error);
process.exit(1);
}
}
main().catch(console.error);