const { execSync } = require('child_process'); const fs = require('fs'); const path = require('path'); const xpiName = 'zotero-pdf-copier-renamer.xpi'; const zipName = 'zotero-pdf-copier-renamer.zip'; console.log('Building and packaging Zotero Attachment Copy extension...'); try { // 1. Run the template's build command console.log('Running zotero-plugin build...'); execSync('npx zotero-plugin build', { stdio: 'inherit' }); // 2. Check if build directory exists const buildDir = path.join('.scaffold', 'build', 'addon'); if (!fs.existsSync(buildDir)) { throw new Error('Build output directory not found!'); } // 3. Delete existing zip/xpi files if they exist if (fs.existsSync(xpiName)) { fs.unlinkSync(xpiName); } if (fs.existsSync(zipName)) { fs.unlinkSync(zipName); } // 4. Package build output using bestzip // We want to run bestzip inside the buildDir and output the zip to the project root const absXpiPath = path.resolve(zipName); console.log(`Zipping contents of ${buildDir} to ${absXpiPath}...`); // Read files dynamically and pass them to bestzip to keep them at the root of the zip const files = fs.readdirSync(buildDir); const filesList = files.map(f => `"${f}"`).join(' '); const zipCommand = `npx -y bestzip "${absXpiPath}" ${filesList}`; console.log(`Running: ${zipCommand} in ${buildDir}`); execSync(zipCommand, { cwd: buildDir, stdio: 'inherit' }); // 5. Rename zip to xpi if (fs.existsSync(zipName)) { fs.renameSync(zipName, xpiName); } console.log(`\nSuccessfully built and packaged: ${xpiName}`); console.log('You can now install this file in Zotero via Tools -> Add-ons -> Install Add-on From File...'); } catch (error) { console.error('Build failed:', error.message); process.exit(1); }