|
| 1 | +import { execSync } from 'node:child_process'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Release type for different version publishing scenarios |
| 5 | + * - prod: Production release (stable version) |
| 6 | + * - beta/alpha/rc: Pre-release versions for testing |
| 7 | + */ |
| 8 | +type ReleaseType = 'alpha' | 'beta' | 'prod' | 'rc'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Get default configuration for each release type |
| 12 | + * @param type - The release type (prod, beta, alpha, rc) |
| 13 | + * @returns Default configuration including preid and release strategy |
| 14 | + */ |
| 15 | +function getDefaultConfig(type: ReleaseType) { |
| 16 | + const configs = { |
| 17 | + alpha: { preid: 'alpha', release: 'prerelease' }, |
| 18 | + beta: { preid: 'beta', release: 'prerelease' }, |
| 19 | + prod: { preid: undefined, release: 'patch' }, |
| 20 | + rc: { preid: 'rc', release: 'prerelease' } |
| 21 | + }; |
| 22 | + |
| 23 | + return configs[type]; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Parse command line arguments |
| 28 | + * @returns Parsed arguments as key-value pairs |
| 29 | + */ |
| 30 | +function parseArgs() { |
| 31 | + const args = process.argv.slice(2); |
| 32 | + const result: Record<string, string | undefined> = { |
| 33 | + push: 'true' |
| 34 | + }; |
| 35 | + |
| 36 | + for (let i = 0; i < args.length; i += 1) { |
| 37 | + const arg = args[i]; |
| 38 | + if ((arg === '--type' || arg === '-t') && args[i + 1]) { |
| 39 | + result.type = args[i + 1]; |
| 40 | + i += 1; |
| 41 | + } else if ((arg === '--path' || arg === '--packageName' || arg === '-p') && args[i + 1]) { |
| 42 | + result.packageName = args[i + 1]; |
| 43 | + i += 1; |
| 44 | + } else if ((arg === '--release' || arg === '-r') && args[i + 1]) { |
| 45 | + result.release = args[i + 1]; |
| 46 | + i += 1; |
| 47 | + } else if ((arg === '--preid' || arg === '-pr') && args[i + 1]) { |
| 48 | + result.preid = args[i + 1]; |
| 49 | + i += 1; |
| 50 | + } else if (arg === '--push' && args[i + 1]) { |
| 51 | + result.push = args[i + 1]; |
| 52 | + i += 1; |
| 53 | + } else if (arg === '--no-push') { |
| 54 | + result.push = 'false'; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return result; |
| 59 | +} |
| 60 | + |
| 61 | +const options = parseArgs(); |
| 62 | + |
| 63 | +// Validate type parameter - must be one of: prod, beta, alpha, rc |
| 64 | +if (!options.type || !['prod', 'beta', 'alpha', 'rc'].includes(options.type)) { |
| 65 | + // eslint-disable-next-line no-console |
| 66 | + console.error('❌ Release type is required: --type prod|beta|alpha|rc'); |
| 67 | + process.exit(1); |
| 68 | +} |
| 69 | + |
| 70 | +const type = options.type as ReleaseType; |
| 71 | +const defaultConfig = getDefaultConfig(type); |
| 72 | + |
| 73 | +// Build the command with appropriate arguments |
| 74 | +const cmdParts = ['sr', 'release']; |
| 75 | + |
| 76 | +if (options.packageName) { |
| 77 | + cmdParts.push('--packageName', options.packageName); |
| 78 | +} |
| 79 | + |
| 80 | +// Use user-specified parameters if provided, otherwise use default values |
| 81 | +const release = options.release || defaultConfig.release; |
| 82 | +cmdParts.push('--release', release); |
| 83 | + |
| 84 | +// Only pre-release versions need preid (beta, alpha, rc) |
| 85 | +const preid = options.preid || defaultConfig.preid; |
| 86 | +if (preid) { |
| 87 | + cmdParts.push('--preid', preid); |
| 88 | +} |
| 89 | + |
| 90 | +if (options.push === 'true') { |
| 91 | + cmdParts.push('--push'); |
| 92 | +} |
| 93 | + |
| 94 | +const cmd = cmdParts.join(' '); |
| 95 | + |
| 96 | +// eslint-disable-next-line no-console |
| 97 | +console.log(`🚀 Executing command: ${cmd}`); |
| 98 | + |
| 99 | +execSync(cmd, { |
| 100 | + stdio: 'inherit' |
| 101 | +}); |
0 commit comments