Skip to content

Commit 8292edd

Browse files
committed
primitive zip file exporting
1 parent e17dea7 commit 8292edd

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/datapack_changes.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import JSZip from "jszip";
12
import type { Datapack } from "./datapack";
23

34
interface DatapackChange {
@@ -70,8 +71,46 @@ export class DatapackModifier {
7071
for (const change of this.changeQueue) {
7172
await this.applyChange(change);
7273
}
74+
7375
// Cache with changes created -> write to zip
76+
let packs: {[key: string]: JSZip} = {};
77+
78+
for (const file_path in this.changeCache) {
79+
if (Object.prototype.hasOwnProperty.call(this.changeCache, file_path)) {
80+
const pack_id = file_path.split(":")[0];
81+
82+
if (!(pack_id in packs)) {
83+
packs[pack_id] = new JSZip();
84+
}
85+
86+
packs[pack_id].file(
87+
file_path.split(":")[1],
88+
this.changeCache[file_path]
89+
);
90+
}
91+
else throw new Error("what");
92+
}
93+
7494
console.timeEnd("Applying changes to packs...");
95+
for (const pack in packs) {
96+
if (Object.prototype.hasOwnProperty.call(packs, pack)) {
97+
const zip = packs[pack];
98+
await this.saveFile(zip);
99+
}
100+
}
101+
}
102+
103+
public async saveFile(zip: JSZip) {
104+
await zip.generateAsync({type:"blob"}).then((content) => {
105+
var link = document.createElement("a"), url = URL.createObjectURL(content);
106+
link.href = url; link.download = `modified datapack test.zip`;
107+
document.body.appendChild(link);
108+
link.click();
109+
setTimeout(function() {
110+
document.body.removeChild(link);
111+
window.URL.revokeObjectURL(url);
112+
}, 0);
113+
});
75114
}
76115

77116
//#region ///// FILE CACHE MANIPULATION /////

0 commit comments

Comments
 (0)