build.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 dev = args.includes('--dev')
  10. function runCommand(cmd: string[]): string | null {
  11. const proc = Bun.spawnSync({
  12. cmd,
  13. cwd: process.cwd(),
  14. stdout: 'pipe',
  15. stderr: 'pipe',
  16. })
  17. if (proc.exitCode !== 0) {
  18. return null
  19. }
  20. return new TextDecoder().decode(proc.stdout).trim() || null
  21. }
  22. function getDevVersion(baseVersion: string): string {
  23. const timestamp = new Date().toISOString()
  24. const date = timestamp.slice(0, 10).replaceAll('-', '')
  25. const time = timestamp.slice(11, 19).replaceAll(':', '')
  26. const sha = runCommand(['git', 'rev-parse', '--short=8', 'HEAD']) ?? 'unknown'
  27. return `${baseVersion}-dev.${date}.t${time}.sha${sha}`
  28. }
  29. function getVersionChangelog(): string {
  30. return (
  31. runCommand(['git', 'log', '--format=%h %s', '-20']) ??
  32. 'Local development build'
  33. )
  34. }
  35. const features: string[] = []
  36. for (let i = 0; i < args.length; i += 1) {
  37. const arg = args[i]
  38. if (arg === '--feature' && args[i + 1]) {
  39. features.push(args[i + 1]!)
  40. i += 1
  41. continue
  42. }
  43. if (arg.startsWith('--feature=')) {
  44. features.push(arg.slice('--feature='.length))
  45. }
  46. }
  47. const outfile = compile
  48. ? dev
  49. ? './dist/cli-dev'
  50. : './dist/cli'
  51. : dev
  52. ? './cli-dev'
  53. : './cli'
  54. const buildTime = new Date().toISOString()
  55. const version = dev ? getDevVersion(pkg.version) : pkg.version
  56. mkdirSync(dirname(outfile), { recursive: true })
  57. const externals = [
  58. '@ant/*',
  59. 'audio-capture-napi',
  60. 'image-processor-napi',
  61. 'modifiers-napi',
  62. 'url-handler-napi',
  63. ]
  64. const defines = {
  65. 'process.env.USER_TYPE': JSON.stringify('external'),
  66. 'process.env.CLAUDE_CODE_FORCE_FULL_LOGO': JSON.stringify('true'),
  67. ...(dev
  68. ? { 'process.env.NODE_ENV': JSON.stringify('development') }
  69. : {}),
  70. ...(dev
  71. ? {
  72. 'process.env.CLAUDE_CODE_EXPERIMENTAL_BUILD': JSON.stringify('true'),
  73. }
  74. : {}),
  75. 'process.env.CLAUDE_CODE_VERIFY_PLAN': JSON.stringify('false'),
  76. 'MACRO.VERSION': JSON.stringify(version),
  77. 'MACRO.BUILD_TIME': JSON.stringify(buildTime),
  78. 'MACRO.PACKAGE_URL': JSON.stringify(pkg.name),
  79. 'MACRO.NATIVE_PACKAGE_URL': 'undefined',
  80. 'MACRO.FEEDBACK_CHANNEL': JSON.stringify('github'),
  81. 'MACRO.ISSUES_EXPLAINER': JSON.stringify(
  82. 'This reconstructed source snapshot does not include Anthropic internal issue routing.',
  83. ),
  84. 'MACRO.VERSION_CHANGELOG': JSON.stringify(
  85. dev ? getVersionChangelog() : 'https://github.com/paoloanzn/claude-code',
  86. ),
  87. } as const
  88. const cmd = [
  89. 'bun',
  90. 'build',
  91. './src/entrypoints/cli.tsx',
  92. '--compile',
  93. '--target',
  94. 'bun',
  95. '--format',
  96. 'esm',
  97. '--outfile',
  98. outfile,
  99. '--minify',
  100. '--bytecode',
  101. '--packages',
  102. 'bundle',
  103. '--conditions',
  104. 'bun',
  105. ]
  106. for (const external of externals) {
  107. cmd.push('--external', external)
  108. }
  109. for (const feature of features) {
  110. cmd.push(`--feature=${feature}`)
  111. }
  112. for (const [key, value] of Object.entries(defines)) {
  113. cmd.push('--define', `${key}=${value}`)
  114. }
  115. const proc = Bun.spawnSync({
  116. cmd,
  117. cwd: process.cwd(),
  118. stdout: 'inherit',
  119. stderr: 'inherit',
  120. })
  121. if (proc.exitCode !== 0) {
  122. process.exit(proc.exitCode ?? 1)
  123. }
  124. if (existsSync(outfile)) {
  125. chmodSync(outfile, 0o755)
  126. }
  127. console.log(`Built ${outfile}`)