Skip to content
Open
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
14 changes: 12 additions & 2 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 35 additions & 3 deletions cli/check/schematic-placement/register.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { analyzeSchematicPlacement } from "@tscircuit/circuit-json-schematic-placement-analysis"
import { mkdir, readdir, rm, writeFile } from "node:fs/promises"
import path from "node:path"
import {
analyzeSchematicPlacement,
createSchematicPlacementIssueArtifacts,
} from "@tscircuit/circuit-json-schematic-placement-analysis"
import type { PlatformConfig } from "@tscircuit/props"
import type { AnyCircuitElement, CircuitJson } from "circuit-json"
import type { Command } from "commander"
Expand All @@ -16,7 +21,14 @@ export const checkSchematicPlacement = async (file?: string) => {
allowPrebuiltCircuitJson: true,
})) as AnyCircuitElement[]

return analyzeSchematicPlacement(circuitJson as CircuitJson).getString()
const analysis = analyzeSchematicPlacement(circuitJson as CircuitJson)
return {
output: analysis.getString(),
artifacts: createSchematicPlacementIssueArtifacts(
circuitJson as CircuitJson,
{ analysis },
),
}
}

export const registerCheckSchematicPlacement = (program: Command) => {
Expand All @@ -27,8 +39,28 @@ export const registerCheckSchematicPlacement = (program: Command) => {
.argument("[file]", "Path to the entry file")
.action(async (file?: string) => {
try {
const output = await checkSchematicPlacement(file)
const { output, artifacts } = await checkSchematicPlacement(file)
console.log(output)
const outputDir = path.resolve("dist", "schematic-placement")
// Remove only files generated by this command, so resolved issues from
// previous runs do not remain alongside the current reports.
const previousFiles = await readdir(outputDir, {
withFileTypes: true,
}).catch((error: NodeJS.ErrnoException) => {
if (error.code === "ENOENT") return []
throw error
})
for (const entry of previousFiles) {
if (entry.isFile() && /^issue-\d+-[A-Za-z]+\.svg$/.test(entry.name)) {
await rm(path.join(outputDir, entry.name))
}
}
if (artifacts.length > 0) await mkdir(outputDir, { recursive: true })
for (const artifact of artifacts) {
const outputPath = path.join(outputDir, artifact.fileName)
await writeFile(outputPath, artifact.content)
console.log(`Schematic placement artifact written to ${outputPath}`)
}
} catch (error) {
console.error(error instanceof Error ? error.message : String(error))
process.exit(1)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@tscircuit/check-shorts": "https://jscdn.tscircuit.com/@tscircuit/check-shorts/0.0.19.tgz",
"@tscircuit/circuit-json-placement-analysis": "^0.0.9",
"@tscircuit/circuit-json-routing-analysis": "^0.0.8",
"@tscircuit/circuit-json-schematic-placement-analysis": "github:tscircuit/circuit-json-schematic-placement-analysis#db2cd1ca2ce51490e3230e53c3d7b361eeb14446",
"@tscircuit/circuit-json-schematic-placement-analysis": "github:tscircuit/circuit-json-schematic-placement-analysis#690e01c161f54a72f24c848e96a74c2584690f86",
"@tscircuit/circuit-json-util": "^0.0.112",
"@tscircuit/eval": "^0.0.1016",
"@tscircuit/fake-snippets": "^0.0.182",
Expand Down
50 changes: 47 additions & 3 deletions tests/cli/check/check-schematic-placement.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "bun:test"
import { rm, writeFile } from "node:fs/promises"
import { readFile, readdir, rm, writeFile } from "node:fs/promises"
import path from "node:path"
import { checkSchematicPlacement } from "../../../cli/check/schematic-placement/register"
import { getCliTestFixture } from "../../fixtures/get-cli-test-fixture"
Expand Down Expand Up @@ -100,7 +100,7 @@ const misalignedPinPairsCircuitJson = [
]

test("tsci check schematic-placement prints schematic placement analysis", async () => {
const { runCommand } = await getCliTestFixture()
const { runCommand, tmpDir } = await getCliTestFixture()
const circuitPath = path.join(
process.cwd(),
`tmp-check-schematic-placement-${Date.now()}-${Math.random().toString(36).slice(2)}.tsx`,
Expand All @@ -117,10 +117,37 @@ test("tsci check schematic-placement prints schematic placement analysis", async

expect(exitCode).toBe(0)
expect(stderr).toBe("")
expect(stdout.trim()).toContain(expected)
expect(stdout.trim()).toContain(expected.output)
expect(stdout).toContain("<SchematicBoxPositions>")
expect(stdout).toContain('componentName="R1"')
expect(stdout).toContain('componentName="C1"')
const outputDir = path.join(tmpDir, "dist", "schematic-placement")
expect((await readdir(outputDir)).sort()).toEqual(
expected.artifacts.map((artifact) => artifact.fileName).sort(),
)
expect(expected.artifacts.length).toBeGreaterThan(0)
for (const artifact of expected.artifacts) {
const svg = await readFile(
path.join(outputDir, artifact.fileName),
"utf8",
)
expect(svg).toBe(artifact.content)
expect(svg.match(/data-issue-index=/g)).toHaveLength(1)
expect(stdout).toContain(path.join(outputDir, artifact.fileName))
}
// A later clean result removes stale generated reports but keeps user files.
await writeFile(path.join(outputDir, "notes.svg"), "keep this")
const cleanCircuitPath = path.join(tmpDir, "clean.circuit.json")
await writeFile(cleanCircuitPath, "[]")
const clean = await runCommand(
`tsci check schematic-placement ${cleanCircuitPath}`,
)
expect(clean.exitCode).toBe(0)
expect(clean.stderr).toBe("")
expect(await readdir(outputDir)).toEqual(["notes.svg"])
expect(await readFile(path.join(outputDir, "notes.svg"), "utf8")).toBe(
"keep this",
)
} finally {
await rm(circuitPath, { force: true })
}
Expand All @@ -141,3 +168,20 @@ test("tsci check schematic-placement reports a better vertical pin alignment", a
'<ComponentPinsWouldAlignWithVerticalShift firstComponentName="U1" secondComponentName="U2" targetComponentName="U2" deltaSchY="-1" newSchY="0" currentlyAlignedPinCount="0" alignedPinCount="2"',
)
})

test("tsci check schematic-placement creates no artifact directory for a clean circuit", async () => {
const { runCommand, tmpDir } = await getCliTestFixture()
const circuitPath = path.join(tmpDir, "clean.circuit.json")
await writeFile(circuitPath, "[]")
const { stdout, stderr, exitCode } = await runCommand(
`tsci check schematic-placement ${circuitPath}`,
)
expect(exitCode).toBe(0)
expect(stderr).toBe("")
expect(stdout.trim()).toBe("")
expect(
await readdir(path.join(tmpDir, "dist", "schematic-placement")).catch(
(error) => error.code,
),
).toBe("ENOENT")
})
Loading