build.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { chmodSync, existsSync, mkdirSync } from 'fs'
  2. import { dirname } from 'path'
  3. const pkg = await Bun.file(new URL('../package.json', import.meta.url)).json() as {
  4. name: string
  5. version: string
  6. }
  7. const args = process.argv.slice(2)
  8. const compile = args.includes('--compile')
  9. const features: string[] = []
  10. for (let i = 0; i < args.length; i += 1) {
  11. const arg = args[i]
  12. if (arg === '--feature' && args[i + 1]) {
  13. features.push(args[i + 1]!)
  14. i += 1
  15. continue
  16. }
  17. if (arg.startsWith('--feature=')) {
  18. features.push(arg.slice('--feature='.length))
  19. }
  20. }
  21. const outfile = compile ? './dist/cli' : './cli'
  22. const buildTime = new Date().toISOString()
  23. mkdirSync(dirname(outfile), { recursive: true })
  24. const externals = [
  25. '@ant/*',
  26. 'audio-capture-napi',
  27. 'image-processor-napi',
  28. 'modifiers-napi',
  29. 'url-handler-napi',
  30. ]
  31. const defines = {
  32. 'process.env.USER_TYPE': JSON.stringify('external'),
  33. 'process.env.CLAUDE_CODE_VERIFY_PLAN': JSON.stringify('false'),
  34. 'MACRO.VERSION': JSON.stringify(pkg.version),
  35. 'MACRO.BUILD_TIME': JSON.stringify(buildTime),
  36. 'MACRO.PACKAGE_URL': JSON.stringify(pkg.name),
  37. 'MACRO.NATIVE_PACKAGE_URL': 'undefined',
  38. 'MACRO.FEEDBACK_CHANNEL': JSON.stringify('github'),
  39. 'MACRO.ISSUES_EXPLAINER': JSON.stringify(
  40. 'This reconstructed source snapshot does not include Anthropic internal issue routing.',
  41. ),
  42. 'MACRO.VERSION_CHANGELOG': JSON.stringify(
  43. 'https://github.com/paoloanzn/claude-code',
  44. ),
  45. } as const
  46. const cmd = [
  47. 'bun',
  48. 'build',
  49. './src/entrypoints/cli.tsx',
  50. '--compile',
  51. '--target',
  52. 'bun',
  53. '--format',
  54. 'esm',
  55. '--outfile',
  56. outfile,
  57. '--minify',
  58. '--bytecode',
  59. '--packages',
  60. 'bundle',
  61. '--conditions',
  62. 'bun',
  63. ]
  64. for (const external of externals) {
  65. cmd.push('--external', external)
  66. }
  67. for (const [key, value] of Object.entries(defines)) {
  68. cmd.push('--define', `${key}=${value}`)
  69. }
  70. const proc = Bun.spawnSync({
  71. cmd,
  72. cwd: process.cwd(),
  73. stdout: 'inherit',
  74. stderr: 'inherit',
  75. })
  76. if (proc.exitCode !== 0) {
  77. process.exit(proc.exitCode ?? 1)
  78. }
  79. if (existsSync(outfile)) {
  80. chmodSync(outfile, 0o755)
  81. }
  82. console.log(`Built ${outfile}`)