picobook/src/server/api/release.ts

59 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-03-25 00:29:43 -07:00
import { Type } from "@sinclair/typebox";
2024-03-31 13:49:09 -07:00
import { FirRouteInput, FirRouteOptions } from "../util/routewrap";
import {git} from "../util/git.ts";
import { randomUUID } from "crypto";
2024-03-26 00:48:09 -07:00
import path from "path";
import {fileURLToPath} from 'url';
2024-03-31 13:49:09 -07:00
import { getCarts } from "../util/carts.ts";
2024-03-31 17:33:27 -07:00
import { getRelease, insertRelease } from "../dbal/dbal.ts";
import { ManifestType } from "../types.ts";
2024-03-26 00:48:09 -07:00
const __dirname = fileURLToPath(new URL('.', import.meta.url));
2024-03-31 13:49:09 -07:00
const reposPath = path.resolve(__dirname, "..", "..", "..", "repos");
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.Any();
2024-03-25 21:35:58 -07:00
const handler = async ({payload}: FirRouteInput<typeof payloadT>) => {
const {manifest, token} = payload;
2024-03-31 13:49:09 -07:00
2024-03-31 16:41:29 -07:00
if (!ManifestType.Check(manifest)) {
return false;
}
2024-03-31 17:33:27 -07:00
const release = await getRelease({author: manifest.author, slug: manifest.id, version: manifest.version});
if (release) {
return false;
}
2024-03-31 13:49:09 -07:00
const uuid = randomUUID();
const repoPath = path.join(reposPath, uuid);
2024-03-25 21:35:58 -07:00
await git.clone({
2024-03-31 13:49:09 -07:00
from: manifest.repo,
to: repoPath,
auth: token,
2024-03-25 21:35:58 -07:00
});
2024-03-31 13:49:09 -07:00
2024-03-31 14:26:16 -07:00
const carts = await getCarts(repoPath, manifest.carts);
2024-03-31 13:49:09 -07:00
2024-03-31 17:44:52 -07:00
await insertRelease({
2024-03-31 17:33:27 -07:00
manifest,
carts,
});
2024-03-25 21:35:58 -07:00
console.log({
manifest,
2024-03-31 13:49:09 -07:00
carts,
2024-03-25 21:35:58 -07:00
});
2024-03-31 13:49:09 -07:00
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>;