Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion bun.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"@tscircuit/props": "^0.0.341",
"@tscircuit/runframe": "^0.0.1036",
"@tscircuit/schematic-match-adapt": "^0.0.22",
"@tscircuit/simple-3d-svg": "^0.0.38",
"@types/bun": "^1.2.2",
"@types/configstore": "^6.0.2",
"@types/debug": "^4.1.12",
Expand Down
51 changes: 36 additions & 15 deletions lib/shared/snapshot-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import path from "node:path"
import { globbySync } from "globby"
import kleur from "kleur"
import looksSame from "looks-same"
import sharp from "sharp"

import {
convertCircuitJsonToPcbSvg,
convertCircuitJsonToSchematicSvg,
} from "circuit-to-svg"
import { convertCircuitJsonToSimple3dSvg } from "circuit-json-to-simple-3d"
import { convertCircuitJsonToGltf } from "circuit-json-to-gltf"
import { renderGLTFToPNGBufferFromGLBBuffer } from "poppygl"
import { generateCircuitJson } from "lib/shared/generate-circuit-json"
import type { PlatformConfig } from "@tscircuit/props"
import {
Expand Down Expand Up @@ -92,18 +91,39 @@ export const snapshotProject = async ({
})
const pcbSvg = convertCircuitJsonToPcbSvg(circuitJson)
const schSvg = convertCircuitJsonToSchematicSvg(circuitJson)
const svg3d = threeD
? await convertCircuitJsonToSimple3dSvg(circuitJson)
: null
let png3d: Buffer | null = null
if (threeD) {
const glbBuffer = await convertCircuitJsonToGltf(circuitJson, {
format: "glb",
})
if (!(glbBuffer instanceof ArrayBuffer)) {
throw new Error(
"Expected ArrayBuffer from convertCircuitJsonToGltf with glb format",
)
}
png3d = await renderGLTFToPNGBufferFromGLBBuffer(glbBuffer, {
camPos: [10, 10, 10],
lookAt: [0, 0, 0],
})
}

const snapDir = path.join(path.dirname(file), "__snapshots__")
fs.mkdirSync(snapDir, { recursive: true })

const base = path.basename(file).replace(/\.tsx$/, "")
const pairs: Array<["pcb" | "schematic" | "3d", string]> = []
if (pcbOnly || !schematicOnly) pairs.push(["pcb", pcbSvg])
if (schematicOnly || !pcbOnly) pairs.push(["schematic", schSvg])
if (threeD && svg3d) pairs.push(["3d", svg3d])
const snapshots: Array<
| { type: "pcb" | "schematic"; content: string; isBinary: false }
| { type: "3d"; content: Buffer; isBinary: true }
> = []
if (pcbOnly || !schematicOnly) {
snapshots.push({ type: "pcb", content: pcbSvg, isBinary: false })
}
if (schematicOnly || !pcbOnly) {
snapshots.push({ type: "schematic", content: schSvg, isBinary: false })
}
if (threeD && png3d) {
snapshots.push({ type: "3d", content: png3d, isBinary: true })
}

if (!looksSame) {
console.error(
Expand All @@ -112,19 +132,20 @@ export const snapshotProject = async ({
return onExit(1)
}

for (const [type, newSvg] of pairs) {
for (const snapshot of snapshots) {
const { type } = snapshot
const is3d = type === "3d"
const snapPath = path.join(
snapDir,
`${base}-${type}.snap.${is3d ? "png" : "svg"}`,
)
const existing = fs.existsSync(snapPath)

const newContentBuffer = is3d
? await sharp(Buffer.from(newSvg)).png().toBuffer()
: Buffer.from(newSvg, "utf8")
const newContentBuffer = snapshot.isBinary
? snapshot.content
: Buffer.from(snapshot.content, "utf8")

const newContentForFile = is3d ? newContentBuffer : newSvg
const newContentForFile = snapshot.content

if (!existing) {
fs.writeFileSync(snapPath, newContentForFile)
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"@tscircuit/props": "^0.0.341",
"@tscircuit/runframe": "^0.0.1036",
"@tscircuit/schematic-match-adapt": "^0.0.22",
"@tscircuit/simple-3d-svg": "^0.0.38",
"@types/bun": "^1.2.2",
"@types/configstore": "^6.0.2",
"@types/debug": "^4.1.12",
Expand Down
Loading