|
| 1 | +#!/usr/bin/env bun |
| 2 | +import { execSync } from 'child_process'; |
| 3 | +import { existsSync } from 'node:fs'; |
| 4 | +import { join } from 'node:path'; |
| 5 | +import { platform, arch } from 'node:os'; |
| 6 | + |
| 7 | +// Simple ANSI colors |
| 8 | +const cyan = '\x1b[36m'; |
| 9 | +const green = '\x1b[32m'; |
| 10 | +const yellow = '\x1b[33m'; |
| 11 | +const red = '\x1b[31m'; |
| 12 | +const dim = '\x1b[2m'; |
| 13 | +const bold = '\x1b[1m'; |
| 14 | +const reset = '\x1b[0m'; |
| 15 | + |
| 16 | +// Map platform/arch to package names |
| 17 | +const platformMap = { |
| 18 | + 'linux-x64': 'codemachine-linux-x64', |
| 19 | + 'darwin-arm64': 'codemachine-darwin-arm64', |
| 20 | + 'darwin-x64': 'codemachine-darwin-x64', |
| 21 | + 'win32-x64': 'codemachine-windows-x64', |
| 22 | +}; |
| 23 | + |
| 24 | +const currentPlatform = platform(); |
| 25 | +const currentArch = arch(); |
| 26 | +const platformKey = `${currentPlatform}-${currentArch}`; |
| 27 | +const packageName = platformMap[platformKey]; |
| 28 | + |
| 29 | +if (!packageName) { |
| 30 | + console.error(`${red}✗${reset} Unsupported platform: ${bold}${platformKey}${reset}`); |
| 31 | + console.error(`${dim}Supported:${reset} ${Object.keys(platformMap).join(', ')}`); |
| 32 | + process.exit(1); |
| 33 | +} |
| 34 | + |
| 35 | +const packageDir = join('./binaries', packageName); |
| 36 | + |
| 37 | +if (!existsSync(packageDir)) { |
| 38 | + console.error(`${red}✗${reset} Package directory not found: ${packageDir}`); |
| 39 | + console.error(`${yellow}⟳${reset} Run ${bold}bun run build${reset} first to build binaries`); |
| 40 | + process.exit(1); |
| 41 | +} |
| 42 | + |
| 43 | +// Parse arguments |
| 44 | +let dryRun = false; |
| 45 | +let tag = 'latest'; |
| 46 | +let access = 'public'; |
| 47 | + |
| 48 | +const args = process.argv.slice(2); |
| 49 | +for (let i = 0; i < args.length; i++) { |
| 50 | + switch (args[i]) { |
| 51 | + case '--dry-run': |
| 52 | + dryRun = true; |
| 53 | + break; |
| 54 | + case '--tag': |
| 55 | + if (i + 1 >= args.length) { |
| 56 | + console.error(`${red}✗${reset} --tag requires a value`); |
| 57 | + process.exit(1); |
| 58 | + } |
| 59 | + i++; |
| 60 | + tag = args[i]; |
| 61 | + break; |
| 62 | + case '--access': |
| 63 | + if (i + 1 >= args.length) { |
| 64 | + console.error(`${red}✗${reset} --access requires a value`); |
| 65 | + process.exit(1); |
| 66 | + } |
| 67 | + i++; |
| 68 | + access = args[i]; |
| 69 | + break; |
| 70 | + default: |
| 71 | + console.error(`${red}✗${reset} Unknown option: ${args[i]}`); |
| 72 | + process.exit(1); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +console.log(`\n${bold}${cyan}╭────────────────────────────────────────╮${reset}`); |
| 77 | +console.log(`${bold}${cyan}│${reset} Publishing Platform Package ${bold}${cyan}│${reset}`); |
| 78 | +console.log(`${bold}${cyan}╰────────────────────────────────────────╯${reset}\n`); |
| 79 | + |
| 80 | +console.log(`${dim}Package:${reset} ${bold}${packageName}${reset}`); |
| 81 | +console.log(`${dim}Directory:${reset} ${packageDir}`); |
| 82 | +console.log(`${dim}Tag:${reset} ${tag}`); |
| 83 | +console.log(`${dim}Access:${reset} ${access}`); |
| 84 | +console.log(`${dim}Dry run:${reset} ${dryRun ? 'yes' : 'no'}\n`); |
| 85 | + |
| 86 | +try { |
| 87 | + // Verify npm authentication |
| 88 | + console.log(`${cyan}→${reset} Verifying npm authentication...`); |
| 89 | + try { |
| 90 | + execSync('npm whoami', { stdio: 'pipe' }); |
| 91 | + console.log(`${green}✓${reset} ${dim}Authenticated${reset}\n`); |
| 92 | + } catch { |
| 93 | + console.error(`${red}✗${reset} Not logged in to npm. Run ${bold}npm login${reset} first.`); |
| 94 | + process.exit(1); |
| 95 | + } |
| 96 | + |
| 97 | + // Build publish command |
| 98 | + const publishCmd = dryRun |
| 99 | + ? `npm publish --dry-run --tag ${tag} --access ${access}` |
| 100 | + : `npm publish --tag ${tag} --access ${access}`; |
| 101 | + |
| 102 | + console.log(`${cyan}→${reset} Publishing ${packageName}...`); |
| 103 | + if (dryRun) { |
| 104 | + console.log(`${yellow}ℹ${reset} ${dim}Dry run mode - no actual publish${reset}`); |
| 105 | + } |
| 106 | + |
| 107 | + execSync(publishCmd, { |
| 108 | + cwd: packageDir, |
| 109 | + stdio: 'inherit', |
| 110 | + }); |
| 111 | + |
| 112 | + if (!dryRun) { |
| 113 | + console.log(`\n${green}✓${reset} ${bold}Published successfully!${reset}`); |
| 114 | + console.log(`\n${dim}View at:${reset} https://www.npmjs.com/package/${packageName}\n`); |
| 115 | + } else { |
| 116 | + console.log(`\n${green}✓${reset} ${bold}Dry run complete${reset}\n`); |
| 117 | + } |
| 118 | +} catch (error) { |
| 119 | + console.error(`\n${red}✗${reset} ${bold}Publish failed${reset}`); |
| 120 | + if (error.message) { |
| 121 | + console.error(`${dim}${error.message}${reset}`); |
| 122 | + } |
| 123 | + process.exit(1); |
| 124 | +} |
0 commit comments