OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-wallet / scripts / patch-wdk-bare.js

76 lines · javascript ref:
#!/usr/bin/env node
// scripts/patch-wdk-bare.js - Patch WDK bare.js files for Bare v1.24.3 compatibility
//
// This script removes `with { imports: ... }` syntax from WDK package bare.js files
// which requires Bare v1.26.0+. This is a temporary workaround until Pear runtime upgrades.
//
// Run: node scripts/patch-wdk-bare.js
// Or add to package.json: "postinstall": "node scripts/patch-wdk-bare.js"

import { readFileSync, writeFileSync } from 'fs'
import { resolve } from 'path'

const packages = [
  '@tetherto/wdk',
  '@tetherto/wdk-wallet',
  '@tetherto/wdk-wallet-btc',
  '@tetherto/wdk-wallet-evm'
]

const BACKUP_MARKER = '// ⚠️  PATCHED FOR BARE v1.24.3 COMPATIBILITY'

function patchBareFile(packageName) {
  const bareFile = resolve(`node_modules/${packageName}/bare.js`)

  try {
    let content = readFileSync(bareFile, 'utf8')

    // Check if already patched
    if (content.includes(BACKUP_MARKER)) {
      console.log(`✓ ${packageName}/bare.js already patched`)
      return
    }

    // Remove `with { imports: '...' }` syntax
    const originalExports = content.match(/export \* from .* with \{ imports: .* \}/g)
    const originalDefault = content.match(/export \{ default \} from .* with \{ imports: .* \}/g)

    if (!originalExports && !originalDefault) {
      console.log(`✓ ${packageName}/bare.js doesn't need patching`)
      return
    }

    // Replace with simple exports
    content = content.replace(
      /export \* from (.*) with \{ imports: .* \}/g,
      'export * from $1'
    )
    content = content.replace(
      /export \{ default \} from (.*) with \{ imports: .* \}/g,
      'export { default } from $1'
    )

    // Add marker comment
    content = BACKUP_MARKER + '\n' + content

    writeFileSync(bareFile, content, 'utf8')
    console.log(`✓ Patched ${packageName}/bare.js`)
  } catch (err) {
    if (err.code === 'ENOENT') {
      console.log(`⚠️  ${packageName}/bare.js not found (package not installed?)`)
    } else {
      console.error(`✖ Error patching ${packageName}/bare.js:`, err.message)
    }
  }
}

console.log('Patching WDK packages for Bare v1.24.3 compatibility...\n')

for (const pkg of packages) {
  patchBareFile(pkg)
}

console.log('\n✓ Patch complete!')
console.log('These patches are temporary and will be removed when Pear upgrades to Bare v1.26.0+')
console.log('Run `npm run check:pear` to check current Pear/Bare version')