Skip to content

Commit ff26431

Browse files
authored
Merge pull request #12584 from CesiumGS/prettier-scripts
Run prettier on `scripts`
2 parents b661763 + 22e1094 commit ff26431

File tree

4 files changed

+106
-104
lines changed

4 files changed

+106
-104
lines changed

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
!Apps/**/
1010
!Documentation/**/
1111
!packages/**/
12+
!scripts/**/
1213
!Specs/**/
1314
!Tools/**/
1415

scripts/ContextCache.js

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
class ContextCache {
2-
constructor(context) {
3-
this.context = context;
4-
this.promise = Promise.resolve();
5-
this.result = undefined;
6-
}
7-
8-
clear() {
9-
this.result = undefined;
10-
}
11-
12-
async rebuild() {
13-
const promise = (this.promise = this.context.rebuild());
14-
const result = (this.result = await promise);
15-
return result;
16-
}
17-
18-
isBuilt() {
19-
return (
20-
this.result &&
21-
this.result.outputFiles &&
22-
this.result.outputFiles.length > 0
23-
);
24-
}
2+
constructor(context) {
3+
this.context = context;
4+
this.promise = Promise.resolve();
5+
this.result = undefined;
256
}
267

27-
export default ContextCache;
8+
clear() {
9+
this.result = undefined;
10+
}
11+
12+
async rebuild() {
13+
const promise = (this.promise = this.context.rebuild());
14+
const result = (this.result = await promise);
15+
return result;
16+
}
17+
18+
isBuilt() {
19+
return (
20+
this.result &&
21+
this.result.outputFiles &&
22+
this.result.outputFiles.length > 0
23+
);
24+
}
25+
}
26+
27+
export default ContextCache;

scripts/build.js

+23-24
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { readFile, writeFile } from "fs/promises";
44
import { EOL } from "os";
55
import path from "path";
66
import { createRequire } from "module";
7-
import { finished } from 'stream/promises';
7+
import { finished } from "stream/promises";
88

99
import esbuild from "esbuild";
1010
import { globby } from "globby";
@@ -27,11 +27,11 @@ if (/\.0$/.test(version)) {
2727

2828
const copyrightHeaderTemplate = readFileSync(
2929
path.join("Source", "copyrightHeader.js"),
30-
"utf8"
30+
"utf8",
3131
);
3232
const combinedCopyrightHeader = copyrightHeaderTemplate.replace(
3333
"${version}",
34-
version
34+
version,
3535
);
3636

3737
function escapeCharacters(token) {
@@ -294,7 +294,7 @@ export async function createCesiumJs() {
294294
for (const workspace of Object.keys(workspaceSourceFiles)) {
295295
const files = await globby(workspaceSourceFiles[workspace]);
296296
const declarations = files.map((file) =>
297-
generateDeclaration(workspace, file)
297+
generateDeclaration(workspace, file),
298298
);
299299
contents += declarations.join(`${EOL}`);
300300
contents += "\n";
@@ -468,7 +468,7 @@ export async function glslToJavaScript(minify, minifyStateFilePath, workspace) {
468468
`packages/${workspace}/`,
469469
"Source",
470470
"Shaders",
471-
"Builtin"
471+
"Builtin",
472472
);
473473
if (
474474
glslFile.indexOf(path.normalize(path.join(baseDir, "Functions"))) === 0
@@ -505,7 +505,7 @@ export async function glslToJavaScript(minify, minifyStateFilePath, workspace) {
505505

506506
let copyrightComments = "";
507507
const extractedCopyrightComments = contents.match(
508-
/\/\*\*(?:[^*\/]|\*(?!\/)|\n)*?@license(?:.|\n)*?\*\//gm
508+
/\/\*\*(?:[^*\/]|\*(?!\/)|\n)*?@license(?:.|\n)*?\*\//gm,
509509
);
510510
if (extractedCopyrightComments) {
511511
copyrightComments = `${extractedCopyrightComments.join("\n")}\n`;
@@ -526,7 +526,7 @@ export async function glslToJavaScript(minify, minifyStateFilePath, workspace) {
526526
export default "${contents}";\n`;
527527

528528
return writeFile(jsFile, contents);
529-
})
529+
}),
530530
);
531531

532532
// delete any left over JS files from old shaders
@@ -538,7 +538,7 @@ export default "${contents}";\n`;
538538
for (let i = 0; i < builtins.length; i++) {
539539
const builtin = builtins[i];
540540
contents.imports.push(
541-
`import czm_${builtin} from './${path}/${builtin}.js'`
541+
`import czm_${builtin} from './${path}/${builtin}.js'`,
542542
);
543543
contents.builtinLookup.push(`czm_${builtin} : ` + `czm_${builtin}`);
544544
}
@@ -554,7 +554,7 @@ export default "${contents}";\n`;
554554
generateBuiltinContents(contents, builtinFunctions, "Functions");
555555

556556
const fileContents = `//This file is automatically rebuilt by the Cesium build process.\n${contents.imports.join(
557-
"\n"
557+
"\n",
558558
)}\n\nexport default {\n ${contents.builtinLookup.join(",\n ")}\n};\n`;
559559

560560
return writeFile(
@@ -563,9 +563,9 @@ export default "${contents}";\n`;
563563
"Source",
564564
"Shaders",
565565
"Builtin",
566-
"CzmBuiltins.js"
566+
"CzmBuiltins.js",
567567
),
568-
fileContents
568+
fileContents,
569569
);
570570
}
571571

@@ -600,7 +600,7 @@ const externalResolvePlugin = {
600600
return {
601601
contents,
602602
};
603-
}
603+
},
604604
);
605605
},
606606
};
@@ -634,7 +634,7 @@ export async function createGalleryList(noDevelopmentGallery) {
634634
newDemos = child_process
635635
.execSync(
636636
`git diff --name-only --diff-filter=A ${tagVersion} Apps/Sandcastle/gallery/*.html`,
637-
{ stdio: ["pipe", "pipe", "ignore"] }
637+
{ stdio: ["pipe", "pipe", "ignore"] },
638638
)
639639
.toString()
640640
.trim()
@@ -647,7 +647,7 @@ export async function createGalleryList(noDevelopmentGallery) {
647647
const files = await globby(fileList);
648648
files.forEach(function (file) {
649649
const demo = filePathToModuleId(
650-
path.relative("Apps/Sandcastle/gallery", file)
650+
path.relative("Apps/Sandcastle/gallery", file),
651651
);
652652

653653
const demoObject = {
@@ -697,8 +697,7 @@ const has_new_gallery_demos = ${newDemos.length > 0 ? "true;" : "false;"}\n`;
697697
],
698698
minify: true,
699699
banner: {
700-
css:
701-
"/* This file is automatically rebuilt by the Cesium build process. */\n",
700+
css: "/* This file is automatically rebuilt by the Cesium build process. */\n",
702701
},
703702
outfile: path.join("Apps", "Sandcastle", "templates", "bucket.css"),
704703
});
@@ -745,7 +744,7 @@ export async function copyEngineAssets(destination) {
745744
await copyFiles(
746745
["packages/engine/Source/Widget/**", "!packages/engine/Source/Widget/*.js"],
747746
path.join(destination, "Widgets/CesiumWidget"),
748-
"packages/engine/Source/Widget"
747+
"packages/engine/Source/Widget",
749748
);
750749
}
751750

@@ -776,7 +775,7 @@ export async function createJsHintOptions() {
776775
const jshintrc = JSON.parse(
777776
await readFile(path.join("Apps", "Sandcastle", ".jshintrc"), {
778777
encoding: "utf8",
779-
})
778+
}),
780779
);
781780

782781
const contents = `\
@@ -785,7 +784,7 @@ export async function createJsHintOptions() {
785784

786785
await writeFile(
787786
path.join("Apps", "Sandcastle", "jsHintOptions.js"),
788-
contents
787+
contents,
789788
);
790789

791790
return contents;
@@ -898,7 +897,7 @@ async function createSpecListForWorkspace(files, workspace, outputPath) {
898897
files.forEach(function (file) {
899898
contents += `import './${filePathToModuleId(file).replace(
900899
`packages/${workspace}/Specs/`,
901-
""
900+
"",
902901
)}.js';\n`;
903902
});
904903

@@ -1005,7 +1004,7 @@ export const buildEngine = async (options) => {
10051004
await glslToJavaScript(
10061005
minify,
10071006
"packages/engine/Build/minifyShaders.state",
1008-
"engine"
1007+
"engine",
10091008
);
10101009

10111010
// Create index.js
@@ -1101,7 +1100,7 @@ export async function buildCesium(options) {
11011100
JSON.stringify({
11021101
type: "commonjs",
11031102
}),
1104-
"utf8"
1103+
"utf8",
11051104
);
11061105

11071106
// Create Cesium.js
@@ -1179,14 +1178,14 @@ export async function buildCesium(options) {
11791178
await copyFiles(
11801179
["packages/engine/Source/ThirdParty/**/*.js"],
11811180
"Source/ThirdParty",
1182-
"packages/engine/Source/ThirdParty"
1181+
"packages/engine/Source/ThirdParty",
11831182
);
11841183

11851184
await copyWidgetsAssets("Source/Widgets");
11861185
await copyFiles(
11871186
["packages/widgets/Source/**/*.css"],
11881187
"Source/Widgets",
1189-
"packages/widgets/Source"
1188+
"packages/widgets/Source",
11901189
);
11911190

11921191
// WORKAROUND:

scripts/createRoute.js

+58-56
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,73 @@ import ContextCache from "./ContextCache.js";
22
import path from "path";
33

44
function formatTimeSinceInSeconds(start) {
5-
return Math.ceil((performance.now() - start) / 100) / 10;
5+
return Math.ceil((performance.now() - start) / 100) / 10;
66
}
77

8-
function serveResult (result, fileName, res, next) {
9-
let bundle, error;
10-
try {
11-
for (const out of result.outputFiles) {
12-
if (path.basename(out.path) === fileName) {
13-
bundle = out.text;
14-
}
8+
function serveResult(result, fileName, res, next) {
9+
let bundle, error;
10+
try {
11+
for (const out of result.outputFiles) {
12+
if (path.basename(out.path) === fileName) {
13+
bundle = out.text;
1514
}
16-
} catch(e) {
17-
error = e;
18-
}
19-
20-
if (!bundle) {
21-
next(new Error(`Failed to generate bundle: ${fileName}`, {
22-
cause: error
23-
}));
24-
return;
2515
}
26-
27-
res.append("Cache-Control", "max-age=0");
28-
res.append("Content-Type", "application/javascript");
29-
res.send(bundle);
30-
};
16+
} catch (e) {
17+
error = e;
18+
}
19+
20+
if (!bundle) {
21+
next(
22+
new Error(`Failed to generate bundle: ${fileName}`, {
23+
cause: error,
24+
}),
25+
);
26+
return;
27+
}
28+
29+
res.append("Cache-Control", "max-age=0");
30+
res.append("Content-Type", "application/javascript");
31+
res.send(bundle);
32+
}
3133

3234
function createRoute(app, name, route, context, dependantCaches) {
33-
const cache = new ContextCache(context);
34-
app.get(route, async function (req, res, next) {
35-
const fileName = path.basename(req.originalUrl);
35+
const cache = new ContextCache(context);
36+
app.get(route, async function (req, res, next) {
37+
const fileName = path.basename(req.originalUrl);
3638

37-
// Multiple files may be requested at this path, calling this function in quick succession.
38-
// Await the previous build before re-building again.
39+
// Multiple files may be requested at this path, calling this function in quick succession.
40+
// Await the previous build before re-building again.
41+
try {
42+
await cache.promise;
43+
} catch {
44+
// Error is reported upstream
45+
}
46+
47+
if (!cache.isBuilt()) {
3948
try {
40-
await cache.promise;
41-
} catch {
42-
// Error is reported upstream
43-
}
44-
45-
if (!cache.isBuilt()) {
46-
try {
47-
const start = performance.now();
48-
if (dependantCaches) {
49-
await Promise.all(
50-
dependantCaches.map((dependantCache) => {
51-
if (!dependantCache.isBuilt()) {
52-
return dependantCache.rebuild();
53-
}
54-
})
55-
);
56-
}
57-
await cache.rebuild();
58-
console.log(
59-
`Built ${name} in ${formatTimeSinceInSeconds(start)} seconds.`
49+
const start = performance.now();
50+
if (dependantCaches) {
51+
await Promise.all(
52+
dependantCaches.map((dependantCache) => {
53+
if (!dependantCache.isBuilt()) {
54+
return dependantCache.rebuild();
55+
}
56+
}),
6057
);
61-
} catch (e) {
62-
next(e);
6358
}
59+
await cache.rebuild();
60+
console.log(
61+
`Built ${name} in ${formatTimeSinceInSeconds(start)} seconds.`,
62+
);
63+
} catch (e) {
64+
next(e);
6465
}
65-
66-
return serveResult(cache.result, fileName, res, next);
67-
});
68-
69-
return cache;
70-
}
66+
}
67+
68+
return serveResult(cache.result, fileName, res, next);
69+
});
70+
71+
return cache;
72+
}
7173

72-
export default createRoute;
74+
export default createRoute;

0 commit comments

Comments
 (0)