Skip to content

Commit

Permalink
add replication in seed
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed Oct 14, 2024
1 parent 0a9feed commit 9a0d524
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
23 changes: 23 additions & 0 deletions fake-snippets-api/lib/db/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,27 @@ exports.A555Timer = A555Timer;
snippet_type: "package",
description: "A simple package with an A555 Timer component",
})

// Add a snippet that outputs a square waveform using the a555timer

db.addSnippet({
name: "seveibar/a555timer-square-wave",
unscoped_name: "a555timer-square-wave",
owner_name: "seveibar",
code: `
import { A555Timer } from "@tsci/seveibar.a555timer"
export default () => (
<A555Timer name="U1" />
)
`.trim(),
dts: "export declare const MyChip: (props: {\n name: string;\n}) => any;\n",
compiled_js:
'"use strict";\n\nObject.defineProperty(exports, "__esModule", {\n value: true\n});\nexports.MyChip = void 0;\nvar _seveibar = require("@tsci/seveibar.a555timer");\nconst MyChip = props => /*#__PURE__*/React.createElement(_seveibar.A555Timer, {\n name: "U1"\n});\nexports.MyChip = MyChip;',
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
snippet_type: "package",
description:
"A simple package that outputs a square waveform using the a555timer",
})
}
24 changes: 22 additions & 2 deletions src/hooks/use-run-tsx/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,31 @@ export const useRunTsx = ({

const preSuppliedImports: Record<string, any> = {}

for (const importName of imports) {
async function addImport(importName: string, depth: number = 0) {
if (depth > 5) {
console.log("Max depth for imports reached")
return
}

const fullSnippetName = importName
.replace("@tsci/", "")
.replace(".", "/")
// Fetch compiled code from the server
const { snippet: importedSnippet } = await fetch(
`${apiBaseUrl}/snippets/get?name=${fullSnippetName}`,
).then((res) => res.json())

const { compiled_js, code } = importedSnippet

const importNames = getImportsFromCode(code!).filter(
(imp) => !preSuppliedImports[imp],
)

for (const importName of importNames) {
if (!preSuppliedImports[importName]) {
await addImport(importName, depth + 1)
}
}

try {
preSuppliedImports[importName] = evalCompiledJs(
importedSnippet.compiled_js,
Expand All @@ -90,6 +106,10 @@ export const useRunTsx = ({
}
}

for (const importName of imports) {
await addImport(importName)
}

preSuppliedImports["@tscircuit/core"] = tscircuitCore
preSuppliedImports["react"] = React
preSuppliedImports["jscad-fiber"] = jscadFiber
Expand Down

0 comments on commit 9a0d524

Please sign in to comment.