Skip to content

Instantly share code, notes, and snippets.

@ahomu
Created December 1, 2012 19:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahomu/4184449 to your computer and use it in GitHub Desktop.
Save ahomu/4184449 to your computer and use it in GitHub Desktop.
LiveScriptでCallback Hellに挑む
# 本当はLiveScript(.ls)だが、シンタックスハイライトの都合で.coffee
# ファイルを読み込む
err, files <-! fs.readdir source
if err then return console.log 'Error finding files: ' + err
# ファイルリストを回す
filename, fileIndex <-! files.forEach
console.log filename
# ファイルサイズを取得
err, values <-! gm source + filename .size
if err then return console.log 'Error identifying file size: ' + err
console.log filename + ' : ' + values
that = @
aspect = (values.width / values.height)
# widthリストを回す
width, widthIndex <-! widths.forEach
height = Match.round width / aspect
console.log 'resizing ' + filename + 'to ' + height + 'x' + height
# リサイズして
err <-! that.resize width, height .write destination + 'w' + width + '_' + filename
if err then return console.log('Error writing file: ' + err)
// reference of original code
// http://callbackhell.com/
fs.readdir(source, function(err, files) {
if (err) {
console.log('Error finding files: ' + err)
} else {
files.forEach(function(filename, fileIndex) {
console.log(filename)
gm(source + filename).size(function(err, values) {
if (err) {
console.log('Error identifying file size: ' + err)
} else {
console.log(filename + ' : ' + values)
aspect = (values.width / values.height)
widths.forEach(function(width, widthIndex) {
height = Math.round(width / aspect)
console.log('resizing ' + filename + 'to ' + height + 'x' + height)
this.resize(width, height).write(destination + 'w' + width + '_' + filename, function(err) {
if (err) console.log('Error writing file: ' + err)
})
}.bind(this))
}
})
})
}
})
// アウトプット
(function(){
fs.readdir(source, function(err, files){
if (err) {
return console.log('Error finding files: ' + err);
}
files.forEach(function(filename, fileIndex){
console.log(filename);
gm(source + filename).size(function(err, values){
var that, aspect;
if (err) {
return console.log('Error identifying file size: ' + err);
}
console.log(filename + ' : ' + values);
that = this;
aspect = values.width / values.height;
widths.forEach(function(width, widthIndex){
var height;
height = Match.round(width / aspect);
console.log('resizing ' + filename + 'to ' + height + 'x' + height);
that.resize(width, height).write(destination + 'w' + width + '_' + filename, function(err){
if (err) {
return console.log('Error writing file: ' + err);
}
});
});
});
});
});
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment