forked from Acode-Foundation/acode-plugin-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpack-zip.js
More file actions
84 lines (70 loc) · 1.93 KB
/
pack-zip.js
File metadata and controls
84 lines (70 loc) · 1.93 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
73
74
75
76
77
78
79
80
81
82
83
84
import { join } from "path";
import {
readFileSync,
existsSync,
createWriteStream,
readdirSync,
statSync,
} from "fs";
import jszip from "jszip";
export default async function packZip() {
const iconFile = "icon.png";
const pluginJSON = "plugin.json";
const distFolder = "dist";
const json = JSON.parse(readFileSync(pluginJSON, "utf8"));
let readmeDotMd;
let changelogDotMd;
console.log(json.readme, json.changelogs);
if (!json.readme) {
readmeDotMd = "readme.md";
if (!existsSync(readmeDotMd)) {
readmeDotMd = "README.md";
}
}
if (!json.changelogs) {
if (!existsSync(changelogDotMd)) {
changelogDotMd = "CHANGELOG.md";
}
if (!existsSync(changelogDotMd)) {
changelogDotMd = "changelog.md";
}
}
// create zip file of dist folder
const zip = new jszip();
zip.file("icon.png", readFileSync(iconFile));
zip.file("plugin.json", readFileSync(pluginJSON));
if (readmeDotMd) {
zip.file("readme.md", readFileSync(readmeDotMd));
}
if (changelogDotMd) {
zip.file("changelog.md", readFileSync(changelogDotMd));
}
loadFile("", distFolder);
return new Promise((resolve, reject) => {
zip
.generateNodeStream({ type: "nodebuffer", streamFiles: true })
.pipe(createWriteStream("plugin.zip"))
.on("finish", () => {
console.log("plugin.zip created successfully");
resolve();
})
.on("error", (err) => {
console.error("Error creating plugin.zip:", err);
reject(err);
});
});
function loadFile(root, folder) {
const distFiles = readdirSync(folder);
distFiles.forEach((file) => {
const stat = statSync(join(folder, file));
if (stat.isDirectory()) {
zip.folder(file);
loadFile(join(root, file), join(folder, file));
return;
}
if (!/LICENSE.txt/.test(file)) {
zip.file(join(root, file), readFileSync(join(folder, file)));
}
});
}
}