Skip to content

Instantly share code, notes, and snippets.

@ryanlewis
Last active December 28, 2015 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanlewis/7486470 to your computer and use it in GitHub Desktop.
Save ryanlewis/7486470 to your computer and use it in GitHub Desktop.
Remove all the ImageGen cache files and directories from a root media folder. npm install underscore q optimist OR npm install underscore q optimist -g
#!/usr/bin/env node
var _ = require('underscore');
var fs = require('fs');
var Q = require('q');
var argv = require('optimist')
.usage('Usage: $0 -base [directory')
.demand(['base'])
.describe('base', 'Media directory')
.argv;
var files = fs.readdirSync(argv.base);
var deleteCacheDir = function(cache) {
var deferred = Q.defer();
fs.exists(cache, function(exists) {
if (exists) {
console.log('Removing ' + cache + '...');
fs.readdir(cache, function(err, cacheFiles) {
_.each(cacheFiles, function(cacheFile) {
cacheFile = cache + '/' + cacheFile;
fs.unlink(cacheFile);
});
fs.rmdir(cache, function() {});
deferred.resolve();
});
} else {
deferred.resolve();
}
});
return deferred.promise;
}
var promises = [];
_.each(files, function(file) {
var cache = argv.base + '/' + file + '/Cached';
promises.push(deleteCacheDir(cache));
});
Q.all(promises).then(function() {
console.log('All done');
}).fail(function(err) {
console.log('error: ' + err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment