Skip to content

Instantly share code, notes, and snippets.

@ahomu ahomu/input.coffee
Created Dec 1, 2012

Embed
What would you like to do?
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
You can’t perform that action at this time.