picobook/src/server/api/release.ts

66 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-03-25 00:29:43 -07:00
import { Type } from "@sinclair/typebox";
import { FirRouteInput, FirRouteOptions } from "../util/routewrap.js";
2024-03-26 00:48:09 -07:00
import {execa} from 'execa';
2024-03-25 21:35:58 -07:00
import fs from "fs";
2024-03-26 00:48:09 -07:00
import path from "path";
2024-03-25 21:35:58 -07:00
import git from "isomorphic-git";
import http from "isomorphic-git/http/node";
2024-03-26 00:48:09 -07:00
import {fileURLToPath} from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = fileURLToPath(new URL('.', import.meta.url));
2024-03-25 00:29:43 -07:00
const method = "POST";
2024-03-25 00:42:23 -07:00
const url = "/api/release";
2024-03-25 00:29:43 -07:00
// const payloadT = Type.Object({
// png: Type.String(),
// });
const payloadT = Type.Any();
2024-03-26 00:48:09 -07:00
const repoPath = path.resolve(__dirname, "..", "..", "..", "repo");
const picoBinPath = path.resolve(__dirname, "..", "..", "..", "pico8", "pico8");
2024-03-25 21:35:58 -07:00
const handler = async ({payload}: FirRouteInput<typeof payloadT>) => {
const {manifest, token} = payload;
2024-03-26 00:48:09 -07:00
if (!fs.existsSync(repoPath)) {
fs.mkdirSync(repoPath, {recursive: true})
2024-03-25 21:35:58 -07:00
}
2024-03-26 00:13:42 -07:00
console.log(manifest);
console.log("cloning...");
2024-03-25 21:35:58 -07:00
await git.clone({
fs,
http,
2024-03-26 00:20:53 -07:00
// headers: {
// "Authorization": `Bearer ${token}`,
2024-03-26 00:11:20 -07:00
// },
2024-03-26 00:20:53 -07:00
onAuth() {
return {
username: 'x-access-token',
password: token,
}
},
2024-03-26 00:48:09 -07:00
dir: repoPath,
2024-03-25 21:35:58 -07:00
url: manifest.repo,
});
2024-03-26 00:13:42 -07:00
console.log("cloned");
console.log("read local manifest");
2024-03-26 00:48:09 -07:00
await execa(picoBinPath, ["-export", path.join(repoPath, "result.html"), path.join(repoPath, manifest.main)]);
await execa(picoBinPath, ["-export", path.join(repoPath, "result.png"), path.join(repoPath, manifest.main)]);
const js = await fs.promises.readFile(path.join(repoPath, "result.js"), "utf8");
const png = new Buffer(await fs.promises.readFile(path.join(repoPath, "result.png"))).toString("base64");
fs.promises.rm(repoPath, {recursive: true, force: true});
2024-03-25 21:35:58 -07:00
console.log({
manifest,
2024-03-26 00:48:09 -07:00
js,
png,
2024-03-25 21:35:58 -07:00
});
return true;
2024-03-25 00:29:43 -07:00
};
export default {
method,
url,
payloadT,
handler,
} as const satisfies FirRouteOptions<typeof payloadT>;