build-npm-package.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/usr/bin/env bash
  2. # Script to build the Cline NPM package with telemetry keys injected
  3. # This script ensures all environment variables are properly set and builds are successful
  4. set -e # Exit on error
  5. # Colors for output
  6. RED='\033[0;31m'
  7. GREEN='\033[0;32m'
  8. YELLOW='\033[1;33m'
  9. BLUE='\033[0;34m'
  10. NC='\033[0m' # No Color
  11. # Required environment variables
  12. REQUIRED_VARS=(
  13. "TELEMETRY_SERVICE_API_KEY"
  14. "ERROR_SERVICE_API_KEY"
  15. )
  16. # Optional but recommended environment variables
  17. OPTIONAL_VARS=(
  18. "CLINE_ENVIRONMENT"
  19. "POSTHOG_TELEMETRY_ENABLED"
  20. )
  21. echo -e "${BLUE}========================================${NC}"
  22. echo -e "${BLUE}Cline NPM Package Build Script${NC}"
  23. echo -e "${BLUE}========================================${NC}"
  24. echo ""
  25. # Step 1: Verify required environment variables are set
  26. echo -e "${BLUE}Step 1: Verifying environment variables...${NC}"
  27. MISSING_VARS=()
  28. for VAR in "${REQUIRED_VARS[@]}"; do
  29. if [ -z "${!VAR}" ]; then
  30. MISSING_VARS+=("$VAR")
  31. echo -e "${RED}✗ $VAR is not set${NC}"
  32. else
  33. # Show first 10 chars for verification (don't expose full key)
  34. VAR_VALUE="${!VAR}"
  35. echo -e "${GREEN}✓ $VAR is set (${VAR_VALUE:0:10}...)${NC}"
  36. fi
  37. done
  38. # Check optional variables
  39. for VAR in "${OPTIONAL_VARS[@]}"; do
  40. if [ -z "${!VAR}" ]; then
  41. echo -e "${YELLOW}⚠ $VAR is not set (optional)${NC}"
  42. else
  43. echo -e "${GREEN}✓ $VAR is set: ${!VAR}${NC}"
  44. fi
  45. done
  46. if [ ${#MISSING_VARS[@]} -gt 0 ]; then
  47. echo -e "\n${RED}Error: Missing required environment variables:${NC}"
  48. printf '%s\n' "${MISSING_VARS[@]}"
  49. echo -e "\n${YELLOW}Please set these variables before running the build:${NC}"
  50. echo -e "export TELEMETRY_SERVICE_API_KEY=\"your_posthog_api_key\""
  51. echo -e "export ERROR_SERVICE_API_KEY=\"your_error_tracking_api_key\""
  52. exit 1
  53. fi
  54. # Step 2: Verify Node.js can see the environment variables
  55. echo -e "\n${BLUE}Step 2: Verifying Node.js can access environment variables...${NC}"
  56. if node -e "
  57. const telemetryKey = process.env.TELEMETRY_SERVICE_API_KEY;
  58. const errorKey = process.env.ERROR_SERVICE_API_KEY;
  59. if (!telemetryKey || !errorKey) {
  60. console.error('Node.js cannot see environment variables!');
  61. process.exit(1);
  62. }
  63. console.log('✓ TELEMETRY_SERVICE_API_KEY visible to Node.js');
  64. console.log('✓ ERROR_SERVICE_API_KEY visible to Node.js');
  65. "; then
  66. echo -e "${GREEN}✓ Node.js can access environment variables${NC}"
  67. else
  68. echo -e "${RED}✗ Node.js cannot access environment variables${NC}"
  69. echo -e "${YELLOW}Make sure to use 'export' when setting variables:${NC}"
  70. echo -e "export TELEMETRY_SERVICE_API_KEY=\"...\""
  71. exit 1
  72. fi
  73. # Step 3: Clean previous builds
  74. echo -e "\n${BLUE}Step 3: Cleaning previous builds...${NC}"
  75. rm -rf dist-standalone
  76. echo -e "${GREEN}✓ Cleaned dist-standalone directory${NC}"
  77. # Step 4: Build Go CLI binaries for all platforms
  78. echo -e "\n${BLUE}Step 4: Building Go CLI binaries for all platforms...${NC}"
  79. if npm run compile-cli-all-platforms; then
  80. echo -e "${GREEN}✓ Go CLI binaries built successfully${NC}"
  81. # Verify binaries were created
  82. if ls cli/bin/cline-* 1> /dev/null 2>&1; then
  83. echo -e "${GREEN}✓ CLI binaries verified:${NC}"
  84. ls -lh cli/bin/cline-* | awk '{print " " $9 " (" $5 ")"}'
  85. else
  86. echo -e "${RED}✗ No CLI binaries found in cli/bin/${NC}"
  87. exit 1
  88. fi
  89. else
  90. echo -e "${RED}✗ Failed to build Go CLI binaries${NC}"
  91. exit 1
  92. fi
  93. # Step 5: Build the standalone package with esbuild
  94. echo -e "\n${BLUE}Step 5: Building standalone package with esbuild...${NC}"
  95. if npm run compile-standalone-npm; then
  96. echo -e "${GREEN}✓ Standalone package built successfully${NC}"
  97. else
  98. echo -e "${RED}✗ Failed to build standalone package${NC}"
  99. exit 1
  100. fi
  101. # Step 6: Verify telemetry keys were injected
  102. echo -e "\n${BLUE}Step 6: Verifying telemetry keys were injected...${NC}"
  103. # Check if the compiled file still has process.env references (bad)
  104. if grep -q "process.env.TELEMETRY_SERVICE_API_KEY" dist-standalone/cline-core.js; then
  105. echo -e "${RED}✗ Keys were NOT injected! Found 'process.env.TELEMETRY_SERVICE_API_KEY' in compiled code${NC}"
  106. echo -e "${YELLOW}This means the environment variables were not replaced during build${NC}"
  107. exit 1
  108. fi
  109. # Check if actual keys are present (good)
  110. if grep -q "data.cline.bot" dist-standalone/cline-core.js; then
  111. # Extract a snippet of the PostHog config
  112. POSTHOG_CONFIG=$(grep -A 3 "data.cline.bot" dist-standalone/cline-core.js | head -5)
  113. if echo "$POSTHOG_CONFIG" | grep -q "apiKey.*phc_"; then
  114. echo -e "${GREEN}✓ Telemetry keys successfully injected into compiled code${NC}"
  115. else
  116. echo -e "${YELLOW}⚠ PostHog config found but apiKey format unclear${NC}"
  117. echo -e "${YELLOW}Config snippet:${NC}"
  118. echo "$POSTHOG_CONFIG"
  119. fi
  120. else
  121. echo -e "${YELLOW}⚠ Could not verify PostHog config in compiled code${NC}"
  122. fi
  123. # Step 7: Display build summary
  124. echo -e "\n${BLUE}========================================${NC}"
  125. echo -e "${GREEN}Build completed successfully!${NC}"
  126. echo -e "${BLUE}========================================${NC}"
  127. echo ""
  128. echo -e "${GREEN}Package location:${NC} dist-standalone/"
  129. echo -e "${GREEN}Package version:${NC} $(node -p "require('./dist-standalone/package.json').version" 2>/dev/null || echo "unknown")"
  130. echo ""
  131. echo -e "${BLUE}Next steps:${NC}"
  132. echo -e "1. Test locally: ${YELLOW}cd dist-standalone && npm link${NC}"
  133. echo -e "2. Verify: ${YELLOW}cline version${NC}"
  134. echo -e "3. Publish: ${YELLOW}cd dist-standalone && npm publish${NC}"
  135. echo ""
  136. echo -e "${YELLOW}Note: Check PostHog dashboard after running cline commands to verify telemetry${NC}"