|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const semver = require('semver'); |
| 4 | + |
| 5 | +// Parse command line arguments |
| 6 | +const args = process.argv.slice(2); |
| 7 | +const command = args[0]; |
| 8 | + |
| 9 | +function showHelp() { |
| 10 | + console.log(` |
| 11 | +Usage: node semver.js <command> [arguments] |
| 12 | +
|
| 13 | +Commands: |
| 14 | + diff <version1> <version2> - Compare two versions and return difference type |
| 15 | + inc <version> <type> - Increment version by type (major|minor|patch) |
| 16 | + current - Get current version from package.json |
| 17 | + |
| 18 | +Examples: |
| 19 | + node semver.js diff 19.1.2 19.2.0 # Returns: minor |
| 20 | + node semver.js inc 1.0.0 major # Returns: 2.0.0 |
| 21 | + node semver.js current # Returns: 1.0.0 |
| 22 | +`); |
| 23 | +} |
| 24 | + |
| 25 | +function getCurrentVersion() { |
| 26 | + try { |
| 27 | + const pkg = require('./package.json'); |
| 28 | + return pkg.version; |
| 29 | + } catch (error) { |
| 30 | + console.error('Error reading package.json:', error.message); |
| 31 | + process.exit(1); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +function updatePackageVersion(newVersion) { |
| 36 | + try { |
| 37 | + const fs = require('fs'); |
| 38 | + const pkg = require('./package.json'); |
| 39 | + pkg.version = newVersion; |
| 40 | + fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 2) + '\n'); |
| 41 | + return newVersion; |
| 42 | + } catch (error) { |
| 43 | + console.error('Error updating package.json:', error.message); |
| 44 | + process.exit(1); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +switch (command) { |
| 49 | + case 'diff': { |
| 50 | + const [version1, version2] = args.slice(1); |
| 51 | + if (!version1 || !version2) { |
| 52 | + console.error('Error: diff requires two versions'); |
| 53 | + process.exit(1); |
| 54 | + } |
| 55 | + |
| 56 | + // Clean versions (remove 'v' prefix if present) |
| 57 | + const clean1 = version1.replace(/^v/, ''); |
| 58 | + const clean2 = version2.replace(/^v/, ''); |
| 59 | + |
| 60 | + try { |
| 61 | + const diff = semver.diff(clean1, clean2); |
| 62 | + console.log(diff || 'patch'); // Default to patch if no diff |
| 63 | + } catch (error) { |
| 64 | + console.error('Error comparing versions:', error.message); |
| 65 | + process.exit(1); |
| 66 | + } |
| 67 | + break; |
| 68 | + } |
| 69 | + |
| 70 | + case 'inc': { |
| 71 | + const [version, type] = args.slice(1); |
| 72 | + if (!version || !type) { |
| 73 | + console.error('Error: inc requires version and type (major|minor|patch)'); |
| 74 | + process.exit(1); |
| 75 | + } |
| 76 | + |
| 77 | + try { |
| 78 | + const newVersion = semver.inc(version, type); |
| 79 | + console.log(newVersion); |
| 80 | + } catch (error) { |
| 81 | + console.error('Error incrementing version:', error.message); |
| 82 | + process.exit(1); |
| 83 | + } |
| 84 | + break; |
| 85 | + } |
| 86 | + |
| 87 | + case 'current': { |
| 88 | + const currentVersion = getCurrentVersion(); |
| 89 | + console.log(currentVersion); |
| 90 | + break; |
| 91 | + } |
| 92 | + |
| 93 | + case 'update': { |
| 94 | + const [newVersion] = args.slice(1); |
| 95 | + if (!newVersion) { |
| 96 | + console.error('Error: update requires new version'); |
| 97 | + process.exit(1); |
| 98 | + } |
| 99 | + |
| 100 | + const result = updatePackageVersion(newVersion); |
| 101 | + console.log(result); |
| 102 | + break; |
| 103 | + } |
| 104 | + |
| 105 | + case 'analyze': { |
| 106 | + // Special command for workflow: analyze multiple version changes and determine bump type |
| 107 | + const iosOld = args[1]; |
| 108 | + const iosNew = args[2]; |
| 109 | + const androidOld = args[3]; |
| 110 | + const androidNew = args[4]; |
| 111 | + |
| 112 | + let bumpType = 'patch'; // Default |
| 113 | + |
| 114 | + if (iosOld && iosNew && iosOld !== 'null' && iosNew !== 'null') { |
| 115 | + const iosDiff = semver.diff( |
| 116 | + iosOld.replace(/^v/, ''), |
| 117 | + iosNew.replace(/^v/, '') |
| 118 | + ); |
| 119 | + console.error(`📱 iOS: ${iosOld} → ${iosNew} (${iosDiff})`); |
| 120 | + |
| 121 | + if (iosDiff === 'major') { |
| 122 | + bumpType = 'major'; |
| 123 | + } else if (iosDiff === 'minor' && bumpType !== 'major') { |
| 124 | + bumpType = 'minor'; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + if ( |
| 129 | + androidOld && |
| 130 | + androidNew && |
| 131 | + androidOld !== 'null' && |
| 132 | + androidNew !== 'null' |
| 133 | + ) { |
| 134 | + const androidDiff = semver.diff( |
| 135 | + androidOld.replace(/^v/, ''), |
| 136 | + androidNew.replace(/^v/, '') |
| 137 | + ); |
| 138 | + console.error( |
| 139 | + `🤖 Android: ${androidOld} → ${androidNew} (${androidDiff})` |
| 140 | + ); |
| 141 | + |
| 142 | + if (androidDiff === 'major') { |
| 143 | + bumpType = 'major'; |
| 144 | + } else if (androidDiff === 'minor' && bumpType !== 'major') { |
| 145 | + bumpType = 'minor'; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + console.error(`📈 Determined bump type: ${bumpType}`); |
| 150 | + console.log(bumpType); |
| 151 | + break; |
| 152 | + } |
| 153 | + |
| 154 | + default: |
| 155 | + console.error(`Unknown command: ${command}`); |
| 156 | + showHelp(); |
| 157 | + process.exit(1); |
| 158 | +} |
0 commit comments