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 #217 from qalbun-salim/feat/cmd-pnp-csv
Browse files Browse the repository at this point in the history
  • Loading branch information
imrishabh18 authored Oct 13, 2024
2 parents 8690543 + 412ad15 commit 604727d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
32 changes: 32 additions & 0 deletions cli/lib/cmd-fns/export-pnp-csv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { z } from "zod"
import { exportPnpCsvToBuffer } from "../export-fns/export-pnp-csv"
import { AppContext } from "../util/app-context"
import fs from "fs/promises"
import kleur from "kleur"

export const exportPnpCsv = async (ctx: AppContext, args: any) => {
const params = z
.object({
file: z.string().optional(),
input: z.string().optional(),
export: z.string().optional(),
outputfile: z.string().optional().default("pnp.csv"),
})
.refine((data) => data.file || data.input, {
message: "Either 'file' or 'input' must be provided",
})
.parse(args)

const inputFile = params.input || params.file

const pnpCsvBuffer = await exportPnpCsvToBuffer(
{
example_file_path: inputFile!,
export_name: params.export,
},
ctx,
)
console.log(kleur.gray(`[writing to ${params.outputfile}]...`))
await fs.writeFile(params.outputfile, pnpCsvBuffer)
console.log(kleur.green(`Pnp CSV file exported to ${params.outputfile}`))
}
1 change: 1 addition & 0 deletions cli/lib/cmd-fns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export { configClear } from "./config-clear"
export { openCmd as open } from "./open"
export { versionCmd as version } from "./version"
export { exportGerbersCmd as exportGerbers } from "./export-gerbers"
export { exportPnpCsv } from "./export-pnp-csv"
export { exportKicadPcb } from "./export-kicad-pcb"
export { devServerFulfillExportRequests } from "./dev-server-fulfill-export-requests"
export { lintCmd as lint } from "./lint"
Expand Down
11 changes: 11 additions & 0 deletions cli/lib/get-program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,17 @@ export const getProgram = (ctx: AppContext) => {
.option("--outputfile <outputfile>", "Output file name", "output.kicad_pcb")
.action((args) => CMDFN.exportKicadPcb(ctx, args))

exportCmd
.command("pnp_csv")
.description("Export Plug n Play CSV file from an example file")
.option("--input <input>", "Input example file")
.option(
"--export <export_name>",
"Name of export to soupify, if not specified, soupify the default/only export",
)
.option("--outputfile <outputfile>", "Output file name", "pnp.csv")
.action((args) => CMDFN.exportPnpCsv(ctx, args))

cmd
.command("soupify")
.description("Convert an example file to tscircuit soup")
Expand Down
24 changes: 24 additions & 0 deletions cli/tests/export-pnp-csv.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { test, expect } from "bun:test"
import { $ } from "bun"
import { temporaryDirectory } from "tempy"
import { join } from "path/posix"
import { existsSync, readFileSync } from "fs"

test("tsci export pnp_csv --input example-project/examples/macrokeypad.tsx", async () => {
const tempDir = temporaryDirectory()
const pnpCsvPath = join(tempDir, "pnp.csv")
const { stdout, stderr } =
await $`bun cli/cli.ts export pnp_csv --input example-project/examples/macrokeypad.tsx --outputfile ${pnpCsvPath} --no-color`

expect(stderr.toString()).toBe("")
expect(stdout.toString()).toContain("pnp.csv")

expect(existsSync(pnpCsvPath)).toBe(true)

const pnpCsvContent = readFileSync(pnpCsvPath, "utf-8")
expect(pnpCsvContent).toContain("Designator")
expect(pnpCsvContent).toContain("Mid X")
expect(pnpCsvContent).toContain("Mid Y")
expect(pnpCsvContent).toContain("Layer")
expect(pnpCsvContent).toContain("Rotation")
})

0 comments on commit 604727d

Please sign in to comment.