build.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. ...(dev
  67. ? { 'process.env.NODE_ENV': JSON.stringify('development') }
  68. : {}),
  69. ...(dev
  70. ? {
  71. 'process.env.CLAUDE_CODE_EXPERIMENTAL_BUILD': JSON.stringify('true'),
  72. }
  73. : {}),
  74. 'process.env.CLAUDE_CODE_VERIFY_PLAN': JSON.stringify('false'),
  75. 'MACRO.VERSION': JSON.stringify(version),
  76. 'MACRO.BUILD_TIME': JSON.stringify(buildTime),
  77. 'MACRO.PACKAGE_URL': JSON.stringify(pkg.name),
  78. 'MACRO.NATIVE_PACKAGE_URL': 'undefined',
  79. 'MACRO.FEEDBACK_CHANNEL': JSON.stringify('github'),
  80. 'MACRO.ISSUES_EXPLAINER': JSON.stringify(
  81. 'This reconstructed source snapshot does not include Anthropic internal issue routing.',
  82. ),
  83. 'MACRO.VERSION_CHANGELOG': JSON.stringify(
  84. dev ? getVersionChangelog() : 'https://github.com/paoloanzn/claude-code',
  85. ),
  86. } as const
  87. const cmd = [
  88. 'bun',
  89. 'build',
  90. './src/entrypoints/cli.tsx',
  91. '--compile',
  92. '--target',
  93. 'bun',
  94. '--format',
  95. 'esm',
  96. '--outfile',
  97. outfile,
  98. '--minify',
  99. '--bytecode',
  100. '--packages',
  101. 'bundle',
  102. '--conditions',
  103. 'bun',
  104. ]
  105. for (const external of externals) {
  106. cmd.push('--external', external)
  107. }
  108. for (const feature of features) {
  109. cmd.push(`--feature=${feature}`)
  110. }
  111. for (const [key, value] of Object.entries(defines)) {
  112. cmd.push('--define', `${key}=${value}`)
  113. }
  114. const proc = Bun.spawnSync({
  115. cmd,
  116. cwd: process.cwd(),
  117. stdout: 'inherit',
  118. stderr: 'inherit',
  119. })
  120. if (proc.exitCode !== 0) {
  121. process.exit(proc.exitCode ?? 1)
  122. }
  123. if (existsSync(outfile)) {
  124. chmodSync(outfile, 0o755)
  125. }
  126. console.log(`Built ${outfile}`)