@tscircuit/cli 0.0.62 → 0.0.63

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cli.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
 
3
+ import kleur from "kleur"
3
4
  import { AppContext } from "./lib/util/app-context"
4
5
  import { createContextAndRunProgram } from "./lib/util/create-context-and-run-program"
5
6
 
@@ -7,6 +8,7 @@ async function main() {
7
8
  await createContextAndRunProgram(process.argv)
8
9
  }
9
10
 
10
- main().catch((e) => {
11
- console.log("Error running CLI:", e.toString())
11
+ main().catch((e: any) => {
12
+ console.log(kleur.gray(e.stack))
13
+ console.log(kleur.red(`Error running CLI: ${e.toString()}`))
12
14
  })
Binary file
@@ -0,0 +1,32 @@
1
+ import { publicMapExportFile } from "src/lib/public-mapping/public-map-export-file"
2
+ import { export_file } from "src/lib/zod/export_file"
3
+ import { withEdgeSpec } from "src/with-edge-spec"
4
+ import { z } from "zod"
5
+
6
+ export default withEdgeSpec({
7
+ methods: ["POST"],
8
+ jsonBody: z.object({
9
+ export_request_id: z.number().int(),
10
+ file_name: z.string(),
11
+ file_content_base64: z.string().transform((a) => Buffer.from(a, "base64")),
12
+ }),
13
+ jsonResponse: z.object({
14
+ export_file,
15
+ }),
16
+ auth: "none",
17
+ })(async (req, ctx) => {
18
+ const db_export_file = await ctx.db
19
+ .insertInto("export_file")
20
+ .values({
21
+ export_request_id: req.jsonBody.export_request_id,
22
+ file_name: req.jsonBody.file_name,
23
+ file_content: req.jsonBody.file_content_base64,
24
+ created_at: new Date().toISOString(),
25
+ })
26
+ .returningAll()
27
+ .executeTakeFirstOrThrow()
28
+
29
+ return ctx.json({
30
+ export_file: publicMapExportFile(db_export_file),
31
+ })
32
+ })
@@ -0,0 +1,24 @@
1
+ import { publicMapExportFile } from "src/lib/public-mapping/public-map-export-file"
2
+ import { export_file } from "src/lib/zod/export_file"
3
+ import { withEdgeSpec } from "src/with-edge-spec"
4
+ import { z } from "zod"
5
+
6
+ export default withEdgeSpec({
7
+ methods: ["GET"],
8
+ queryParams: z.object({
9
+ export_file_id: z.coerce.number().int(),
10
+ }),
11
+ auth: "none",
12
+ })(async (req, ctx) => {
13
+ const db_export_file = await ctx.db
14
+ .selectFrom("export_file")
15
+ .selectAll()
16
+ .where("export_file_id", "=", req.query.export_file_id)
17
+ .executeTakeFirstOrThrow()
18
+
19
+ return new Response(db_export_file.file_content, {
20
+ headers: {
21
+ "Content-Disposition": `attachment; filename="${db_export_file.file_name}"`,
22
+ },
23
+ })
24
+ })
@@ -3,20 +3,14 @@ import { NotFoundError } from "edgespec/middleware"
3
3
  import { z } from "zod"
4
4
  import { export_request } from "src/lib/zod/export_request"
5
5
  import { publicMapExportRequest } from "src/lib/public-mapping/public-map-export-request"
6
+ import { export_parameters } from "../../../src/lib/zod/export_parameters"
6
7
 
7
8
  export default withEdgeSpec({
8
9
  methods: ["POST"],
9
10
  jsonBody: z.object({
10
11
  example_file_path: z.string(),
11
12
  export_name: z.string().nullable().optional(),
12
- export_parameters: z.object({
13
- should_export_gerber_zip: z.boolean().default(false),
14
- gerbers_zip_file_name: z
15
- .string()
16
- .nullable()
17
- .optional()
18
- .default("gerbers.zip"),
19
- }),
13
+ export_parameters: export_parameters,
20
14
  }),
21
15
  jsonResponse: z.object({
22
16
  export_request,
@@ -0,0 +1,37 @@
1
+ import { withEdgeSpec } from "src/with-edge-spec"
2
+ import { z } from "zod"
3
+ import { export_request } from "src/lib/zod/export_request"
4
+ import { publicMapExportRequest } from "src/lib/public-mapping/public-map-export-request"
5
+
6
+ export default withEdgeSpec({
7
+ methods: ["GET", "POST"],
8
+ commonParams: z.object({
9
+ export_request_id: z.coerce.number(),
10
+ }),
11
+ jsonResponse: z.object({
12
+ export_request: export_request,
13
+ }),
14
+ auth: "none",
15
+ })(async (req, ctx) => {
16
+ const { export_request_id } = req.commonParams
17
+ const db_export_request: any = await ctx.db
18
+ .selectFrom("export_request")
19
+ .selectAll()
20
+ .where("export_request_id", "=", export_request_id)
21
+ .executeTakeFirstOrThrow()
22
+
23
+ db_export_request.file_summary = (
24
+ await ctx.db
25
+ .selectFrom("export_file")
26
+ .where("export_request_id", "=", export_request_id)
27
+ .selectAll()
28
+ .execute()
29
+ ).map((ef) => ({
30
+ file_name: ef.file_name,
31
+ export_file_id: ef.export_file_id,
32
+ }))
33
+
34
+ return ctx.json({
35
+ export_request: publicMapExportRequest(db_export_request),
36
+ })
37
+ })
@@ -0,0 +1,28 @@
1
+ import { withEdgeSpec } from "src/with-edge-spec"
2
+ import { z } from "zod"
3
+ import { export_request } from "src/lib/zod/export_request"
4
+ import { publicMapExportRequest } from "src/lib/public-mapping/public-map-export-request"
5
+
6
+ export default withEdgeSpec({
7
+ methods: ["GET", "POST"],
8
+ commonParams: z.object({
9
+ is_complete: z.boolean().nullable().default(null),
10
+ }),
11
+ jsonResponse: z.object({
12
+ export_requests: z.array(export_request),
13
+ }),
14
+ auth: "none",
15
+ })(async (req, ctx) => {
16
+ const { is_complete } = req.commonParams
17
+ const db_export_requests = await ctx.db
18
+ .selectFrom("export_request")
19
+ .selectAll()
20
+ .$if(is_complete !== null, (q) =>
21
+ q.where("export_request.is_complete", "=", is_complete ? 1 : 0)
22
+ )
23
+ .execute()
24
+
25
+ return ctx.json({
26
+ export_requests: db_export_requests.map((er) => publicMapExportRequest(er)),
27
+ })
28
+ })
@@ -0,0 +1,26 @@
1
+ import { withEdgeSpec } from "src/with-edge-spec"
2
+ import { NotFoundError } from "edgespec/middleware"
3
+ import { z } from "zod"
4
+ import { export_request } from "src/lib/zod/export_request"
5
+ import { publicMapExportRequest } from "src/lib/public-mapping/public-map-export-request"
6
+ import { export_parameters } from "src/lib/zod/export_parameters"
7
+
8
+ export default withEdgeSpec({
9
+ methods: ["POST"],
10
+ jsonBody: z.object({
11
+ export_request_id: z.coerce.number(),
12
+ is_complete: z.boolean(),
13
+ }),
14
+ jsonResponse: z.object({}),
15
+ auth: "none",
16
+ })(async (req, ctx) => {
17
+ await ctx.db
18
+ .updateTable("export_request")
19
+ .set({
20
+ is_complete: req.jsonBody.is_complete ? 1 : 0,
21
+ })
22
+ .where("export_request_id", "=", req.jsonBody.export_request_id)
23
+ .execute()
24
+
25
+ return ctx.json({})
26
+ })
@@ -26,7 +26,6 @@ export interface ExportFile {
26
26
  export_file_id: Generated<number>
27
27
  file_name: string
28
28
  file_content: Buffer
29
- is_complete: 1 | 0
30
29
  export_request_id: number
31
30
  created_at: string
32
31
  }
@@ -0,0 +1,17 @@
1
+ import type { ExportRequest } from "src/db/get-db"
2
+ import { export_file } from "src/lib/zod/export_file"
3
+ import { z } from "zod"
4
+
5
+ export const publicMapExportFile = (db_export_file: {
6
+ export_file_id: number
7
+ export_request_id: number
8
+ file_name: string
9
+ created_at: string
10
+ }): z.infer<typeof export_file> => {
11
+ return {
12
+ export_request_id: db_export_file.export_request_id as any,
13
+ export_file_id: db_export_file.export_file_id as any,
14
+ file_name: db_export_file.file_name,
15
+ created_at: db_export_file.created_at,
16
+ }
17
+ }
@@ -8,12 +8,16 @@ export const publicMapExportRequest = (db_export_request: {
8
8
  export_parameters: string
9
9
  export_name: string
10
10
  is_complete: 1 | 0
11
+ file_summary?: Array<any>
11
12
  created_at: string
12
13
  }): z.infer<typeof export_request> => {
13
14
  return {
14
15
  export_request_id: db_export_request.export_request_id as any,
15
- is_completed: db_export_request.is_complete === 1,
16
+ example_file_path: db_export_request.example_file_path,
17
+ export_name: db_export_request.export_name,
18
+ is_complete: db_export_request.is_complete === 1,
19
+ export_parameters: JSON.parse(db_export_request.export_parameters),
20
+ file_summary: db_export_request.file_summary,
16
21
  created_at: db_export_request.created_at,
17
- file_summary: [],
18
22
  }
19
23
  }
@@ -0,0 +1,8 @@
1
+ import { z } from "zod"
2
+
3
+ export const export_file = z.object({
4
+ export_file_id: z.number().int(),
5
+ export_request_id: z.number().int(),
6
+ file_name: z.string(),
7
+ created_at: z.string(),
8
+ })
@@ -0,0 +1,10 @@
1
+ import { z } from "zod"
2
+
3
+ export const export_parameters = z.object({
4
+ should_export_gerber_zip: z.boolean().default(false),
5
+ gerbers_zip_file_name: z
6
+ .string()
7
+ .nullable()
8
+ .optional()
9
+ .default("gerbers.zip"),
10
+ })
@@ -1,13 +1,19 @@
1
1
  import { z } from "zod"
2
+ import { export_parameters } from "./export_parameters"
2
3
 
3
4
  export const export_request = z.object({
4
5
  export_request_id: z.coerce.number(),
5
- is_completed: z.boolean(),
6
+ is_complete: z.boolean(),
6
7
  created_at: z.string(),
7
- file_summary: z.array(
8
- z.object({
9
- file_name: z.string(),
10
- is_complete: z.boolean(),
11
- })
12
- ),
8
+ export_name: z.string(),
9
+ example_file_path: z.string(),
10
+ export_parameters,
11
+ file_summary: z
12
+ .array(
13
+ z.object({
14
+ file_name: z.string(),
15
+ export_file_id: z.number().int(),
16
+ })
17
+ )
18
+ .optional(),
13
19
  })
@@ -45,14 +45,51 @@ export const useGerberExportDialog = () => {
45
45
  setExportError(null)
46
46
  setIsExporting(true)
47
47
  try {
48
- await axios.post("/api/export_requests/create", {
49
- example_file_path: activeDevExamplePackage.file_path,
50
- export_name: activeDevExamplePackage.export_name,
51
- export_parameters: {
52
- should_export_gerber_zip: true,
53
- gerbers_zip_file_name: outputName,
54
- },
55
- })
48
+ let export_request = await axios
49
+ .post("/api/export_requests/create", {
50
+ example_file_path: activeDevExamplePackage.file_path,
51
+ export_name: activeDevExamplePackage.export_name,
52
+ export_parameters: {
53
+ should_export_gerber_zip: true,
54
+ gerbers_zip_file_name: outputName,
55
+ },
56
+ })
57
+ .then((r) => r.data.export_request)
58
+
59
+ const pollExportRequest = async () => {
60
+ while (!export_request.is_complete) {
61
+ try {
62
+ export_request = await axios
63
+ .post("/api/export_requests/get", {
64
+ export_request_id: export_request.export_request_id,
65
+ })
66
+ .then((r) => r.data.export_request)
67
+ } catch (e: any) {
68
+ console.error(e)
69
+ setExportError(
70
+ `${e.toString()}\n\n${e.response?.data?.error?.message}`
71
+ )
72
+ setIsExporting(false)
73
+ return
74
+ }
75
+ await new Promise((resolve) => setTimeout(resolve, 100))
76
+ }
77
+ setIsExporting(false)
78
+ }
79
+
80
+ await pollExportRequest()
81
+
82
+ // open /api/export_files/download?export_file_id=... in new tab
83
+
84
+ const export_file_id =
85
+ export_request.file_summary[0].export_file_id
86
+
87
+ window.open(
88
+ `/api/export_files/download?export_file_id=${export_file_id}`,
89
+ "_blank"
90
+ )
91
+
92
+ setIsExporting(false)
56
93
  } catch (e: any) {
57
94
  console.error(e)
58
95
  setExportError(