Repository navigation
Expand file tree
/
Copy pathgulpfile.makezip.js
More file actions
275 lines (254 loc) · 8.64 KB
/
Copy pathgulpfile.makezip.js
File metadata and controls
275 lines (254 loc) · 8.64 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// @ts-check
import { dirname, join } from "path";
import gulp from "gulp";
import { ZipFile } from "yazl";
import { glslToJavaScript } from "./scripts/build-utilities.js";
import { release } from "./gulpfile.js";
import { createWriteStream, rmSync } from "fs";
import { readFile, writeFile } from "fs/promises";
import { Transform, Writable } from "stream";
import { finished } from "stream/promises";
import { createRequire } from "module";
import { buildSandcastleApp } from "./scripts/buildSandcastle.js";
/** @import Vinyl from "vinyl"; */
const require = createRequire(import.meta.url);
const packageJson = require("./package.json");
let version = packageJson.version;
if (/\.0$/.test(version)) {
version = version.substring(0, version.length - 2);
}
/**
* Removes scripts from package.json files to ensure that
* they still work when run from within the ZIP file.
*
* @param {string} packageJsonPath The path to the package.json.
* @returns {Promise<NodeJS.ReadWriteStream>} A stream that writes to the updated package.json file.
*/
async function pruneScriptsForZip(packageJsonPath) {
// Read the contents of the file.
const contents = await readFile(packageJsonPath, "utf8");
const contentsJson = JSON.parse(contents);
const scripts = contentsJson.scripts;
// Remove prepare step from package.json to avoid running "prepare" an extra time.
delete scripts.prepare;
// Remove build and transform tasks since they do not function as intended from within the release zip
delete scripts.build;
delete scripts["build-release"];
delete scripts["build-watch"];
delete scripts["build-ts"];
delete scripts["build-third-party"];
delete scripts["build-apps"];
delete scripts["build-sandcastle"];
delete scripts.clean;
delete scripts.cloc;
delete scripts["build-docs"];
delete scripts["build-docs-watch"];
delete scripts["make-zip"];
delete scripts.release;
delete scripts.prettier;
// Remove deploy tasks
delete scripts["deploy-status"];
delete scripts["deploy-set-version"];
delete scripts["website-release"];
// Set server tasks to use production flag
scripts["start"] = "node server.js --production";
scripts["start-public"] = "node server.js --public --production";
scripts["test"] = "gulp test --production";
scripts["test-all"] = "gulp test --all --production";
scripts["test-webgl"] = "gulp test --include WebGL --production";
scripts["test-non-webgl"] = "gulp test --exclude WebGL --production";
scripts["test-webgl-validation"] = "gulp test --webglValidation --production";
scripts["test-webgl-stub"] = "gulp test --webglStub --production";
scripts["test-release"] = "gulp test --release --production";
// Write to a temporary package.json file.
const noPreparePackageJson = join(
dirname(packageJsonPath),
"package.noprepare.json",
);
await writeFile(noPreparePackageJson, JSON.stringify(contentsJson, null, 2));
return gulp.src(noPreparePackageJson, {
base: ".",
});
}
/**
* Creates a stream that calls a function on each file passing through it.
*
* @param {(file: Vinyl) => void} callback The function to call on each file. It may modify the file.
* @returns {Transform} A stream that passes each file through after calling the callback.
*/
function gulpTap(callback) {
return new Transform({
objectMode: true,
//eslint-disable-next-line no-unused-vars
transform(file, encoding, done) {
callback(file);
done(null, file);
},
});
}
/**
* Creates a stream that writes the files piped into it to a ZIP file, using each file's
* relative path as its path in the ZIP file. The stream finishes once the ZIP file has been written.
*
* @param {string} zipPath The path of the ZIP file to write.
* @returns {Writable} The stream to pipe files into.
*/
function gulpZip(zipPath) {
const zipFile = new ZipFile();
const output = zipFile.outputStream.pipe(createWriteStream(zipPath));
return new Writable({
objectMode: true,
//eslint-disable-next-line no-unused-vars
write(file, encoding, done) {
// Paths in ZIP files always use forward slashes.
const pathInZip = file.relative.replaceAll("\\", "/");
if (file.isDirectory()) {
// Directory modes are omitted because they make ZIP files created on Windows unreadable on macOS.
zipFile.addEmptyDirectory(pathInZip, { mtime: file.stat?.mtime });
} else if (file.isBuffer()) {
zipFile.addBuffer(file.contents, pathInZip, {
mtime: file.stat?.mtime,
mode: file.stat?.mode,
});
}
done();
},
final(done) {
zipFile.end();
finished(output).then(() => done(), done);
},
});
}
export async function makeZip() {
await release();
await buildSandcastleApp({
outputToBuildDir: false,
includeDevelopment: false,
outerOrigin: "http://localhost:8080",
innerOrigin: "http://localhost:8081",
});
await createZipFile();
}
async function createZipFile() {
//For now we regenerate the JS glsl to force it to be unminified in the release zip
//See https://github.com/CesiumGS/cesium/pull/3106#discussion_r42793558 for discussion.
await glslToJavaScript(false, "Build/minifyShaders.state", "engine");
const packageJsonSrc = await pruneScriptsForZip("package.json");
const corePackageJsonSrc = await pruneScriptsForZip(
"packages/core/package.json",
);
const gltfPackageJsonSrc = await pruneScriptsForZip(
"packages/gltf/package.json",
);
const enginePackageJsonSrc = await pruneScriptsForZip(
"packages/engine/package.json",
);
const widgetsPackageJsonSrc = await pruneScriptsForZip(
"packages/widgets/package.json",
);
const src = gulp
.src("index.release.html")
.pipe(
gulpTap((file) => {
if (file.stem === "index.release") {
file.stem = "index";
}
}),
)
.pipe(corePackageJsonSrc)
.pipe(gltfPackageJsonSrc)
.pipe(enginePackageJsonSrc)
.pipe(widgetsPackageJsonSrc)
.pipe(packageJsonSrc)
.pipe(
gulpTap((file) => {
if (file.stem === "package.noprepare") {
file.stem = "package";
}
}),
)
.pipe(
gulp.src(
[
"Build/Cesium/**",
"Build/CesiumUnminified/**",
"Build/Documentation/**",
"Build/Specs/**",
"Build/package.json",
"packages/core/Build/**",
"packages/gltf/Build/**",
"packages/engine/Build/**",
"packages/widgets/Build/**",
"!Build/Specs/e2e/**",
"!Build/InlineWorkers.js",
"!packages/core/Build/Specs/**",
"!packages/gltf/Build/Specs/**",
"!packages/engine/Build/Specs/**",
"!packages/widgets/Build/Specs/**",
"!packages/engine/Build/minifyShaders.state",
],
{
encoding: false,
base: ".",
},
),
)
.pipe(
gulp.src(
[
"Apps/**",
"packages/core/index.js",
"packages/core/index.d.ts",
"packages/core/LICENSE.md",
"packages/core/README.md",
"packages/core/scripts/**",
"packages/core/Source/**",
"packages/gltf/index.js",
"packages/gltf/index.d.ts",
"packages/gltf/LICENSE.md",
"packages/gltf/README.md",
"packages/gltf/scripts/**",
"packages/gltf/Source/**",
"packages/engine/index.js",
"packages/engine/index.d.ts",
"packages/engine/LICENSE.md",
"packages/engine/README.md",
"packages/engine/scripts/**",
"packages/engine/Source/**",
"packages/widgets/index.js",
"packages/widgets/index.d.ts",
"packages/widgets/LICENSE.md",
"packages/widgets/README.md",
"packages/widgets/scripts/**",
"packages/widgets/Source/**",
"Source/**",
"Specs/**",
"scripts/**",
"favicon.ico",
".prettierignore",
"eslint.config.js",
"gulpfile.js",
"server.js",
"index.cjs",
"LICENSE.md",
"CHANGES.md",
"README.md",
"web.config",
"!scripts/buildSandcastle.js",
"!**/*.gitignore",
"!Specs/e2e/*-snapshots/**",
],
{
encoding: false,
base: ".",
},
),
)
.pipe(gulpZip(`Cesium-${version}.zip`));
await finished(src);
rmSync("./package.noprepare.json", { force: true });
rmSync("./packages/core/package.noprepare.json", { force: true });
rmSync("./packages/gltf/package.noprepare.json", { force: true });
rmSync("./packages/engine/package.noprepare.json", { force: true });
rmSync("./packages/widgets/package.noprepare.json", { force: true });
}