-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
run a server in dev for snapshot testing
- Loading branch information
Showing
6 changed files
with
153 additions
and
3 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,14 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<link rel="icon" type="image/svg+xml" href="/vite.svg" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>EasyEDA Converter</title> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script type="module" src="/site/main.tsx"></script> | ||
<script src="https://cdn.tailwindcss.com"></script> | ||
</body> | ||
</html> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { RunFrame } from "@tscircuit/runframe/runner" | ||
import { useEffect, useState } from "react" | ||
export default () => { | ||
const params = new URLSearchParams(window.location.search) | ||
const jlcpcb_part_number = params.get("jlcpcb_part_number") | ||
|
||
const [error, setError] = useState<string | null>(null) | ||
const [tscircuitCode, setTscircuitCode] = useState<string>("") | ||
|
||
useEffect(() => { | ||
if (!jlcpcb_part_number) { | ||
return | ||
} | ||
setError(null) | ||
|
||
fetch("/api", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ jlcpcb_part_number }), | ||
}) | ||
.then((res) => res.json()) | ||
.then((data) => { | ||
if (data.error) { | ||
throw new Error(data.error) | ||
} | ||
setTscircuitCode(data.tscircuitCode) | ||
}) | ||
.catch((error) => { | ||
setError(error.message) | ||
}) | ||
}, [jlcpcb_part_number]) | ||
|
||
if (!jlcpcb_part_number) { | ||
return ( | ||
<div>No part number provided, add ?jlcpcb_part_number= to the URL</div> | ||
) | ||
} | ||
|
||
if (!tscircuitCode) { | ||
return <div>Loading...</div> | ||
} | ||
|
||
return ( | ||
<div> | ||
<RunFrame | ||
entrypoint="entrypoint.tsx" | ||
fsMap={{ | ||
"entrypoint.tsx": ` | ||
import * as Snippet from "./snippet.tsx" | ||
let bestImport = Object.keys(Snippet).find((key) => !key.startsWith("use")) | ||
const Component = Snippet[bestImport] | ||
circuit.add(<board width="10mm" height="10mm"><Component name="U1" /></board>) | ||
`, | ||
"snippet.tsx": tscircuitCode, | ||
}} | ||
/> | ||
<pre className="bg-gray-100 p-4 rounded-lg overflow-x-auto"> | ||
<code className="text-sm font-mono">{tscircuitCode}</code> | ||
</pre> | ||
</div> | ||
) | ||
} |
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,9 @@ | ||
import React from "react" | ||
import ReactDOM from "react-dom/client" | ||
import App from "./App" | ||
|
||
ReactDOM.createRoot(document.getElementById("root")!).render( | ||
<React.StrictMode> | ||
<App /> | ||
</React.StrictMode>, | ||
) |
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,49 @@ | ||
import { defineConfig } from "vite" | ||
import react from "@vitejs/plugin-react" | ||
import tsconfigPaths from "vite-tsconfig-paths" | ||
import type { Plugin } from "vite" | ||
import { fetchEasyEDAComponent, convertRawEasyEdaToTs } from "./dist/lib/index" | ||
|
||
// Create plugin to handle JLCPCB part number requests | ||
const jlcpcbPlugin = (): Plugin => ({ | ||
name: "jlcpcb-api", | ||
configureServer(server) { | ||
server.middlewares.use(async (req, res, next) => { | ||
if (req.url === "/api" && req.method === "POST") { | ||
try { | ||
const chunks = [] | ||
for await (const chunk of req) { | ||
chunks.push(chunk) | ||
} | ||
const body = JSON.parse(Buffer.concat(chunks).toString()) | ||
|
||
if (!body.jlcpcb_part_number) { | ||
res.statusCode = 400 | ||
res.end(JSON.stringify({ error: "Missing jlcpcb_part_number" })) | ||
return | ||
} | ||
|
||
const rawEasyJson = await fetchEasyEDAComponent( | ||
body.jlcpcb_part_number, | ||
) | ||
const tsxComponent = await convertRawEasyEdaToTs(rawEasyJson) | ||
|
||
res.setHeader("Content-Type", "application/json") | ||
res.end(JSON.stringify({ tscircuitCode: tsxComponent })) | ||
} catch (error: any) { | ||
res.statusCode = 500 | ||
res.end(JSON.stringify({ error: error.message })) | ||
} | ||
} else { | ||
next() | ||
} | ||
}) | ||
}, | ||
}) | ||
|
||
export default defineConfig({ | ||
plugins: [react(), tsconfigPaths(), jlcpcbPlugin()], | ||
define: { | ||
global: "globalThis", | ||
}, | ||
}) |