@tscircuit/cli 0.0.256 → 0.0.258
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bun.lockb +0 -0
- package/cli/lib/cmd-fns/gen-jlcpcb-component.ts +64 -0
- package/cli/lib/cmd-fns/index.ts +1 -0
- package/cli/lib/get-program.ts +8 -0
- package/dist/cli.js +54 -12
- package/package.json +3 -2
package/bun.lockb
CHANGED
|
Binary file
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import path from "path"
|
|
2
|
+
import fs from "fs/promises"
|
|
3
|
+
import { AppContext } from "../util/app-context"
|
|
4
|
+
import { convertEasyEdaJsonToVariousFormats } from "easyeda"
|
|
5
|
+
|
|
6
|
+
export const genJlcpcbComponent = async (
|
|
7
|
+
ctx: AppContext,
|
|
8
|
+
{ partNumberOrUrl }: { partNumberOrUrl: string },
|
|
9
|
+
): Promise<void> => {
|
|
10
|
+
try {
|
|
11
|
+
const resolvedPartNumber = resolvePartNumber(partNumberOrUrl)
|
|
12
|
+
const outputDir = path.resolve(ctx.cwd, "gen")
|
|
13
|
+
const outputFile = path.join(outputDir, `${resolvedPartNumber}.tsx`)
|
|
14
|
+
|
|
15
|
+
await fs.mkdir(outputDir, { recursive: true })
|
|
16
|
+
|
|
17
|
+
await convertEasyEdaJsonToVariousFormats({
|
|
18
|
+
jlcpcbPartNumberOrFilepath: resolvedPartNumber,
|
|
19
|
+
outputFilename: outputFile,
|
|
20
|
+
formatType: "tsx",
|
|
21
|
+
})
|
|
22
|
+
} catch (error) {
|
|
23
|
+
console.error(
|
|
24
|
+
`Error generating JLCPCB component:`,
|
|
25
|
+
(error as Error).message,
|
|
26
|
+
)
|
|
27
|
+
throw error
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const PART_NUMBER_PREFIX = "C"
|
|
32
|
+
|
|
33
|
+
const resolvePartNumber = (partNumberOrUrl: string): string => {
|
|
34
|
+
if (
|
|
35
|
+
partNumberOrUrl.startsWith("http://") ||
|
|
36
|
+
partNumberOrUrl.startsWith("https://")
|
|
37
|
+
) {
|
|
38
|
+
const partNumber = extractPartNumberFromUrl(partNumberOrUrl)
|
|
39
|
+
|
|
40
|
+
if (!partNumber) {
|
|
41
|
+
throw new Error("Could not extract a valid part number from URL")
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return partNumber
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (!isValidPartNumber(partNumberOrUrl)) {
|
|
48
|
+
throw new Error(
|
|
49
|
+
`Invalid part number: Must start with '${PART_NUMBER_PREFIX}'`,
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return partNumberOrUrl.toUpperCase()
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const extractPartNumberFromUrl = (url: string): string | null => {
|
|
57
|
+
const partNumber = url.split("/").at(-1)
|
|
58
|
+
return partNumber && isValidPartNumber(partNumber)
|
|
59
|
+
? partNumber.toUpperCase()
|
|
60
|
+
: null
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const isValidPartNumber = (partNumber: string): boolean =>
|
|
64
|
+
partNumber.toUpperCase().startsWith(PART_NUMBER_PREFIX)
|
package/cli/lib/cmd-fns/index.ts
CHANGED
|
@@ -43,3 +43,4 @@ export { exportKicadPcb } from "./export-kicad-pcb"
|
|
|
43
43
|
export { devServerFulfillExportRequests } from "./dev-server-fulfill-export-requests"
|
|
44
44
|
export { lintCmd as lint } from "./lint"
|
|
45
45
|
export { renderCmd as render } from "./render"
|
|
46
|
+
export { genJlcpcbComponent } from "./gen-jlcpcb-component"
|
package/cli/lib/get-program.ts
CHANGED
|
@@ -67,6 +67,14 @@ export const getProgram = (ctx: AppContext) => {
|
|
|
67
67
|
.option("-t, --type <type>", "Output file type (png or svg)")
|
|
68
68
|
.action((args) => CMDFN.render(ctx, args))
|
|
69
69
|
|
|
70
|
+
const genCmd = cmd.command("gen").description("Generate components")
|
|
71
|
+
genCmd
|
|
72
|
+
.command("jlcpcb <jlcpcbPartNumberOrUrl>")
|
|
73
|
+
.description("Generate JLCPCB-specific files")
|
|
74
|
+
.action((partNumberOrUrl, args) =>
|
|
75
|
+
CMDFN.genJlcpcbComponent(ctx, { partNumberOrUrl, ...args }),
|
|
76
|
+
)
|
|
77
|
+
|
|
70
78
|
const authCmd = cmd.command("auth").description("Login/logout")
|
|
71
79
|
authCmd
|
|
72
80
|
.command("login")
|