Spaces:
Running
Running
Update generate_html.cjs
Browse files- generate_html.cjs +35 -18
generate_html.cjs
CHANGED
|
@@ -1,24 +1,41 @@
|
|
| 1 |
-
// generate_html.
|
| 2 |
-
const
|
|
|
|
| 3 |
const fs = require("fs");
|
| 4 |
|
| 5 |
-
|
| 6 |
-
const
|
|
|
|
| 7 |
|
| 8 |
-
if (!
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
//
|
| 14 |
-
const
|
|
|
|
| 15 |
|
| 16 |
-
//
|
| 17 |
-
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 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 |
+
});
|