-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (45 loc) · 1.35 KB
/
index.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
const path = require('path');
const process = require('process');
const fs = require('fs');
const JSZip = require('jszip');
const zip = new JSZip();
const getPaths = (source) => {
const dirs = fs.readdirSync(source);
let arr = [];
for (let i = 0; i < dirs.length; i++) {
const
dirPath = path.join(source, dirs[i]),
stat = fs.statSync(dirPath);
if (stat.isDirectory() && !stat.isFile()) {
arr = arr.concat(getPaths(dirPath));
}
else {
arr.push(dirPath);
}
}
return arr;
};
const buildZip = (source, destination) => {
try {
const
initCwdPath = process.cwd(),
processRootPath = path.join(initCwdPath, source);
process.chdir(processRootPath);
const
foundFilesPaths = getPaths('.'),
root = zip.folder(source);
for (let i = 0; i < foundFilesPaths.length; i++) {
root.file(foundFilesPaths[i], fs.createReadStream(foundFilesPaths[i]));
}
process.chdir(initCwdPath);
return root.generateNodeStream({
type: "nodebuffer",
streamFiles: true,
compression: "DEFLATE"
})
.pipe(fs.createWriteStream(destination));
} catch (e) {
throw new Error(e.message);
}
};
module.exports = buildZip;