Code coverage report for lib/tasks/md5.js

Statements: 100% (22 / 22)      Branches: 100% (10 / 10)      Functions: 100% (4 / 4)      Lines: 100% (22 / 22)     

All files » lib/tasks/ » md5.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56            1         1 25             25 3   25     25 25   7 7 1   6 6 6   6 6 6     6 4   2   6       18          
/*
Copyright (c) 2012, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://yuilibrary.com/license/
*/
 
var crypto = require('crypto'),
    fs = require('fs'),
    log = require('../log'),
    exists = require('../util').exists;
 
exports.md5check = function (options, blob, done) {
    var md5sum,
        md5,
        md5sumCurrent,
        md5Current,
        err = null,
        current = options.current,
        end = function () {
            if (!options.error && err) {
                err = null;
            }
            done(err, new blob.constructor(blob.result, blob));
        };
 
    exists(current, function (yes) {
        if (yes) {
            //File exists, check it's md5 before processing
            fs.readFile(current, 'utf8', function (readErr, data) {
                if (readErr) {
                    end(); //Continue Build
                } else {
                    md5sum = crypto.createHash('md5');
                    md5sum.update(blob.result.trim());
                    md5 = md5sum.digest('hex');
 
                    md5sumCurrent = crypto.createHash('md5');
                    md5sumCurrent.update(data.trim());
                    md5Current = md5sumCurrent.digest('hex');
                    //log.log('current: ' + md5Current);
                    //log.log('processing: ' + md5);
                    if (md5 === md5Current) {
                        err = 'file has not changed, bailing the build';
                    } else {
                        log.log('file has changed, continuing build');
                    }
                    end();
                }
            });
        } else {
            end(); //Continue Build
        }
    });
};