1 | const { isNode } = require('browser-or-node')
|
2 | const sha1 = require('hash.js/lib/hash/sha/1')
|
3 | const JSZip = require('jszip')
|
4 | const versionHash = require('./version-hash')
|
5 |
|
6 | module.exports = async (files) => {
|
7 | const zip = new JSZip()
|
8 | const fileMap = {}
|
9 | await Promise.all(files.map((file) =>
|
10 | new Promise(async (resolve) => {
|
11 | zip.file(file.path, file.data)
|
12 |
|
13 | const fileContentsSha = sha1().update(file.data).digest('hex')
|
14 | const fileMapHash = sha1().update(fileContentsSha).update(file.path).digest('hex')
|
15 | fileMap[fileMapHash] = file.path
|
16 | resolve()
|
17 | })
|
18 | ))
|
19 |
|
20 | return {
|
21 | zip: await zip.generateAsync({ type: isNode ? 'nodebuffer' : 'blob' }),
|
22 | fileMap,
|
23 | versionHash: versionHash(fileMap)
|
24 | }
|
25 | }
|