Skip to content

Commit

Permalink
Add gen jlcpcb <jlcpcbPartNumber> command (#210)
Browse files Browse the repository at this point in the history
* Add gen jlcpcb command

* Install easyeda

* Implement command function to generate jlcpcb component

* Use convertEasyEdaJsonToVariousFormats from easyeda instead of cli command

* Support generating components from URL, improve code readability
  • Loading branch information
andrii-balitskyi authored Oct 14, 2024
1 parent 424c566 commit 6d9f15f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
Binary file modified bun.lockb
Binary file not shown.
64 changes: 64 additions & 0 deletions cli/lib/cmd-fns/gen-jlcpcb-component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import path from "path"
import fs from "fs/promises"
import { AppContext } from "../util/app-context"
import { convertEasyEdaJsonToVariousFormats } from "easyeda"

export const genJlcpcbComponent = async (
ctx: AppContext,
{ partNumberOrUrl }: { partNumberOrUrl: string },
): Promise<void> => {
try {
const resolvedPartNumber = resolvePartNumber(partNumberOrUrl)
const outputDir = path.resolve(ctx.cwd, "gen")
const outputFile = path.join(outputDir, `${resolvedPartNumber}.tsx`)

await fs.mkdir(outputDir, { recursive: true })

await convertEasyEdaJsonToVariousFormats({
jlcpcbPartNumberOrFilepath: resolvedPartNumber,
outputFilename: outputFile,
formatType: "tsx",
})
} catch (error) {
console.error(
`Error generating JLCPCB component:`,
(error as Error).message,
)
throw error
}
}

const PART_NUMBER_PREFIX = "C"

const resolvePartNumber = (partNumberOrUrl: string): string => {
if (
partNumberOrUrl.startsWith("http://") ||
partNumberOrUrl.startsWith("https://")
) {
const partNumber = extractPartNumberFromUrl(partNumberOrUrl)

if (!partNumber) {
throw new Error("Could not extract a valid part number from URL")
}

return partNumber
}

if (!isValidPartNumber(partNumberOrUrl)) {
throw new Error(
`Invalid part number: Must start with '${PART_NUMBER_PREFIX}'`,
)
}

return partNumberOrUrl.toUpperCase()
}

const extractPartNumberFromUrl = (url: string): string | null => {
const partNumber = url.split("/").at(-1)
return partNumber && isValidPartNumber(partNumber)
? partNumber.toUpperCase()
: null
}

const isValidPartNumber = (partNumber: string): boolean =>
partNumber.toUpperCase().startsWith(PART_NUMBER_PREFIX)
1 change: 1 addition & 0 deletions cli/lib/cmd-fns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ export { exportKicadPcb } from "./export-kicad-pcb"
export { devServerFulfillExportRequests } from "./dev-server-fulfill-export-requests"
export { lintCmd as lint } from "./lint"
export { renderCmd as render } from "./render"
export { genJlcpcbComponent } from "./gen-jlcpcb-component"
8 changes: 8 additions & 0 deletions cli/lib/get-program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ export const getProgram = (ctx: AppContext) => {
.option("-t, --type <type>", "Output file type (png or svg)")
.action((args) => CMDFN.render(ctx, args))

const genCmd = cmd.command("gen").description("Generate components")
genCmd
.command("jlcpcb <jlcpcbPartNumberOrUrl>")
.description("Generate JLCPCB-specific files")
.action((partNumberOrUrl, args) =>
CMDFN.genJlcpcbComponent(ctx, { partNumberOrUrl, ...args }),
)

const authCmd = cmd.command("auth").description("Login/logout")
authCmd
.command("login")
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"dax-sh": "^0.39.2",
"debug": "^4.3.4",
"delay": "^6.0.0",
"easyeda": "^0.0.36",
"esbuild": "^0.20.2",
"fast-glob": "^3.3.2",
"glob": "^10.4.5",
Expand Down

0 comments on commit 6d9f15f

Please sign in to comment.