imrishabh18/corne-keyboard

The code defines and renders two surface-mount chip components with SMT pads, silkscreen outlines, and 3D CAD models (OBJ and STEP) for PCB assembly.

Version
2.0.21
License
unset
Stars
1

scripts/patch-copper-pour-obstacles.mjs

import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

// Copy to scripts/ in the keyboard project, or pass a package directory to
// exercise a private copy. Never silently apply this to another solver version.
const base = process.argv[2]
  ? path.resolve(process.argv[2])
  : fileURLToPath(new URL('../node_modules/@tscircuit/copper-pour-solver/', import.meta.url))
const pkg = JSON.parse(fs.readFileSync(path.join(base, 'package.json'), 'utf8'))
if (pkg.version !== '0.0.52') {
  throw new Error('Review the copper-pour obstacle patch before upgrading copper-pour-solver')
}
const file = path.join(base, 'dist/index.js')
let code = fs.readFileSync(file, 'utf8')
const patches = [
  {
    marker: '/* corne-polygon-pad-clearance */',
    before: '    if (isPolygonPad(pad)) {\n      const margin = getMargin(0);',
    after: '    if (isPolygonPad(pad)) {\n      /* corne-polygon-pad-clearance */\n      const margin = getMargin(padMargin);',
  },
  {
    marker: '/* corne-pill-hole-rect-pad-obstacle */',
    before: '      } else if (platedHole.shape === "circular_hole_with_rect_pad") {\n        const rectWidth = platedHole.rect_pad_width;',
    after: `      } else if (
        /* corne-pill-hole-rect-pad-obstacle */
        platedHole.shape === "circular_hole_with_rect_pad" ||
        platedHole.shape === "pill_hole_with_rect_pad" ||
        platedHole.shape === "rotated_pill_hole_with_rect_pad"
      ) {
        const rectWidth = platedHole.rect_pad_width;`,
  },
]
let changed = false
for (const { marker, before, after } of patches) {
  if (code.includes(marker)) continue
  if (code.split(before).length !== 2) {
    throw new Error(`Unexpected copper-pour bundle: refusing to apply ${marker}`)
  }
  code = code.replace(before, after)
  changed = true
}
if (changed) {
  // Atomic replacement avoids changing a hard-linked package cache inode.
  const temporaryFile = `${file}.corne-patched-${process.pid}`
  fs.writeFileSync(temporaryFile, code)
  fs.renameSync(temporaryFile, file)
  console.log('Patched copper-pour polygon clearance and pill-hole rectangular pad obstacles')
}