Created
November 1, 2014 08:19
-
-
Save hakatashi/fdb92c98dbcdf33076a5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Zip = require('node-7z'); | |
var async = require('async'); | |
var fs = require('fs'); | |
var archive = new Zip(); | |
// Polyfill String.prototype.endsWith | |
if (!String.prototype.endsWith) { | |
Object.defineProperty(String.prototype, 'endsWith', { | |
enumerable: false, | |
configurable: false, | |
writable: false, | |
value: function (searchString, position) { | |
position = position || this.length; | |
position = position - searchString.length; | |
return this.lastIndexOf(searchString) === position; | |
} | |
}); | |
} | |
var now = new Date(); | |
// set threshold to the beginning of the month | |
var threshold = new Date(now.getFullYear(), now.getMonth(), 1); | |
var pending = {}; | |
var filenames = fs.readdirSync('.'); | |
filenames.forEach(function(filename) { | |
if (filename.endsWith('.log')) { | |
var params = filename.split('-'); | |
var timestamp = new Date(params[1], params[2] - 1, params[3]); | |
if (timestamp < threshold) { | |
var archivename = params[1] + '-' + params[2] + '.7z'; | |
if (!pending[archivename]) pending[archivename] = []; | |
pending[archivename].push(filename); | |
} | |
} | |
}); | |
for (var archivename in pending) if (pending.hasOwnProperty(archivename)) { | |
var filenames = pending[archivename]; | |
archive.add(archivename, filenames).catch(function (error) { | |
console.error(error); | |
}).then(function () { | |
filenames.forEach(function (filename) { | |
fs.unlink(filename); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment