|
| 1 | +/** |
| 2 | + * scripts/zip.mjs |
| 3 | + * |
| 4 | + * Packages the dist/ folder into a Chrome Web Store–ready ZIP. |
| 5 | + * Output: dist-zip/ai-chat-backup-<version>.zip |
| 6 | + * |
| 7 | + * Usage: |
| 8 | + * node scripts/zip.mjs |
| 9 | + * npm run zip (after adding "zip" to package.json scripts) |
| 10 | + */ |
| 11 | + |
| 12 | +import { execSync } from "node:child_process"; |
| 13 | +import fs from "node:fs"; |
| 14 | +import path from "node:path"; |
| 15 | +import { createReadStream, createWriteStream } from "node:fs"; |
| 16 | +import { pipeline } from "node:stream/promises"; |
| 17 | + |
| 18 | +const __dirname = path.dirname(new URL(import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, "$1")); |
| 19 | +const root = path.resolve(__dirname, ".."); |
| 20 | +const distDir = path.join(root, "dist"); |
| 21 | +const outDir = path.join(root, "dist-zip"); |
| 22 | + |
| 23 | +/* ── read version from built manifest.json ────────────────── */ |
| 24 | +const manifestPath = path.join(distDir, "manifest.json"); |
| 25 | +if (!fs.existsSync(manifestPath)) { |
| 26 | + console.error("ERROR: dist/manifest.json not found. Run `npm run build` first."); |
| 27 | + process.exit(1); |
| 28 | +} |
| 29 | +const { version } = JSON.parse(fs.readFileSync(manifestPath, "utf8")); |
| 30 | + |
| 31 | +/* ── ensure output dir ────────────────────────────────────── */ |
| 32 | +if (!fs.existsSync(outDir)) fs.mkdirSync(outDir, { recursive: true }); |
| 33 | + |
| 34 | +const zipName = `ai-chat-backup-${version}.zip`; |
| 35 | +const zipPath = path.join(outDir, zipName); |
| 36 | + |
| 37 | +/* ── remove stale zip ─────────────────────────────────────── */ |
| 38 | +if (fs.existsSync(zipPath)) fs.rmSync(zipPath); |
| 39 | + |
| 40 | +/* ── zip using platform tools ─────────────────────────────── */ |
| 41 | +const isWindows = process.platform === "win32"; |
| 42 | + |
| 43 | +try { |
| 44 | + if (isWindows) { |
| 45 | + // PowerShell 5+ Compress-Archive (available on all modern Windows) |
| 46 | + const distEscaped = distDir.replace(/'/g, "''"); |
| 47 | + const zipEscaped = zipPath.replace(/'/g, "''"); |
| 48 | + execSync( |
| 49 | + `powershell -NoProfile -Command "Compress-Archive -Path '${distEscaped}\\*' -DestinationPath '${zipEscaped}' -Force"`, |
| 50 | + { stdio: "inherit" } |
| 51 | + ); |
| 52 | + } else { |
| 53 | + // Unix: zip -r |
| 54 | + execSync(`zip -r "${zipPath}" .`, { cwd: distDir, stdio: "inherit" }); |
| 55 | + } |
| 56 | + |
| 57 | + console.log(`\nPackaged: dist-zip/${zipName}`); |
| 58 | + console.log(`Upload this file at: https://chrome.google.com/webstore/developer/dashboard\n`); |
| 59 | +} catch (err) { |
| 60 | + console.error("Packaging failed:", err.message); |
| 61 | + process.exit(1); |
| 62 | +} |
0 commit comments