-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
186 lines (178 loc) · 4.91 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
/**
* @description Minecraft resourcepack production-ready compilation tool
* @author stdpi
*/
import gulp from "gulp";
import { series } from "async";
import { default as zip } from "gulp-zip";
import { deleteAsync } from "del";
import { relative } from "node:path";
import { Image } from "imagescript";
import { readFileSync, readdirSync, writeFile } from "node:fs";
import { Readable as ReadableStream } from "node:stream";
import { Buffer } from "node:buffer";
const { task, src, dest } = gulp;
const pdot = (s) => `.${s}`;
const typeignore = ["bbmodel", "gitignore"].map(pdot),
typejson = ["json", "mcmeta"].map(pdot),
typeimage = ["png"].map(pdot);
const ignoreGlob = typeignore.map((s) => `!./assets/**/*${s}`);
const minJSON = (json) => Buffer.from(JSON.stringify(JSON.parse(json))),
optimizePNG = (img) =>
new ReadableStream({
async read() {
if (
img[0] !== 137 ||
img[1] !== 80 ||
img[2] !== 78 ||
img[3] !== 71 ||
img[4] !== 13 ||
img[5] !== 10 ||
img[6] !== 26 ||
img[7] !== 10
) {
throw new Error("Invalid PNG signature");
}
try {
const image = await Image.decode(img, { type: "png" });
const optimized = await image.encode({
format: "png",
compression: 9,
});
this.push(Buffer.from(optimized));
this.push(null);
} catch (err) {
console.error("Error optimizing PNG:", err);
this.push(img);
this.push(null);
}
},
}),
opt = (file) => {
if (typejson.includes(file.extname)) {
file.contents = minJSON(file.contents);
} else if (typeimage.includes(file.extname)) {
file.contents = optimizePNG(file.contents);
}
},
dist = (list, dist, base, patch) =>
src([...list, ...ignoreGlob], {
base,
encoding: false,
buffer: true,
removeBOM: false,
})
// .on("data", opt)
.on("data", (file) => {
// console.log(
// "\x1b[32m+\x1b[0m",
// relative(import.meta.dirname, file.path),
// );
process.stdout.write("\x1b[32m+\x1b[0m");
})
.pipe(dest(`./dist/${patch || "base"}/${dist}`)),
// .on("data", async (file) => {
// const outputPath = `./dist/${patch || "base"}/${dist}/${file.relative}`;
// await Deno.writeFile(outputPath, new Uint8Array(file.contents));
// }),
zipack = (outname, patch) =>
src([`dist/${patch || "base"}/**/*`], { encoding: false })
.pipe(zip(outname))
.pipe(dest(".")),
patch = (cb, list, outname, patch) =>
series(
[
(cb) =>
dist(["./pack.mcmeta", "./pack.png", "LICENSE"], "./").on("end", cb),
(cb) =>
series(
//
list.map((l) => (cb) => dist(...l, patch).on("end", cb)),
cb,
),
(cb) => zipack(`./dist/${outname}`, patch).on("end", cb),
(cb) =>
!process.env.noclean
? deleteAsync(["./dist/pack/**/*"]).then(() => cb())
: cb(),
],
cb,
);
const disabled_patches = readFileSync("./patches/.disabled.txt", {
encoding: "utf8",
}).split("\n");
const patches = readdirSync("./patches").filter(
(i) => !i.startsWith(".") && !disabled_patches.includes(i),
);
task("default", (end) =>
patch(
end,
[
[["./assets/**/*"], "./", "./"],
...patches.map((pn) => [
[`./patches/${pn}/**/*`],
"./",
`./patches/${pn}`,
]),
],
"tfh.fullpatch.zip",
),
);
task("patch", (end) => {
series(
[
// Base patch
(end) => patch(end, [[["./assets/**/*"], "./", "./"]], "tfh.base.zip"),
...patches.map(
(pn) => (end) =>
patch(
end,
[[[`./patches/${pn}/**/*`], "./", `./patches/${pn}`]],
`tfh.${pn}.zip`,
pn.split(".")[0],
),
),
],
end,
);
});
task("pdev", (end) =>
series(
[
...patches.map(
(pn) => (cb) =>
series(
[
(cb) =>
src([`./patches/${pn}/**/*`, ...ignoreGlob], {
base: `./patches/${pn}`,
encoding: false,
})
.pipe(dest(`../DEV--${pn}`))
.on("end", cb),
(cb) =>
writeFile(
`../DEV--${pn}/pack.mcmeta`,
JSON.stringify({
pack: {
pack_format: 6,
description: [
"",
{
text: "AUTO GENERATED. DO NOT EDIT",
bold: true,
color: "red",
},
],
},
}),
cb,
),
],
cb,
),
),
],
end,
),
);