picobook/src/server/api/release.ts
2024-03-26 00:48:09 -07:00

66 lines
1.8 KiB
TypeScript

import { Type } from "@sinclair/typebox";
import { FirRouteInput, FirRouteOptions } from "../util/routewrap.js";
import {execa} from 'execa';
import fs from "fs";
import path from "path";
import git from "isomorphic-git";
import http from "isomorphic-git/http/node";
import {fileURLToPath} from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = fileURLToPath(new URL('.', import.meta.url));
const method = "POST";
const url = "/api/release";
// const payloadT = Type.Object({
// png: Type.String(),
// });
const payloadT = Type.Any();
const repoPath = path.resolve(__dirname, "..", "..", "..", "repo");
const picoBinPath = path.resolve(__dirname, "..", "..", "..", "pico8", "pico8");
const handler = async ({payload}: FirRouteInput<typeof payloadT>) => {
const {manifest, token} = payload;
if (!fs.existsSync(repoPath)) {
fs.mkdirSync(repoPath, {recursive: true})
}
console.log(manifest);
console.log("cloning...");
await git.clone({
fs,
http,
// headers: {
// "Authorization": `Bearer ${token}`,
// },
onAuth() {
return {
username: 'x-access-token',
password: token,
}
},
dir: repoPath,
url: manifest.repo,
});
console.log("cloned");
console.log("read local manifest");
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});
console.log({
manifest,
js,
png,
});
return true;
};
export default {
method,
url,
payloadT,
handler,
} as const satisfies FirRouteOptions<typeof payloadT>;