Code coverage report for tasks/utils/bump.js

Statements: 89.39% (59 / 66)      Branches: 79.41% (27 / 34)      Functions: 93.33% (14 / 15)      Lines: 89.39% (59 / 66)      Ignored: none     

All files » tasks/utils/ » bump.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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 1081 1 1 1   1 16 16     1 9     1 7 7     1 14 14 14     1 15 15   15 1   14 14 1   14     1 8 8   8 8   1   7 7 7 7 7 7     1                           1 6 6 6 6 6   6       1 5 5 5 6 6         5     1 5 5 5     5   5         1
var Promise = require('es6-promise').Promise;
var semver = require('semver');
var fs = require('./fs');
var log = require('./log');
 
function Bump(files, opts){
    this.files = files;
    this.options = opts || {};
}
 
Bump.prototype.possibleNewline = function possibleNewline(jsonSting) {
    return (jsonSting.slice(-1) === '\n') ? '\n' : '';
};
 
Bump.prototype.space = function space(json) {
    var match = json.match(/^(?:(\t+)|( +))"/m);
    return match ? (match[1] ? '\t' : match[2].length) : '';
};
 
Bump.prototype.getPreid = function getPreid(version){
    var preid = version.split('-')[1];
    preid = (preid) ? preid.split('.')[0] : 'beta';
    return preid;
};
 
Bump.prototype.bumpVersion = function bumpVersion(version){
    var type = this.options.type || 'patch';
    Iif (type.indexOf('--version=')>-1) {
        type = type.split('--version=')[1];
    } else if (type == 'current'){
        return  semver.valid(version);
    }
    version = semver.inc(version, type, this.getPreid(version)) || semver.valid(type);
    if (!version){
        log.onError('Invalid semVer type: ' + type);
    }
    return version;
};
 
Bump.prototype.updateJson = function updateJson(fileObj){
    var content, contentJSON;
    content = Buffer.isBuffer(fileObj.contents) ? fileObj.contents.toString('utf-8') : fileObj.contents ;
 
    try {
        contentJSON = JSON.parse(content);
    } catch (e) {
        return {err: 'Problem parsing JSON file'};
    }
    this.currentVersion = contentJSON.version;
    this.updatedVersion = this.bumpVersion(this.currentVersion);
    contentJSON.version = this.updatedVersion;
    fileObj.version  = this.updatedVersion;
    fileObj.contents = new Buffer(JSON.stringify(contentJSON, null, void 0 || this.space(content)) + this.possibleNewline(content));
    return fileObj;
};
 
Bump.prototype.updateFile = function updateFile(fileObj){
    if (!this.updatedVersion) {
        log.onError([
            'Please ensure the glob passed to Bump includes a JSON file first.',
            ' * i.e. `package.json`.',
            'This is used to determine the new version.'].join('\n'));
    }
    var replacements = [{
        replace : /("|\/)[0-9]+\.[0-9]+\.[0-9]\-?(?:(?:[0-9A-Za-z-]+\.?)+)?("|\/)/g,
        with: '$1' + this.updatedVersion + '$2'}
    ];
    return fs.replace(fileObj.path, replacements);
};
 
Bump.prototype.updateJsonFile = function updateJsonFile(fileObj) {
    var self = this;
    return new Promise(function(resolve, reject){
        fileObj = self.updateJson(fileObj);
        fileObj.err && reject(fileObj.err);
        !fileObj.err && resolve(fileObj);
    }).then(function(fileObj){
        return fs.write(fileObj);
    }).catch(log.onError);
};
 
Bump.prototype.update = function update(fileObjs){
    var self = this;
    var promises = [];
    fileObjs.forEach(function(fileObj){
        Eif (fileObj.ext == 'json'){
            promises.push(self.updateJsonFile(fileObj));
        } else  {
            promises.push(self.updateFile(fileObj));
        }
    });
    return Promise.all(promises);
};
 
Bump.prototype.run = function run(){
    var self = this;
    return fs.read(this.files).then(function(fileObjs){
        Iif (fileObjs.length === 0 ){
            return log.warn(' * no files found matching: ' + self.files);
        }
        return self.update(fileObjs);
    }).then(function(){
        return self.updatedVersion;
    }).catch(log.onError);
};
 
 
module.exports = Bump;