72 lines
2.1 KiB
JavaScript

#!/usr/bin/env node
import { readFile } from "node:fs/promises";
import {
applyCanvasOperation,
getCanvas,
getCurrentUser,
listCanvases,
} from "./api-client.mjs";
function printHelp() {
process.stdout.write(`Usage:
yedir me
yedir canvases list [--workspace-type canvas|agent|video] [--limit 24]
yedir canvases get <canvas-id>
yedir canvas apply <canvas-id> --operation-file <path|->
Environment:
YEDIR_BASE_URL Site URL, default http://localhost:3000
YEDIR_ACCESS_TOKEN User Access Key created in the site
`);
}
function readFlag(args, name, fallback) {
const index = args.indexOf(name);
return index >= 0 && args[index + 1] ? args[index + 1] : fallback;
}
async function readJsonFile(path) {
const raw = path === "-" ? await new Response(process.stdin).text() : await readFile(path, "utf8");
return JSON.parse(raw);
}
async function main() {
const args = process.argv.slice(2);
let result;
if (args[0] === "me") {
result = await getCurrentUser();
} else if (args[0] === "canvases" && args[1] === "list") {
result = await listCanvases({
workspaceType: readFlag(args, "--workspace-type", "canvas"),
limit: Number(readFlag(args, "--limit", "24")),
});
} else if (args[0] === "canvases" && args[1] === "get" && args[2]) {
result = await getCanvas(args[2]);
} else if (args[0] === "canvas" && args[1] === "apply" && args[2]) {
const operationFile = readFlag(args, "--operation-file", "");
if (!operationFile) throw new Error("--operation-file is required");
const raw = await readJsonFile(operationFile);
result = await applyCanvasOperation(args[2], raw.operationPlan || raw);
} else {
printHelp();
process.exitCode = args.length === 0 || args.includes("--help") ? 0 : 2;
return;
}
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
}
main().catch((error) => {
process.stderr.write(
`${JSON.stringify({
success: false,
error: error instanceof Error ? error.message : String(error),
status: Number(error?.status) || undefined,
details: error?.payload || undefined,
})}\n`
);
process.exitCode = 1;
});