izuemon commited on
Commit
0d2e983
·
verified ·
1 Parent(s): 1ea4dfe

Update generate_html.cjs

Browse files
Files changed (1) hide show
  1. generate_html.cjs +35 -18
generate_html.cjs CHANGED
@@ -1,24 +1,41 @@
1
- // generate_html.cjs
2
- const packager = require("@turbowarp/packager");
 
3
  const fs = require("fs");
4
 
5
- const projectId = process.argv[2];
6
- const optionsFile = process.argv[3];
 
7
 
8
- if (!projectId) {
9
- console.error("Project ID is required.");
10
- process.exit(1);
11
- }
 
 
 
 
 
 
 
 
12
 
13
- // JSONオプションを読み込み
14
- const options = optionsFile ? JSON.parse(fs.readFileSync(optionsFile, "utf8")) : {};
 
15
 
16
- // packProject は default ロパティに格納されている
17
- const packProject = packager.default;
18
 
19
- packProject(projectId, options)
20
- .then(html => console.log(html))
21
- .catch(err => {
22
- console.error(err);
23
- process.exit(1);
24
- });
 
 
 
 
 
 
 
1
+ // generate_html.js
2
+ const Packager = require("@turbowarp/packager");
3
+ const fetch = require("cross-fetch").default;
4
  const fs = require("fs");
5
 
6
+ async function main() {
7
+ const id = process.argv[2];
8
+ const optionsJsonPath = process.argv[3];
9
 
10
+ if (!id) throw new Error("project id required.");
11
+ const optionsInput = JSON.parse(fs.readFileSync(optionsJsonPath, "utf8"));
12
+
13
+ // Scratchプロジェクトの取得
14
+ const metaRes = await fetch(`https://trampoline.turbowarp.org/api/projects/${id}`);
15
+ const meta = await metaRes.json();
16
+ const token = meta.project_token;
17
+ const projectRes = await fetch(`https://projects.scratch.mit.edu/${id}?token=${token}`);
18
+ const projectData = await projectRes.arrayBuffer();
19
+
20
+ // ロード
21
+ const loaded = await Packager.loadProject(projectData);
22
 
23
+ // Packagerの生成
24
+ const pack = new Packager.Packager();
25
+ pack.project = loaded;
26
 
27
+ // 既存のオションを維持しつつ上書き
28
+ Object.assign(pack.options, optionsInput);
29
 
30
+ // パッケージ化
31
+ const { data, type } = await pack.package();
32
+
33
+ if (type !== "text/html") throw new Error("not html");
34
+
35
+ // HTMLとして出力
36
+ process.stdout.write(Buffer.from(data).toString("utf8"));
37
+ }
38
+ main().catch(err => {
39
+ console.error(err);
40
+ process.exit(1);
41
+ });