Ctrl K
ComunidadGuías…

scripts/generate-bas-account-numbers.ts

Rama main · réplica de lectura del repositorio original.

Archivo original ↗Preguntar a KiBot
/**
 * Regenerates lib/bookkeeping/bas-account-numbers.ts from the BAS chart.
 *
 *   npx tsx scripts/generate-bas-account-numbers.ts          # write
 *   npx tsx scripts/generate-bas-account-numbers.ts --check  # exit 1 if stale
 *
 * The generated module is a sorted list of the ~1,276 standard account
 * numbers (~9 KB) so client components can answer "is this a standard BAS
 * account?" without the 315 KB data chunk. A unit test pins parity too.
 */
import fs from 'node:fs'
import path from 'node:path'
import { BAS_REFERENCE } from '../src/lib/bookkeeping/bas-data'

const OUT = path.resolve(__dirname, '..', 'src', 'lib', 'bookkeeping', 'bas-account-numbers.ts')

export function renderBasAccountNumbers(numbers: readonly string[]): string {
  const sorted = [...new Set(numbers)].sort()
  const rows: string[] = []
  for (let i = 0; i < sorted.length; i += 12) {
    rows.push('  ' + sorted.slice(i, i + 12).map((n) => `'${n}'`).join(', ') + ',')
  }
  return `// GENERATED by scripts/generate-bas-account-numbers.ts from lib/bookkeeping/bas-data.
// Do not edit by hand: run \`npx tsx scripts/generate-bas-account-numbers.ts\`.
//
// The sorted list of standard BAS account numbers (~9 KB) so client code can
// check membership without importing the full chart (315 KB uncompressed).
// lib/bookkeeping/__tests__/bas-account-numbers.test.ts pins parity.

export const BAS_ACCOUNT_NUMBERS: readonly string[] = [
${rows.join('\n')}
]

const BAS_ACCOUNT_NUMBER_SET: ReadonlySet<string> = new Set(BAS_ACCOUNT_NUMBERS)

/** Whether the number exists in the BAS chart (same answer as isStandardBASAccount). */
export function isStandardBASAccountNumber(accountNumber: string): boolean {
  return BAS_ACCOUNT_NUMBER_SET.has(accountNumber)
}
`
}

if (require.main === module) {
  const rendered = renderBasAccountNumbers(BAS_REFERENCE.map((a) => a.account_number))
  if (process.argv.includes('--check')) {
    const current = fs.existsSync(OUT) ? fs.readFileSync(OUT, 'utf8') : ''
    if (current !== rendered) {
      console.error('src/lib/bookkeeping/bas-account-numbers.ts is stale: run npx tsx scripts/generate-bas-account-numbers.ts')
      process.exit(1)
    }
    console.log('bas-account-numbers.ts is up to date')
  } else {
    fs.writeFileSync(OUT, rendered)
    console.log(`wrote ${OUT}`)
  }
}