-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchiver.js
130 lines (109 loc) · 3.62 KB
/
archiver.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
124
125
126
127
128
129
130
import fs from "fs";
import path from "path";
import archiver from "archiver";
import { green, blue } from 'colorette';
async function recursiveReadDir(dir, excludedFiles) {
let results = [];
const list = await fs.promises.readdir(dir);
for (const file of list) {
const fullPath = path.join(dir, file);
const stat = await fs.promises.stat(fullPath);
if (stat.isDirectory()) {
if (!excludedFiles.includes(file)) {
results = results.concat(await recursiveReadDir(fullPath, excludedFiles));
}
} else {
if (!shouldExcludeFile(file)) {
results.push(fullPath);
}
}
}
return results;
}
function shouldExcludeFile(file) {
const excludedFiles = [
".git",
".gitignore",
"node_modules",
"package.json",
"lerna-debug.log",
"lerna.json",
"package-lock.json",
"archiver.js",
];
return excludedFiles.some(excludedFile => file === excludedFile || file.includes(excludedFile));
}
function formatSize(size) {
if (size >= 1024 * 1024) {
return (size / (1024 * 1024)).toFixed(2) + " MB";
} else {
return (size / 1024).toFixed(2) + " KB";
}
}
async function archive() {
const folderPath = path.resolve(); // Use __dirname to get the directory of the script
const rootPathName = path.basename(folderPath);
const outputFilename = `${rootPathName}.zip`;
const outputPath = path.resolve("../", outputFilename);
// List of files and directories to exclude
const excludedFiles = [
".git",
".gitignore",
"node_modules",
"package.json",
"lerna-debug.log",
"lerna.json",
"package-lock.json",
"archiver.js",
"composer.json",
"composer.lock"
];
const archive = archiver("zip", {
zlib: { level: 9 } // Configure the compression level
});
const output = fs.createWriteStream(outputPath);
// Listen for close event to know when the archiving is done
output.on("close", function () {
console.log(green(`Zip created successfully as ${outputFilename} `) +
" " +
blue(`Size ${formatSize(archive.pointer())}`));
renameZipFileWithVersion(outputFilename);
});
archive.pipe(output);
const files = await recursiveReadDir(folderPath, excludedFiles);
const totalFiles = files.length;
let processedFiles = 0;
// Listen for the progress event to display the percentage progress
archive.on("progress", function (progressData) {
processedFiles = progressData.entries.processed;
const progress = (processedFiles / totalFiles) * 100;
process.stdout.write(`Archiving progress: ${progress.toFixed(2)}% \r`);
});
for (const file of files) {
const zipPath = path.relative(folderPath, file);
// Add the project name directory within the ZIP archive
const projectName = path.basename(folderPath);
archive.file(file, { name: path.join(projectName, zipPath) });
}
archive.finalize();
}
async function renameZipFileWithVersion(originalFileName) {
let currentFilename = originalFileName;
let packageVersion = "unknown";
const filenameWithoutExtension = currentFilename.replace(/\.[^.]*$/, "");
try {
const packageJson = JSON.parse(await fs.promises.readFile("package.json"));
packageVersion = packageJson.version;
} catch (err) {
console.error("Error reading package.json:", err);
}
const newName = `${filenameWithoutExtension}-v${packageVersion}.zip`;
const newPath = path.resolve("../", newName);
try {
await fs.promises.rename(path.resolve("../", currentFilename), newPath);
console.log(`Zip file renamed to ` + blue(`${newName}`));
} catch (err) {
console.error("Error renaming the zip file:", err);
}
}
archive().catch(console.error);