-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
gen jlcpcb <jlcpcbPartNumber>
command (#210)
* 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
1 parent
424c566
commit 6d9f15f
Showing
5 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters