-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-zip.js
More file actions
80 lines (66 loc) · 1.88 KB
/
create-zip.js
File metadata and controls
80 lines (66 loc) · 1.88 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
const fs = require('fs');
const archiver = require('archiver');
const path = require('path');
// Create a file to stream archive data to
const output = fs.createWriteStream('securenet-platform.zip');
const archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level
});
// Listen for all archive data to be written
output.on('close', function() {
console.log('Archive created successfully!');
console.log(archive.pointer() + ' total bytes');
});
// Good practice to catch warnings (ie stat failures and other non-blocking errors)
archive.on('warning', function(err) {
if (err.code === 'ENOENT') {
console.warn('Warning:', err);
} else {
throw err;
}
});
// Good practice to catch this error explicitly
archive.on('error', function(err) {
throw err;
});
// Pipe archive data to the file
archive.pipe(output);
// Add files and directories, excluding certain patterns
const excludePatterns = [
'node_modules',
'dist',
'.git',
'*.log',
'.env',
'securenet-platform.zip',
'create-zip.js'
];
function shouldExclude(filePath) {
return excludePatterns.some(pattern => {
if (pattern.includes('*')) {
const regex = new RegExp(pattern.replace('*', '.*'));
return regex.test(filePath);
}
return filePath.includes(pattern);
});
}
function addDirectory(dirPath, archivePath = '') {
const items = fs.readdirSync(dirPath);
items.forEach(item => {
const fullPath = path.join(dirPath, item);
const archiveItemPath = archivePath ? path.join(archivePath, item) : item;
if (shouldExclude(fullPath)) {
return;
}
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
addDirectory(fullPath, archiveItemPath);
} else {
archive.file(fullPath, { name: archiveItemPath });
}
});
}
// Add all files from current directory
addDirectory('.');
// Finalize the archive
archive.finalize();