-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpack-zip.js
More file actions
77 lines (64 loc) · 2.07 KB
/
Copy pathpack-zip.js
File metadata and controls
77 lines (64 loc) · 2.07 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
const path = require("path");
const fs = require("fs");
const jszip = require("jszip");
const iconFile = path.join(__dirname, "icon.png");
const licenseFile = path.join(__dirname, "LICENSE");
const pluginJSON = path.join(__dirname, "plugin.json");
const distFolder = path.join(__dirname, "dist");
const json = JSON.parse(fs.readFileSync(pluginJSON, "utf8"));
const readmeDotMd = resolveMetadataFile(json.readme, ["readme.md", "README.md"]);
const changelogDotMd = resolveMetadataFile(json.changelogs, [
"changelogs.md",
"changelog.md",
"CHANGELOG.md",
]);
const zip = new jszip();
zip.file("icon.png", fs.readFileSync(iconFile));
zip.file("plugin.json", fs.readFileSync(pluginJSON));
if (fs.existsSync(licenseFile)) {
zip.file("LICENSE", fs.readFileSync(licenseFile));
}
if (readmeDotMd) {
zip.file(json.readme || path.basename(readmeDotMd), fs.readFileSync(readmeDotMd));
}
if (changelogDotMd) {
zip.file(
json.changelogs || path.basename(changelogDotMd),
fs.readFileSync(changelogDotMd),
);
}
loadFile("", distFolder);
zip
.generateNodeStream({ type: "nodebuffer", streamFiles: true })
.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)));
}
});
}
function resolveMetadataFile(configuredPath, fallbacks) {
if (configuredPath) {
const file = path.join(__dirname, configuredPath);
if (!fs.existsSync(file)) {
throw new Error(`Missing plugin metadata file: ${configuredPath}`);
}
return file;
}
for (const fallback of fallbacks) {
const file = path.join(__dirname, fallback);
if (fs.existsSync(file)) return file;
}
return null;
}