diff --git a/lib/cmd-fns/dev-server-fulfill-export-requests.ts b/lib/cmd-fns/dev-server-fulfill-export-requests.ts index e695be0d..81d6a295 100644 --- a/lib/cmd-fns/dev-server-fulfill-export-requests.ts +++ b/lib/cmd-fns/dev-server-fulfill-export-requests.ts @@ -1,15 +1,11 @@ -import { AppContext } from "../util/app-context" -import { z } from "zod" -import { getDevServerAxios } from "./dev/get-dev-server-axios" import kleur from "kleur" +import { AppContext } from "../util/app-context" import { fulfillExportRequests } from "./dev/fulfill-export-requests" +import { getDevServerAxios } from "./dev/get-dev-server-axios" export const devServerFulfillExportRequests = async ( ctx: AppContext, - args: any ) => { - const params = z.object({}).parse(args) - let server_url = `http://localhost:3020` let dev_server_axios = getDevServerAxios({ serverUrl: server_url }) diff --git a/lib/cmd-fns/dev-server-upload.ts b/lib/cmd-fns/dev-server-upload.ts index d3e19c4c..c51d2966 100644 --- a/lib/cmd-fns/dev-server-upload.ts +++ b/lib/cmd-fns/dev-server-upload.ts @@ -1,10 +1,9 @@ -import { AppContext } from "../util/app-context" +import kleur from "kleur" import { z } from "zod" +import { AppContext } from "../util/app-context" import { getDevServerAxios } from "./dev/get-dev-server-axios" -import { uploadExamplesFromDirectory } from "./dev/upload-examples-from-directory" import { startFsWatcher } from "./dev/start-fs-watcher" -import kleur from "kleur" -import { AxiosInstance } from "axios" +import { uploadExamplesFromDirectory } from "./dev/upload-examples-from-directory" export const devServerUpload = async (ctx: AppContext, args: any) => { const params = z @@ -12,9 +11,12 @@ export const devServerUpload = async (ctx: AppContext, args: any) => { dir: z.string().optional().default(ctx.cwd), port: z.coerce.number().optional().nullable().default(null), watch: z.boolean().optional().default(false), + no_cleanup: z.boolean().optional().default(true), }) .parse(args) + ctx.args.no_cleanup = params.no_cleanup + let serverUrl = `http://localhost:${params.port ?? 3020}` let devServerAxios = getDevServerAxios({ serverUrl }) diff --git a/lib/cmd-fns/dev/fulfill-export-requests.ts b/lib/cmd-fns/dev/fulfill-export-requests.ts index 454c0d99..7917b9d8 100644 --- a/lib/cmd-fns/dev/fulfill-export-requests.ts +++ b/lib/cmd-fns/dev/fulfill-export-requests.ts @@ -1,11 +1,11 @@ -import { AppContext } from "../../util/app-context" +import { ExportRequest } from "@server/lib/zod/export_request" +import { AxiosInstance } from "axios" import kleur from "kleur" +import { exportBomCsvToBuffer } from "lib/export-fns/export-bom-csv" import { exportGerbersToZipBuffer } from "lib/export-fns/export-gerbers" -import { AxiosInstance } from "axios" -import { ExportRequest } from "@server/lib/zod/export_request" import { exportPnpCsvToBuffer } from "lib/export-fns/export-pnp-csv" -import { exportBomCsvToBuffer } from "lib/export-fns/export-bom-csv" import { soupify } from "lib/soupify" +import { AppContext } from "../../util/app-context" export const uploadBufferToExportFile = async ({ dev_server_axios, @@ -48,6 +48,8 @@ export const fulfillExportRequests = async ( }) .then((r) => r.data.export_requests) + const no_cleanup = ctx.args.no_cleanup + for (const export_request of export_requests) { console.log( kleur.gray( @@ -99,12 +101,13 @@ export const fulfillExportRequests = async ( { example_file_path: export_request.example_file_path, export_name: export_request.export_name, + no_cleanup, }, ctx ) - + const pnpFileName = `${export_request.export_name}-${export_request.export_parameters.pnp_csv_file_name!}` - + await uploadBufferToExportFile({ dev_server_axios, file_buffer: csv_buffer, @@ -122,9 +125,9 @@ export const fulfillExportRequests = async ( }, ctx ) - + const bomFileName = `${export_request.export_name}-${export_request.export_parameters.bom_csv_file_name!}` - + await uploadBufferToExportFile({ dev_server_axios, file_buffer: csv_buffer, @@ -151,4 +154,4 @@ export const fulfillExportRequests = async ( }) } } -} +} \ No newline at end of file diff --git a/lib/cmd-fns/dev/index.ts b/lib/cmd-fns/dev/index.ts index f8e57dbf..395e5077 100644 --- a/lib/cmd-fns/dev/index.ts +++ b/lib/cmd-fns/dev/index.ts @@ -24,12 +24,15 @@ export const devCmd = async (ctx: AppContext, args: any) => { const params = z .object({ port: z.coerce.number().optional().default(3020), + no_cleanup: z.boolean().optional().default(true), }) .parse(args) - let { port } = params + let { port, no_cleanup } = params const { cwd } = ctx + ctx.args.no_cleanup = no_cleanup + // Find an available port port = await findAvailablePort(port) @@ -152,16 +155,16 @@ export const devCmd = async (ctx: AppContext, args: any) => { fs_watcher.stop() er_watcher.stop() ee_watcher.stop() - + posthog.capture({ distinctId: projectHash, event: 'tsci_dev_stopped' }) - + if (posthog.shutdown) { await posthog.shutdown() } - + break } } diff --git a/lib/cmd-fns/dev/soupify-and-upload-example-file.ts b/lib/cmd-fns/dev/soupify-and-upload-example-file.ts index d9bf574e..1837dea9 100644 --- a/lib/cmd-fns/dev/soupify-and-upload-example-file.ts +++ b/lib/cmd-fns/dev/soupify-and-upload-example-file.ts @@ -15,7 +15,10 @@ export const soupifyAndUploadExampleFile = async ( exampleFileName: string devServerAxios: AxiosInstance }, - ctx: { runtime: "node" | "bun" } + ctx: { + runtime: "node" | "bun", + args: { no_cleanup: boolean } + } ) => { try { const startTime = Date.now() diff --git a/lib/cmd-fns/dev/start-fs-watcher.ts b/lib/cmd-fns/dev/start-fs-watcher.ts index f6aaba18..86fd644b 100644 --- a/lib/cmd-fns/dev/start-fs-watcher.ts +++ b/lib/cmd-fns/dev/start-fs-watcher.ts @@ -9,9 +9,12 @@ export const startFsWatcher = async ( devServerAxios, }: { cwd: string - devServerAxios: AxiosInstance + devServerAxios: AxiosInstance, }, - ctx: { runtime: "node" | "bun" } + ctx: { + runtime: "node" | "bun", + args: { no_cleanup: boolean } + } ) => { const watcher = chokidar.watch([`${cwd}/**/*.tsx`, `${cwd}/**/*.ts`], { ignored: /node_modules/, @@ -22,6 +25,7 @@ export const startFsWatcher = async ( dirty: false, should_run: true, } + watcher.on("change", async (path) => { console.log(path) if (path.endsWith(".__tmp_entrypoint.tsx")) return diff --git a/lib/cmd-fns/dev/upload-examples-from-directory.ts b/lib/cmd-fns/dev/upload-examples-from-directory.ts index 9a4b0e0b..f2c95443 100644 --- a/lib/cmd-fns/dev/upload-examples-from-directory.ts +++ b/lib/cmd-fns/dev/upload-examples-from-directory.ts @@ -13,9 +13,12 @@ export const uploadExamplesFromDirectory = async ( devServerAxios, }: { cwd: string - devServerAxios: AxiosInstance + devServerAxios: AxiosInstance, }, - ctx: { runtime: "node" | "bun" } + ctx: { + runtime: "node" | "bun", + args: { no_cleanup: boolean } + } ) => { const examplesDir = joinPath(cwd, "examples") const exampleFileNames = await readdir(examplesDir).catch((e) => { @@ -23,11 +26,13 @@ export const uploadExamplesFromDirectory = async ( throw e }) + const no_cleanup = ctx.args.no_cleanup + // Mark all examples as being "reloaded" in the database await markAllExamplesLoading({ devServerAxios }) for (const exampleFileName of exampleFileNames) { - if (exampleFileName.endsWith(".__tmp_entrypoint.tsx")) continue + if (exampleFileName.endsWith(".__tmp_entrypoint.tsx") && !no_cleanup) continue if (!exampleFileName.endsWith(".tsx")) continue await soupifyAndUploadExampleFile( { diff --git a/lib/cmd-fns/package-examples-create.ts b/lib/cmd-fns/package-examples-create.ts index 0033e388..399128d8 100644 --- a/lib/cmd-fns/package-examples-create.ts +++ b/lib/cmd-fns/package-examples-create.ts @@ -9,6 +9,7 @@ export const packageExamplesCreate = async (ctx: AppContext, args: any) => { packageNameWithVersion: z.string(), file: z.string(), export: z.string().optional().default("default"), + no_cleanup: z.boolean().optional().default(true), }) .parse(args) @@ -16,6 +17,7 @@ export const packageExamplesCreate = async (ctx: AppContext, args: any) => { { filePath: params.file, exportName: params.export, + no_cleanup: params.no_cleanup, }, ctx ) diff --git a/lib/cmd-fns/publish/index.ts b/lib/cmd-fns/publish/index.ts index 38643119..3f2e5606 100644 --- a/lib/cmd-fns/publish/index.ts +++ b/lib/cmd-fns/publish/index.ts @@ -1,18 +1,17 @@ -import kleur from "kleur" -import { z } from "zod" -import { AppContext } from "../../util/app-context" -import * as Path from "path" -import * as fs from "fs/promises" +import $ from "dax-sh" import { existsSync, readFileSync } from "fs" +import * as fs from "fs/promises" +import { unlink } from "fs/promises" +import kleur from "kleur" import { getAllPackageFiles } from "lib/util/get-all-package-files" +import * as Path from "path" import prompts from "prompts" -import { getGeneratedReadme } from "../init/get-generated-readme" +import semver from "semver" +import { z } from "zod" import { soupify } from "../../soupify" +import { AppContext } from "../../util/app-context" import { inferExportNameFromSource } from "../dev/infer-export-name-from-source" -import $ from "dax-sh" -import semver from "semver" -import { unlink } from "fs/promises" -import esbuild from "esbuild" +import { getGeneratedReadme } from "../init/get-generated-readme" export const publish = async (ctx: AppContext, args: any) => { const params = z @@ -20,6 +19,7 @@ export const publish = async (ctx: AppContext, args: any) => { increment: z.boolean().optional(), patch: z.boolean().optional(), lock: z.boolean().optional(), + no_cleanup: z.boolean().optional().default(true), }) .parse(args) @@ -269,6 +269,7 @@ export const publish = async (ctx: AppContext, args: any) => { { filePath, exportName, + no_cleanup: params.no_cleanup, }, ctx ).catch((e) => e) @@ -298,11 +299,10 @@ export const publish = async (ctx: AppContext, args: any) => { `${name.replace(/\//g, "-")}-${version}.tgz` ) await fs.mkdir(Path.dirname(tmpTarballPath), { recursive: true }) - const npm_pack_outputs = await $`cd ${ - ctx.cwd - } && npm pack --json --pack-destination ${Path.dirname( - tmpTarballPath - )}`.json() + const npm_pack_outputs = await $`cd ${ctx.cwd + } && npm pack --json --pack-destination ${Path.dirname( + tmpTarballPath + )}`.json() if (!existsSync(tmpTarballPath)) { console.log(kleur.red(`Couldn't find tarball at ${tmpTarballPath}`)) @@ -319,7 +319,9 @@ export const publish = async (ctx: AppContext, args: any) => { }) // Clean up .tscircuit/tmp - await unlink(tmpTarballPath) + if (!params.no_cleanup) { + await unlink(tmpTarballPath) + } // 8. Lock/set release to latest version await ctx.axios.post("/package_releases/update", { diff --git a/lib/cmd-fns/soupify.ts b/lib/cmd-fns/soupify.ts index b09517fe..03143be7 100644 --- a/lib/cmd-fns/soupify.ts +++ b/lib/cmd-fns/soupify.ts @@ -11,6 +11,7 @@ export const soupifyCmd = async (ctx: AppContext, args: any) => { file: z.string(), export: z.string().optional(), output: z.string().optional(), + no_cleanup: z.boolean().optional().default(true), }) .parse(args) @@ -18,6 +19,7 @@ export const soupifyCmd = async (ctx: AppContext, args: any) => { { filePath: params.file, exportName: params.export, + no_cleanup: params.no_cleanup, }, ctx ) diff --git a/lib/export-fns/export-bom-csv.ts b/lib/export-fns/export-bom-csv.ts index 10511dc2..8a50003d 100644 --- a/lib/export-fns/export-bom-csv.ts +++ b/lib/export-fns/export-bom-csv.ts @@ -11,6 +11,7 @@ export const exportBomCsvToBuffer = async ( ctx: AppContext ) => { console.log(kleur.gray("[soupifying]...")) + const soup = await soupify( { filePath: params.example_file_path, diff --git a/lib/export-fns/export-pnp-csv.ts b/lib/export-fns/export-pnp-csv.ts index 6a8df5e9..0a25dbc9 100644 --- a/lib/export-fns/export-pnp-csv.ts +++ b/lib/export-fns/export-pnp-csv.ts @@ -1,21 +1,15 @@ -import { AppContext } from "../util/app-context" -import { z } from "zod" -import * as Path from "path" -import { unlink } from "node:fs/promises" -import { soupify } from "lib/soupify" -import * as fs from "fs" import { - stringifyGerberCommandLayers, - convertSoupToGerberCommands, - convertSoupToPickAndPlaceCsv, + convertSoupToPickAndPlaceCsv } from "@tscircuit/builder" import kleur from "kleur" -import archiver from "archiver" +import { soupify } from "lib/soupify" +import { AppContext } from "../util/app-context" export const exportPnpCsvToBuffer = async ( params: { example_file_path: string export_name?: string + no_cleanup: boolean }, ctx: AppContext ) => { @@ -24,6 +18,7 @@ export const exportPnpCsvToBuffer = async ( { filePath: params.example_file_path, exportName: params.export_name, + no_cleanup: params.no_cleanup, }, ctx ) diff --git a/lib/get-program.ts b/lib/get-program.ts index b3a02190..408d0beb 100644 --- a/lib/get-program.ts +++ b/lib/get-program.ts @@ -1,7 +1,6 @@ import { Command } from "commander" -import packageJson from "../package.json" -import { AppContext } from "./util/app-context" import * as CMDFN from "lib/cmd-fns" +import { AppContext } from "./util/app-context" export const getProgram = (ctx: AppContext) => { const cmd = new Command("tsci").description( @@ -13,6 +12,7 @@ export const getProgram = (ctx: AppContext) => { .description("Run development server in current directory") .option("--cwd ", "Current working directory") .option("--port ", "Port to run dev server on") + .option("--no-cleanup", "Don't delete the temporary directory", true) .action((args) => CMDFN.dev(ctx, args)) cmd @@ -313,11 +313,13 @@ export const getProgram = (ctx: AppContext) => { ) .option("-w, --watch", "Watch for changes") .option("-p, --port", "Port dev server is running on (default: 3020)") + .option("--no-cleanup", "Don't delete the temporary directory", true) .action((args) => CMDFN.devServerUpload(ctx, args)) devServerCmd .command("fulfill-export-requests") - .action((args) => CMDFN.devServerFulfillExportRequests(ctx, args)) + .option("--no-cleanup", "Don't delete the temporary directory", true) + .action(() => CMDFN.devServerFulfillExportRequests(ctx)) cmd .command("open") diff --git a/lib/soupify.ts b/lib/soupify.ts index bcd484a5..99520017 100644 --- a/lib/soupify.ts +++ b/lib/soupify.ts @@ -16,13 +16,18 @@ export const soupify = async ( exportName, }: { filePath: string - exportName?: string + exportName?: string, }, - ctx: { runtime: "node" | "bun" } + ctx: { + runtime: "node" | "bun", + args: { no_cleanup: boolean } + } ) => { debug(`reading ${filePath}`) const targetFileContent = await readFile(filePath, "utf-8") + const no_cleanup = ctx.args.no_cleanup + if (!exportName) { if (targetFileContent.includes("export default")) { exportName = "default" @@ -89,8 +94,10 @@ console.log(JSON.stringify(elements)) const rawSoup = processResult.stdout.replace(/^[^\[]*/, "") const errText = processResult.stderr - debug(`deleting ${tmpFilePath}`) - await unlink(tmpFilePath) + if (!no_cleanup) { + debug(`deleting ${tmpFilePath}`) + await unlink(tmpFilePath) + } try { debug(`parsing result of soupify...`) diff --git a/package.json b/package.json index f63066c8..cecf219a 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "bootstrap": "bun i && cd dev-server-api && bun i && cd ../dev-server-frontend && bun i", "bootstrap:ci": "bun i --frozen-lockfile && cd dev-server-api && bun i --frozen-lockfile && cd ../dev-server-frontend && bun i --frozen-lockfile", "start": "bun cli.ts", - "dev": "TSCI_DEV_SERVER_DB=$(pwd)/.tscircuit/devdb concurrently 'cd dev-server-api && bun run build && bun start' 'cd dev-server-frontend && bun start' 'bun run dev-with-test-project'", + "dev": "TSCI_DEV_SERVER_DB=$(pwd)/.tscircuit/devdb concurrently 'cd dev-server-api && bun run build && bun start' 'cd dev-server-frontend && bun start' 'bun run dev-with-test-project' —no-cleanup", "clear": "rm -rf .tscircuit ./dev-server-api/.edgespec", "start:dev-server": "bun build:dev-server && bun cli.ts dev -y --cwd ./example-project", "build:dev-server": "cd dev-server-api && bun run build && cd ../dev-server-frontend && bun run build",