-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexport.js
49 lines (41 loc) · 1.27 KB
/
export.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Usage: node export.js [<filename>]
const walk = require("walk");
const path = require("path");
const JSZip = require("jszip");
const fs = require("fs");
const exportAddon = async (filename) => {
const zip = new JSZip();
const saveZipFileAs = (zip) => { saveZipFile(zip, filename) };
exportFolder(zip, "RP", (zip) => { exportFolder(zip, "BP", saveZipFileAs) });
};
const exportFolder = async (zip, folder, callback) => {
const walker = walk.walk(folder);
walker.on("file", (root, fileStats, next) => {
const filePath = path.join(root, fileStats.name);
fs.readFile(filePath, (err, data) => {
if (err) {
console.log(err);
} else {
zip.file(filePath, data);
};
});
next();
});
walker.on("directory", (root, dirStats, next) => {
const dirPath = path.join(root, dirStats.name);
console.log("Adding files from directory:", dirPath);
next();
});
walker.on("end", () => { callback(zip); });
};
const saveZipFile = (zip, filename) => {
console.log("Saving addon to", filename);
zip.generateAsync({ type: "uint8array" }).then(function (content) {
fs.writeFile(filename, content, (err) => {
if (err) {
console.log(err);
};
});
});
};
exportAddon(process.argv[2] || "Galacticraft.mcaddon");