-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
50 lines (43 loc) · 997 Bytes
/
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
/**
* @copyright Maichong Software Ltd. 2016 http://maichong.it
* @date 2016-11-24
* @author Liang <[email protected]>
*/
'use strict';
var archiver = require('archiver');
module.exports = function (file, options) {
var archive = archiver(file, options);
var done;
var error;
var promise;
var onSuccess;
var onError;
archive.on('finish', function () {
done = true;
if (onSuccess) {
onSuccess();
}
});
// good practice to catch this error explicitly
archive.on('error', function (err) {
error = err;
if (onError) {
onError(err);
}
throw err;
});
let finalize = archive.finalize;
archive.finalize = function () {
if (error) return Promise.reject(error);
if (done) return Promise.resolve();
if (!promise) {
promise = new Promise(function (resolve, reject) {
onSuccess = resolve;
onError = reject;
});
finalize.call(archive);
}
return promise;
};
return archive;
};