|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { execSync } from 'child_process' |
| 4 | +import fs from 'fs' |
| 5 | +import path from 'path' |
| 6 | +import { fileURLToPath } from 'url' |
| 7 | + |
| 8 | +const __filename = fileURLToPath(import.meta.url) |
| 9 | +const __dirname = path.dirname(__filename) |
| 10 | + |
| 11 | +function log(message) { |
| 12 | + console.log(`📦 ${message}`) |
| 13 | +} |
| 14 | + |
| 15 | +function run(command, options = {}) { |
| 16 | + log(`Running: ${command}`) |
| 17 | + try { |
| 18 | + return execSync(command, { stdio: 'inherit', ...options }) |
| 19 | + } catch (error) { |
| 20 | + console.error(`❌ Command failed: ${command}`) |
| 21 | + process.exit(1) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +function main() { |
| 26 | + const args = process.argv.slice(2) |
| 27 | + const isDryRun = args.includes('--dry-run') |
| 28 | + |
| 29 | + log('Starting SDK publishing process...') |
| 30 | + |
| 31 | + // Clean and build |
| 32 | + log('Cleaning previous build...') |
| 33 | + run('bun run clean') |
| 34 | + |
| 35 | + log('Building TypeScript...') |
| 36 | + run('bun run build') |
| 37 | + |
| 38 | + // Prepare package.json for publishing |
| 39 | + log('Preparing package.json for publishing...') |
| 40 | + const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8')) |
| 41 | + |
| 42 | + // Remove workspace dependencies and replace with actual versions |
| 43 | + if (packageJson.dependencies && packageJson.dependencies['@codebuff/common']) { |
| 44 | + // For now, we'll make it a peer dependency since it's not published yet |
| 45 | + delete packageJson.dependencies['@codebuff/common'] |
| 46 | + if (!packageJson.peerDependencies) { |
| 47 | + packageJson.peerDependencies = {} |
| 48 | + } |
| 49 | + packageJson.peerDependencies['@codebuff/common'] = '*' |
| 50 | + } |
| 51 | + |
| 52 | + // Update paths for publishing from dist directory |
| 53 | + packageJson.main = './index.js' |
| 54 | + packageJson.types = './index.d.ts' |
| 55 | + packageJson.exports = { |
| 56 | + '.': { |
| 57 | + types: './index.d.ts', |
| 58 | + import: './index.js', |
| 59 | + default: './index.js' |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Update files field to include all built files |
| 64 | + packageJson.files = [ |
| 65 | + '*.js', |
| 66 | + '*.d.ts', |
| 67 | + '*.d.ts.map', |
| 68 | + 'README.md', |
| 69 | + 'CHANGELOG.md' |
| 70 | + ] |
| 71 | + |
| 72 | + // Write the modified package.json to dist |
| 73 | + fs.writeFileSync('dist/package.json', JSON.stringify(packageJson, null, 2)) |
| 74 | + |
| 75 | + // Copy other files |
| 76 | + log('Copying additional files...') |
| 77 | + const filesToCopy = ['README.md', 'CHANGELOG.md'] |
| 78 | + |
| 79 | + for (const file of filesToCopy) { |
| 80 | + if (fs.existsSync(file)) { |
| 81 | + fs.copyFileSync(file, `dist/${file}`) |
| 82 | + log(`Copied ${file}`) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Verify the package |
| 87 | + log('Verifying package contents...') |
| 88 | + run('npm pack --dry-run', { cwd: 'dist' }) |
| 89 | + |
| 90 | + if (isDryRun) { |
| 91 | + log('Dry run complete! Package is ready for publishing.') |
| 92 | + log('To publish for real, run: bun run publish-sdk') |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + // Publish |
| 97 | + log('Publishing to npm...') |
| 98 | + const publishCommand = 'npm publish' |
| 99 | + run(publishCommand, { cwd: 'dist' }) |
| 100 | + log('✅ SDK published successfully!') |
| 101 | + log(`📦 Package: ${packageJson.name}@${packageJson.version}`) |
| 102 | +} |
| 103 | + |
| 104 | +if (import.meta.url === `file://${process.argv[1]}`) { |
| 105 | + main() |
| 106 | +} |
| 107 | + |
0 commit comments