Created
December 1, 2012 19:41
-
-
Save ahomu/4184449 to your computer and use it in GitHub Desktop.
LiveScriptでCallback Hellに挑む
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
# 本当は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) |
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
// 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)) | |
} | |
}) | |
}) | |
} | |
}) |
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
// アウトプット | |
(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