-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzip.js
45 lines (42 loc) · 1.33 KB
/
zip.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
import fs from 'fs';
import sevenBin from '7zip-bin';
import {add as addToZip} from 'node-7z';
import destroy from 'destroy';
module.exports = function getPromisedZipFilePath(sourceDir) {
return new Promise((resolve, reject) => {
// check if the .zip file already exists next to the sourceDir
const zipName = sourceDir + '.zip';
// if the .zip file does not exist, create it next to the sourceDir and return the path to it
if (shouldGenerateNewZipFile(sourceDir)) {
console.log(`Generating zip-file from: ${sourceDir}`);
if (fs.existsSync(zipName)) {
fs.unlinkSync(zipName);
}
const zipStream = addToZip(zipName, sourceDir, {
$bin: sevenBin.path7za,
recursive: true
});
zipStream.on('end', () => {
console.log(`Zip-file generation complete.`);
destroy(zipStream);
resolve(zipName);
});
zipStream.on('error', reject);
}
else {
resolve(zipName);
}
});
};
export function shouldGenerateNewZipFile(filePath) {
var zipPath = filePath + '.zip';
var zipExists = fs.existsSync(zipPath);
if (!zipExists) {
return true;
}
else {
var folderLastModifiedAtTime = fs.statSync(filePath).mtime;
var zipLastModifiedAtTime = fs.statSync(zipPath).mtime;
return (folderLastModifiedAtTime - zipLastModifiedAtTime) > 0
}
}