Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
Merge pull request #119 from tscircuit/feat/render-image-cmd
Browse files Browse the repository at this point in the history
feat: new `render` image command added
  • Loading branch information
imrishabh18 authored Aug 7, 2024
2 parents e578db2 + 10d4f9c commit 76b185d
Show file tree
Hide file tree
Showing 5 changed files with 351 additions and 43 deletions.
1 change: 1 addition & 0 deletions lib/cmd-fns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ export { versionCmd as version } from "./version"
export { exportGerbersCmd as exportGerbers } from "./export-gerbers"
export { devServerFulfillExportRequests } from "./dev-server-fulfill-export-requests"
export { lintCmd as lint } from "./lint"
export { renderCmd as render } from "./render"
45 changes: 45 additions & 0 deletions lib/cmd-fns/render.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { circuitToPng } from "circuit-to-png"
import fs from "fs"
import { soupify } from "lib/soupify"
import type { AppContext } from "lib/util/app-context"
import path from "path"

export const renderCmd = async (
ctx: AppContext,
args: {
input: string
pcb?: boolean
schematic?: boolean
output?: string
type?: string
},
) => {
const inputFile = path.resolve(args.input)
let outputFile = args.output
const outputType =
args.type || path.extname(args.output || "").slice(1) || "png"

if (!outputFile) {
const inputBase = path.basename(inputFile, path.extname(inputFile))
outputFile = `${inputBase}.${outputType}`
}

if (!args.pcb && !args.schematic) {
console.error("Error: You must specify either --pcb or --schematic")
process.exit(1)
}

const viewType = args.pcb ? "pcb" : "schematic"

console.log(`Rendering ${viewType} view of ${inputFile} to ${outputFile}`)
const soup = await soupify(
{
filePath: inputFile,
},
ctx,
)

const soupBuffer = circuitToPng(soup, viewType)

fs.writeFileSync(outputFile, soupBuffer)
}
14 changes: 13 additions & 1 deletion lib/get-program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ export const getProgram = (ctx: AppContext) => {
.description("Open browser to the TSCircuit Get Started tutorial")
.action((args) => CMDFN.go(ctx, args))

cmd
.command("render")
.description("Render circuit as image")
.requiredOption("--input <input>", "Input TypeScript file path")
.option("--pcb", "Render PCB view")
.option("--schematic", "Render schematic view")
.option("--output <output>", "Output file path")
.option("-t, --type <type>", "Output file type (png or svg)")
.action((args) => CMDFN.render(ctx, args))

const authCmd = cmd.command("auth").description("Login/logout")
authCmd
.command("login")
Expand Down Expand Up @@ -331,7 +341,9 @@ export const getProgram = (ctx: AppContext) => {

function recursiveUnhelp(cmd: Command) {
cmd.helpCommand(false)
cmd.commands.forEach((c) => recursiveUnhelp(c))
for (const c of cmd.commands) {
recursiveUnhelp(c)
}
}
recursiveUnhelp(cmd)

Expand Down
Loading

0 comments on commit 76b185d

Please sign in to comment.