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

Commit

Permalink
refactor export gerbers
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed Apr 10, 2024
1 parent aaa926a commit fa52552
Show file tree
Hide file tree
Showing 5 changed files with 374 additions and 109 deletions.
Empty file.
48 changes: 5 additions & 43 deletions lib/cmd-fns/export-gerbers.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
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,
} from "@tscircuit/builder"
import { zip } from "zip-a-folder"
import kleur from "kleur"
import archiver from "archiver"
import { exportGerbersToFile } from "lib/export-gerbers"

export const exportGerbersCmd = async (ctx: AppContext, args: any) => {
const params = z
Expand All @@ -21,40 +11,12 @@ export const exportGerbersCmd = async (ctx: AppContext, args: any) => {
})
.parse(args)

console.log(kleur.gray("[soupifying]..."))
const soup = await soupify(
await exportGerbersToFile(
{
filePath: params.file,
exportName: params.export,
example_file_path: params.file,
export_name: params.export,
output_zip_path: params.outputfile,
},
ctx
)

console.log(kleur.gray("[soup to gerber json]..."))
const gerber_layer_cmds = convertSoupToGerberCommands(soup)

console.log(kleur.gray("[stringify gerber json]..."))
const gerber_file_contents = stringifyGerberCommandLayers(gerber_layer_cmds)

console.log(kleur.gray("[writing gerbers to tmp dir]..."))
const tempDir = Path.join(".tscircuit", "tmp-gerber-export")
await fs.mkdirSync(tempDir, { recursive: true })
for (const [fileName, fileContents] of Object.entries(gerber_file_contents)) {
const filePath = Path.join(tempDir, fileName)
await fs.writeFileSync(filePath, fileContents)
}

console.log(kleur.gray("[zipping tmp dir]..."))
const output = fs.createWriteStream(params.outputfile)
const archive = archiver("zip", {
zlib: { level: 9 },
})

archive.pipe(output)
archive.directory(tempDir, false)

await new Promise((resolve, reject) => {
output.on("close", resolve)
output.on("error", reject)
})
}
84 changes: 84 additions & 0 deletions lib/export-gerbers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
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,
} from "@tscircuit/builder"
import kleur from "kleur"
import archiver from "archiver"

export const exportGerbersToFile = async (
params: {
example_file_path: string
export_name?: string
output_zip_path: string
},
ctx: AppContext
) => {
console.log(kleur.gray("[soupifying]..."))
const soup = await soupify(
{
filePath: params.example_file_path,
exportName: params.export_name,
},
ctx
)

console.log(kleur.gray("[soup to gerber json]..."))
const gerber_layer_cmds = convertSoupToGerberCommands(soup)

console.log(kleur.gray("[stringify gerber json]..."))
const gerber_file_contents = stringifyGerberCommandLayers(gerber_layer_cmds)

console.log(kleur.gray("[writing gerbers to tmp dir]..."))
const tempDir = Path.join(".tscircuit", "tmp-gerber-export")
fs.mkdirSync(tempDir, { recursive: true })
for (const [fileName, fileContents] of Object.entries(gerber_file_contents)) {
const filePath = Path.join(tempDir, fileName)
await fs.writeFileSync(filePath, fileContents)
}

console.log(kleur.gray("[zipping tmp dir]..."))
const output = fs.createWriteStream(params.output_zip_path)
const archive = archiver("zip", {
zlib: { level: 9 },
})

archive.pipe(output)
archive.directory(tempDir, false)

await new Promise((resolve, reject) => {
output.on("close", resolve)
output.on("error", reject)
})
}

export const exportGerbersToZipBuffer = async (
params: {
example_file_path: string
export_name?: string
},
ctx: AppContext
) => {
const tempDir = Path.join(".tscircuit", "tmp-gerber-zip")
fs.mkdirSync(tempDir, { recursive: true })

await exportGerbersToFile(
{
example_file_path: params.example_file_path,
export_name: params.export_name,
output_zip_path: Path.join(tempDir, "gerbers.zip"),
},
ctx
)

const buffer = fs.readFileSync(Path.join(tempDir, "gerbers.zip"))

fs.rmSync(tempDir, { recursive: true })

return buffer
}
Loading

0 comments on commit fa52552

Please sign in to comment.