-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
51 lines (49 loc) · 1.45 KB
/
utils.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
function makeZip(output, fileName = 'dist.zip') {
const path = require('path')
const fs = require('fs')
const JSZip = require('jszip');
const zip = new JSZip()
const distPath = path.resolve(output)
const readDir = function (zip, dirPath) {
// 读取dist下的根文件目录
const files = fs.readdirSync(dirPath);
files.forEach(fileName => {
const fillPath = path.join(dirPath, "./", fileName)
const file = fs.statSync(fillPath);
// 如果是文件夹的话需要递归遍历下面的子文件
if (file.isDirectory()) {
const dirZip = zip.folder(fileName);
readDir(dirZip, fillPath);
} else {
// 读取每个文件为buffer存到zip中
zip.file(fileName, fs.readFileSync(fillPath))
}
});
}
const removeExistedZip = () => {
const dest = path.join(distPath, './' + fileName)
if (fs.existsSync(dest)) {
fs.unlinkSync(dest)
}
}
const zipDir = function () {
readDir(zip, distPath);
zip.generateAsync({
type: "nodebuffer", // 压缩类型
compression: "DEFLATE", // 压缩算法
compressionOptions: { // 压缩级别
level: 9
}
}).then(content => {
const dest = path.join(distPath, './' + fileName)
removeExistedZip()
// 把zip包写到硬盘中,这个content现在是一段buffer
fs.writeFileSync(dest, content);
});
}
removeExistedZip()
zipDir(distPath)
}
module.exports = {
makeZip
}