This repository has been archived by the owner on Jan 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* soupify tmp outputs, support for running with core * more core integration * fix test * add minor comment * support using core * fix builder not recognizing <chip /> * fix types * fix path not resolved issue with builds * fix type issues with dependency update * update core with fixed schematic symbols types
- Loading branch information
Showing
20 changed files
with
269 additions
and
126 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
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
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
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 was deleted.
Oops, something went wrong.
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,29 @@ | ||
import { readFile } from "node:fs/promises" | ||
import Debug from "debug" | ||
|
||
const debug = Debug("tscircuit:soupify") | ||
|
||
export const getExportNameFromFile = async (filePath: string) => { | ||
debug(`reading ${filePath}`) | ||
const targetFileContent = await readFile(filePath, "utf-8") | ||
|
||
let exportName: string | undefined | ||
if (targetFileContent.includes("export default")) { | ||
exportName = "default" | ||
} else { | ||
// Look for "export const <name>" or "export function <name>" | ||
const exportRegex = /export\s+(?:const|function)\s+(\w+)/g | ||
const match = exportRegex.exec(targetFileContent) | ||
if (match) { | ||
exportName = match[1] | ||
} | ||
} | ||
|
||
if (!exportName) { | ||
throw new Error( | ||
`Couldn't derive an export name and didn't find default export in "${filePath}"`, | ||
) | ||
} | ||
|
||
return exportName | ||
} |
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,13 @@ | ||
import Path from "node:path" | ||
|
||
export const getTmpEntrypointFilePath = (filePath: string) => { | ||
const tmpEntrypointPath = Path.join( | ||
Path.dirname(filePath), | ||
Path.basename(filePath).replace(/\.[^\.]+$/, "") + ".__tmp_entrypoint.tsx", | ||
) | ||
const tmpOutputPath = Path.join( | ||
Path.dirname(filePath), | ||
Path.basename(filePath).replace(/\.[^\.]+$/, "") + ".__tmp_output.json", | ||
) | ||
return { tmpEntrypointPath, tmpOutputPath } | ||
} |
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 @@ | ||
export * from "./soupify" |
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,60 @@ | ||
import Debug from "debug" | ||
import { writeFileSync } from "fs" | ||
import { readFile, unlink } from "node:fs/promises" | ||
import $ from "dax-sh" | ||
import kleur from "kleur" | ||
import { AppContext } from "../util/app-context" | ||
|
||
const debug = Debug("tscircuit:soupify") | ||
|
||
/** | ||
* Runs the entrypoint file to generate circuit json (soup) for a given file | ||
*/ | ||
export const runEntrypointFile = async ( | ||
{ | ||
tmpEntrypointPath, | ||
tmpOutputPath, | ||
}: { tmpEntrypointPath: string; tmpOutputPath: string }, | ||
ctx: Pick<AppContext, "runtime" | "params">, | ||
) => { | ||
debug(`using runtime ${ctx.runtime}`) | ||
const processCmdPart1 = | ||
ctx.runtime === "node" | ||
? $`npx tsx ${tmpEntrypointPath}` | ||
: $`bun ${tmpEntrypointPath}` | ||
|
||
debug(`starting process....`) | ||
const processResult = await processCmdPart1 | ||
.stdout(debug.enabled ? "inheritPiped" : "piped") | ||
.stderr(debug.enabled ? "inheritPiped" : "piped") | ||
.noThrow() | ||
|
||
const rawSoup = await readFile(tmpOutputPath, "utf-8") | ||
const errText = processResult.stderr | ||
|
||
if (ctx.params.cleanup !== false) { | ||
debug(`deleting ${tmpEntrypointPath}`) | ||
await unlink(tmpEntrypointPath) | ||
debug(`deleting ${tmpOutputPath}`) | ||
await unlink(tmpOutputPath) | ||
} | ||
|
||
try { | ||
debug(`parsing result of soupify...`) | ||
const soup = JSON.parse(rawSoup) | ||
|
||
if (soup.COMPILE_ERROR) { | ||
// console.log(kleur.red(`Failed to compile ${filePath}`)) | ||
console.log(kleur.red(soup.COMPILE_ERROR)) | ||
throw new Error(soup.COMPILE_ERROR) | ||
} | ||
|
||
return soup | ||
} catch (e: any) { | ||
// console.log(kleur.red(`Failed to parse result of soupify: ${e.toString()}`)) | ||
const t = Date.now() | ||
console.log(`Dumping raw output to .tscircuit/err-${t}.log`) | ||
writeFileSync(`.tscircuit/err-${t}.log`, rawSoup + "\n\n" + errText) | ||
throw new Error(errText) | ||
} | ||
} |
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,52 @@ | ||
import { AppContext } from "../util/app-context" | ||
import { z } from "zod" | ||
import $ from "dax-sh" | ||
import * as Path from "path" | ||
import { unlink } from "node:fs/promises" | ||
import kleur from "kleur" | ||
import { writeFileSync } from "fs" | ||
import { readFile } from "fs/promises" | ||
import Debug from "debug" | ||
import { getExportNameFromFile } from "./get-export-name-from-file" | ||
import { getTmpEntrypointFilePath } from "./get-tmp-entrpoint-filepath" | ||
import { runEntrypointFile } from "./run-entrypoint-file" | ||
|
||
const debug = Debug("tscircuit:soupify") | ||
|
||
export const soupifyWithCore = async ( | ||
params: { | ||
filePath: string | ||
exportName?: string | ||
}, | ||
ctx: Pick<AppContext, "runtime" | "params">, | ||
) => { | ||
let { filePath, exportName } = params | ||
|
||
exportName ??= await getExportNameFromFile(filePath) | ||
|
||
const { tmpEntrypointPath, tmpOutputPath } = | ||
getTmpEntrypointFilePath(filePath) | ||
|
||
debug(`writing to ${tmpEntrypointPath}`) | ||
writeFileSync( | ||
tmpEntrypointPath, | ||
` | ||
import React from "react" | ||
import { Project } from "@tscircuit/core" | ||
import * as EXPORTS from "./${Path.basename(filePath)}" | ||
import { writeFileSync } from "node:fs" | ||
const Component = EXPORTS["${exportName}"] | ||
const project = new Project() | ||
project.add(<Component />) | ||
project.render() | ||
writeFileSync("${tmpOutputPath}", JSON.stringify(project.getCircuitJson())) | ||
`.trim(), | ||
) | ||
|
||
return await runEntrypointFile({ tmpEntrypointPath, tmpOutputPath }, ctx) | ||
} |
Oops, something went wrong.