ShiboSoftwareDev/i-wan

This code defines a PCB and schematic for a microcontroller development board, incorporating various surface-mount components (resistors, capacitors, headers, connectors, crystals, and an IC), with detailed footprints, electrical nets, and routing instructions.

Version
1.0.10
License
unset
Stars
0

scripts/kicad-prefix.mjs

import { readFileSync, writeFileSync } from "node:fs"

const [inputPath, outputPath, countText] = process.argv.slice(2)
const text = readFileSync(inputPath, "utf8")
const rootStart = text.indexOf("(kicad_pcb")
if (rootStart < 0) throw new Error("Missing kicad_pcb root")

let depth = 0
let inString = false
let escaped = false
let childStart = -1
const children = []

for (let index = rootStart; index < text.length; index += 1) {
  const char = text[index]
  if (inString) {
    if (escaped) escaped = false
    else if (char === "\\") escaped = true
    else if (char === '"') inString = false
    continue
  }
  if (char === '"') {
    inString = true
    continue
  }
  if (char === "(") {
    depth += 1
    if (depth === 2) childStart = index
  } else if (char === ")") {
    if (depth === 2 && childStart >= 0) {
      children.push(text.slice(childStart, index + 1))
      childStart = -1
    }
    depth -= 1
  }
}

const count = Math.min(Number(countText ?? children.length), children.length)
writeFileSync(outputPath, `(kicad_pcb\n${children.slice(0, count).join("\n")}\n)\n`)
console.log(`Wrote ${count}/${children.length} top-level KiCad nodes`)