-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate-zip.js
executable file
·89 lines (79 loc) · 2.02 KB
/
create-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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
let fs = require('fs'),
path = require('path'),
archiver = require('archiver'),
currDate = new Date(),
utils = require('./utils'),
ignoredFile = utils.getIgnoredFile(currDate);
/**
* @desc Creates a directory if not exists already
* @param {String} dir - path to a directory
* @param {function} cb - callback function
* @returns {function} cb
*/
exports.createDir = function (dir, cb) {
try {
const splitPath = dir.split('/');
splitPath.reduce(function (dirPath, subPath) {
let currentPath;
if (subPath != '.') {
currentPath = dirPath + '/' + subPath;
if (!fs.existsSync(currentPath)) {
fs.mkdirSync(currentPath);
}
} else {
currentPath = subPath;
}
return currentPath;
}, '');
return cb(null);
} catch(err) {
return cb(err);
}
};
/**
* @desc archived log files on every first day of a month
* @param {String} dir - path to a directory
* @param {function} cb - callback function
* @returns {function} cb
*/
exports.archiveLogFiles = function(dir, cb) {
let output = fs.createWriteStream(path.join(dir, currDate.getTime() + '.zip'));
let archive = archiver('zip', {});
let logPath = __dirname + '/logs';
output.on('close', function () {
try {
if (fs.existsSync(logPath)) {
fs.readdirSync(logPath).forEach(function (file) {
let curPath = logPath + '/' + file;
if (!fs.lstatSync(logPath).isFile()) {
if(!(file === ignoredFile || file === 'archive.log')) {
fs.unlinkSync(curPath);
}
}
});
}
return cb(null);
} catch(err) {
return cb(err);
}
});
output.on('end', function () {});
const onWarning = archive.on;
onWarning('warning', function (err) {
if (err.code === 'ENOENT') {
return cb(err);
} else {
return cb(err);
}
});
archive.on('error', function (err) {
return cb(err);
});
archive.pipe(output);
archive
.glob('./logs/**/*', {
ignore: ['./logs/'+ignoredFile, './logs/archive.log']
})
.finalize();
};
/*************************************** END OF FILE *************************************/