From 24c24030328c870adb3bea4ac9b804018a0114ff Mon Sep 17 00:00:00 2001 From: Bhavya Jain Date: Thu, 11 Sep 2025 21:56:51 +0530 Subject: [PATCH] Add CLI export command for GLB/JSON using circuit-json-to-gltf --- cli/main.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/cli/main.ts b/cli/main.ts index b2f81800e..b4bb445ea 100644 --- a/cli/main.ts +++ b/cli/main.ts @@ -5,6 +5,8 @@ import * as chokidar from "chokidar" import * as fs from "fs" import { createServer } from "../lib/server/createServer" import { getLocalFileDependencies } from "../lib/dependency-analysis/getLocalFileDependencies" +// Add GLB export support +import { convertCircuitJsonToGltf } from "circuit-json-to-gltf" const program = new Command() @@ -93,4 +95,44 @@ circuit.add() console.log(`Watching ${file} and its dependencies...`) }) + +program + .command("export") + .description("Export snippet to a file") + .argument("", "Path to the snippet file") + .option("-f, --format ", "Export format (json|glb)", "json") + .option("-o, --output ", "Output file path") + .action(async (file: string, options: { format: string, output?: string }) => { + const absolutePath = path.resolve(file); + const format = options.format; + const outputPath = options.output || (format === "glb" ? "snippet.glb" : "snippet.json"); + + // Load the snippet data (assumes default export) + let snippetData; + try { + snippetData = (await import(absolutePath)).default; + } catch (err) { + console.error("Failed to load snippet:", err); + process.exit(1); + } + + if (format === "glb") { + const glbBuffer = await convertCircuitJsonToGltf(snippetData, { format: "glb" }); + if (glbBuffer instanceof ArrayBuffer) { + await fs.promises.writeFile(outputPath, Buffer.from(new Uint8Array(glbBuffer))); + } else if (Buffer.isBuffer(glbBuffer)) { + await fs.promises.writeFile(outputPath, glbBuffer); + } else if (glbBuffer instanceof Uint8Array) { + await fs.promises.writeFile(outputPath, Buffer.from(glbBuffer)); + } else { + throw new Error("Unsupported GLB buffer type returned by convertCircuitJsonToGltf"); + } + console.log(`Exported GLB to ${outputPath}`); + } else { + const gltf = await convertCircuitJsonToGltf(snippetData, { format: "gltf" }); + await fs.promises.writeFile(outputPath, JSON.stringify(gltf, null, 2)); + console.log(`Exported JSON to ${outputPath}`); + } + }); + program.parse()