UNPKG

@honor-minigame/cli

Version:

honor minigame pack cli

63 lines (52 loc) 1.59 kB
import JSZip from "jszip" import { COMPRESS_OPTS } from "./constant.js" /** * 根据文件列表创建ZIP流 * @param fileList {array} * @param fileList[].path {string} 文件路径 * @param fileList[].content {Buffer|string} 文件内容 * @param comment */ async function createZipBufferFromFileList(fileList, comment = null) { const zipInst = new JSZip() fileList.forEach((fileItem) => { if (!fileItem.path || !fileItem.content) { throw new Error( `### HONOR PACK ### 创建ZIP流时的文件路径或内容不能为空:${fileItem}` ) } zipInst.file(fileItem.path, fileItem.content) }) const zipBuffer = await zipInst.generateAsync({ ...COMPRESS_OPTS, comment, }) return zipBuffer } /** * 根据ZIP流获取文件实例 * @param zipBuffer { ArrayBuffer } */ async function createFileListFromZipBuffer(zipBuffer) { const zipInst = await JSZip.loadAsync(zipBuffer, COMPRESS_OPTS) async function iterator(filePath) { const content = await zipInst.files[filePath].async("nodebuffer") return { path: filePath, content: content, } } const fileList = await Promise.all(Object.keys(zipInst.files).map(iterator)) return { fileList: fileList, comment: zipInst.comment, getFileBuffer(path) { let buffer = fileList.find((item) => item.path === path) if (buffer) { buffer = buffer.content } return buffer }, } } export { createZipBufferFromFileList, createFileListFromZipBuffer }