Skip to content
Closed
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
4 changes: 2 additions & 2 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 23 additions & 9 deletions cli/import/register.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getQueryFromParts } from "cli/utils/get-query-from-parts"
import type { Command } from "commander"
import kleur from "kleur"
import { getQueryFromParts } from "cli/utils/get-query-from-parts"
import { importComponentFromJlcpcb } from "lib/import/import-component-from-jlcpcb"
import { getRegistryApiKy } from "lib/registry-api/get-ky"
import { addPackage } from "lib/shared/add-package"
Expand All @@ -17,21 +17,30 @@ export const registerImport = (program: Command) => {
.option("--jlcpcb", "Search JLCPCB components")
.option("--lcsc", "Alias for --jlcpcb")
.option("--tscircuit", "Search tscircuit registry packages")
.option(
"--model-format <format>",
"CAD model format to import from JLCPCB (obj, step)",
"obj",
)
.action(
async (
queryParts: string[],
opts: {
jlcpcb?: boolean
lcsc?: boolean
tscircuit?: boolean
modelFormat?: "obj" | "step"
},
) => {
const query = getQueryFromParts(queryParts)
const hasFilters = opts.jlcpcb || opts.lcsc || opts.tscircuit
const searchJlc = opts.jlcpcb || opts.lcsc || !hasFilters
const searchTscircuit = opts.tscircuit || !hasFilters
const ky = getRegistryApiKy()
const spinner = ora("Searching...").start()
const spinner = ora({
text: "Searching...",
stream: process.stdout,
}).start()

let registryResults: Array<{
name: string
Expand Down Expand Up @@ -134,7 +143,10 @@ export const registerImport = (program: Command) => {
}

if (choice.type === "registry") {
const installSpinner = ora(`Installing ${choice.name}...`).start()
const installSpinner = ora({
text: `Installing ${choice.name}...`,
stream: process.stdout,
}).start()
try {
await addPackage(choice.name)
installSpinner.succeed(kleur.green(`Installed ${choice.name}`))
Expand All @@ -144,13 +156,15 @@ export const registerImport = (program: Command) => {
return process.exit(1)
}
} else {
const importSpinner = ora(
`Importing "C${choice.part}" from JLCPCB...`,
).start()
const importSpinner = ora({
text: `Importing "C${choice.part}" from JLCPCB...`,
stream: process.stdout,
}).start()
try {
const { filePath } = await importComponentFromJlcpcb(
`C${String(choice.part)}`,
)
const { filePath } = await importComponentFromJlcpcb({
jlcpcbPartNumber: `C${String(choice.part)}`,
modelFormat: opts.modelFormat,
})
importSpinner.succeed(kleur.green(`Imported ${filePath}`))
} catch (error) {
importSpinner.fail(kleur.red("Failed to import part"))
Expand Down
18 changes: 13 additions & 5 deletions lib/import/import-component-from-jlcpcb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ import { fetchEasyEDAComponent, convertRawEasyToTsx } from "easyeda/browser"
import fs from "node:fs/promises"
import path from "node:path"

export const importComponentFromJlcpcb = async (
jlcpcbPartNumber: string,
projectDir: string = process.cwd(),
) => {
export const importComponentFromJlcpcb = async ({
jlcpcbPartNumber,
projectDir = process.cwd(),
modelFormat = "obj",
}: {
jlcpcbPartNumber: string
projectDir?: string
modelFormat?: "obj" | "step"
}) => {
const component = await fetchEasyEDAComponent(jlcpcbPartNumber)
const tsx = await convertRawEasyToTsx(component)
const tsx = await convertRawEasyToTsx({
rawEasy: component,
format: modelFormat,
})
const fileName = tsx.match(/export const (\w+) = .*/)?.[1]
if (!fileName) {
throw new Error("Could not determine file name of converted component")
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"debug": "^4.4.0",
"delay": "^6.0.0",
"dsn-converter": "^0.0.63",
"easyeda": "^0.0.248",
"easyeda": "^0.0.251",
"fuse.js": "^7.1.0",
"get-port": "^7.1.0",
"globby": "^14.1.0",
Expand Down
21 changes: 21 additions & 0 deletions tests/cli/import/import-with-step-cad-model.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { test, expect } from "bun:test"
import { getCliTestFixture } from "../../fixtures/get-cli-test-fixture"
import path from "node:path"
import fs from "node:fs"

test("import command generates package from JLCPCB part", async () => {
if (process.env.CI) {
return
}

const { tmpDir, runCommand } = await getCliTestFixture({ loggedIn: true })
const { stdout, stderr } = await runCommand(
"tsci import C2040 --model-format step",
)
const filePath = path.join(tmpDir, "imports", "RP2040.tsx")
expect(fs.existsSync(filePath)).toBe(true)
const fileContent = fs.readFileSync(filePath, "utf8")
expect(fileContent).toContain("stepUrl")
expect(stderr).toBe("")
expect(stdout.toLowerCase()).toContain("imported")
}, 20_000)
Loading