-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathgulpfile.js
317 lines (291 loc) · 8.96 KB
/
gulpfile.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//获取Node插件和工作路径
let ideModuleDir = "./node_modules/";
let workSpaceDir = "./";
//引用插件模块
let gulp = require(ideModuleDir + "gulp");
let fs = require("fs");
var del = require('del');
let gulpSequence = require(ideModuleDir + 'gulp-sequence');
let sd = require(ideModuleDir + "silly-datetime");
let zip = require(ideModuleDir + 'gulp-zip');
let rename = require(ideModuleDir + "gulp-rename");
var prog = require('child_process');
let publishConfig = null;
let publishPath = `${process.cwd()}/release/web/5006`;
let versionFile = null;
let gameVersion = "1.5.";
let buildId = 1;
let gameId = 5006;
let curDate = null;
//本地build路径
var GULP_PATH = null;
if (process.env.GULP_BUILD) {
GULP_PATH = process.env.GULP_BUILD.split(';');
}
//读取发布配置
function ReadPublishConfig() {
console.log("读取发布配置");
var tempPath = `${process.cwd()}/.laya/pubset.json`;
var readData = fs.readFileSync(tempPath, 'utf8');
if (readData) {
publishConfig = JSON.parse(readData);
console.log("找到发布配置");
} else {
console.log("未找到发布配置");
}
}
//读取版本文件 主要是拿到 index.js 对应的文件名
function ReadVersionFile() {
if (versionFile) {
console.log("已经读取了version.json文件");
return;
}
console.log("读取version.json文件");
var tempPath = `${publishPath}/version.json`;
var readData = fs.readFileSync(tempPath, 'utf8');
if (readData) {
versionFile = JSON.parse(readData);
let indexName = versionFile["index.js"];
console.log("找到index.js : ", indexName);
return indexName;
} else {
console.log("未找到index.js");
return "index.js";
}
}
//每次发布版本都会增加 build ID
function addBuildId(cb) {
var tempPath = `${process.cwd()}/version_config`;
fs.exists(`${tempPath}/buildConfig.json`, function (exists) {
var content;
if (exists) {
let readData = fs.readFileSync(`${tempPath}/buildConfig.json`, 'utf8');
let buildContent = JSON.parse(readData);
buildId = parseInt(buildContent.buildId) + 1;
content = `{\n\t"buildId":"${buildId}"\n}`;
} else {
content = `{\n\t"buildId":"${buildId}"\n}`;
}
console.log("build ID-------------------- :" + buildId);
fs.writeFileSync(`${tempPath}/buildConfig.json`, content);
cb();
});
}
//获取build ID ,一直累加
gulp.task('buildIdTask', function (cb) {
var tempPath = `${process.cwd()}/version_config/`;
fs.exists(`${tempPath}`, function (exists) {
if (exists) {
addBuildId(cb);
} else {
fs.mkdir(`${tempPath}`, function (err) {
if (err) {
console.error("error :" + err);
cb();
} else {
console.log(`创建${gameId}目录成功`);
addBuildId(cb);
}
});
}
});
});
//更改main.js index.js文件名
gulp.task("changeIndexjs", function (cb) {
ReadPublishConfig();
let outpath = publishConfig[0]["outPath"];
publishPath = outpath.replace(/\\/g, '/');
console.log("发布路径: ", publishPath);
var indexName = ReadVersionFile();
console.log("更改main.js里的index.js字段");
var tempPath = `${publishPath}/main.js`;
var readData = fs.readFileSync(tempPath, 'utf8');
if (readData) {
let index = readData.indexOf("index.js");
if (index > 0) {
console.log("查找到index.js字段,并更改");
let changeFile = readData.replace(/index.js/, indexName);
fs.writeFileSync(tempPath, changeFile, 'utf8');
cb();
} else {
console.log("没有查找到index.js字段");
cb();
}
} else {
console.log("未找到mian.js文件");
cb();
}
});
//更改index.html的版本日期
gulp.task('setHtmlVersion', function (cb) {
console.log("更改版本日期");
curDate = new Date();
let path = `${publishPath}/index.html`;
let changeFile = null;
var readData = fs.readFileSync(path, 'utf8');
if (readData) {
let index = readData.indexOf("value=\"123\"");
if (index > 0) {
changeFile = readData.replace(/value=\"123\"/, `value=\"${gameVersion}${sd.format(curDate, 'YYMMDD')}.${buildId}\"`);
console.log("正在写入index.html");
fs.writeFileSync(path, changeFile, 'utf8');
cb();
} else {
//如果找不到直接return 不进行更改文件
console.log("index.html没有找到vale=123");
cb();
}
} else {
console.log(path + '读取文件失败');
cb();
}
});
/**
* 打包zip
*/
gulp.task("pack_zip", function (cb) {
console.log("创建压缩包");
curDate = new Date();
return gulp.src(`${publishPath}/**`)
.pipe(zip(`${gameId}.zip`))
.pipe(gulp.dest(`${publishPath}/`));
});
//删除ZIP包
gulp.task("zip_del", function (cb) {
console.log("删除" + gameId + " zip包");
return del([
`${publishPath}/*.zip`
], cb);
});
/**
* 添加version.json
*/
gulp.task("zip_version", function (cb) {
console.log("创建更新文件version.json");
curDate = new Date();
var content = `{\n\t"version":"${sd.format(curDate, 'YYYY.MM.DD.HH.mm.ss')}",\n\t"index":"${gameId}/index.html"\n}`;
fs.writeFileSync(`${publishPath}/version.json`, content);
cb();
});
//因为打包后和FG大厅里的版本更新文件 version.json 命名冲突。需要更改名字
gulp.task("rename", function () {
console.log("version.json进行更名成gameVersion.json");
return gulp.src(`${publishPath}/version.json`)
.pipe(rename(`${publishPath}/gameVersion.json`))
.pipe(gulp.dest("./"));
});
gulp.task("cleanVersion", ['rename'], function (cb) {
console.log("删除原来的version.json");
return del([
`${publishPath}/version.json`,
], cb);
});
//修改index.js 引入bundle.js 库
function wirteIndexjs(indexName, bundleName) {
let indexjsPath = `${publishPath}/${indexName}`;
let readData = fs.readFileSync(indexjsPath, 'utf8');
console.log("打开: ", indexjsPath);
if (readData) {
let changeFile = readData.replace(/loadLib\(\"js\/bundle.*\.js\"\)/, `loadLib("js/${bundleName}")`);
fs.writeFileSync(indexjsPath, changeFile, 'utf8');
console.log(indexjsPath + " 更改成功");
} else {
console.log(indexjsPath + ' 读取文件失败');
}
}
gulp.task("updateIndexjs", function (cb) {
let bundleName = "";
let indexName = "";
console.log("更新修改index.js引入bundle.js库");
fs.readdir(publishPath + '/js/', function (err, files) {
console.log("查找发布bundle文件");
console.log("err:", err);
if (!err) {
if (files.length > 0) {
bundleName = files[0];
console.log("找到文件: ", bundleName);
fs.readdir(publishPath, function (_err, _files) {
console.log("查找发布目录文件:");
if (!_err) {
for (var index in _files) {
var reg = /index.*\.js/;
if (reg.test(_files[index])) {
console.log("匹配到值:");
indexName = _files[index];
console.log(indexName);
wirteIndexjs(indexName, bundleName);
}
}
}
cb();
});
} else {
console.log("未找到bundle.js文件");
cb();
}
}
});
});
//上传至服务器
gulp.task('upload', function (cb) {
console.log("上传至 dev");
let proPath = process.cwd();
let origin_path = proPath + "/release/web";
var uploadCmd = `${GULP_PATH[1]} /console /command "option batch continue" "option confirm off" "open sftp://by:[email protected]/ -hostkey=""ssh-ed25519 256 5c:a0:0b:0a:82:21:c0:ec:a7:4b:d6:30:2c:e9:28:b1""" "option transfer binary" "rmdir ${gameId}" "put ${origin_path}\\*" "chmod 775 ${gameId}" "exit" `
console.log(uploadCmd);
if (uploadCmd != null) {
try {
var s = prog.execSync(uploadCmd);
console.log(s.toString());
console.log("已经上传至服务器");
} catch (ex) {
console.log(ex);
}
} else {
console.log("uploadCmd is null");
}
cb();
});
gulp.task("clearDist", function (cb) {
console.log("清除dist包");
return del([
`./dist`,
], cb);
});
gulp.task("pdev", ['clearDist'], function (cb) {
console.log("拷贝dist数据包");
return gulp.src(`${publishPath}/**/*`).pipe(gulp.dest(`./dist/${gameId}`));
});
gulp.task("buildPdev", ['pdev'], function (cb) {
console.log("创建dist:");
fs.readdir(`./dist/${gameId}`, function (err, files) {
console.log("遍历文件");
console.log(files);
});
fs.readdir(`./dist/${gameId}` + '/js/', function (err, files) {
console.log("查找bundjs文件");
console.log(files);
cb();
});
});
gulp.task('ci', function () {
console.log("CI自动部署PDEV");
});
//打包更改 并且 上传
gulp.task("default", function (cb) {
gulpSequence('changeIndexjs', 'updateIndexjs', 'cleanVersion', 'buildIdTask', 'setHtmlVersion', 'zip_del', 'pack_zip', 'zip_version', 'upload', function () {
console.log("gulp输出完成!");
})(cb);
});
//打包更改 但是不上传
gulp.task("packnotup", function (cb) {
gulpSequence('changeIndexjs', 'updateIndexjs', 'cleanVersion', 'buildIdTask', 'setHtmlVersion', 'zip_del', 'pack_zip', 'zip_version', function () {
console.log("gulp输出完成!");
})(cb);
});
//Pdev打包 会自动拷贝dist 但是不上传
gulp.task("packpdev", function (cb) {
gulpSequence('changeIndexjs', 'updateIndexjs', 'cleanVersion', 'buildIdTask', 'setHtmlVersion', 'zip_del', 'pack_zip', 'zip_version', 'buildPdev', function () {
console.log("gulp输出完成!");
})(cb);
});