From ac800523d30ad8b41d96de5f55be44663110c337 Mon Sep 17 00:00:00 2001 From: Shibo Date: Sat, 28 Sep 2024 16:24:18 +0200 Subject: [PATCH] cleanup --- cli/lib/soupify/run-entrypoint-file.ts | 71 ++++++++++++-------------- cli/lib/soupify/soupify-with-core.ts | 69 +++++++++++++------------ 2 files changed, 68 insertions(+), 72 deletions(-) diff --git a/cli/lib/soupify/run-entrypoint-file.ts b/cli/lib/soupify/run-entrypoint-file.ts index de812fe8..826bc910 100644 --- a/cli/lib/soupify/run-entrypoint-file.ts +++ b/cli/lib/soupify/run-entrypoint-file.ts @@ -17,49 +17,42 @@ export const runEntrypointFile = async ( }: { tmpEntrypointPath: string; tmpOutputPath: string }, ctx: Pick, ) => { - 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) } } diff --git a/cli/lib/soupify/soupify-with-core.ts b/cli/lib/soupify/soupify-with-core.ts index 4524a088..95555cb1 100644 --- a/cli/lib/soupify/soupify-with-core.ts +++ b/cli/lib/soupify/soupify-with-core.ts @@ -22,49 +22,52 @@ export const soupifyWithCore = async ( ctx: Pick, ) => { 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() - } 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() +} 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)