-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharchive-script.js
More file actions
66 lines (50 loc) · 1.62 KB
/
archive-script.js
File metadata and controls
66 lines (50 loc) · 1.62 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
/**
* The post-build packaging process that makes the package ready for upload.
*
* @author Pihedy
*/
const fs = require('fs');
const path = require('path');
const archiver = require('archiver');
const distFolder = path.resolve(__dirname, 'dist');
const buildFolder = path.resolve(__dirname, 'build');
const outputZip = path.join(buildFolder, 'dist.zip');
if (!fs.existsSync(buildFolder)) {
fs.mkdirSync(buildFolder);
}
const Output = fs.createWriteStream(outputZip);
const Archive = archiver('zip', {
zlib: { level: 9 }
});
Output.on('close', () => {
console.log(`Zip file created: ${outputZip} (${Archive.pointer()} bytes)`);
});
Archive.on('error', (err) => {
throw err;
});
const excludedFiles = ['.gitkeep', 'styles.js'];
/**
* Recursively adds files from the specified directory to the given Archiver instance.
*
* @param {string} dir - The directory path to add files from.
* @param {archiver} Archive - The Archiver instance to add files to.
* @param {string} [base=''] - The base path to use for relative file paths.
*/
function addFilesToArchive(dir, Archive, base = '') {
const files = fs.readdirSync(dir);
files.forEach((file) => {
const filePath = path.join(dir, file);
const relativePath = path.join(base, file);
if (excludedFiles.includes(file)) {
return;
}
if (fs.lstatSync(filePath).isDirectory()) {
addFilesToArchive(filePath, Archive, relativePath);
return;
}
Archive.file(filePath, { name: relativePath });
});
}
Archive.pipe(Output);
addFilesToArchive(distFolder, Archive);
Archive.finalize();