Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bom_csv in command line as per #183 #250

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
32 changes: 32 additions & 0 deletions cli/lib/cmd-fns/export-bom-csv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import fs from "fs/promises"
import kleur from "kleur"
import { z } from "zod"
import { exportBomCsvToBuffer } from "../export-fns/export-bom-csv"
import { AppContext } from "../util/app-context"

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

const bomCsvBuffer = await exportBomCsvToBuffer(
{
example_file_path: params.input!,
export_name: params.export,
},
ctx,
)

console.log(kleur.gray(`[writing to ${params.outputfile}]...`))
await fs.writeFile(params.outputfile, bomCsvBuffer)
console.log(
kleur.green(`Bill of Material 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 @@ -44,3 +44,4 @@ export { devServerFulfillExportRequests } from "./dev-server-fulfill-export-requ
export { lintCmd as lint } from "./lint"
export { renderCmd as render } from "./render"
export { genJlcpcbComponent } from "./gen-jlcpcb-component"
export { exportBomCsv } from "./export-bom-csv"
13 changes: 12 additions & 1 deletion cli/lib/get-program.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Command } from "commander"
import * as CMDFN from "cli/lib/cmd-fns"
import { Command } from "commander"
import type { AppContext } from "./util/app-context"

export const getProgram = (ctx: AppContext) => {
Expand Down Expand Up @@ -323,6 +323,17 @@ export const getProgram = (ctx: AppContext) => {
.option("--outputfile <outputfile>", "Output file name", "pnp.csv")
.action((args) => CMDFN.exportPnpCsv(ctx, args))

exportCmd
.command("bom_csv")
.description("Export BOM 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", "bom.csv")
.action((args) => CMDFN.exportBomCsv(ctx, args))

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

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

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

expect(existsSync(bomCsvPath)).toBe(true)

const bomCsvContent = readFileSync(bomCsvPath, "utf-8")
expect(bomCsvContent).toContain("Designator")
expect(bomCsvContent).toContain("Comment")
expect(bomCsvContent).toContain("Value")
expect(bomCsvContent).toContain("Footprint")
expect(bomCsvContent).toContain("JLCPCB Part#")
})