import { mkdir, readdir, readFile, rm, writeFile, chmod, lstat } from "node:fs/promises"; import { dirname, join, resolve, relative } from "node:path"; import { fileURLToPath } from "node:url"; import { deflateRawSync } from "node:zlib"; const root = resolve(dirname(fileURLToPath(import.meta.url)), ".."); const manifest = JSON.parse(await readFile(join(root, "idea-plugin.json"), "utf8")); const stage = join(root, "build", "package"); const archive = join(root, "build", `cline-ai-plugin-${manifest.version}.zip`); await rm(join(root, "build"), { recursive: true, force: true }); await mkdir(stage, { recursive: true }); const entries = []; async function collect(path) { const name = relative(root, path).split("\\").join("/"); const info = await lstat(path); if (info.isSymbolicLink()) throw new Error(`Package source symlink is unsupported: ${name}`); if (info.isDirectory()) { for (const child of (await readdir(path)).sort()) { if (child === ".bin" || child === ".cache" || child === "tests" || child === "test" || child === "__tests__") continue; await collect(join(path, child)); } } else if (info.isFile()) { if (name.endsWith(".map") || name.endsWith(".d.ts") || name.endsWith(".d.mts") || name.endsWith(".tsbuildinfo")) return; const data = await readFile(path); const mode = name === manifest.contributes.agentAdapters[0].command ? 0o100755 : 0o100644; const target = join(stage, name); await mkdir(dirname(target), { recursive: true }); await writeFile(target, data); await chmod(target, mode & 0o777); entries.push({ name, data: deflateRawSync(data, { level: 9 }), length: data.length, crc: crc32(data), mode }); } } for (const name of ["idea-plugin.json", "package.json", "package-lock.json", "README.md", "bin", "src", "node_modules"]) { await collect(join(root, name)); } entries.sort((a, b) => a.name.localeCompare(b.name, "en")); const body = [], directory = []; let offset = 0; for (const entry of entries) { const name = Buffer.from(entry.name, "utf8"); const local = Buffer.alloc(30); local.writeUInt32LE(0x04034b50); local.writeUInt16LE(20, 4); local.writeUInt16LE(0x800, 6); local.writeUInt16LE(8, 8); local.writeUInt16LE(33, 12); local.writeUInt32LE(entry.crc, 14); local.writeUInt32LE(entry.data.length, 18); local.writeUInt32LE(entry.length, 22); local.writeUInt16LE(name.length, 26); const central = Buffer.alloc(46); central.writeUInt32LE(0x02014b50); central.writeUInt16LE(0x314, 4); central.writeUInt16LE(20, 6); central.writeUInt16LE(0x800, 8); central.writeUInt16LE(8, 10); central.writeUInt16LE(33, 14); central.writeUInt32LE(entry.crc, 16); central.writeUInt32LE(entry.data.length, 20); central.writeUInt32LE(entry.length, 24); central.writeUInt16LE(name.length, 28); central.writeUInt32LE((entry.mode * 65536) >>> 0, 38); central.writeUInt32LE(offset, 42); body.push(local, name, entry.data); directory.push(central, name); offset += local.length + name.length + entry.data.length; } if (entries.length > 65535 || offset > 0xffffffff) throw new Error("Package exceeds ZIP v1 limits."); const central = Buffer.concat(directory); const end = Buffer.alloc(22); end.writeUInt32LE(0x06054b50); end.writeUInt16LE(entries.length, 8); end.writeUInt16LE(entries.length, 10); end.writeUInt32LE(central.length, 12); end.writeUInt32LE(offset, 16); await writeFile(archive, Buffer.concat([...body, central, end])); console.log(`Created ${relative(root, archive)} (${entries.length} files); directory install: build/package`); function crc32(data) { let crc = 0xffffffff; for (const byte of data) { crc ^= byte; for (let bit = 0; bit < 8; bit++) crc = (crc >>> 1) ^ (crc & 1 ? 0xedb88320 : 0); } return (crc ^ 0xffffffff) >>> 0; }