-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchive.js
37 lines (29 loc) · 1.08 KB
/
archive.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
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const JSZip = require('jszip');
const defaultGlobOptions = { nodir: true, dot: true };
const prepare = pluginConfig => {
const { output, assets, globOptions: customGlobOptions } = pluginConfig;
const globOptions = { ...defaultGlobOptions, ...customGlobOptions };
const zip = new JSZip();
if (!output) {
throw new Error('Output is required');
}
if (!Array.isArray(assets)) {
throw new Error('assets must be an array');
}
assets.forEach(asset => {
const { pattern, relative = '' } = asset instanceof String ? { pattern: asset } : asset;
const matches = glob.sync(pattern, globOptions);
matches.forEach(match => {
zip.file(path.relative(relative, match), fs.readFileSync(match));
});
});
return new Promise(resolve => {
zip.generateNodeStream({ type: 'nodebuffer', streamFiles: true })
.pipe(fs.createWriteStream(output))
.on('finish', resolve);
});
};
module.exports = { prepare };