nodes/store/bin/upload.ts

66 lines
1.5 KiB
TypeScript
Raw Permalink Normal View History

2024-12-17 18:15:21 +01:00
import * as path from "jsr:@std/path";
const arg = Deno.args[0];
const base = arg.startsWith("/") ? arg : path.join(Deno.cwd(), arg);
const dirs = Deno.readDir(base);
type Node = {
user: string;
system: string;
id: string;
path: string;
};
const nodes: Node[] = [];
for await (const dir of dirs) {
if (dir.isDirectory) {
const userDir = path.join(base, dir.name);
for await (const userName of Deno.readDir(userDir)) {
if (userName.isDirectory) {
const nodeSystemDir = path.join(userDir, userName.name);
for await (const nodeDir of Deno.readDir(nodeSystemDir)) {
if (nodeDir.isDirectory && !nodeDir.name.startsWith(".")) {
const wasmFilePath = path.join(
nodeSystemDir,
nodeDir.name,
"pkg",
"index_bg.wasm",
);
nodes.push({
user: dir.name,
system: userName.name,
id: nodeDir.name,
path: wasmFilePath,
});
}
}
}
}
}
}
async function postNode(node: Node) {
const wasmContent = await Deno.readFile(node.path);
2024-12-20 13:40:14 +01:00
const url = `http://localhost:8000/nodes`;
2024-12-20 15:55:45 +01:00
// const url = "https://node-store.app.max-richter.dev/nodes";
2024-12-17 18:15:21 +01:00
const res = await fetch(url, {
method: "POST",
body: wasmContent,
});
if (res.ok) {
2024-12-18 17:26:24 +01:00
console.log(`Uploaded ${node.id}`);
} else {
const text = await res.text();
2024-12-20 13:40:14 +01:00
console.log(`Failed to upload ${node.id}: ${res.status} ${text}`);
2024-12-17 18:15:21 +01:00
}
}
for (const node of nodes) {
await postNode(node);
}