generated from Acode-Foundation/acode-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpack-zip.js
More file actions
72 lines (59 loc) · 1.8 KB
/
pack-zip.js
File metadata and controls
72 lines (59 loc) · 1.8 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const path = require("path");
const fs = require("fs");
const jszip = require("jszip");
const iconFile = path.join(__dirname, "icon.png");
const pluginJSON = path.join(__dirname, "plugin.json");
const distFolder = path.join(__dirname, "dist");
const json = JSON.parse(fs.readFileSync(pluginJSON, "utf8"));
let readmeDotMd = path.join(__dirname, "readme.md");
let changelogDotMd = path.join(__dirname, "changelog.md");
if (!json.readme) {
readmeDotMd = path.join(__dirname, "readme.md");
if (!fs.existsSync(readmeDotMd)) {
readmeDotMd = path.join(__dirname, "README.md");
}
}
if (!json.changelogs) {
if (!fs.existsSync(changelogDotMd)) {
changelogDotMd = path.join(__dirname, "CHANGELOG.md");
}
if (!fs.existsSync(changelogDotMd)) {
changelogDotMd = path.join(__dirname, "changelog.md");
}
}
// create zip file of dist folder
const zip = new jszip();
zip.file("icon.png", fs.readFileSync(iconFile));
zip.file("plugin.json", fs.readFileSync(pluginJSON));
if (readmeDotMd) {
zip.file("readme.md", fs.readFileSync(readmeDotMd));
}
if (changelogDotMd) {
zip.file("changelog.md", fs.readFileSync(changelogDotMd));
}
loadFile("", distFolder);
zip
.generateNodeStream({
type: "nodebuffer",
streamFiles: true,
compression: "DEFLATE",
compressionOptions: { level: 9 },
})
.pipe(fs.createWriteStream(path.join(__dirname, "plugin.zip")))
.on("finish", () => {
console.log("Plugin plugin.zip written.");
});
function loadFile(root, folder) {
const distFiles = fs.readdirSync(folder);
distFiles.forEach((file) => {
const stat = fs.statSync(path.join(folder, file));
if (stat.isDirectory()) {
zip.folder(file);
loadFile(path.join(root, file), path.join(folder, file));
return;
}
if (!/LICENSE.txt/.test(file)) {
zip.file(path.join(root, file), fs.readFileSync(path.join(folder, file)));
}
});
}