revision.js 7.2 KB
/**
 * 版本管理工具类
 * Created by xaviertung on 16/7/25.
 */
var fs = require('fs');
var path = require('path');
var cheerio = require('cheerio');
var Q = require('q');


var _path = __dirname + '/../../package.json';
var indexPagePath = __dirname + '/../../server/views/index.ejs';

/**
 * 版本类型枚举类:
 * MASTER_NO:主版本号
 * BRANCH_NO:分支版本号
 * PATCH_NO:补丁版本号
 * @type {{MASTER_NO: number, BRANCH_NO: number, PATCH_NO: number}}
 */
var VERSION_TYPES = {
    MASTER_NO: 0,
    BRANCH_NO: 1,
    PATCH_NO: 2
}

/**
 * 文件读取
 * @param file
 * @returns {*}
 */
var readFrom = function (file) {
    var deferred = Q.defer();
    fs.readFile(file, 'utf8', function (error, result) {
        if (error) {
            deferred.reject(error.toString().red);
        }
        deferred.resolve(result);
    });

    return deferred.promise;
};


/**
 * 文件写入
 * @param _conetnt
 * @param _path
 */
var writeTo = function (_conetnt, _path) {

    //var ws = fs.createWriteStream(_path, {start: 0});

    var ws = fs.createWriteStream(_path, {start: 0, encoding: 'utf8',flag: 'w'});
    var buffer = new Buffer(_conetnt);

    ws.write(buffer, 'utf8', function (err, buffer) {
        //console.log(arguments);
        //console.log('写入完成,回调函数没有参数')
    })
}

/**
 * 版本升级
 *
 * 版本类型说明:
 * 1、VERSION_TYPES.MASTER_NO:主版本号;
 * 2、VERSION_TYPES.BRANCH_NO:分支版本号;
 * 3、VERSION_TYPES.PATCH_NO:补丁版本号。
 *
 */
var upgradeVersion = function (version_type) {
    readFrom(_path).then(function (config) {
            //console.log(config.toString());
            var configObj = JSON.parse(config);

            var oldVersion = configObj.version;
            var VERSION_STR = oldVersion.split(".");
            var newVersion;
            switch (version_type) {
                case VERSION_TYPES.MASTER_NO :  //主版本升级时,分支版本号、补丁版本号清零;
                    VERSION_STR[VERSION_TYPES.MASTER_NO] = parseInt(VERSION_STR[VERSION_TYPES.MASTER_NO]) + 1;
                    VERSION_STR[VERSION_TYPES.BRANCH_NO] = 0;
                    VERSION_STR[VERSION_TYPES.PATCH_NO] = 0;
                    newVersion = VERSION_STR.join(".");
                    break;
                case VERSION_TYPES.BRANCH_NO :  // 分支版本升级时,补丁版本号清零
                    VERSION_STR[VERSION_TYPES.BRANCH_NO] = parseInt(VERSION_STR[VERSION_TYPES.BRANCH_NO]) + 1;
                    VERSION_STR[VERSION_TYPES.PATCH_NO] = 0;
                    newVersion = VERSION_STR.join(".");
                    break;
                case VERSION_TYPES.PATCH_NO :  // 补丁版本号升级时累加1,未做上限设置
                    VERSION_STR[VERSION_TYPES.PATCH_NO] = parseInt(VERSION_STR[VERSION_TYPES.PATCH_NO]) + 1;
                    newVersion = VERSION_STR.join(".");
                    break;
                default :
                    newVersion = oldVersion;
                    break;
            }

            configObj.version = newVersion;
            //console.log('configObj', configObj);
            writeTo(JSON.stringify(configObj, null, 4), _path)
        }, function (error) {
            console.log(error.toString().red);
        }
    );

}

/**
 * 给index.ejs模板页引用app.js添加版本号
 */
var grantIndexVersion = function () {
    //var _path = __dirname + '/package.json';
    //console.log('_path1', _path);
    readFrom(_path).then(function (config) {
            //console.log(config.toString());
            var configObj = JSON.parse(config);
            var version = configObj.version;

            readFrom(indexPagePath).then(function (html) {
                    console.log(html.toString());

                    $ = cheerio.load(html, {decodeEntities: false});
                    //var script = $('script').html();
                    //console.log('script', script);
                    var oldSrcURL = $('script#krhr').attr('src');
                    var oldSrcURL_STR = oldSrcURL.split('\/');
                    var oldSrc = oldSrcURL_STR[oldSrcURL_STR.length-1];
                    //var oldSrc = $('script#krhr').attr('src');

                    //console.log('new oldSrc url is:', oldSrc);

                    var newSrc;
                    if (oldSrc.indexOf('-') != -1) {
                        var oldSrcStr = oldSrc.split("-");
                        oldSrcStr[1] = version + ".js";
                        newSrc = oldSrcStr.join('-');
                    } else {
                        var oldSrcStr = oldSrc.split(".");
                        oldSrcStr[0] = oldSrcStr[0] + "-" + version;
                        newSrc = oldSrcStr.join(".");
                    }

                    //console.log('new source url is:', newSrc);


                    oldSrcURL_STR[oldSrcURL_STR.length-1] = newSrc;
                    $('script#krhr').attr('src', oldSrcURL_STR.join('\/'));
                    var html1 = $.html();
                    //console.log('html', html1);

                    writeTo(html1, indexPagePath);


                }, function (error) {
                    console.log(error.toString());
                }
            );
        }, function (error) {
            console.log(error.toString());
        }
    );
}

/**
 * 在DEBUG开发模式状态下,执行npm start的时候同时执行该方法,取消index.ejs模板页中引用app-[VERSION-NO]的版本号
 */
var revokeIndexVersion = function () {
    //var _path = __dirname + '/package.json';
    readFrom(_path).then(function (config) {
            //console.log(config.toString());
            var configObj = JSON.parse(config);
            var version = configObj.version;

            //var indexPagePath = __dirname + '/index.html';
            readFrom(indexPagePath).then(function (html) {
                    //console.log(html.toString());

                    $ = cheerio.load(html, {decodeEntities: false});
                    //var script = $('script').html();
                    //console.log('script', script);
                    var oldSrcURL = $('script#krhr').attr('src');
                    var oldSrcURL_STR = oldSrcURL.split('\/');
                    var oldSrc = oldSrcURL_STR[oldSrcURL_STR.length-1];
                    oldSrc = oldSrc.substring(0, oldSrc.length-3);
                    //console.log('oldSrc', oldSrc);
                    var oldSrcStr = oldSrc.split("-");
                    var newSrc = oldSrcStr[0] + ".js";
                    oldSrcURL_STR[oldSrcURL_STR.length-1] = newSrc;
                    $('script#krhr').attr('src', oldSrcURL_STR.join('\/'));
                    var html1 = $.html();
                    //console.log('html', html1);

                    writeTo(html1, indexPagePath);


                }, function (error) {
                    console.log(error.toString());
                }
            );
        }, function (error) {
            console.log(error.toString());
        }
    );
}


var Revision = {
    VERSION_TYPES : VERSION_TYPES,
    readFrom : readFrom,
    writeTo : writeTo,
    upgradeVersion : upgradeVersion,
    grantIndexVersion : grantIndexVersion,
    revokeIndexVersion : revokeIndexVersion
}

module.exports = Revision;