-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy patharchiver.js
57 lines (47 loc) · 1.29 KB
/
archiver.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
'use strict';
const tar = require('tar-fs');
const fs = require('fs');
const zlib = require('zlib');
const path = require('path');
const IGNORE_REGEX = /SOURCES|SPECS|RPMS|SRPMS|\.git/;
const REQUIRED_ENTRIES = [
'package.json',
'node_modules'
];
function getEntries(whitelist) {
if (whitelist.files) {
// Extract the property from the whitelist
const { service, main, files } = whitelist;
const whitelistFromSections = [service];
/*
* It flattens the content of both properties (main and files) in the
* "whitelistFromSections" array.
*/
if (main) {
whitelistFromSections.push(main);
}
if (files) {
whitelistFromSections.push(...files);
}
if (!whitelistFromSections.length) {
return;
}
return REQUIRED_ENTRIES.concat(whitelistFromSections);
}
}
module.exports.compress = async function (source, target, whitelist) {
const gzip = zlib.createGzip();
const ws = fs.createWriteStream(target);
const rs = tar.pack(source, {
ignore: function (name) {
return IGNORE_REGEX.test(path.relative(source, name));
},
entries: getEntries(whitelist)
});
return new Promise((resolve, reject) => {
rs.on('error', reject);
ws.on('error', reject);
ws.on('close', resolve);
rs.pipe(gzip).pipe(ws);
});
};