-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgulpfile.js
More file actions
261 lines (221 loc) · 5.75 KB
/
gulpfile.js
File metadata and controls
261 lines (221 loc) · 5.75 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
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
var gulp = require("gulp");
const crypto = require("crypto");
var ts = require("gulp-typescript");
var concat = require("gulp-concat");
var uglify = require("gulp-terser");
var cleanCSS = require("gulp-clean-css");
var rename = require("gulp-rename");
var showdown = require('showdown')
var MdConverter = new showdown.Converter({tables:true,strikethrough:true })
var tsProject = ts.createProject("tsconfig.json");
const fs = require("fs");
const buidTime = new Date().toISOString();
const buidMode = process.argv[2] === "dis" ? "Release" : "DEBUG";
var HASHVALUE = "--";
function beijingtime() {
return new Date(Date.now() + 3600000 * 8)
.toISOString()
.replace("T", " ")
.replace("Z", " +0800");
}
function getugliyconfig() {
const global_defs = {
__BUILD_TIME__: beijingtime(),
__BUILD_MOD__: `${buidMode} ${HASHVALUE || ""}`,
};
const ugliyconfigDebug = {
mangle: false,
output: {
beautify: true,
comments: "all",
// ascii_only:true
},
compress: {
// defaults:false,
// dead_code:true,
// conditionals:true,
global_defs: {
__DEBUG__: true,
...global_defs,
},
},
};
const ugliyconfigRelease = {
mangle: false,
output: {
beautify: true,
ascii_only: true,
},
compress: {
defaults: true,
dead_code: true,
conditionals: true,
global_defs: {
__DEBUG__: false,
...global_defs,
},
pure_funcs: ["console.log"],
},
};
console.log("global_defs",global_defs)
var ugliyconfig =
process.argv[2] == "dev" ? ugliyconfigDebug : ugliyconfigRelease;
return ugliyconfig;
}
gulp.task("build", function () {
return tsProject.src().pipe(tsProject()).pipe(gulp.dest("tmp"));
});
gulp.task("cpjs", function () {
return gulp.src("./static/*.js").pipe(gulp.dest("tmp"));
});
gulp.task("cpfile", function () {
return gulp.src("./static/*.wasm").pipe(gulp.dest("www/js"));
});
gulp.task("copystatic", gulp.parallel(["cpfile", "cpjs"]));
function clean(cb) {
let rm = fs.rm ? fs.rm : fs.rmdir;
console.log("clean",rm);
try {
rm("./tmp", () => {
console.log("clean3");
if(fs.existsSync('./www/js')){
rm("./www/js", (e) => {
console.log("clean3333");
console.log(e);
cb();
});
}else{
cb()
}
});
} catch (error) {
console.log(error);
}
}
gulp.task("clear", clean);
gulp.task("combinejs", async function (cb) {
// fs.renameSync("www/js/wasm_gzip_bg.wasm", "www/js/tool_bg.wasm");
return gulp
.src(["./tmp/!(index).js"])
.pipe(concat("tool.js"))
.pipe(uglify(getugliyconfig()))
.pipe(gulp.dest("www/js"));
});
gulp.task("indexjs", async function (cb) {
return gulp
.src(["./tmp/index.js"])
.pipe(uglify(getugliyconfig()))
.pipe(gulp.dest("www/js"));
});
gulp.task("inlineHtml", function (cb) {
try {
const htmlPath = "./www/index.html";
const cssPath = "./www/css/style.min.css";
const toolJsPath = "./www/js/tool.js";
const indexJsPath = "./www/js/index.js";
let html = fs.readFileSync(htmlPath, "utf8");
const css = fs.readFileSync(cssPath, "utf8");
const toolJs = fs.readFileSync(toolJsPath, "utf8");
const indexJs = fs.readFileSync(indexJsPath, "utf8");
html = html.replace(
/<link\s+rel="stylesheet"\s+type="text\/css"\s+href="css\/style\.min\.css"\s*\/>/,
`<style>\n${css}\n</style>`
);
html = html.replace(
/<script\s+type="module"\s+src="js\/tool\.js"><\/script>/,
`<script type="module">\n${toolJs}\n</script>`
);
html = html.replace(
/<script\s+type="module"\s+src="js\/index\.js"><\/script>/,
`<script type="module">\n${indexJs}\n</script>`
);
fs.writeFileSync(htmlPath, html);
cb();
} catch (error) {
cb(error);
}
});
gulp.task("removetest", function (cb) {
fs.unlink("./tmp/test.js", () => {
cb();
});
});
gulp.task("cssmin", function () {
return gulp
.src("css/*.css")
.pipe(cleanCSS())
.pipe(rename({ suffix: ".min" }))
.pipe(gulp.dest("www/css/"));
});
gulp.task("genReadMe", function (cb) {
let htmlBody = MdConverter.makeHtml (fs.readFileSync("./README.md").toString());
let html = `
<html>
<style>
${fs.readFileSync("./css/readme.css").toString()}
</style>
${htmlBody}
`
fs.writeFileSync("./www/README.html", html);
cb();
});
gulp.task("genHashFile", function () {
var files = fs.readdirSync("tmp");
files = files.sort((a, b) => {
return a < b ? -1 : a > b ? 1 : 0;
});
files = files.map((e) => {
if (/.*js$/.test(e) || /.*wasm$/.test(e)) {
return "tmp/" + e;
}
}).filter(e=>e);
// files.push("static/wasm_gzip_bg.wasm");
files.push("www/index.html");
console.log(files)
return gulp.src(files).pipe(concat("md5")).pipe(gulp.dest("tmp"));
});
gulp.task("calHash", function (cb) {
let md5file = fs.readFileSync("tmp/md5");
let sha256 = crypto.createHash("sha256");
let b64 = sha256.update(md5file).digest("binary");
sha256 = crypto.createHash("sha256");
HASHVALUE = sha256.update(b64).digest("hex").substring(0,8);
console.log('HASHVALUE',HASHVALUE)
try {
let commit = fs.readFileSync('hash.txt').toString()
HASHVALUE = `cmt: ${commit.trim()} hash: ${HASHVALUE}`;
} catch (error) {
}
console.log('HASHVALUE',HASHVALUE)
cb();
});
gulp.task("hash", gulp.series(["genHashFile", "calHash"]));
gulp.task(
"dev",
gulp.series([
"clear",
"genReadMe",
"cssmin",
"copystatic",
"build",
"hash",
"combinejs",
"indexjs",
"inlineHtml",
])
);
gulp.task(
"dis",
gulp.series([
"clear",
"genReadMe",
"cssmin",
"copystatic",
"build",
"removetest",
"hash",
"combinejs",
"indexjs",
"inlineHtml",
])
);