picobook/src/server/api/release.ts

77 lines
2.4 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");
2024-03-26 19:27:00 -07:00
const picoDirPath = path.resolve(__dirname, "..", "..", "..", "pico8");
const picoBinPath = path.resolve(picoDirPath, "pico8");
2024-03-26 00:48:09 -07:00
2024-03-26 01:16:47 -07:00
// const {stdout, } = await execa(picoBinPath, ["/home/dylan/.lexaloffle/pico-8/carts/candles/candles.p8", "-export", path.join(__dirname, "result.js")]);
2024-03-26 19:41:39 -07:00
const {stdout, } = await execa("ls", ["-la", picoDirPath]);
2024-03-26 19:27:00 -07:00
console.log(stdout);
2024-03-26 01:16:47 -07:00
2024-03-26 19:30:16 -07:00
const {stdout: stdout2, } = await execa(picoBinPath, ["-x"]);
console.log(stdout2);
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 01:04:57 -07:00
console.log("pico exists: ", fs.existsSync(picoBinPath));
2024-03-26 01:06:53 -07:00
console.log("manifest exists: ", fs.existsSync(path.join(repoPath, "picobook.json")));
console.log("main exists: ", fs.existsSync(path.join(repoPath, manifest.main)));
2024-03-26 00:55:55 -07:00
await execa(picoBinPath, [path.join(repoPath, manifest.main), "-export", path.join(repoPath, "result.js")]);
2024-03-26 01:04:57 -07:00
// await execa(picoBinPath, [path.join(repoPath, manifest.main), "-export", path.join(repoPath, "result.png")]);
2024-03-26 00:48:09 -07:00
const js = await fs.promises.readFile(path.join(repoPath, "result.js"), "utf8");
2024-03-26 01:04:57 -07:00
// const png = Buffer.from(await fs.promises.readFile(path.join(repoPath, "result.png"))).toString("base64");
2024-03-26 00:48:09 -07:00
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,
2024-03-26 01:04:57 -07:00
// 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>;