Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiboSoftwareDev committed Sep 28, 2024
1 parent c623299 commit ac80052
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 72 deletions.
71 changes: 32 additions & 39 deletions cli/lib/soupify/run-entrypoint-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,42 @@ export const runEntrypointFile = async (
}: { tmpEntrypointPath: string; tmpOutputPath: string },
ctx: Pick<AppContext, "runtime" | "params">,
) => {
try {
debug(`using runtime ${ctx.runtime}`)
const processCmdPart1 =
ctx.runtime === "node"
? $`npx tsx ${tmpEntrypointPath}`
: $`bun ${tmpEntrypointPath}`
debug(`starting process....`)
debug(`using runtime ${ctx.runtime}`)
const processCmdPart1 =
ctx.runtime === "node"
? $`npx tsx ${tmpEntrypointPath}`
: $`bun ${tmpEntrypointPath}`
debug(`starting process....`)

try {
const processResult = await processCmdPart1
.stderr(debug.enabled ? "inheritPiped" : "piped")
.stdout(debug.enabled ? "inheritPiped" : "piped")
} catch (e) {
console.log(e)
}
const rawSoup = await readFile(tmpOutputPath, "utf-8")
// const errText = processResult.stderr
const processResult = await processCmdPart1
.stderr(debug.enabled ? "inheritPiped" : "piped")
.stdout(debug.enabled ? "inheritPiped" : "piped")

if (ctx.params.cleanup !== false) {
debug(`deleting ${tmpEntrypointPath}`)
await unlink(tmpEntrypointPath)
debug(`deleting ${tmpOutputPath}`)
await unlink(tmpOutputPath)
}
const rawSoup = await readFile(tmpOutputPath, "utf-8")
const errText = processResult.stderr

try {
debug(`parsing result of soupify...`)
const soup = JSON.parse(rawSoup)
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)
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)
}
} catch (e) {
console.log(e)
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)
}
}
69 changes: 36 additions & 33 deletions cli/lib/soupify/soupify-with-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,49 +22,52 @@ export const soupifyWithCore = async (
ctx: Pick<AppContext, "runtime" | "params">,
) => {
let { filePath, exportName } = params

exportName ??= await getExportNameFromFile(filePath)

let { tmpEntrypointPath, tmpOutputPath } =
const { tmpEntrypointPath, tmpOutputPath } =
await getTmpEntrypointFilePath(filePath)

// Remove existing entrypoint or tmp output files
await fs.unlink(tmpEntrypointPath).catch(() => {})
await fs.unlink(tmpOutputPath).catch(() => {})

debug(`writing to ${tmpEntrypointPath}`)
writeFileSync(
tmpEntrypointPath,
`
import React from "react"
import { Circuit } from "@tscircuit/core"
import * as EXPORTS from "./${Path.basename(filePath)}"
import { writeFileSync } from "node:fs"
const Component = EXPORTS["${exportName}"]
const project = new Circuit()
try {
project.add(<Component />)
} catch (e: any) {
console.log("[during .add()] ", e.toString())
writeFileSync("${tmpOutputPath}", JSON.stringify({
COMPILE_ERROR: e.toString() + "\\n\\n" + e.stack,
}))
throw e
}
try {
project.render()
} catch (e: any) {
console.log(e.toString())
writeFileSync("${tmpOutputPath}", JSON.stringify({
COMPILE_ERROR: e.toString() + "\\n\\n" + e.stack,
}))
throw e
}
writeFileSync("${tmpOutputPath}", JSON.stringify(project.getCircuitJson()))
`.trim(),
import React from "react"
import { Circuit } from "@tscircuit/core"
import * as EXPORTS from "./${Path.basename(filePath)}"
import { writeFileSync } from "node:fs"
const Component = EXPORTS["${exportName}"]
const project = new Circuit()
try {
project.add(<Component />)
} catch (e: any) {
console.log("[during .add()] ", e.toString())
writeFileSync("${tmpOutputPath}", JSON.stringify({
COMPILE_ERROR: e.toString() + "\\n\\n" + e.stack,
}))
throw e
}
try {
project.render()
} catch (e: any) {
console.log(e.toString())
writeFileSync("${tmpOutputPath}", JSON.stringify({
COMPILE_ERROR: e.toString() + "\\n\\n" + e.stack,
}))
throw e
}
writeFileSync("${tmpOutputPath}", JSON.stringify(project.getCircuitJson()))
`.trim(),
)

return await runEntrypointFile({ tmpEntrypointPath, tmpOutputPath }, ctx)
Expand Down

0 comments on commit ac80052

Please sign in to comment.