-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgulpfile.js
More file actions
184 lines (171 loc) · 5.37 KB
/
gulpfile.js
File metadata and controls
184 lines (171 loc) · 5.37 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
const { watch, task, src } = require("gulp");
const { exec } = require("child_process");
const { server, reload } = require("gulp-connect");
const rev = require("gulp-rev-append");
const os = require('os');
const gulpfileConst = require("./.node/gulpfileConst");
const ZIPStart = require("./.node/ZIPSceneJson");
const openServer = require("./.node/openServer");
const setDebugIndexHtml = require("./.node/setDebugIndexHtml");
//是否正在编译
let _ifCompile = false;
//主页地址
let _homePage;
//记录时间
let _time = 0;
//记录自动唤醒编译时间
let _awakeTime = 0;
//创建一个名称为compile的gulp任务
task("compile", function (f) {
f();
console.log('\033[35m', '增量编译。。。', '\033[0m');
//自动编译
compile((code, signal) => {
console.log('\033[35m', '正在创建服务。。。', '\033[0m');
//获取ip
let _ip = getLocalIP();
//主页地址
_homePage = '游戏主页: http://' + _ip + ':' + gulpfileConst.port + gulpfileConst.homepage;
//新建一个服务
server({
name: gulpfileConst.itemName,//项目名字
livereload: gulpfileConst.livereload,//
port: gulpfileConst.port,//端口
root: gulpfileConst.root,//默认路径
host: _ip,
//建立服务完成
serverInit: (_server) => {
//
console.log(' ----▷');
// console.log(' ----▷ 配置参数:\n', gulpfileConst);
console.log(' ----▷');
console.log('\033[34m', '----▶ ' + _homePage, '\033[0m');
console.log(' --');
console.log('\033[33m', ' ----▷ 按 F5 键启动 DeBug浏览器,并打开 游戏主页 地址', '\033[0m');
console.log(' -');
/**
* @ 监听src目录下的所有子目录的所有文件,
* @ //
* @ 监听生效后执行的函数
*/
watch(gulpfileConst.watchFileList, undefined, watchCompile);
//创建一个自动编译
autoCompile();
},
});
});
});
//创建一个名称为webpack的gulp任务
task("webpackCompile", function (f) {
f();
let process = exec("webpack");
//
process.stdout.on("data", (data) => {
console.log(data);
});
process.stderr.on("data", (data) => {
console.log(data);
});
});
/** 监听编译 */
function watchCompile(cb) {
let _onTime = (new Date()).getTime();
console.log('监听到文件变化');
if (_onTime - _time >= gulpfileConst.watchFileDelay) {
console.log('\033[35m', '开始编译。。。', '\033[0m');
_time = _onTime;
_awakeTime = gulpfileConst.autoAwakeCompileTime;
} else {
return;
}
//开始编译
compile((code, signal) => {
console.log(code, signal);
//抛出提示
console.log('\033[34m', '----▶ ' + _homePage, '\033[0m');
if (gulpfileConst.ifAutoUpdate) {
//重新刷新页面
src(gulpfileConst.indexPage)
.pipe(rev())
.pipe(reload());
}
//
cb();
});
}
/** 自动编译 */
function autoCompile() {
if (!gulpfileConst.ifAutoAwakeCompile) {
return;
}
//
_awakeTime = gulpfileConst.autoAwakeCompileTime;
//开启自动唤醒计时器
setInterval(_autoCompile, 100);
}
//配合自动编译
function _autoCompile() {
//判断是否正在编译中
if (_ifCompile) { return; }
_awakeTime -= 100;
// console.log(_awakeTime);
if (_awakeTime <= 0) {
_awakeTime = gulpfileConst.autoAwakeCompileTime;
console.log('\n ----▷ 自动唤醒编译');
compile((code, signal) => {
console.log('\n ----▷ 自动唤醒编译完成');
console.log(code, signal);
//抛出提示
console.log('\033[34m', '----▶ ' + _homePage, '\033[0m');
});
}
}
/** 编译代码 */
function compile(_back) {
if (_ifCompile) {
console.log('\033[31m', '正在编译中。。。', '\033[0m');
return;
}
_ifCompile = true;
//
let process = exec("layaair2-cmd compile");
process.stdout.on("data", (data) => {
console.log(data);
});
process.stderr.on("data", (data) => {
console.log(data);
});
process.on("exit", (code, signal) => {
_ifCompile = false;
console.log('\n ----▷ 编译完成', new Date());
_back(code, signal);
});
}
// 获取局域网ip
function getLocalIP() {
let _ip = 'localhost';
var interfaces = os.networkInterfaces();
for (var devName in interfaces) {
var iface = interfaces[devName];
for (var i = 0; i < iface.length; i++) {
var alias = iface[i];
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
_ip = alias.address;
}
}
}
// console.log(_ip);
return _ip;
}
//创建一个名称为compile的gulp任务
task("httpServer", function (f) {
openServer(f);
});
//创建一个获取本地调式首页内容的任务
task("setDebugIndexHtml", function (f) {
setDebugIndexHtml(f);
});
//压缩场景配置表,当场景配置表过大时就压缩
task("ZIPSceneJson", function (f) {
ZIPStart(f);
});