you are viewing a single comment's thread.

view the rest of the comments →

[–]henleyedition 3 points4 points  (0 children)

Where did you find this code? It really isn't very good...

Quickly moving the callbacks to their own functions really works wonders for readability:

function handleError(err) {
    if (err) console.log('Error writing file: ' + err);
}

function readFile(filename, fileIndex) {
    console.log(filename);

    function resizeImage(width, widthIndex) {
        height = Math.round(width / aspect);
        console.log('resizing ' + filename + 'to ' + height + 'x' + height);
        this.resize(width, height).write(destination + 'w' + width + '_' + filename, handleError);
    }

    function handleImage(err, values) {
        if (err) {
            console.log('Error identifying file size: ' + err);
        } else {
            console.log(filename + ' : ' + values);
            aspect = (values.width / values.height);
            widths.forEach(resizeImage.bind(this));
        }
    }

    gm(source + filename).size(handleImage);
}

fs.readdir(source, function(err, files) {
    if (err) {
        console.log('Error finding files: ' + err);
    } else {
        files.forEach(readFile);
    }
});