-
Notifications
You must be signed in to change notification settings - Fork 13
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
- Loading branch information
Showing
14 changed files
with
240 additions
and
119 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 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(), | ||
) | ||
|
||
await runEntrypointFile({ tmpEntrypointPath, tmpOutputPath }, ctx) | ||
} |
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,66 @@ | ||
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 { soupifyWithCore } from "./soupify-with-core" | ||
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 soupify = async ( | ||
params: { | ||
filePath: string | ||
exportName?: string | ||
/** | ||
* Use @tscircuit/core instead of @tscircuit/builder, this will be the | ||
* default eventually | ||
*/ | ||
useCore?: boolean | ||
}, | ||
ctx: Pick<AppContext, "runtime" | "params">, | ||
) => { | ||
let { filePath, exportName, useCore } = params | ||
if (useCore) return soupifyWithCore(params, ctx) | ||
|
||
exportName ??= await getExportNameFromFile(filePath) | ||
|
||
const { tmpEntrypointPath, tmpOutputPath } = | ||
getTmpEntrypointFilePath(filePath) | ||
|
||
debug(`writing to ${tmpEntrypointPath}`) | ||
writeFileSync( | ||
tmpEntrypointPath, | ||
` | ||
import React from "react" | ||
import { createRoot } from "@tscircuit/react-fiber" | ||
import { createProjectBuilder } from "@tscircuit/builder" | ||
import { writeFileSync } from "node:fs" | ||
import * as EXPORTS from "./${Path.basename(filePath)}" | ||
const Component = EXPORTS["${exportName}"] | ||
if (!Component) { | ||
console.log(JSON.stringify({ | ||
COMPILE_ERROR: 'Failed to find "${exportName}" export in "${filePath}"' | ||
})) | ||
process.exit(0) | ||
} | ||
const projectBuilder = createProjectBuilder() | ||
const elements = await createRoot().render(<Component />, projectBuilder) | ||
writeFileSync("${tmpOutputPath}", JSON.stringify(elements)) | ||
`.trim(), | ||
) | ||
|
||
await runEntrypointFile({ tmpEntrypointPath, tmpOutputPath }, ctx) | ||
} |
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,10 @@ | ||
import { test, expect, describe } from "bun:test" | ||
import { $ } from "bun" | ||
|
||
test("soupify (core)", async () => { | ||
const result = | ||
await $`bun cli/cli.ts soupify --core -y --file ./example-project/examples/basic-chip.tsx`.text() | ||
|
||
console.log(result) | ||
expect(result).toContain("10kohm") | ||
Check failure on line 9 in cli/tests/soupify-core.test.ts GitHub Actions / testserror: expect(received).toContain(expected)
|
||
}) |
Oops, something went wrong.