-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
import snippets from registry to development server (#423)
* import snippets from registry to development server * only run in development * only log on failed import * added env variable to dev script * used redaxios
- Loading branch information
1 parent
44fa01f
commit 7e737aa
Showing
4 changed files
with
116 additions
and
2 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,84 @@ | ||
import axios from "redaxios" | ||
import fs from "fs" | ||
import path from "path" | ||
import { DbClient } from "./db-client" | ||
|
||
const extractTsciDependencies = ( | ||
code: string, | ||
): Array<{ owner: string; name: string }> => { | ||
const regex = /@tsci\/([^.]+)\.([^"'\s]+)/g | ||
const matches = Array.from(code.matchAll(regex)) | ||
return matches.map((match) => ({ | ||
owner: match[1], | ||
name: match[2], | ||
})) | ||
} | ||
|
||
const registryApi = axios.create({ | ||
baseURL: "https://registry-api.tscircuit.com", | ||
headers: { | ||
Accept: "application/json", | ||
"Content-Type": "application/json", | ||
}, | ||
}) | ||
|
||
const fetchSnippetFromRegistry = async (owner: string, name: string) => { | ||
const response = await registryApi.get( | ||
`/snippets/get?owner_name=${owner}&unscoped_name=${name}`, | ||
) | ||
return response.data.snippet | ||
} | ||
|
||
const loadSnippetWithDependencies = async ( | ||
db: DbClient, | ||
owner: string, | ||
name: string, | ||
loadedSnippets = new Set<string>(), | ||
) => { | ||
const snippetKey = `${owner}/${name}` | ||
if (loadedSnippets.has(snippetKey)) { | ||
return | ||
} | ||
|
||
try { | ||
const snippet = await fetchSnippetFromRegistry(owner, name) | ||
|
||
if (db.getSnippetByAuthorAndName(owner, name)) return | ||
|
||
db.addSnippet(snippet) | ||
loadedSnippets.add(snippetKey) | ||
|
||
const dependencies = extractTsciDependencies(snippet.code) | ||
for (const dep of dependencies) { | ||
loadSnippetWithDependencies(db, dep.owner, dep.name, loadedSnippets) | ||
} | ||
} catch (e) { | ||
console.error(`✗ Failed to load ${snippetKey}:`, e) | ||
} | ||
} | ||
|
||
export const loadAutoloadSnippets = async (db: DbClient) => { | ||
try { | ||
const autoloadPath = path.join( | ||
path.dirname(__dirname), | ||
"db", | ||
"autoload-snippets.json", | ||
) | ||
if (fs.existsSync(autoloadPath)) { | ||
const autoloadContent = JSON.parse(fs.readFileSync(autoloadPath, "utf8")) | ||
console.log("Loading development snippets from registry...") | ||
|
||
const loadedSnippets = new Set<string>() | ||
for (const snippetRef of autoloadContent.snippets) { | ||
loadSnippetWithDependencies( | ||
db, | ||
snippetRef.owner, | ||
snippetRef.name, | ||
loadedSnippets, | ||
) | ||
} | ||
} | ||
} catch (e) { | ||
console.error("Failed to load autoload-snippets.json:", e) | ||
} | ||
} |
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,24 @@ | ||
{ | ||
"snippets": [ | ||
{ | ||
"owner": "Abse2001", | ||
"name": "Arduino-Nano-Servo-Breakout" | ||
}, | ||
{ | ||
"owner": "ShiboSoftwareDev", | ||
"name": "Wifi-Camera-Module" | ||
}, | ||
{ | ||
"owner": "imrishabh18", | ||
"name": "Arduino-nano" | ||
}, | ||
{ | ||
"owner": "seveibar", | ||
"name": "usb-c-flashlight" | ||
}, | ||
{ | ||
"owner": "AnasSarkiz", | ||
"name": "grid-of-LEDs-with-an-ESP32" | ||
} | ||
] | ||
} |
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