MustafaMulla29/rp2040-bldc-motor-controller-new

73×72 mm 4-layer USB‑C PD (5/9/15/20 V) RP2040 controller PCB with protected power switching, MCT8329A sensorless BLDC three-phase MOSFET bridge, USB data/programming, onboard controls, and three motor outputs feeding a 2212 motor through rigid bullet adapters.

Version
0.3.100
License
unset
Stars
0

scripts/push-package.mjs

/** Publish a portable copy; keep local assembly authoring and audit caches. */
import { cpSync, existsSync, mkdtempSync, readFileSync, readdirSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os"
import { dirname, join, resolve } from "node:path"
import { fileURLToPath } from "node:url"

const root = resolve(dirname(fileURLToPath(import.meta.url)), "..")
const config = JSON.parse(readFileSync(join(root, "tscircuit.config.json"), "utf8"))
const patterns = [...(config.ignoredFiles ?? []), "**/__pycache__/**", "**/*.pyc"]
const ignored = patterns.map(pattern => new Bun.Glob(pattern))
const stage = mkdtempSync(join(tmpdir(), "motor-controller-publish-"))
const originalPackage = readFileSync(join(root, "package.json"), "utf8")
let files = 0
function copyDirectory(relative = "") {
  for (const entry of readdirSync(join(root, relative), { withFileTypes: true })) {
    if (entry.name.startsWith(".")) continue
    const path = relative ? `${relative}/${entry.name}` : entry.name
    if (ignored.some(glob => glob.match(path) || entry.isDirectory() && glob.match(`${path}/`))) continue
    if (entry.isDirectory()) copyDirectory(path)
    else if (entry.isFile()) {
      cpSync(join(root, path), join(stage, path))
      files++
    }
  }
}
copyDirectory()
for (const path of ["system.circuit.tsx", "index.circuit.tsx", "assets/connected-system.glb", "circuit/route-plan.json"]) {
  if (!existsSync(join(stage, path))) throw new Error(`Publish copy is missing ${path}`)
}
console.log(`Prepared ${files} files, including the assembly model, in ${stage}`)
if (!process.argv.includes("--prepare-only")) {
  const cli = fileURLToPath(import.meta.resolve("@tscircuit/cli"))
  // Use the Node runtime declared by the CLI's own shebang.
  const child = Bun.spawn(["node", cli, "push", "--compress"], {
    cwd: stage, stdin: "inherit", stdout: "inherit", stderr: "pipe",
  })
  const [code, stderr] = await Promise.all([child.exited, new Response(child.stderr).text()])
  // Uncaught CLI request errors can include Authorization headers.
  process.stderr.write(stderr.replace(/Bearer\s+[^"'\s,}]+/g, "Bearer [REDACTED]"))
  const published = JSON.parse(readFileSync(join(stage, "package.json"), "utf8"))
  // Retain the CLI's consumed version even if its upload fails; preserve other edits.
  if (readFileSync(join(root, "package.json"), "utf8") === originalPackage) {
    const local = JSON.parse(originalPackage)
    local.version = published.version
    writeFileSync(join(root, "package.json"), JSON.stringify(local, null, 2))
  } else console.log("Local package.json changed during upload; its version was left untouched.")
  process.exitCode = code
}