Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
Support for @tscircuit/core (#131)
Browse files Browse the repository at this point in the history
* 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
seveibar authored Aug 30, 2024
1 parent 80960d0 commit 45eb46d
Show file tree
Hide file tree
Showing 20 changed files with 269 additions and 126 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions cli/lib/cmd-fns/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const devCmd = async (ctx: AppContext, args: any) => {
const params = z
.object({
port: z.coerce.number().optional().default(3020),
core: z.boolean().optional(),
})
.parse(args)

Expand Down
1 change: 1 addition & 0 deletions cli/lib/cmd-fns/dev/soupify-and-upload-example-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const soupifyAndUploadExampleFile = async (
{
filePath: examplePath,
exportName,
useCore: ctx.params.core,
},
ctx,
)
Expand Down
2 changes: 1 addition & 1 deletion cli/lib/cmd-fns/publish/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { existsSync, readFileSync } from "fs"
import { getAllPackageFiles } from "cli/lib/util/get-all-package-files"
import prompts from "prompts"
import { getGeneratedReadme } from "../init/get-generated-readme"
import { soupify } from "../../soupify"
import { soupify } from "../../soupify/soupify"
import { inferExportNameFromSource } from "../dev/infer-export-name-from-source"
import $ from "dax-sh"
import semver from "semver"
Expand Down
2 changes: 2 additions & 0 deletions cli/lib/cmd-fns/soupify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ export const soupifyCmd = async (ctx: AppContext, args: any) => {
file: z.string(),
export: z.string().optional(),
output: z.string().optional(),
core: z.boolean().optional(),
})
.parse(args)

const soup = await soupify(
{
filePath: params.file,
exportName: params.export,
useCore: params.core,
},
ctx,
)
Expand Down
2 changes: 1 addition & 1 deletion cli/lib/cmd-fns/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const versionCmd = async (ctx: AppContext, args: any) => {
table.push({ name: "@tscircuit/cli", current: cliPackageJson.version })
table.push({
name: "@tscircuit/react-fiber",
current: cliPackageJson.devDependencies["@tscircuit/react-fiber"],
current: cliPackageJson.dependencies["@tscircuit/react-fiber"],
})
table.push({
name: "@tscircuit/schematic-viewer",
Expand Down
2 changes: 2 additions & 0 deletions cli/lib/get-program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const getProgram = (ctx: AppContext) => {
.description("Run development server in current directory")
.option("--cwd <cwd>", "Current working directory")
.option("--port <port>", "Port to run dev server on")
.option("--core", "Use @tscircuit/core beta")
.option("--no-cleanup", "Don't cleanup temporary files (for debugging)")
.action((args) => CMDFN.dev(ctx, args))

Expand Down Expand Up @@ -293,6 +294,7 @@ export const getProgram = (ctx: AppContext) => {
.description("Convert an example file to tscircuit soup")
.requiredOption("--file <file>", "Input example files")
.option("--output <output.json>", "Output file")
.option("--core", "Use @tscircuit/core to build (future version)")
.option(
"--export <export_name>",
"Name of export to soupify, if not specified, soupify the default/only export",
Expand Down
115 changes: 0 additions & 115 deletions cli/lib/soupify.ts

This file was deleted.

29 changes: 29 additions & 0 deletions cli/lib/soupify/get-export-name-from-file.ts
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
}
13 changes: 13 additions & 0 deletions cli/lib/soupify/get-tmp-entrpoint-filepath.ts
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 }
}
1 change: 1 addition & 0 deletions cli/lib/soupify/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./soupify"
60 changes: 60 additions & 0 deletions cli/lib/soupify/run-entrypoint-file.ts
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)
}
}
52 changes: 52 additions & 0 deletions cli/lib/soupify/soupify-with-core.ts
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)
}
Loading

0 comments on commit 45eb46d

Please sign in to comment.