|
| 1 | +const path = require('path'); |
| 2 | +const fs = require('fs'); |
| 3 | +const JSZip = require('jszip'); |
| 4 | + |
| 5 | +const rootDir = path.join(__dirname, '..'); |
| 6 | +const distFolder = path.join(rootDir, 'dist'); |
| 7 | +const zip = new JSZip(); |
| 8 | + |
| 9 | +function safeAddFile(zipObj, name, filePath) { |
| 10 | + if (fs.existsSync(filePath)) { |
| 11 | + zipObj.file(name, fs.readFileSync(filePath)); |
| 12 | + console.log(`Added: ${name}`); |
| 13 | + } else { |
| 14 | + console.warn(`Missing: ${filePath}`); |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +// tambahin file wajib di root zip |
| 19 | +safeAddFile(zip, 'icon.png', path.join(rootDir, 'icon.png')); |
| 20 | +safeAddFile(zip, 'plugin.json', path.join(rootDir, 'plugin.json')); |
| 21 | + |
| 22 | +// readme case-insensitive |
| 23 | +let readmePath = path.join(rootDir, 'readme.md'); |
| 24 | +if (!fs.existsSync(readmePath)) { |
| 25 | + readmePath = path.join(rootDir, 'README.md'); |
| 26 | +} |
| 27 | +safeAddFile(zip, 'readme.md', readmePath); |
| 28 | + |
| 29 | +// ambil main.js dari dist langsung di root zip |
| 30 | +safeAddFile(zip, 'main.js', path.join(distFolder, 'main.js')); |
| 31 | + |
| 32 | +// copy folder assets (kalau ada) |
| 33 | +const assetsFolder = path.join(distFolder, 'assets'); |
| 34 | +if (fs.existsSync(assetsFolder)) { |
| 35 | + function addFolder(zipObj, folderPath, relativePath = '') { |
| 36 | + const entries = fs.readdirSync(folderPath); |
| 37 | + entries.forEach((file) => { |
| 38 | + const fullPath = path.join(folderPath, file); |
| 39 | + const stat = fs.statSync(fullPath); |
| 40 | + if (stat.isDirectory()) { |
| 41 | + const subFolder = zipObj.folder(path.join(relativePath, file)); |
| 42 | + addFolder(subFolder, fullPath, path.join(relativePath, file)); |
| 43 | + } else { |
| 44 | + zipObj.file(path.join(relativePath, file), fs.readFileSync(fullPath)); |
| 45 | + } |
| 46 | + }); |
| 47 | + } |
| 48 | + addFolder(zip.folder('assets'), assetsFolder, ''); |
| 49 | +} |
| 50 | + |
| 51 | +// generate fresh zip |
| 52 | +const outPath = path.join(rootDir, 'AI.zip'); |
| 53 | +if (fs.existsSync(outPath)) { |
| 54 | + fs.unlinkSync(outPath); |
| 55 | +} |
| 56 | + |
| 57 | +zip |
| 58 | + .generateNodeStream({ type: 'nodebuffer', streamFiles: true }) |
| 59 | + .pipe(fs.createWriteStream(outPath)) |
| 60 | + .on('finish', () => { |
| 61 | + console.log('✅ AI.zip created cleanly'); |
| 62 | + }); |
0 commit comments