astra/pd-power-supply

USB-PD Power Supply with RP2040 controller

Version
1.1.6
License
unset
Stars
0

scripts/package-review.mjs

import {readFileSync,writeFileSync,existsSync,cpSync} from 'node:fs'
import {resolve} from 'node:path'
// Package each entry with the native tsci viewer, embedded JSON and CAD assets.
const template=readFileSync('dist/index.html','utf8')
const bundle=readFileSync('dist/standalone.min.js').toString('base64')
for(const [entry,directory,filename,title] of [
 ['index.circuit.tsx','index','review.html','PD Supply — assembled enclosure'],
 ['board.circuit.tsx','board','board-review.html','PD Supply — board only'],
]){
 const data=JSON.parse(readFileSync(`dist/${directory}/circuit.json`,'utf8'))
 if(data.some(x=>x.type.includes('error')))throw new Error(`${directory} contains circuit errors; fix before packaging.`)
 for(const c of data.filter(x=>x.type==='cad_component')){
  for(const [key,mime] of [['model_obj_url','text/plain'],['model_step_url','application/step'],['model_glb_url','model/gltf-binary']]){
   if(key==='model_obj_url'&&c[key]?.includes('/C221660.obj'))c[key]=`data:text/plain;base64,${readFileSync('imports/JS102011SAQN.obj').toString('base64')}`
   const url=c[key]
   if(typeof url==='string'&&!url.includes('://')&&!url.startsWith('data:')){
    const path=resolve(url)
    if(!existsSync(path))throw new Error(`Missing CAD asset: ${url}`)
    c[key]=`data:${mime};base64,${readFileSync(path).toString('base64')}`
   }
  }
 }
 const asset=`data:application/json;base64,${Buffer.from(JSON.stringify(data)).toString('base64')}`
 const files=[{filePath:entry,fileStaticAssetUrl:asset}]
 let html=template.replace(/window\.TSCIRCUIT_RUNFRAME_STATIC_FILE_LIST = .*?;/,`window.TSCIRCUIT_RUNFRAME_STATIC_FILE_LIST = ${JSON.stringify(files)};`)
 html=html.replace(/<title>.*?<\/title>/,`<title>${title}</title>`)
 html=html.replace('src="./standalone.min.js"',`src="data:text/javascript;base64,${bundle}"`)
 writeFileSync(filename,html)
 writeFileSync(`dist/${filename}`,html)
 console.log(`Packaged ${filename}`)
}
cpSync('imports','dist/imports',{recursive:true})