-
Notifications
You must be signed in to change notification settings - Fork 0
/
packager.js
123 lines (100 loc) · 3.03 KB
/
packager.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const fs = require("fs");
const { c } = require("tar");
function pack(folder, file) {
const files = listFiles(folder);
c(
{
gzip: { level: 9 },
file: file,
},
files
);
}
function listFiles(folder) {
let files = [];
fs.readdirSync(folder).forEach((file) => {
const absolutePath = folder + "/" + file;
if (fs.statSync(absolutePath).isDirectory()) {
const filesFromNestedFolder = listFiles(absolutePath);
filesFromNestedFolder.forEach((file) => {
files.push(file);
});
} else {
files.push(absolutePath);
}
});
return files;
}
function packFiles() {
pack("renderer", "renderer.tgz");
return;
}
const path = require("path");
const crypto = require("crypto");
async function calculateChecksum(filePath) {
return new Promise((resolve, reject) => {
const hash = crypto.createHash("sha256");
const stream = fs.createReadStream(filePath);
stream.on("data", (data) => {
hash.update(data);
});
stream.on("end", () => {
const checksum = hash.digest("hex");
resolve(checksum);
});
stream.on("error", (error) => {
reject(error);
});
});
}
async function generateChecksums(folders) {
const checksums = {};
async function traverseFolder(folderPath, currentObject) {
try {
const entries = await fs.promises.readdir(folderPath, { withFileTypes: true });
for (const entry of entries) {
const entryPath = path.join(folderPath, entry.name);
if (entry.isDirectory()) {
const folderObject = {};
currentObject[entry.name] = folderObject;
await traverseFolder(entryPath, folderObject);
} else if (entry.isFile()) {
const checksum = await calculateChecksum(entryPath);
currentObject[entry.name] = checksum;
}
}
} catch (error) {
console.error("Error traversing folder:", error);
}
}
for (const folder of folders) {
const folderName = path.basename(folder);
checksums[folderName] = {};
await traverseFolder(folder, checksums[folderName]);
}
return checksums;
}
function copyFoldersRecursive(sourceFolders, targetFolder) {
if (!fs.existsSync(targetFolder)) {
fs.mkdirSync(targetFolder, { recursive: true });
}
for (const sourceFolder of sourceFolders) {
const targetFolderPath = targetFolder + "/" + sourceFolder.split("/").slice(-1)[0];
if (!fs.existsSync(targetFolderPath)) {
fs.mkdirSync(targetFolderPath, { recursive: true });
}
const files = fs.readdirSync(sourceFolder);
for (const file of files) {
const sourceFilePath = sourceFolder + "/" + file;
const targetFilePath = targetFolderPath + "/" + file;
if (fs.lstatSync(sourceFilePath).isDirectory()) {
copyFoldersRecursive([sourceFilePath], targetFolderPath);
} else {
fs.copyFileSync(sourceFilePath, targetFilePath);
}
}
}
}
const sourceFolders = ["renderer", "app", "saved"];
const targetFolder = "build/Homunculus";
copyFoldersRecursive(sourceFolders, targetFolder);