-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (72 loc) · 2.75 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const fs = require('fs')
const path = require('path')
const yazl = require('yazl')
const crypto = require('crypto')
class KcMcPackage {
constructor(opt) {
this.opt = opt
}
apply(compiler) {
const { projectName } = this.opt
compiler.plugin('afterEmit', async (comilation, cb) => {
console.log(
'\n============================打包开始=========================='
)
// 从node-module开始找到根路径
const pre = path.resolve(__dirname, '../../') + '/'
const zipFile = new yazl.ZipFile()
const zipFile2 = new yazl.ZipFile()
for (const name in comilation.assets) {
let source = comilation.assets[name].source()
zipFile.addBuffer(Buffer.from(source), name)
}
zipFile.end()
await new Promise((resolve) => {
zipFile.outputStream.pipe(
fs.createWriteStream(`${pre}dist.zip`).on('close', () => {
const buffer = fs.readFileSync(
`${pre}dist.zip`
)
const hash = crypto.createHash('md5')
hash.update(buffer, 'utf8')
const md5 = hash.digest('hex')
const str = `${md5} ${projectName}.zip`
fs.writeFile(
`${pre}${projectName}.txt`,
str,
function (err) {
if (err) {
// 写入文件失败
} else {
// 写入文件成功
resolve('结束')
}
}
)
})
)
})
await new Promise((resolve) => {
zipFile2.addFile(`${pre}dist.zip`, `${projectName}.zip`)
zipFile2.addFile(
`${pre}${projectName}.txt`,
`${projectName}.txt`
)
zipFile2.end()
zipFile2.outputStream.pipe(
fs
.createWriteStream(
`${pre}${projectName}.zip`
)
.on('close', () => {
console.log('打包完成!')
resolve()
})
)
})
fs.unlink(`${pre}${projectName}.txt`, () => {})
fs.unlink(`${pre}dist.zip`, () => {})
})
}
}
module.exports = KcMcPackage