izuemon commited on
Commit
e29d1e5
·
verified ·
1 Parent(s): 4e33b49

Update generate_html.cjs

Browse files
Files changed (1) hide show
  1. generate_html.cjs +56 -36
generate_html.cjs CHANGED
@@ -1,37 +1,25 @@
1
  const Packager = require("@turbowarp/packager");
2
  const fetch = require("cross-fetch").default;
 
3
 
4
  async function fetchProjectData(projectId) {
5
- // Node.js では trampoline ではなく Scratch API を直接使う
6
  const metaRes = await fetch(`https://api.scratch.mit.edu/projects/${encodeURIComponent(projectId)}`);
7
- if (!metaRes.ok) {
8
- throw new Error(`project metadata fetch failed: ${metaRes.status}`);
9
- }
10
-
11
  const meta = await metaRes.json();
12
  const token = meta.project_token;
13
- if (!token) {
14
- throw new Error("project_token が取得できませんでした");
15
- }
16
 
17
  const projectRes = await fetch(
18
  `https://projects.scratch.mit.edu/${encodeURIComponent(projectId)}?token=${encodeURIComponent(token)}`
19
  );
20
- if (!projectRes.ok) {
21
- throw new Error(`project data fetch failed: ${projectRes.status}`);
22
- }
23
-
24
  return Buffer.from(await projectRes.arrayBuffer());
25
  }
26
 
27
  async function imageFromUrl(url) {
28
  if (!url) return undefined;
29
-
30
  const res = await fetch(url);
31
- if (!res.ok) {
32
- throw new Error(`image fetch failed: ${res.status}`);
33
- }
34
-
35
  const contentType = (res.headers.get("content-type") || "image/png").split(";")[0].trim();
36
  const buffer = Buffer.from(await res.arrayBuffer());
37
  return new Packager.Image(contentType, buffer);
@@ -46,44 +34,76 @@ function bool(v, fallback = false) {
46
  async function main() {
47
  const projectId = process.argv[2];
48
  const optionsJson = process.argv[3];
49
-
50
  if (!projectId) throw new Error("project id required");
51
 
52
- const options = optionsJson ? JSON.parse(require("fs").readFileSync(optionsJson, "utf8")) : {};
53
  const projectData = await fetchProjectData(projectId);
54
-
55
  const loadedProject = await Packager.loadProject(projectData);
56
 
57
  const packager = new Packager.Packager();
58
  packager.project = loadedProject;
59
 
60
- // ここでは「存在する値だけ」上書きする
61
  if ("turbo" in options) packager.options.turbo = bool(options.turbo);
62
- if ("compiler" in options) packager.options.compiler = bool(options.compiler);
63
- if ("warpTimer" in options) packager.options.warpTimer = bool(options.warpTimer);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
- if (typeof options.customJs === "string") {
66
- packager.options.custom.js = options.customJs;
 
 
 
 
67
  }
68
- if (typeof options.customCss === "string") {
69
- packager.options.custom.css = options.customCss;
 
 
 
 
70
  }
71
 
72
- if (typeof options.title === "string" && options.title.trim() !== "") {
73
- packager.options.app.title = options.title;
 
 
74
  }
75
 
76
- // 画像系は null を入れず、指定された時だけ設定する
77
- const icon = await imageFromUrl(options.iconUrl);
78
- if (icon) {
79
- packager.options.app.icon = icon;
80
  }
81
 
82
- const loadingImage = await imageFromUrl(options.loadingImageUrl);
83
- if (loadingImage) {
84
- packager.options.loading.image = loadingImage;
 
 
 
85
  }
86
 
 
 
 
87
  const result = await packager.package();
88
  process.stdout.write(Buffer.from(result.data).toString("utf8"));
89
  }
 
1
  const Packager = require("@turbowarp/packager");
2
  const fetch = require("cross-fetch").default;
3
+ const fs = require("fs");
4
 
5
  async function fetchProjectData(projectId) {
 
6
  const metaRes = await fetch(`https://api.scratch.mit.edu/projects/${encodeURIComponent(projectId)}`);
7
+ if (!metaRes.ok) throw new Error(`project metadata fetch failed: ${metaRes.status}`);
 
 
 
8
  const meta = await metaRes.json();
9
  const token = meta.project_token;
10
+ if (!token) throw new Error("project_token が取得できませんでした");
 
 
11
 
12
  const projectRes = await fetch(
13
  `https://projects.scratch.mit.edu/${encodeURIComponent(projectId)}?token=${encodeURIComponent(token)}`
14
  );
15
+ if (!projectRes.ok) throw new Error(`project data fetch failed: ${projectRes.status}`);
 
 
 
16
  return Buffer.from(await projectRes.arrayBuffer());
17
  }
18
 
19
  async function imageFromUrl(url) {
20
  if (!url) return undefined;
 
21
  const res = await fetch(url);
22
+ if (!res.ok) throw new Error(`image fetch failed: ${res.status}`);
 
 
 
23
  const contentType = (res.headers.get("content-type") || "image/png").split(";")[0].trim();
24
  const buffer = Buffer.from(await res.arrayBuffer());
25
  return new Packager.Image(contentType, buffer);
 
34
  async function main() {
35
  const projectId = process.argv[2];
36
  const optionsJson = process.argv[3];
 
37
  if (!projectId) throw new Error("project id required");
38
 
39
+ const options = optionsJson ? JSON.parse(fs.readFileSync(optionsJson, "utf8")) : {};
40
  const projectData = await fetchProjectData(projectId);
 
41
  const loadedProject = await Packager.loadProject(projectData);
42
 
43
  const packager = new Packager.Packager();
44
  packager.project = loadedProject;
45
 
46
+ // 基本オプション
47
  if ("turbo" in options) packager.options.turbo = bool(options.turbo);
48
+ if ("interpolation" in options) packager.options.interpolation = bool(options.interpolation);
49
+ if ("highQualityPen" in options) packager.options.highQualityPen = bool(options.highQualityPen);
50
+ if ("maxClones" in options) packager.options.maxClones = Number(options.maxClones);
51
+ if ("stageWidth" in options) packager.options.stageWidth = Number(options.stageWidth);
52
+ if ("stageHeight" in options) packager.options.stageHeight = Number(options.stageHeight);
53
+ if ("resizeMode" in options) packager.options.resizeMode = options.resizeMode;
54
+ if ("autoplay" in options) packager.options.autoplay = bool(options.autoplay);
55
+ if ("username" in options) packager.options.username = options.username;
56
+ if ("closeWhenStopped" in options) packager.options.closeWhenStopped = bool(options.closeWhenStopped);
57
+
58
+ // カスタム CSS/JS
59
+ if (options.custom?.js) packager.options.custom.js = options.custom.js;
60
+ if (options.custom?.css) packager.options.custom.css = options.custom.css;
61
+
62
+ // appearance
63
+ if (options.appearance) {
64
+ packager.options.appearance.background = options.appearance.background || packager.options.appearance.background;
65
+ packager.options.appearance.foreground = options.appearance.foreground || packager.options.appearance.foreground;
66
+ if (options.appearance.accent) packager.options.appearance.accent = options.appearance.accent;
67
+ }
68
 
69
+ // loading screen
70
+ if (options.loadingScreen) {
71
+ packager.options.loading.progressBar = bool(options.loadingScreen.progressBar, true);
72
+ if (options.loadingScreen.text) packager.options.loading.text = options.loadingScreen.text;
73
+ const loadingImage = await imageFromUrl(options.loadingScreen.image);
74
+ if (loadingImage) packager.options.loading.image = loadingImage;
75
  }
76
+
77
+ // app
78
+ if (options.app) {
79
+ if (options.app.windowTitle) packager.options.app.windowTitle = options.app.windowTitle;
80
+ const icon = await imageFromUrl(options.app.icon);
81
+ if (icon) packager.options.app.icon = icon;
82
  }
83
 
84
+ // compiler
85
+ if (options.compiler) {
86
+ packager.options.compiler.enabled = bool(options.compiler.enabled);
87
+ packager.options.compiler.warpTimer = bool(options.compiler.warpTimer);
88
  }
89
 
90
+ // chunks
91
+ if (options.chunks) {
92
+ packager.options.chunks.gamepad = bool(options.chunks.gamepad);
93
+ packager.options.chunks.pointerlock = bool(options.chunks.pointerlock);
94
  }
95
 
96
+ // cloud variables
97
+ if (options.cloudVariables) {
98
+ packager.options.cloudVariables.mode = options.cloudVariables.mode || packager.options.cloudVariables.mode;
99
+ packager.options.cloudVariables.cloudHost = options.cloudVariables.cloudHost || packager.options.cloudVariables.cloudHost;
100
+ packager.options.cloudVariables.specialCloudBehaviors = bool(options.cloudVariables.specialCloudBehaviors);
101
+ packager.options.cloudVariables.unsafeCloudBehaviors = bool(options.cloudVariables.unsafeCloudBehaviors);
102
  }
103
 
104
+ // bake extensions
105
+ if ("bakeExtensions" in options) packager.options.bakeExtensions = bool(options.bakeExtensions);
106
+
107
  const result = await packager.package();
108
  process.stdout.write(Buffer.from(result.data).toString("utf8"));
109
  }