Created
April 30, 2014 22:31
-
-
Save JonBons/f6679ee4461478387478 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 sys = require("sys"); | |
var async = require('async'); | |
var child_process = require('child_process'); | |
var fs = require('fs'); | |
var path = require('path'); | |
var walk = function(dir, done) { | |
var results = []; | |
fs.readdir(dir, function(err, list) { | |
if (err) return done(err); | |
var pending = list.length; | |
if (!pending) return done(null, results); | |
list.forEach(function(file) { | |
file = dir + '/' + file; | |
fs.stat(file, function(err, stat) { | |
if (stat && stat.isDirectory()) { | |
walk(file, function(err, res) { | |
results = results.concat(res); | |
if (!--pending) done(null, results); | |
}); | |
} else { | |
results.push(file); | |
if (!--pending) done(null, results); | |
} | |
}); | |
}); | |
}); | |
}; | |
var args = process.argv.slice(2); | |
var targetFolder = args[0]; | |
var queue = []; | |
var MAX = 20; | |
var count = 0; | |
function pngOpti_callback() { | |
count -= 1; | |
if (queue.length > 0 && count < MAX) { // get next item in the queue | |
count += 1; | |
var file = queue.shift(); | |
child_process.spawn('Pal2PacE.exe', [file, file.replace('.paa', '.png')]).on('close', function () { | |
console.log('Converted ' + file + ' to png...'); | |
child_process.spawn('optipng.exe', ['-o5', file.replace('.paa', '.png')]).on('close', function( ) { | |
child_process.spawn('Pal2PacE.exe', [file.replace('.paa', '.png'), file]).on('close', function() { | |
console.log('Optimized ' + file + '...'); | |
fs.unlink(file.replace('.paa', '.png')); | |
pngOpti_callback(); | |
}); | |
}); | |
}); | |
} | |
} | |
walk(targetFolder, function (err, files) { | |
if (err) throw err; | |
var paaFiles = files.filter(function (file) { | |
return fs.statSync(file).isFile() && file.indexOf(".paa") != -1; | |
}).sort(function(a, b) { | |
return fs.statSync(a).size - fs.statSync(b).size; | |
}).reverse(); | |
async.forEach(paaFiles, function (file, callback) { | |
if (count < MAX) { | |
count += 1; | |
child_process.spawn('Pal2PacE.exe', [file, file.replace('.paa', '.png')]).on('close', function () { | |
console.log('Converted ' + file + ' to png...'); | |
child_process.spawn('optipng.exe', ['-o5', file.replace('.paa', '.png')]).on('close', function( ) { | |
child_process.spawn('Pal2PacE.exe', [file.replace('.paa', '.png'), file]).on('close', function() { | |
console.log('Optimized ' + file + '...'); | |
fs.unlink(file.replace('.paa', '.png')); | |
pngOpti_callback(); | |
});; | |
}); | |
}); | |
} else { // queue up if we have maxed out our process limit | |
queue.push(file); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment