revision.js
7.2 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
* 版本管理工具类
* 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;